Solving 100 Language Processing Knock 2020 (01. "Patatokukashi")

Introduction

Let's try knocking 100 natural languages that are popular in the streets with Python. I think there are various ways to solve it, so I would like to show you various ways to solve it. Of course, it is not possible to cover all the solutions. If you have any questions such as "There is such a solution" or "I'm wrong here", please let me know in the comments.

What is 100 Language Processing Knock 2020?

Languistic processing 100 knock 2020 is Inui Suzuki Laboratory of Tohoku University. /) This is the teaching material used in the Programming Basic Research Study Group. For details, refer to About 100 language processing knocks.

Link to Qiita article

1st question 2nd question 3rd question 4th question 5th question 6th question 7th question 8th question 9th question 10th question
Chapter 1 00 01 02 03 04 05 06 07 08 09
Chapter 2 10 11 12 13 14 15 16 17 18 19
Chapter 3 20 21 22 23 24 25 26 27 28 29
Chapter 4 30 31 32 33 34 35 36 37 38 39
Chapter 5 40 41 42 43 44 45 46 47 48 49
Chapter 6 50 51 52 53 54 55 56 57 58 59
Chapter 7 60 61 62 63 64 65 66 67 68 69
Chapter 8 70 71 72 73 74 75 76 77 78 79
Chapter 9 80 81 82 83 84 85 86 87 88 89
Chapter 10 90 91 92 93 94 95 96 97 98 99

01. "Patatokukashi"

problem

