[PYTHON] Language processing 100 knocks 00 ~ 09 Answer

00

00.py


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

Execution result

desserts

01

01.py


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

Execution result

Police car

02

02.py


ret = ''
s1 = 'Police car'
s2 = 'taxi'
s = ''.join([a + b for a, b in zip(s1, s2)])
print(s)

Execution result

Patatoku Kashii

03

03.py


s = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
s = s.replace(',','')
s = s.replace('.', '')
l = s.split()
ret = [len(s) for s in l]
print(ret)

Execution result

[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]

04

04.py


s = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
i = 1
n = (0, 4, 5, 6, 7, 8, 14, 15, 18) 
d = {i: word[0:1] if i in n else word[0:2] for i, word in enumerate(s.split())}
print(d)

Execution result

{0: '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'}

05

05.py


def ngram(s, n):
    return [s[i:i+n] for i, _ in enumerate(list(s))]

print(ngram("I am an NLPer", 2))

Execution result

['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er', 'r']

06

06.py


def ngram(s, n):
    return {s[i:i+n] for i, _ in enumerate(list(s))}

s1 = "paraparaparadise"
s2 = "paragraph"
X = ngram(s1, 2)
Y = ngram(s2, 2)
print("X | Y :", X | Y)
print("X & Y :", X & Y)
print("X - Y :", X - Y)
print("se" in X)
print("se" in Y)

Execution result

X | Y : {'ra', 'h', 'gr', 'pa', 'ph', 'ap', 'ar', 'ad', 'di', 'is', 'se', 'ag', 'e'}
X & Y : {'pa', 'ap', 'ra', 'ar'}
X - Y : {'ad', 'di', 'se', 'is', 'e'}
True
False

07

07.py


def template(x, y, z):
    return str(x) + "of time" + y + "Is" + str(z)

print(template(x=12, y="temperature", z=22.4))

Execution result

'The temperature at 12:00 is 22.4'

08

08.py


def cipher(s):
    return ''.join([chr(219-ord(s1)) if s1.islower() else s1 for s1 in s])

s = "Are you sure?"
s_encrypted = cipher(s)
print(s_encrypted)

Execution result

Aiv blf hfiv?

09

09.py


import random

def typoglycemia(s):
    return ' '.join([s1 if len(s1)<=4 else shuffle_string(s1) for s1 in s.split()])

def shuffle_string(s):
    return s[0] + ''.join(random.sample(s[1:-1], len(s[1:-1])))+ s[-1]

s = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."

print(typoglycemia(s))

Execution result

I c'odulnt bievele that I colud aatcluly uasdnnertd what I was rdeaing : the poenhaenml poewr of the hamun mind .

Recommended Posts

100 language processing knocks 2020 [00 ~ 89 answer]
Language processing 100 knocks 00 ~ 09 Answer
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): 39
100 language processing knocks (2020): 22
100 language processing knocks (2020): 26
100 language processing knocks (2020): 34
100 language processing knocks (2020): 42
100 language processing knocks (2020): 29
100 language processing knocks 06 ~ 09
100 language processing knocks (2020): 43
100 language processing knocks (2020): 24
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: 71
100 language processing knock 2020 [00 ~ 39 answer]
100 amateur language processing knocks: 56
100 amateur language processing knocks: 24
100 amateur language processing knocks: 50
100 language processing knock 2020 [00-79 answer]
100 language processing knock 2020 [00 ~ 69 answer]
100 amateur language processing knocks: 59
100 amateur language processing knocks: 70
100 amateur language processing knocks: 62
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 language processing knock 2020 [00 ~ 49 answer]
100 amateur language processing knocks: 81
100 amateur language processing knocks: 33
100 amateur language processing knocks: 46
100 amateur language processing knocks: 88
100 amateur language processing knocks: 89
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