1.binary-count 找出一个数字的二进制中1的个数:
checkio = lambda a: bin(a).count('1')
2.absolute-sorting 用绝对值进行排序:
def checkio(num_list): num_list = list(num_list) num_list.sort(cmp = lambda a,b:cmp(abs(a), abs(b))) return num_list
3.number-radix 任意进制转换,异常时候返回-1:
def checkio(s_num, base): try: return int(s_num, base) except ValueError: return -1
4.three-words 一个string中是否有三个连续单词,单词中不能出现非字母的字符:
def checkio(words): words_list = words.split(' ') length = len(words_list) if length < 3: return False is_word = map(lambda s: s.isalpha(), words_list) for i in xrange(0, length-2): if all((is_word[i], is_word[i + 1], is_word[i + 2])): return True return False
5.end-of-other 一些单词中是否有一个单词是以其他单词结尾的:
def checkio(words): words = list(words) length = len(words) if length == 1: return False if length == 2: if words[0].endswith(words[1]) or words[1].endswith(words[0]): return True #print words[0] #return True for i in xrange(length): for j in xrange(i + 1, length): if words[i].endswith(words[j]) or words[j].endswith(words[i]): return True return False
6. hamming-distance2 计算两个数字的汉明距离:
def checkio(n,m): return bin(n^m).count('1')
7. funny-adding checkio也有a+b问题:
def checkio(*arg): return sum(arg)