Skip to content

Instantly share code, notes, and snippets.

@seven0525
Last active June 3, 2018 14:55
Show Gist options
  • Save seven0525/ef3de1929d358485a1b5e9245d143e96 to your computer and use it in GitHub Desktop.
Save seven0525/ef3de1929d358485a1b5e9245d143e96 to your computer and use it in GitHub Desktop.
「自作Python100本ノック」8日目(半年ぶりの運動:41本〜52本目) ref: https://qiita.com/ahpjop/items/170c7450604c00f37230
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."
#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())
secret = "\U0001f4a4"
secret
#Unicode名の表示
import unicodedata
print(unicodedata.name(secret))
pop_bytes = secret.encode("utf-8")
pop_bytes
pop_string = pop_bytes.decode("utf-8")
pop_string
import re
sentence = "Chicken Little"
#ソースの先頭が、指定したパターンと一致しているか
m = re.match("Chi", sentence)
if m:
print(m.group())
#ソース内に、指定したパターンと一致しているか
m1 = re.match(".*ttle", sentence)# .*(ワイルドカード)を加えることによって先頭じゃない場合もヒットさせることができる、
if m1:
print(m1.group())
ms = re.search("ttle", sentence) # search()を使うとワイルドカード不要
if ms:
print(ms.group())
#”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
m = re.sub("n", "s", sentence)
m
pat = r'\bc\w*'
re.findall(pat, poetry)
#\bで単語同と日非単語の境界を先頭にするという意味である。単語の先頭か末尾を指定するために使う。
#リテラルのcは探している単語の先頭文字.
#\wは任意の単語文字
#*は、前の単語の文字が0個以上という意味
#rは未処理の文字列。(これがないと\bをバックスペースだと認識してしまうので、サーチは失敗する)
par = r'\bc\w{3}\b'
re.findall(par, poetry)
#\bをつけると「単語」のみ取り出せる。つけないとcで始まる全ての単語の4文字が返されてしまう。
pat_3 = r'\b\w*r\b'
re.findall(pat_3, poetry)
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