[Python] Challenge 100 knocks! (000-005)

Introduction

Originally, I was a network engineer who did all the work of designing, building, maintaining and operating an in-house network, but at one point I became involved in in-house network security. I was involved in security operations using IPS and WAF, but as things I didn't know at all came out, I wanted to learn more about security. From there, when studying security, it became necessary to "handle a large amount of logs more as desired", "automate processing", and "apply a simple exploit code", so I decided to study python after considering various things. .. We will reach this challenge. I will post the challenge content to Qiita as my memorandum.

About programming history and application development history

Degree of learning C language at university App development is about developing web applications with php long ago

What is 100 knocks?

Language processing 100 knocks 2015, provided by Professor Okazaki of Tohoku University's Inui-Okazaki Laboratory. This is a collection of problems. (Thanks!)

Implementation environment

PC:MacBookPro2016 IDE:pycharm version:anaconda3-4.1.1

Knock status

9/24 added

000. Reverse order of strings

Get a string in which the characters of the string "stressed" are arranged in reverse (from the end to the beginning).

000.py


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

result


stressed
desserts
Process finished with exit code 0

Impressions: Just add -1 in the slice option

001. "Patatokukashi"

Take out the 1st, 3rd, 5th, and 7th characters of the character string "Patatokukashi" and get the concatenated character string.

001.py


msg  = "Patatoku Kashii"
print(msg)
msg_find = msg[::2]
print(msg_find)

result


Patatoku Kashii
Police car
Process finished with exit code 0

Impressions: Just add the slice option

002. "Police car" + "Taxi" = "Patatokukashi"

Get the character string "Patatokukashi" by alternately connecting the characters "Police car" + "Taxi" from the beginning.

002.py


msg1 = "Police car"
msg2= "taxi"
msg_add=''

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

print(msg_add)

result


Patatoku Kashii
Process finished with exit code 0

Impressions: The feeling of for sentences is different from C language ...

003. Pi

Break down the sentence "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics." Into words. Create a list of the number of characters (in the alphabet) of each word, arranged in order of appearance from the beginning.

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

Impressions :, etc. are not completely processed. .. .. 8/18 Update to hotfix

004. Element symbol

"Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can." Is decomposed into words, and the first, 5, 6, 7, 8, 9, 15, 16, 19th word is the first letter, and the other words are the first two letters. Create an associative array (dictionary type or map type) from to the position of the word (what number from the beginning).

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

Impressions: The if statement seems to be a little smarter

  1. n-gram Create a function that creates an n-gram from a given sequence (string, list, etc.). Use this function to get the word bi-gram and the letter bi-gram from the sentence "I am an NLPer".

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_Get the number of words with the range specified by N from list and word_Assign to 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_Get the number of characters for which the range specified by N is set from list and word_Assign to 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=For char
['Ia', 'am', 'ma', 'an', 'nN', 'NL', 'LP', 'Pe', 'er']
Process finished with exit code 0

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

Impressions: What is ngram? Start from that place. Is this really a beginner's content? .. .. was difficult. .. ..

Recommended Posts

[Python] Challenge 100 knocks! (015 ~ 019)
[Python] Challenge 100 knocks! (030-034)
[Python] Challenge 100 knocks! (000-005)
[Python] Challenge 100 knocks! (010-014)
[Python] Challenge 100 knocks! (025-029)
[Python] Challenge 100 knocks! (020-024)
python challenge diary ①
Challenge 100 data science knocks
Python
Sparta Camp Python 2019 Day2 Challenge
100 Pandas knocks for Python beginners
Challenge Python3 and Selenium Webdriver
Challenge LOTO 6 with Python without discipline
Image processing with Python 100 knocks # 3 Binarization
# 2 Python beginners challenge AtCoder! ABC085C --Otoshidama
Image processing with Python 100 knocks # 2 Grayscale
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Image processing with Python 100 knocks # 8 Max pooling
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
Installing Python 3.4.3.
Try python
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals
python underscore
Start python
[Python] Sort
Note: Python
Python basics ③
python log
Python basics
[Scraping] Python scraping
Python update (2.6-> 2.7)