Common Words

1 def checkio(first, second):
2     first = first.split(',')
3     second = second.split(',')
4     res = []
5     for i in first:
6         if i in second:
7             res.append(i)
8     res = sorted(res)
9     return ','.join(res) if len(res) > 0 else ""

The Angles of a Triangle

 1 def checkio(a: int, b: int, c: int) -> List[int]:
 2     # replace this for solution
 3     import math
 4     if a + b > c and a + c > b and b + c > a:
 5         A = math.degrees(math.acos((a * a - b * b - c * c) / (-2 * b * c)))
 6         B = math.degrees(math.acos((b * b - a * a - c * c) / (-2 * a * c)))
 7         C = math.degrees(math.acos((c * c - a * a - b * b) / (-2 * a * b)))
 8         res = sorted([round(A), round(B), round(C)])
 9         return res
10     else:
11         return [0, 0, 0]

Friendly Number

 1 from math import log
 2 
 3 
 4 def friendly_number(number, base=1000, decimals=0, suffix='',
 5                     powers=['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']):
 6     """
 7     Format a number as friendly text, using common suffixes.
 8     """
 9     # 负数的处理
10     if number < 0:
11         signal = '-'
12         number = -1 * number
13     else:
14         signal = ''
15 
16     # 小于base的处理
17     if number < base and decimals == 0:
18         # print('case1',signal + str(number) + powers[0] + suffix)
19         return signal + str(number) + powers[0] + suffix
20     elif number < base and decimals != 0:
21         # print('case2',signal + str(number) + '.' + '0'*decimals + powers[0] + suffix)
22         return signal + str(number) + '.' + '0' * decimals + powers[0] + suffix
23     else:
24         root = int(log(number, base))
25 
26     # 若root值大于powers长度,取最大
27     if root >= len(powers):
28         number = number / (base ** (len(powers) - 1))
29         max_power = powers[-1]
30     else:
31         number = number / (base ** root)
32         max_power = powers[root]
33 
34     # 此时number为形如'1.3234M','24232.223d'的前缀数字,下面保留小数
35     zeros = ''
36     if decimals == 0:
37         # print('case3',signal+str(int(number))+max_power+suffix)
38         return signal + str(int(number)) + max_power + suffix
39     number = round(number, decimals)  # 直接保留小数
40     decimals = decimals - len(str(number - int(number))[2:])
41     # 若小数位数不够,则补0
42     while decimals > 0:
43         zeros += '0'
44         decimals -= 1
45     # print('case4',signal+str(number)+'.'+zeros+max_power+suffix)
46     return signal + str(number) + zeros + max_power + suffix

The Fastest Horse

思路就是先求出每场比赛第一的马的序号,然后再统计出现次数最多的序号是哪个,就得到跑的最快的马是哪一匹。

 1 def fastest_horse(horses: list) -> int:
 2     # replace this for solution
 3     import collections
 4     data = []
 5     for horse in horses:
 6         d = {}
 7         for i in range(len(horse)):
 8             d[i + 1] = horse[i]
 9         data.append(d)
10     fast = []
11     for i in data:
12         f = sorted(i.items(), key=lambda x: x[1])[0][0]
13         fast.append(f)
14     fastest = collections.Counter(fast).most_common(1)
15     return fastest[0][0]

Hacker Language

由题意可知HackerLanguage类里应该有明文和密文两个属性,write()就相当于对密文进行加密操作,read()就相当于对明文进行解密操作,send()就是返回明文,delete(n)是删除密文末尾的n个字符。

因为日期和时间是指定的格式,在加密解密的时候并没有发生变化,这个可以用正则表达式进行匹配,如果是日期或者时间,就不用进行加密解密。

如果出现[‘.’, ‘:’, ‘!’, ‘?’, ‘@’, ‘$’, ‘%’]其中的字符,也不用进行加密解密,对于空格(’1000000‘)要单独拿出来进行考虑。

对于英文字母,其ASCII码值对应的二进制是七位,先用ord()获取其ASCII码值,再用bin()方法获得对应的二进制。

 

 1 import re
 2 
 3 day = r'[0-9][0-9][.][0-9][0-9][.][0-9][0-9][0-9][0-9]'
 4 time = r'[0-9][0-9]:[0-9][0-9]'
 5 other = ['.', ':', '!', '?', '@', '$', '%']
 6 
 7 
 8 class HackerLanguage:
 9     def __init__(self):
10         self.cipher = ""  # 密文
11         self.clear = ""  # 明文
12 
13     def write(self, text):
14         self.cipher += text
15         txt = ""
16         i = 0
17         while i < len(self.cipher):
18             if self.cipher[i].isalpha(): 
19                 txt += bin(ord(self.cipher[i])).lstrip('0b')
20                 i += 1
21             elif self.cipher[i] in other:
22                 txt += self.cipher[i]
23                 i += 1
24             elif self.cipher[i] == ' ':
25                 txt += '1000000'
26                 i += 1
27             elif re.match(time, self.cipher[i:i + 5]):
28                 txt += self.cipher[i:i + 5]
29                 i += 5
30             elif re.match(day, self.cipher[i:i + 10]):
31                 txt += self.cipher[i:i + 10]
32                 i += 10
33         self.clear = txt
34 
35     def delete(self, n):
36         self.cipher = self.cipher[:-n]
37         self.write('')
38 
39     def send(self):
40         return self.clear
41 
42     def read(self, text):
43         txt = ""
44         i = 0
45         while i < len(text):
46             if text[i:i + 7] == '1000000':
47                 i += 7
48                 txt += ' '
49             elif text[i] in other:
50                 txt += text[i]
51                 i += 1
52             elif re.match(time, text[i:i + 5]):
53                 print(text[i:i + 5])
54                 txt += text[i:i + 5]
55                 i += 5
56             elif re.match(day, text[i:i + 10]):
57                 print(text[i:i + 10])
58                 txt += text[i:i + 10]
59                 i += 10
60             else:
61                 txt += chr(int(text[i:i + 7], 2))
62                 i += 7
63         return txt

 

版权声明:本文为TM0831原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/TM0831/p/9453589.html