It is a challenge record of Language processing 100 knock 2015. The environment is Ubuntu 16.04 LTS + Python 3.5.2 : : Anaconda 4.1.1 (64-bit). Click here for a list of past knocks (http://qiita.com/segavvy/items/fb50ba8097d59475f760).
Break down the sentence "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can." Into words 1, 5, 6, 7, 8, 9, 15, 16, The 19th word is the first character, and the other words are the first two characters. Create.
The finished code:
main.py
# coding: utf-8
num_first_only = (1, 5, 6, 7, 8, 9, 15, 16, 19)
target = 'Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.'
result = {}
words = target.split(' ')
for (num, word) in enumerate(words, 1):
if num in num_first_only:
result[word[0:1]] = num
else:
result[word[0:2]] = num
print(result)
Execution result:
Terminal
{'K': 19, 'S': 16, 'He': 2, 'Al': 13, 'B': 5, 'Mi': 12, 'C': 6, 'O': 8, 'Li': 3, 'N': 7, 'Na': 11, 'H': 1, 'Si': 14, 'P': 15, 'Ne': 10, 'Ca': 20, 'Ar': 18, 'F': 9, 'Be': 4, 'Cl': 17}
that? Only Mg (magnesium) with atomic number 12 is strange. It is Mi. However, the word in the corresponding position is Might, so the program seems to be correct.
It is convenient that ʻenumerate () ` can specify the start number. The code seems to be simple, such as when you want to enumerate from the middle of an advanced iterable.
I noticed that the result changes every time I run this program.
Execution result 2
{'B': 5, 'Al': 13, 'Be': 4, 'Cl': 17, 'P': 15, 'Si': 14, 'Ca': 20, 'Ne': 10, 'O': 8, 'K': 19, 'S': 16, 'Li': 3, 'He': 2, 'Na': 11, 'C': 6, 'Ar': 18, 'Mi': 12, 'H': 1, 'F': 9, 'N': 7}
Execution result 3
{'B': 5, 'He': 2, 'H': 1, 'C': 6, 'Be': 4, 'Si': 14, 'O': 8, 'F': 9, 'P': 15, 'Ca': 20, 'Al': 13, 'Ne': 10, 'Li': 3, 'K': 19, 'S': 16, 'Ar': 18, 'Cl': 17, 'Mi': 12, 'Na': 11, 'N': 7}
Execution result 4
{'Cl': 17, 'Si': 14, 'Al': 13, 'C': 6, 'Ca': 20, 'O': 8, 'He': 2, 'N': 7, 'F': 9, 'Ar': 18, 'Na': 11, 'Mi': 12, 'H': 1, 'P': 15, 'Be': 4, 'Ne': 10, 'Li': 3, 'B': 5, 'S': 16, 'K': 19}
The storage order in the dictionary is exactly the same, so it's quite interesting behavior. Is the hash function changing every time it is executed? But for what? Oh, maybe at the time of print ()
, the order changes due to the influence of the address used?
As an aside, when I was a C programmer, I had a hard time with the trade-off between performance deterioration due to collision and increased memory consumption of hash table due to avoidance, so I was worried about internal implementation. I will. I heard that Python is implemented in C, so I'd like to take a look if I have time to spare.
That's all for the fifth knock. If you have any mistakes, I would appreciate it if you could point them out.
Recommended Posts