LoginSignup
0
1

More than 3 years have passed since last update.

言語処理100本ノック 00~09 解答

Posted at

00

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

実行結果

desserts

01

01.py
s = "パタトクカシーー"
print(s[::2])

実行結果

パトカー

02

02.py
ret = ''
s1 = 'パトカー'
s2 = 'タクシー'
s = ''.join([a + b for a, b in zip(s1, s2)])
print(s)

実行結果

パタトクカシーー

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)

実行結果

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

実行結果

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

実行結果

['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)

実行結果

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) + "時の" + y + "は" + str(z)

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

実行結果

'12時の気温は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)

実行結果

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

実行結果

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1