PythonでThe Most Wanted Letter

f:id:g_YUYUYU:20140427024244j:plain

コード3

def checkio(text):
	s = text.lower()
	print max("abcdefghijklmnopqrstuvwxyz", key=s.count)

コード2

import string

def checkio(text):
	s = text.lower()
	return max(string.ascii_lowercase, key=s.count)

一言

  • max()を忘れていた、公式ドキュメントからStringモジュールを見つけた。
  • string.ascii_lowercaseは "abcdefghijklmnopqrstuvwxyz"を指し示す。

コード1

#coding:utf-8
import re

def checkio(text):

	x = re.sub(re.compile("[!-/:-@[-`{-~]"), '', text)
	y = re.sub(re.compile("[0123456789]"), '', x)
	z = y.strip()
	t = sorted(list(z.lower()))
	count = 0
	chara = []
	for i in t:
		if t.count(i) > count and i.isalpha():
		count = t.count(i)
		chara = i
	if count == 1:
		return sorted(t)[0]
	elif count > 1: 
		return chara

if __name__ == '__main__':
	checkio("Hello World!")# == "l"
	checkio("Lorem ipsum dolor sit amet")# == "m"
	checkio("One")# == "e"
	checkio("Oops!")# == "o"
	checkio("AAaooo!!!!")# == "a"
	checkio("abe")# == "a"
	checkio("a-z")# == "a"
	checkio(" d ")
	checkio("12345,12345,12345 S 12345,12345")

一言

デバッグでゴリゴリゴリ押したコード