[01. "Patatokukashi"](https://nlp100.github.io/ja/ch01.html#01-%E3%83%91%E3%82%BF%E3%83%88%E3%82%AF The problem of% E3% 82% AB% E3% 82% B7% E3% 83% BC% E3% 83% BC) is as follows.

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

answer

If you read the question, the answer is police car because it is the 1,3,5,7th character of Patatokukasi. All of the answers below will return police car when you evaluate the last formula.

simple

There is a method to add it to the end of the character string obediently. ʻEnumerate (s)is an iterator for scanning(0, s [0]), ..., (n-1, s [n-1])withn = len (s)` Returns.

#simple
s = "Patatoku Kashii"

result = ""
for n,c in enumerate(s):
    if n%2==0:
        result += c
result #=> 'Police car'

Functional

You can also write using map and filter derived from functional types. You can't really feel the benefits in this example ...

#Functional
s = "Patatoku Kashii"

"".join(
    map(
        lambda x:x[1],
        filter(
            lambda x:x[0]%2==0,
            enumerate(s)
        )
    )
) #=> 'Police car'

Is it slightly better to name the function defined in the lambda expression?

#Functional
s = "Patatoku Kashii"

second_elem = lambda x:x[1]
is_first_elem_even = lambda x:x[0]%2==0

"".join(map(
        second_elem,
        filter(
            is_first_elem_even,
            enumerate(s)
        )
    )
) #=> 'Police car'

It feels overkill if you give it too many names.

#Functional
s = "Patatoku Kashii"

first_elem = lambda x:x[0]
second_elem = lambda x:x[1]
is_first_elem_even = lambda x:first_elem(x)%2==0

"".join(map(
        second_elem,
        filter(
            is_first_elem_even,
            enumerate(s)
        )
    )
) #=> 'Police car'

I hope that such a function is prepared. The function definition by the lambda expression is the same as the normal function definition below.

def first_elem(x):
    return x[0]

def second_elem(x):
    return x[1]

def is_first_elem_even(x):
    return first_elem(x)%2==0

You may want to put it in a variable quietly. Since filter and map return an iterator (generator), the list is not generated every time you put it in a variable. Like JavaScript, I don't feel like the callback should be on the back side.

#Functional
s = "Patatoku Kashii"

first_elem = lambda x:x[0]
second_elem = lambda x:x[1]
is_first_elem_even = lambda x:first_elem(x)%2==0

filtered = filter(is_first_elem_even, enumerate(s))
mapped = map(second_elem, filtered)
"".join(mapped) #=> 'Police car'

Comprehension notation

It is easier to write in comprehension notation without sticking to map or filter. Writing [x for x in l] is a list comprehension, and writing (x for x in l) is a generator comprehension. f ((x for x in l)) can be written with f (x for x in l) and one less parenthesis. Assigning values to multiple variables at the same time is easier to understand than receiving them with x and retrieving them withx [0]orx [1].

#Comprehension notation
s = "Patatoku Kashii"

"".join(c for n,c in enumerate(s) if n%2==0) #=> 'Police car'

Pythonic

Being Python-like is called ** Pythonic **, but is this code the most Pythonic? The method of accessing a list or string with s [start: end: step] is called slicing. If you specify 2 for step, it will be skipped by one.

# Pythonic
s = "Patatoku Kashii"

s[::2] #=> 'Police car'

Other answers

Turn by index

There is also a way to prepare your own index and rotate it without using ʻenumerate`.

#Turn by yourself
s = "Patatoku Kashii"

result = ""
n = 0
for c in s:
    if n%2==0:
        result += c
    n += 1
result #=> 'Police car'

Add to list and join later

Solve 100 language processing knock 2020 (01. Reverse order of character strings) You can add it to the list and combine it later. I can do it.

#simple
s = "Patatoku Kashii"

l = []
for n, c in enumerate(s):
    if n%2==0:
        l.append(c)
"".join(l) #=> 'Police car'

Looking back

You may be wondering which is better, "combine strings" or "add to list". If you handle a list of general objects instead of characters, you will inevitably add it to the list, but if the goal is to generate a string like this time, it doesn't matter. If it gets bigger and slower, stop and think about it.

#simple
s = "Patatoku Kashii"

result = ""
for n,c in enumerate(s):
    if n%2==0:
        result += c
result #=> 'Police car'
#simple
s = "Patatoku Kashii"

l = []
for n, c in enumerate(s):
    if n%2==0:
        l.append(c)
"".join(l) #=> 'Police car'

Recommended Posts

Solving 100 Language Processing Knock 2020 (01. "Patatokukashi")
100 Language Processing Knock (2020): 38
100 language processing knock 00 ~ 02
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]
100 Language Processing Knock-52: Stemming
100 Language Processing Knock Chapter 1
100 Amateur Language Processing Knock: 07
100 Language Processing Knock 2020 Chapter 2
100 Amateur Language Processing Knock: 47
100 Amateur Language Processing Knock: 97
100 language processing knock 2020 [00 ~ 59 answer]
100 Amateur Language Processing Knock: 67
100 Language Processing with Python Knock 2015
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 Chapter 2 (Python)
100 Language Processing Knock-25: Template Extraction
100 Language Processing Knock-87: Word Similarity
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 with Python (Chapter 1)
100 Language Processing Knock Chapter 1 in Python
100 Language Processing Knock 2020 Chapter 4: Morphological Analysis
100 Language Processing Knock 2020 Chapter 9: RNN, CNN
100 language processing knock-76 (using scikit-learn): labeling
100 language processing knock-55: named entity extraction
I tried 100 language processing knock 2020: Chapter 3
100 Language Processing Knock-82 (Context Word): Context Extraction
100 Language Processing Knock with Python (Chapter 3)
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
100 Language Processing Knock-28: MediaWiki Markup Removal
100 Language Processing Knock 2020 Chapter 7: Word Vector
100 Language Processing Knock-59: Analysis of S-expressions
Python beginner tried 100 language processing knock 2015 (05 ~ 09)
100 Language Processing Knock-31 (using pandas): Verb
I tried 100 language processing knock 2020: Chapter 1
100 Language Processing Knock 2020 Chapter 1: Preparatory Movement
100 language processing knock-73 (using scikit-learn): learning
100 Language Processing Knock Chapter 1 by Python
100 Language Processing Knock 2020 Chapter 3: Regular Expressions
100 Language Processing Knock-24: Extract File Reference
100 Language Processing Knock 2015 Chapter 4 Morphological Analysis (30-39)
100 language processing knock-74 (using scikit-learn): Prediction
I tried 100 language processing knock 2020: Chapter 2
I tried 100 language processing knock 2020: Chapter 4
100 Language Processing Knock-38 (using pandas): Histogram
Python beginner tried 100 language processing knock 2015 (00 ~ 04)
100 language processing knocks (2020): 40