Pythonで Letter Queue
コード
# coding:utf-8 def letter_queue(c): r = [] for i in c: s = i.split() if len(s) == 2 and s[0] == "PUSH": r.extend(s[1]) if len(s) == 1 and r != []: r = r[1:] if r == []: return "" else: return "".join(r) if __name__ == '__main__': letter_queue(["PUSH A", "POP", "POP", "PUSH Z", "PUSH D", "PUSH O", "POP", "PUSH T"]) #== "DOT", "dot example" letter_queue(["POP", "POP"]) #== "", "Pop, Pop, empty" letter_queue(["PUSH H", "PUSH I"])# == "HI", "Hi!" letter_queue([])# == "", "Nothing"
PythonでSimple Areas
コード
import math def simple_areas(*args): m = args if len(args) == 1: r = m[0] / 2.0 print round(r * r * math.pi,2) elif len(args) == 2: print m[0] * m[1] elif len(args) == 3: if m[0] == m[1] == m[2]: m = sorted(m) print round(m[0] * m[1] * math.sqrt(3) / 4 , 2) else: s = (m[0] + m[1] + m[2]) / 2.0 print round(math.sqrt(s*(s - m[0])*(s - m[1]) * (s - m[2])) , 2) if __name__ == '__main__': simple_areas(3)#, 7.07), "Circle" simple_areas(2, 2)#, 4), "Square" simple_areas(2, 3)#, 6), "Rectangle" simple_areas(3, 5, 4)#, 6), "Triangle" simple_areas(1.5, 2.5, 2)#, 1.5), "Small triangle" simple_areas(1,1,1) simple_areas(10,2,9)
PythonでMorse Clock
コード
def checkio(time_string): s = time_string.encode("utf-8").split(":") b = "" for i in s: i = i.zfill(2) li = list(i) if b == "": b = b + format(int(li[0]),"b").zfill(2) + " " else: b = b + format(int(li[0]),"b").zfill(3) + " " b = b + format(int(li[1]),"b").zfill(4) + " : " b = b.replace("1","-").replace("0",".") print b[:-3] if __name__ == '__main__': #These "asserts" using only for self-checking and not necessary for auto-testing #checkio(u"10:37:49") #== ".- .... : .-- .--- : -.. -..-", "First Test" #checkio(u"21:34:56")# == "-. ...- : .-- .-.. : -.- .--.", "Second Test" checkio(u"00:1:02") #== ".. .... : ... ...- : ... ..-.", "Third Test" #checkio(u"23:59:59") #== "-. ..-- : -.- -..- : -.- -..-", "Fourth Test"
参考にしたサイト
一言
- 継続は力なり
- 工夫したところは、HHの最初だけzfill(2)で0詰めした所
- 工夫したところは、li = list(i)で二桁の数字を一桁ずつのリストに分割した所