[PYTHON] 100 amateur language processing knocks: 04

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).

Chapter 1: Preparatory movement

04. Element symbol

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.

The result changes every time you run it! ??

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

100 amateur language processing knocks: 41
100 amateur language processing knocks: 56
100 amateur language processing knocks: 24
100 amateur language processing knocks: 50
100 amateur language processing knocks: 59
100 amateur language processing knocks: 70
100 amateur language processing knocks: 62
100 amateur language processing knocks: 92
100 amateur language processing knocks: 06
100 amateur language processing knocks: 81
100 amateur language processing knocks: 46
100 amateur language processing knocks: 88
100 amateur language processing knocks: 89
100 amateur language processing knocks: 43
100 amateur language processing knocks: 55
100 amateur language processing knocks: 94
100 amateur language processing knocks: 54
100 amateur language processing knocks: 04
100 amateur language processing knocks: 63
100 amateur language processing knocks: 78
100 amateur language processing knocks: 12
100 amateur language processing knocks: 14
100 amateur language processing knocks: 08
100 amateur language processing knocks: 42
100 amateur language processing knocks: 19
100 amateur language processing knocks: 73
100 amateur language processing knocks: 75
100 amateur language processing knocks: 98
100 amateur language processing knocks: 83
100 amateur language processing knocks: 95
100 amateur language processing knocks: 32
100 amateur language processing knocks: 96
100 amateur language processing knocks: 87
100 amateur language processing knocks: 72
100 amateur language processing knocks: 79
100 amateur language processing knocks: 23
100 amateur language processing knocks: 05
100 amateur language processing knocks: 00
100 amateur language processing knocks: 02
100 amateur language processing knocks: 37
100 amateur language processing knocks: 21
100 amateur language processing knocks: 68
100 amateur language processing knocks: 11
100 amateur language processing knocks: 90
100 amateur language processing knocks: 74
100 amateur language processing knocks: 66
100 amateur language processing knocks: 28
100 amateur language processing knocks: 64
100 amateur language processing knocks: 34
100 amateur language processing knocks: 36
100 amateur language processing knocks: 77
100 amateur language processing knocks: 01
100 amateur language processing knocks: 16
100 amateur language processing knocks: 27
100 amateur language processing knocks: 10
100 amateur language processing knocks: 03
100 amateur language processing knocks: 82
100 amateur language processing knocks: 69
100 amateur language processing knocks: 53
100 amateur language processing knocks: 18
100 amateur language processing knocks: 35