[PYTHON] Solve 100 language processing knocks 2020 (00. Reverse order of character strings)

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

00. Reverse order of strings

problem

[00. Reverse order of strings](https://nlp100.github.io/ja/ch01.html#00-%E6%96%87%E5%AD%97%E5%88%97%E3%81% The problem of AE% E9% 80% 86% E9% A0% 86) is as follows.

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

answer

If you read the question, the answer is desserts because it's a question of getting a string of stressed in reverse. All of the answers below will return desserts when the last expression is evaluated.

simple

There is a method to add it to the beginning of the character string obediently. In Python, you could combine strings with + to generate a new string.

#simple
s = "stressed"

result = ""
for c in s:
    result = c + result
result #=> 'desserts'

Functional

It's too simple to be functional, but it can be expressed by a combination of functions. The reversed function returns an iterator to retrieve in reverse order given an iterator, so concatenate with an empty string to get the desired result.

#Functional
s = "stressed"

"".join(reversed(s)) #=> 'desserts'

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. Specifying -1 for step reverses the order. It goes by -1.

# Pythonic
s = "stressed"

s[::-1] #=> 'desserts'

Other answers

combination

There is also the following method that combines the above. If you add something to the end or beginning of the string, a copy will occur, and if you add an element other than the end of the list, a copy will occur. To avoid that, I add an element to the end of the list and then string it with " ". Join.

#A combination of simple and functional
s = "stressed"

l = []
for c in reversed(s):
    l.append(c)
"".join(l) #=> 'desserts'

Turn by index

There is also a method of turning by index obediently. for n in range (len (s)): is one of the syntaxes for turning for for an index. len (s) returns the length of the string s, andrange (n)returns an iterator to scan 0, ..., n-1.

#Perform the process equivalent to reversed by yourself
s = "stressed"

l = []
for n in range(len(s)):
    l.append(s[len(s)-n-1])
"".join(l) #=> 'desserts'

There is another way to index. ʻEnumerate (s)is an iterator for scanning(0, s [0]), ..., (n-1, s [n-1])withn = len (s)Returns. I also use_` to mean variables that I don't use. I just put it in.

#Perform the process equivalent to reversed by yourself
s = "stressed"

l = []
for n,_ in enumerate(s):
    l.append(s[len(s)-n-1])
"".join(l) #=> 'desserts'

List comprehension

You can also write in list comprehension.

s = "stressed"

l = [c for c in reversed(s)]
"".join(l) #=> 'desserts'

However, if you think about it carefully, you may think that the generator comprehension may be used.

s = "stressed"

g = (c for c in reversed(s))
"".join(g) #=> 'desserts'

Then, it will return to the following functional type.

#Functional
s = "stressed"

"".join(reversed(s)) #=> 'desserts'

Looking back

I think each person has their own taste, but my favorite answer is below.

#A combination of simple and functional
s = "stressed"

l = []
for c in reversed(s):
    l.append(c)
"".join(l) #=> 'desserts'

It is easy to understand that the for statement conveys the feeling that you want to turn it in the opposite direction, and that you are obediently adding elements to the list. Also, when solving an actual problem, I often write the process of processing each element and putting it in another list, so it is the basic form. " ". Join (reversed (s)) and s [:: -1] are great for solving this problem, but they make it harder to add additional processing.

Recommended Posts

Solve 100 language processing knocks 2020 (00. Reverse order of character strings)
00. Reverse order of strings
100 language processing knocks 03 ~ 05
100 language processing knocks (2020): 40
100 language processing knocks (2020): 32
100 language processing knocks (2020): 35
100 language processing knocks (2020): 47
100 language processing knocks (2020): 39
100 language processing knocks (2020): 22
100 language processing knocks (2020): 42
100 language processing knocks (2020): 29
100 language processing knocks (2020): 49
100 language processing knocks (2020): 45
100 language processing knocks (2020): 10-19
100 language processing knocks (2020): 30
100 language processing knocks (2020): 00-09
100 language processing knocks (2020): 31
100 language processing knocks (2020): 48
100 language processing knocks (2020): 44
100 language processing knocks (2020): 41
100 language processing knocks (2020): 37
100 language processing knocks (2020): 25
100 language processing knocks (2020): 23
100 language processing knocks (2020): 33
100 language processing knocks (2020): 20
100 language processing knocks (2020): 27
100 language processing knocks (2020): 46
100 language processing knocks (2020): 21
100 language processing knocks (2020): 36
100 amateur language processing knocks: 41
100 amateur language processing knocks: 71
100 amateur language processing knocks: 56
100 amateur language processing knocks: 24
100 amateur language processing knocks: 59
100 amateur language processing knocks: 70
100 amateur language processing knocks: 60
100 amateur language processing knocks: 92
100 amateur language processing knocks: 30
100 amateur language processing knocks: 06
100 amateur language processing knocks: 84
100 amateur language processing knocks: 81
100 amateur language processing knocks: 33
100 amateur language processing knocks: 40
100 amateur language processing knocks: 45
100 amateur language processing knocks: 43
100 amateur language processing knocks: 55
100 amateur language processing knocks: 22
100 amateur language processing knocks: 61
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: 12
100 amateur language processing knocks: 14
100 amateur language processing knocks: 08
100 amateur language processing knocks: 42
100 language processing knocks ~ Chapter 1
100 amateur language processing knocks: 19
100 amateur language processing knocks: 73
100 amateur language processing knocks: 75
100 amateur language processing knocks: 98