Last active
June 3, 2018 14:55
-
-
Save seven0525/ef3de1929d358485a1b5e9245d143e96 to your computer and use it in GitHub Desktop.
「自作Python100本ノック」8日目(半年ぶりの運動:41本〜52本目) ref: https://qiita.com/ahpjop/items/170c7450604c00f37230
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
poetry = "We have seen thee, queen of cheese, | |
Lying quietly at your ease, | |
Gently fanned by evening breeze, | |
Thy fair form no flies dare seize. | |
All gaily dressed soon you'll go | |
To the great Provincial show, | |
To be admired by many a beau | |
In the city of Toronto. | |
Cows numerous as a swarm of bees, | |
Or as the leaves upon the trees, | |
It did require to make thee please, | |
And stand unrivalled, queen of cheese. | |
May you not receive a scar as | |
We have heard that Mr. Harris | |
Intends to send you off as far as | |
The great world's show at Paris. | |
Of the youth beware of these, | |
For some of them might rudely squeeze | |
And bite your cheek, then songs or glees | |
We could not sing, oh! queen of cheese. | |
We'rt thou suspended from balloon, | |
You'd cast a shade even at noon, | |
Folks would think it was the moon | |
About to fall and crush them soon." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Laserクラス | |
class Laser: | |
def does(self): | |
return "disintegrate" | |
#Clawクラス | |
class Claw: | |
def does(self): | |
return "crush" | |
#Gunクラス | |
class Gun: | |
def does(self): | |
return "shoot" | |
#Robotクラス | |
class Robot: | |
def __init__(self): | |
self.laser = Laser() | |
self.claw = Claw() | |
self.gun = Gun() | |
def does(self): | |
return '''I have many attachments: My laser is to: %s, My claw is to: %s , My gun is to: %s ''' % ( | |
self.laser.does(), | |
self.claw.does(), | |
self.gun.does() ) | |
robbie = Robot() | |
print(robbie.does()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
secret = "\U0001f4a4" | |
secret | |
#Unicode名の表示 | |
import unicodedata | |
print(unicodedata.name(secret)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pop_bytes = secret.encode("utf-8") | |
pop_bytes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pop_string = pop_bytes.decode("utf-8") | |
pop_string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
sentence = "Chicken Little" | |
#ソースの先頭が、指定したパターンと一致しているか | |
m = re.match("Chi", sentence) | |
if m: | |
print(m.group()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ソース内に、指定したパターンと一致しているか | |
m1 = re.match(".*ttle", sentence)# .*(ワイルドカード)を加えることによって先頭じゃない場合もヒットさせることができる、 | |
if m1: | |
print(m1.group()) | |
ms = re.search("ttle", sentence) # search()を使うとワイルドカード不要 | |
if ms: | |
print(ms.group()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#”n”という文字だけヒット | |
m3 = re.findall("n", sentence) | |
m3 | |
print(len(m3)) | |
#"n"の後ろに任意の文字1字 | |
#sentenceの最後の"n"がマッチしていないことに注目 | |
m4 = re.findall("n.", sentence) | |
m4 | |
# 最後の"n"もマッチさせたい場合 | |
m5 = re.findall("n.?", sentence) # 0か1文字の直線の文字にマッチする(オプション) | |
m5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
m = re.sub("n", "s", sentence) | |
m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pat = r'\bc\w*' | |
re.findall(pat, poetry) | |
#\bで単語同と日非単語の境界を先頭にするという意味である。単語の先頭か末尾を指定するために使う。 | |
#リテラルのcは探している単語の先頭文字. | |
#\wは任意の単語文字 | |
#*は、前の単語の文字が0個以上という意味 | |
#rは未処理の文字列。(これがないと\bをバックスペースだと認識してしまうので、サーチは失敗する) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
par = r'\bc\w{3}\b' | |
re.findall(par, poetry) | |
#\bをつけると「単語」のみ取り出せる。つけないとcで始まる全ての単語の4文字が返されてしまう。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pat_3 = r'\b\w*r\b' | |
re.findall(pat_3, poetry) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pat_4 = r'\b\w*[aiueo]{3}[^aiueo\s]\w*\b' | |
re.findall(pat_4, poetry) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment