Python beginner tried 100 language processing knock 2015 (00 ~ 04)

Preface

I was wondering if there was a site where I could practice python (3.X) to kill time, but I found a site called 100 Language Processing Knock 2015, so I tried only the first five problems. http://www.cl.ecei.tohoku.ac.jp/nlp100/

Please point out if there is something wrong with this.

00. Reverse order of strings

Get a string in which the characters of the string "stressed" are arranged in reverse (from the end to the beginning).

00.py


s = "stressed"
print(s[::-1])
desserts

In s [i: j: k], it means that k pieces of s [j-1] are skipped from s [i], so s [:: -1] means from the end to the beginning one by one. Right.

01. "Patatokukashi"

Take out the 1st, 3rd, 5th, and 7th characters of the character string "Patatokukashi" and get the concatenated character string.

01.py


s = "Patatoku Kashii"
print(s[::2])
Police car

It is an application of 00. (I can't think of anything in particular)

02. "Police car" + "Taxi" = "Patatokukashi"

Obtain the character string "Patatokukashi" by alternately connecting the characters "Police car" + "Taxi" from the beginning.

02.py


s = ""
for i,j in zip("Police car", "taxi"):
    s += i+j
print(s)
Patatoku Kashii

There was a convenient zip function that allows you to loop through multiple sequences at once, so you can use this to bring characters one by one and stick them together.

03. Pi

Break down the sentence "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."

03.py


s = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
count_list = []
for i in s.split():
    count_list.append(len(i.strip(",.")))
print(count_list)
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]

When I first saw this problem, where was the factor of pi? I thought, If you look closely, it's like that ... ~~ It's too aggressive ~~

The way to do this is to separate the strings with split (), remove the',' and'.' With strip, and get the number of characters with len (). It feels like processing by looping as much as this is divided.

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, and the associative array (dictionary type or map type) from the extracted character string to the word position (what number of words from the beginning) Create.

04.py


s = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
dict = {}
for (i, w) in enumerate(s.replace(".", "").split(), 1):
    if i in (1,5,6,7,8,9,15,16,19):
        dict.update({w[0]:i})
    else:
        dict.update({w[:2]:i})
print(dict)
{'H': 1, 'He': 2, 'Li': 3, 'Be': 4, 'B': 5, 'C': 6, 'N': 7, 'O': 8, 'F': 9, 'Ne': 10, 'Na': 11, 'Mi': 12, 'Al': 13, 'Si': 14, 'P': 15, 'S': 16, 'Cl': 17, 'Ar': 18, 'K': 19, 'Ca': 20}

I used the enumerate function to get the element with an index when looping. This will get the string and index with the'.' Removed, and only the first character for the 1,5,6,7,8,9,15,16,19th, otherwise up to the second character , And so on, write an if statement and you're done.

I saw the element symbol after a long time. ~~ By the way, what is Mi? ~~


I would like to continue when I feel like it.

Recommended Posts

Python beginner tried 100 language processing knock 2015 (05 ~ 09)
Python beginner tried 100 language processing knock 2015 (00 ~ 04)
100 Language Processing Knock Chapter 1 (Python)
100 Language Processing Knock Chapter 2 (Python)
I tried 100 language processing knock 2020
100 Language Processing Knock with Python (Chapter 1)
100 Language Processing Knock Chapter 1 in Python
100 Language Processing Knock (2020): 28
I tried 100 language processing knock 2020: Chapter 3
100 Language Processing Knock with Python (Chapter 3)
100 Language Processing Knock (2020): 38
I tried 100 language processing knock 2020: Chapter 1
100 language processing knock 00 ~ 02
100 Language Processing Knock Chapter 1 by Python
I tried 100 language processing knock 2020: Chapter 2
100 Language Processing Knock with Python (Chapter 2, Part 2)
100 Language Processing Knock with Python (Chapter 2, Part 1)
100 language processing knock 2020 [00 ~ 39 answer]
100 language processing knock 2020 [00-79 answer]
100 language processing knock 2020 [00 ~ 69 answer]
100 Language Processing Knock 2020 Chapter 1
100 Amateur Language Processing Knock: 17
100 language processing knock 2020 [00 ~ 49 answer]
Python: Natural language processing
100 Language Processing Knock-52: Stemming
100 Language Processing Knock Chapter 1
100 Amateur Language Processing Knock: 07
100 Language Processing Knock 2020 Chapter 3
100 Language Processing Knock 2020 Chapter 2
100 Amateur Language Processing Knock: 09
100 Amateur Language Processing Knock: 47
100 Language Processing Knock-53: Tokenization
100 Amateur Language Processing Knock: 97
100 language processing knock 2020 [00 ~ 59 answer]
100 Amateur Language Processing Knock: 67
Python inexperienced person tries to knock 100 language processing 14-16
Python inexperienced person tries to knock 100 language processing 07-09
Python inexperienced person tries to knock 100 language processing 10 ~ 13
Python inexperienced person tries to knock 100 language processing 05-06
Python inexperienced person tries to knock 100 language processing 00-04
100 Language Processing Knock-51: Word Clipping
100 Language Processing Knock-58: Tuple Extraction
100 Language Processing Knock-57: Dependency Analysis
100 language processing knock-50: sentence break
100 Language Processing Knock-25: Template Extraction
100 Language Processing Knock-87: Word Similarity
100 language processing knock-56: co-reference analysis
Solving 100 Language Processing Knock 2020 (01. "Patatokukashi")
100 Amateur Language Processing Knock: Summary
100 Language Processing Knock 2020 Chapter 2: UNIX Commands
100 Language Processing Knock 2015 Chapter 5 Dependency Analysis (40-49)
100 Language Processing Knock 2020 Chapter 4: Morphological Analysis
100 Language Processing Knock 2020 Chapter 9: RNN, CNN
100 language processing knock-55: named entity extraction
100 Language Processing Knock-82 (Context Word): Context Extraction
100 Language Processing Knock: Chapter 1 Preparatory Movement
100 Language Processing Knock 2020 Chapter 6: Machine Learning
100 Language Processing Knock Chapter 4: Morphological Analysis
Language processing 100 knock-86: Word vector display
100 Language Processing Knock 2020 Chapter 10: Machine Translation (90-98)
100 Language Processing Knock 2020 Chapter 5: Dependency Analysis