CheckIO 题目解法(2)

1.  Roman numerals 将阿拉伯数字转成罗马数字,可以这样写,不过不是很高效:

def checkio(num):
    a_num = range(1, 11) + [50, 100, 500, 1000]
    r_num = ('I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X',
             'L', 'C', 'D', 'M')
    num_dict = (dict(zip(a_num, r_num)))
    res = ''
    if num == 0:
        return ''
    if num in a_num:
        return num_dict[num]
    else:
        str_num = str(num)
        length = len(str_num)
         
        if length == 2:
            first_num = int(str_num[0])
            last_num = int(str_num[1])
            if first_num <= 3:
                res += num_dict[first_num].replace('I', 'X') + 
                       num_dict[last_num]
            elif first_num == 4:
                res += ('XL' + num_dict[last_num])
            elif first_num == 5:
                res += ('L' + num_dict[last_num])
            elif first_num != 9:
                res += ('L' + num_dict[first_num - 5].replace('I', 'X') + num_dict[last_num])
            else:
                res += ('XC' + num_dict[last_num])
 
        if length == 3:
            first_num = int(str_num[0])
            if first_num <= 3:
                res += num_dict[first_num].replace('I', 'C') + 
                       checkio(int(str_num[1:]))
            elif first_num == 4:
                res += ('CD' + checkio(int(str_num[1:])))
            elif first_num == 5:
                res += ('D' + checkio(int(str_num[1:])))
            elif first_num != 9:
                res += ('D' + num_dict[first_num - 5].replace('I', 'C') + checkio(int(str_num[1:])))
            else:
                res += ('CM' + checkio(int(str_num[1:])))
 
        if length == 4:
            first_num = int(str_num[0])
            res += ('M' * first_num + checkio(int(str_num[1:])))
             
    return res

2. Transposed Matrix  转置矩阵 Python CookBook里的方法:

def checkio(data):
    return map(list, zip(*data))

3. The Angles of a Triangle 给出三角形三条边,求出来三个角度,需要用余弦定理(CosA = (b*b + c*c - a*a)/(2*b*c))

import math

def checkio(a, b, c):
    res = [0, 0, 0]
    if not (a+b>c and a+c>b and b+c>a):
        return res
    else:
        cos_A = (b**2 + c**2 - a**2) / (2.0*b*c)
        num1 = int(round(math.degrees(math.acos(cos_A)))) # 四舍五入方法 int(round(num))
        cos_B = (c**2 + a**2 - b**2) / (2.0*a*c)
        num2 = int(round(math.degrees(math.acos(cos_B))))
        num3 = 180 - num1 - num2
        res = sorted([num1, num2, num3])
    return res

4.restricted-sum 计算一个数字列表的加和,但是不许使用sum, for, while, import, reduce这几个keywords,可以写这么一行:

def checkio(alist):
    return eval('+'.join(map(str,alist)))

 如果再加一个不能使用'+'的条件,那就这样:

def checkio(alist):
    return eval(chr(43).join(map(str,alist)))

 5.x-o-referee 判断井字棋游戏是否获胜的问题:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

def checkio(game_status):
    # 判断3行
    row = [i[0] for i in game_status if i[0] in 'XO' and i.count(i[0]) == 3]
    if row:
        return row[0]
    # 根据中心点判断
    center = game_status[1][1]
    if center in 'XO':
        if [center, game_status[0][2], game_status[2][0]].count(center) == 3:
            return center
        elif [center, game_status[0][0], game_status[2][2]].count(center) == 3:
            return center
    # 判断3列
    for i in xrange(0, 3):
        first = game_status[0][i]
        if first in 'XO':
            second = game_status[1][i]
            third = game_status[2][i]
            if [first, second, third].count(first) == 3:
                return first
    return 'D'    

 6.even-last 把一个list的偶数下标和最后一个元素乘积返回:

def checkio(array = None):
    if array:
        #print array[::2]
        return array[-1] * sum(array[::2]) 
    return 0

7.  most-numbers 计算一个list中的最大值和最小值的差:

def checkio(*array):
    if len(array) == 0:
        return 0
    return round(max(array) - min(array), 3)

 8. digits-multiplication 把一个数字的非0数相乘起来:

def checkio(num):
    return reduce(lambda a,b: a*b, map(lambda s_num: int(s_num) if (s_num!='0') else 1, str(num)))

 9.common-words 得到两个字符串按照','分割的相同单词:

def checkio(stra, strb):
   return ','.join(sorted(list(reduce(lambda a,b: a&b, map(lambda s: set(s), [i.split(',') for i in [stra, strb]])))))

 10. fizz-buzz问题:

def checkio(num):
    if num % 3 == 0 and num % 5 == 0:
        return "Fizz Buzz"
    if num % 3 == 0:
        return "Fizz"
    if num % 5 == 0:
        return "Buzz"
    return str(num)
原文地址:https://www.cnblogs.com/jaw-crusher/p/3493015.html