CheckIO是一个用Python语言解决问题的网站,有一些好玩的问题可以试试解决一下.看起来很像是一场冒险,赶快开始吧!
1. Extra dashes 去除多余的"-"比如输入"I---Like---Python"输出"I-Like-Python" 可以这么写:
def checkio(line): return '-'.join([x for x in line.split('-') if x != ''])
2. Non-unique Elements 去除一个list中只出现一次的数字 可以这么写:
def checkio(data): return [i for i in data if data.count(i) != 1]
3. Median 找出一个list的中位数字,奇数个数字就是排序后的中间数,偶数个数字就是排序后的中间两个数字取均值 可以这么写:
def checkio(data): length = len(data) data.sort() if length % 2 == 0: res = (data[int(length / 2) - 1] + data[int(length / 2)]) / 2.0 else: res = data[int(length / 2)] return res
4.House password 判断口令是不是够strong,长度不少于10,至少一个数字,一个大写字母,一个小写字母 可以这么写:
1 import string 2 3 def checkio(data): 4 condtions = [False] * 4 5 if len(data) >= 10: 6 condtions[0] = True 7 for i in data: 8 if i in string.digits: 9 condtions[1] = True 10 elif i in string.ascii_lowercase: 11 condtions[2] = True 12 elif i in string.ascii_uppercase: 13 condtions[3] = True 14 return all(condtions)
5.The Most Wanted Letter 找出来最想要的字母: 出现次数最多,如果相同按照字母顺序a-z 优先级 可以这样做:
1 import string 2 3 def checkio(text): 4 low_text = text.lower() 5 # print low_text 6 index = 0 7 for c in range(len(low_text)): 8 if low_text[c] in string.ascii_lowercase: 9 if low_text.count(low_text[c]) > low_text.count(low_text[index]): 10 index = c 11 elif low_text.count(low_text[c]) == low_text.count(low_text[index]): 12 if string.ascii_lowercase.index(low_text[c]) < string.ascii_lowercase.index(low_text[index]): 13 index = c 14 return low_text[index]
6.Speech Module 从0-999的数字翻译成英文 最简单的就是挨个写一遍 不过太麻烦 可以这样做:
1 def checkio(num): 2 res = '' 3 one_num_dict = {'0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four','5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', 4 '10': 'ten', '11': 'eleven', '12': 'twelve', '13': 'thirteen', 5 '14': 'fourteen','15': 'fifteen', '16': 'sixteen', 6 '17': 'seventeen', '18': 'eighteen', '19': 'nineteen', 7 '20': 'twenty', '30': 'thirty', '40': 'forty', '50': 'fifty', 8 '60': 'sixty' , '70': 'seventy', '80': 'eighty', '90': 'ninety'} 9 # keys = one_num_dict.keys() 10 str_num = str(num) 11 if len(str_num) == 1: 12 res = one_num_dict[str_num] 13 elif len(str_num) == 2: 14 if str_num == '00': 15 return '' 16 if str_num[0] == '1': 17 res = one_num_dict[str_num] 18 else: 19 if str_num[1] == '0': 20 res = one_num_dict[str_num] 21 else: 22 res = one_num_dict[str_num[0] + '0'] + ' ' + checkio(int(str_num[1])) 23 else: 24 res = one_num_dict[str_num[0]] + ' ' + 'hundred ' + checkio(int(str_num[1:3])) 25 if res.endswith('zero'): 26 res = res[0:-5].strip() 27 return res.strip()
7.Find-Ip 从一个字串中匹配出来IP地址,不含前导0,最简单的做法是用正则表达式 不过自己实现的话 可以这么写:
def checkio(ip_string): res = [] words = ip_string.split(' ') words = filter(lambda x: len(x.split('.')) == 4, words) for word in words: is_ip = True nums = word.split('.') if not nums[0].isdigit() or int(nums[0]) == 0: is_ip = False for num in nums: if not num.isdigit(): is_ip = False break if (len(num) > 3) or (not (int(num) >= 0 and int(num) <= 255)): is_ip = False break if is_ip: res.append(word) return res
8.Brackets 监测一个表达式的括号是否正确的匹配了,可以使用一个list当栈来检查:
def checkio(expr): a_stack = [] for i in expr: if i in '({[': a_stack.append(i) elif i in '}])': if a_stack == []: return False else: poped = a_stack.pop() if poped == '(' and i != ')': return False elif poped == '[' and i != ']': return False elif poped == '{' and i != '}': return False return len(a_stack) == 0
9. Morse-Clock 转换10进制到2进制,比较简单 不过我写的效率...:
def checkio(timestr): numlist = timestr.split(':') alist = [] numlist = ["{:0>2d}".format(int(i)) for i in numlist] for i in numlist: for c in i: alist.append(bin(int(c))) res = '' res += "{:0>2d}".format(int(alist[0].replace('0b',''))) + ' ' res += "{:0>4d}".format(int(alist[1].replace('0b', ''))) + ' : ' res += "{:0>3d}".format(int(alist[2].replace('0b', ''))) + ' ' res += "{:0>4d}".format(int(alist[3].replace('0b', ''))) + ' : ' res += "{:0>3d}".format(int(alist[4].replace('0b', ''))) + ' ' res += "{:0>4d}".format(int(alist[5].replace('0b', ''))) res = res.replace('1', '-') res = res.replace('0', '.') return res
10.Striped Words 如果有两个字母都是元音和辅音的话不算,其他算一个:
1 import string 2 import re 3 4 def checkio(text): 5 re_str = "[?!;,. ]" 6 vowels = 'AEIOUY' 7 con = 'BCDFGHJKLMNPQRSTVWXZ' 8 count = 0 9 res = [] 10 words = [i for i in re.split(re_str, text) if i and len(i) != 1] 11 for word in words: 12 length = len(word) - 1 13 ismyword = True 14 for i in xrange(length): 15 if (word[i].upper() in vowels and word[i+1].upper() in vowels) 16 or (word[i].upper() in con and word[i+1].upper() in con) 17 or (word[i] in string.digits): 18 ismyword = False 19 break 20 if ismyword: 21 count += 1 22 return count