string = "stressed"
ans = string[::-1]
print(ans)
string = "パタトクカシーー"
ans = string[0::2]
print(ans)
s1 = "パトカー"
s2 = "タクシー"
ans = ""
for i,j in zip(s1,s2):
ans += i+j
print(ans)
#短く
ans2 = ''.join([i+j for i,j in zip(s1,s2)])
print(ans)
sentence = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
#コンマとコロンを抜く
sentence = sentence.replace(',','')
sentence = sentence.replace('.','')
#単語の配列にする
word_list = sentence.split()
ans = list()
for word in word_list:
ans.append(len(word))
print(ans)
sentence = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
# コンマをとって単語の配列に
sentence = sentence.replace('.','')
word_list = sentence.split()
ans = dict()
for index,word in enumerate(word_list):
will_take_first = [1, 5, 6, 7, 8, 9, 15, 16, 19]
if index+1 in will_take_first:
ans[word[0]] = index+1
else:
ans[word[0:2]] = index+1
print(ans)
def n_gram(string,n):
"""文字列stringと自然数nを受け取り、単語n-gramと文字n-gramを返す"""
#単語n_gramを作る
word_n_gram = []
sentence = string.replace(',','').replace('.','')
word_list = sentence.split()
for i in range(len(word_list)-n+1):
word_n_gram.append(word_list[i:i+n])
#文字n_gramを作る
letter_n_gram = []
for i in range(len(string)-n+1):
letter_n_gram.append(string[i:i+n])
return word_n_gram, letter_n_gram
word_n_gram,letter_n_gram = n_gram('I am an NLPer',2)
print("単語bi-gram→",word_n_gram)
print("文字bi-gram→",letter_n_gram)
# 05で作ったn_gram関数を使う。
_,X = n_gram('paraparaparadise',2)
_,Y = n_gram('paragraph',2)
X = set(X)
Y = set(Y)
print('Xは',X)
print('Yは',Y)
print('和集合は',X|Y)
print('積集合は',X&Y)
print('差集合は',X-Y)
if "se" in X and "se" in Y:
print("seはX,Yの両方に含まれます。")
elif "se" in X:
print("seはXのみに含まれます。")
elif "se" in Y:
print("seはYのみに含まれます。")
else:
print("seはどちらにも含まれません。")
def create_sentence(x,y,z):
return "{}時の{}は{}".format(x,y,z)
print(create_sentence(12,"気温",22.4))
def cipher(string):
ans = ""
for letter in string:
if letter.islower():
ans += chr(219 - ord(letter))
else:
ans += letter
return ans
example = "Men willingly believe what they wish."
encoded = cipher(example)
decoded = cipher(encoded)
print("暗号化されたもの→",encoded)
print("復号化されたもの→",decoded)
import random
def shuffle_string(string):
"""文字列stringをランダムに並び替える関数"""
letter_list = list(string)
random.shuffle(letter_list)
return ''.join(letter_list)
def generate_typo(word):
""" 単語wordを受け取り、長さが5以上であれば先頭と末尾以外をランダムにする関数 """
if len(word) > 4:
return word[0] + shuffle_string(word[1:-1]) + word[-1]
else:
return word
example = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."
word_list = example.split()
ans = ""
for word in word_list:
ans += generate_typo(word) + " "
print(ans)