[Python] Challenge 100 knocks! (006-009)

About the history so far

Please refer to First Post

Knock status

9/24 added

006. Set

Find the set of character bi-grams contained in "paraparaparadise" and "paragraph" as X and Y, respectively, and find the union, intersection, and complement of X and Y, respectively. In addition, find out if the bi-gram'se'is included in X and Y.

set_006.py


from training.bigram_005 import ngram

msg1 = "paraparaparadise"
msg2 = "paragraph"
msg3 = "se"

type = 'char'
X_list = ngram(msg1,2,type)
Y_list = ngram(msg2,2,type)
msg3_list = ngram(msg3,2,type)

set_X=set(X_list)
set_Y=set(Y_list)
set_msg3=set(msg3_list)

plus_list = set_X | set_Y
multi_list = set_X & set_Y
sub_list = set_X - set_Y

print("X_list=",X_list)
print("Y_list=",Y_list)
print("set_X=",set_X)
print("set_Y=",set_Y)
print("plus_list=",plus_list)
print("multi_list=",multi_list)
print("sub_list=",sub_list)
print("set se_True if included in X",set_X.issuperset(set_msg3))
print("set se_True if included in Y",set_Y.issuperset(set_msg3))

result


X_list= ['pa', 'ar', 'ra', 'ap', 'pa', 'ar', 'ra', 'ap', 'pa', 'ar', 'ra', 'ad', 'di', 'is', 'se']
Y_list= ['pa', 'ar', 'ra', 'ag', 'gr', 'ra', 'ap', 'ph']
set_X= {'ar', 'ap', 'ra', 'is', 'se', 'ad', 'pa', 'di'}
set_Y= {'ar', 'ap', 'ra', 'ag', 'pa', 'ph', 'gr'}
plus_list= {'ar', 'ap', 'ra', 'is', 'ag', 'se', 'ad', 'pa', 'ph', 'di', 'gr'}
multi_list= {'pa', 'ar', 'ap', 'ra'}
sub_list= {'ad', 'di', 'se', 'is'}
set se_True if included in X True
set se_True if included in Y False

Process finished with exit code 0

Impressions: SET is amazing ... it's too convenient.

007. Sentence generation by template

Implement a function that takes arguments x, y, z and returns the string "y at x is z". Furthermore, set x = 12, y = "temperature", z = 22.4, and check the execution result.

template_007.py


from string import Template

def template_print(x,y,z):
    value ={'time':x,'name':y,'tempture':z}
    t = Template("$at time$name is$tempture")
    return print(t.substitute(value))

if __name__ == "__main__":
    x=12
    y='temperature'
    z=22.4
    template_print(x,y,z)

result


The temperature at 12:00 is 22.4
Process finished with exit code 0

Impressions: I've never heard of the template function, but I don't know how to use it. .. ..

008. Ciphertext

Implement the function cipher that converts each character of the given character string with the following specifications. If it is lowercase, replace it with the character (219 --character code). Other characters are output as they are. Use this function to encrypt / decrypt English messages

cipher_008.py


import re

def cipher(msg):
    pattern = re.compile("[a-z]")
    cipher_msg = ''
    for temp in msg:
        if pattern.match(temp):
            cipher_msg += chr(219-ord(temp))
            continue
        else:
            cipher_msg += temp
            continue
    return  cipher_msg

if __name__=="__main__":
    msg = "Cipher_msg_012345"
    print("Former msg=",msg)
    c = cipher(msg)
    print("encryption=",c)
    d = cipher(c)
    print("Decryption=",d)

result


Former msg= Cipher_msg_012345
encryption= Crksvi_nht_012345
Decryption= Cipher_msg_012345
Process finished with exit code 0

Impression: I learned that it cannot be calculated as an ascii code value without using ord or chr. And at first, I prepared another function for decryption processing, and thought that adding 219 would result in decryption, but that was not the case.

  1. Typoglycemia Create a program that randomly rearranges the order of the other letters, leaving the first and last letters of each word for the word string separated by spaces. However, words with a length of 4 or less are not rearranged. Give an appropriate English sentence ("I couldn't believe that I could actually understand what I was reading: the phenomenal power of the human mind.") And check the execution result.

typoglycemia_010.py


from training.word2list_003 import word2list
import random

#A function that randomizes strings
#random_Returns msg
def random_word(word):
    random_msg=''
    for temp in word:
#Do not process less than 4 characters
        if(len(temp)<=4):
            random_msg += temp
            random_msg += " "
            continue
        else:
#The first of the string is first_Store in str
#The beginning of the string is last_Store in str
            temp_length = len(temp)
            first_str= temp[0]
            last_str=temp[temp_length-1]
            random_word_list=[]
            random_word=''
            temp_random_list = []
#Do not process the beginning and end of the retrieved string
#Random by listing intermediate strings_word_Put in list
            for index,temp_word in enumerate(temp):
                if(index==0 or index==temp_length-1):
                    continue
                else:
                    random_word_list.append(temp_word)
                continue
#Randomly extracted from the listed strings, temp_random_Put in list
#Delete the extracted character string from the list of extraction sources
            for i in range(len(random_word_list)):
                temp_random_list.append(random.choice(random_word_list))
                random_word_list.remove(temp_random_list[i])
                continue
#Random list temp_Add to word
            for temp_word in temp_random_list:
                random_word+=temp_word
                continue
#Random by combining first, random and last_Put in msg
            random_msg += (first_str + random_word + last_str)
            random_msg += " "

    return  random_msg

if __name__ == "__main__":
    msg = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."
    word = word2list(msg)
    random = random_word(word)
    print(random)

result


I cdlonu't beivele that I could alalutcy uanserntdd what I was rindeag : the paeemnonhl pweor of the hmuan mind . 
Process finished with exit code 0

Impression: The point is to take the value at random and delete the acquired value from the list.

Recommended Posts

[Python] Challenge 100 knocks! (015 ~ 019)
[Python] Challenge 100 knocks! (030-034)
[Python] Challenge 100 knocks! (006-009)
[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 memo
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals
python underscore
Python summary
Start python
[Python] Sort
Note: Python
Python basics ③
python log