SHARE
TWEET

Untitled

a guest Feb 23rd, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # 00. 文字列の逆順
  2. # 文字列"stressed"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ.
  3. print("#00")
  4. str = "stressed"
  5. print("".join(reversed(str)))
  6.  
  7.  
  8. # 01. 「パタトクカシーー」
  9. # 「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ.
  10. print("#01")
  11. str = "パタトクカシーー"
  12. print(str[0::2])
  13.  
  14. # 02. 「パトカー」+「タクシー」=「パタトクカシーー」
  15. # 「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.
  16. print("#02")
  17. str1 = "パトカー"
  18. str2 = "タクシー"
  19. ret = ""
  20. for (s1, s2) in zip(str1, str2):
  21.   ret += s1 + s2
  22. print(ret)
  23.  
  24. # 03. 円周率
  25. # "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.
  26. print("#03")
  27. from collections import OrderedDict
  28. str = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics"
  29. ret = OrderedDict()
  30. for s in str:
  31.   if s == ' ' or s == ',' or s == '.':
  32.     continue
  33.  
  34.   if s not in ret.keys():
  35.     ret[s] = 0
  36.   ret[s] += 1
  37. print(ret)
  38.  
  39. # 04. 元素記号
  40. # "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."という文を単語に分解し,
  41. # 1, 5, 6, 7, 8, 9, 15, 16, 19番目の単語は先頭の1文字,それ以外の単語は先頭に2文字を取り出し,取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ.
  42. print("#04")
  43. str = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
  44. arr = str.split(' ')
  45. ret = {}
  46. targetIndex = [1, 5, 6, 7, 8, 9, 15, 16, 19]
  47. for i in range(0, len(arr)):
  48.   if i in targetIndex:
  49.     ret[arr[i][0]] = i
  50.   else:
  51.     ret[arr[i][0:2]] = i
  52. print(ret)
  53.  
  54. # 05. n-gram
  55. # 与えられたシーケンス(文字列やリストなど)からn-gramを作る関数を作成せよ.この関数を用い,"I am an NLPer"という文から単語bi-gram,文字bi-gramを得よ.
  56. print("#05")
  57. str = "I am an NLPer"
  58.  
  59. def ngramchar(n, s):
  60.   ret = []
  61.   for i in range(len(s) - (n - 1)):
  62.     ret.append(s[i:i+n])
  63.   return ret
  64. print(ngramchar(2, str))
  65.  
  66. def ngramword(n, s):
  67.   arr = s.split()
  68.   ret = []
  69.   for i in range(len(arr) - (n - 1)):
  70.     tmp = ""
  71.     for j in range(n):
  72.       tmp += arr[i+j]
  73.     ret.append(tmp)
  74.   return ret
  75. print(ngramword(2, str))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top