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.
Degree of learning C language at university App development is about developing web applications with php long ago
Language processing 100 knocks 2015, provided by Professor Okazaki of Tohoku University's Inui-Okazaki Laboratory. This is a collection of problems. (Thanks!)
PC:MacBookPro2016 IDE:pycharm version:anaconda3-4.1.1
9/24 added
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
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
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 ...
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
"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
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