LoginSignup
22
46

More than 5 years have passed since last update.

【python】100本ノックにチャレンジ!(000〜005)

Last updated at Posted at 2017-08-16

はじめに

 元々ネットワークエンジニアで社内ネットワークの設計・構築・保守運用など一通りやってましたが、とある時に社内のネットワークセキュリティに携わることになりました。IPSやWAFを使ったセキュリティオペレーションに携わったんですが、自分の全く知らないことがどんどん出て来て、もっともっとセキュリティのことを勉強したくなりました。
 そこからセキュリティに関して勉強していくと「大量のログをもっと思い通りに扱えること」や「処理を自動化すること」や「簡単なexploitコードがかけること」が必要となり、色々検討しpythonの学習を決意。今回のチャレンジに至ります。Qiitaには自分の備忘録としてチャレンジ内容を投稿していこうと思います。

プログラミング歴やアプリ開発歴について

C言語を大学で学んだ程度
アプリ開発は大昔にphpでwebアプリケーションを開発した程度

100本ノックとは

言語処理100本ノック 2015のことであり、東北大学の乾・岡崎研究室の岡崎先生が提供してくれている問題集です。(感謝!)

実施環境について

PC:MacBookPro2016
IDE:pycharm
version:anaconda3-4.1.1

ノック状況

9/24追加

000. 文字列の逆順

文字列"stressed"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ.

000.py
msg="stressed"
print(msg)
print(msg[::-1])
result
stressed
desserts
Process finished with exit code 0

感想:スライスのオプションで-1をつけるだけ

001. 「パタトクカシーー」

「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ.

001.py
msg  = "パタトクカシーー"
print(msg)
msg_find = msg[::2]
print(msg_find)
result
パタトクカシーー
パトカー
Process finished with exit code 0

感想:スライスのオプションをつけるだけ

002. 「パトカー」+「タクシー」=「パタトクカシーー」

「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.

002.py
msg1 = "パトカー"
msg2= "タクシー"
msg_add=''

for i in range(0,len(msg1)):
    msg_add += msg1[i]
    msg_add += msg2[i]

print(msg_add)
result
パタトクカシーー
Process finished with exit code 0

感想:for文の感覚がC言語とは違う、、、

003. 円周率

"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解。各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.

word2list_003.py
# -*- coding: utf-8 -*-

def word2list(msg):
    temp_msg = ''
    list = []
    for temp in msg:
        temp=temp.rstrip(",.")
        if(temp==' '):
            list.append(temp_msg)
            temp_msg=''
        else:
            temp_msg += temp

    list.append(temp_msg)
    return list

if __name__ == "__main__":
    msg = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
    list = []
    num_list = []
    temp = ''

    list = word2list(msg)
    for num in list:
        num_list.append(len(num))

    print(num_list)

result
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]
Process finished with exit code 0

感想:,とか.の処理がしきれてない。。。
8/18 修正プログラムに更新

004. 元素記号

"Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
という文を単語に分解し,1, 5, 6, 7, 8, 9, 15, 16, 19番目の単語は先頭の1文字,それ以外の単語は先頭に2文字を取り出し,取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ.

word2dict_004.py
def word2dict(msg):
    temp = ''
    temp_msg = ''
    temp_list=[]
    i = 1

    for temp in msg:
        if(temp==' ' or temp=='. '):
            temp_list.append((i, temp_msg))
            temp_msg = ''
            i = i + 1
        else:
            temp_msg+=temp
    temp_list.append((i,temp_msg))
    return  temp_list

if __name__=="__main__":
    msg = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
    dic = {}
    temp_list = []
    temp_list = word2dict(msg)
    for key,item in temp_list:
        if(key==1 or key == 5 or key == 6 or key == 7or key == 8or key == 9or key == 15or key == 16or key == 19):
            dic[key]=item[:1]
        else:
            dic[key]=item[:2]
    print(dic)

result
{1: 'H', 2: 'He', 3: 'Li', 4: 'Be', 5: 'B', 6: 'C', 7: 'N', 8: 'O', 9: 'F', 10: 'Ne', 11: 'Na', 12: 'Mi', 13: 'Al', 14: 'Si', 15: 'P', 16: 'S', 17: 'Cl', 18: 'Ar', 19: 'K', 20: 'Ca'}
Process finished with exit code 0

感想:if文のところはもうちょっとスマートなやり方がアリそう

005. n-gram

与えられたシーケンス(文字列やリストなど)からn-gramを作る関数を作成せよ.
この関数を用い,"I am an NLPer"という文から単語bi-gram,文字bi-gramを得よ.

ngram_005.py
from training.word2list_003 import word2list

def ngram(msg,N,type):
    if(type=='word'):
        bigram_word=''
        bigram_list=[]
        temp_list=word2list(msg)
        i=0
        while(i < int(len(temp_list))-1):
#temp_listからNで指定した数のrangeを設定した単語数を取得してword_listへ代入
            word_list = temp_list[i:(N+i)]
            for word in word_list:
                bigram_word += word
                continue
            bigram_list.append(bigram_word)
            bigram_word =''
            i +=1
        return bigram_list

    elif(type=='char'):
        temp_char_list=[]
        temp_bigram_list=[]
        bigram_char = ''
        bigram_list = []
        for temp_char in msg:
            if(temp_char==' ' or temp_char==','):
                continue
            else:
                temp_char_list.append(temp_char)
            continue
        i=0
#temp_listからNで指定した数のrangeを設定した文字数を取得してword_listへ代入
        while(i<int(len(temp_char_list))-1):
            temp_bigram_list=temp_char_list[i:(N+i)]
            for char in temp_bigram_list:
                bigram_char += char
                continue
            bigram_list.append(bigram_char)
            bigram_char=''
            i+=1
            continue
        return bigram_list

if __name__ == "__main__":
        msg = "I am an NLPer"
        N=2
        type='char'
#       type='word'
        bigram_list = ngram(msg,N,type)
        print(bigram_list)

result
type=charの場合
['Ia', 'am', 'ma', 'an', 'nN', 'NL', 'LP', 'Pe', 'er']
Process finished with exit code 0

type=wordの場合
['Iam', 'aman', 'anNLPer']
Process finished with exit code 0

感想:ngramってなに?ってところからスタート。これほんと初級の内容かよ。。。難しかった。。。

22
46
4

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
22
46