【CheckIO】ELECTRONIC STATION部分题目

2018-08-17 09:48:11来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Find Sequence

 1 def checkio(matrix):
 2     if len(matrix)<4:
 3         return False
 4     for i in range(len(matrix)-3):
 5         for j in range(len(matrix)-3):
 6             if (matrix[i][j] == matrix[i][j+1] and matrix[i][j] == matrix[i][j+2] and matrix[i][j] == matrix[i][j+3]) or (matrix[i][j] == matrix[i+1][j] and matrix[i][j] == matrix[i+2][j] and matrix[i][j] == matrix[i+3][j]) or (matrix[i][j] == matrix[i+1][j+1] and matrix[i][j] == matrix[i+2][j+2] and matrix[i][j] == matrix[i+3][j+3]):
 7                 return True
 8     for i in range(3,len(matrix)):
 9         for j in range(len(matrix)-3):
10             if (matrix[i][j] == matrix[i][j+1] and matrix[i][j] == matrix[i][j+2] and matrix[i][j] == matrix[i][j+3]) or (matrix[i][j] == matrix[i-1][j-1] and matrix[i][j] == matrix[i-2][j-2] and matrix[i][j] == matrix[i-3][j-3]):
11                 return True
12     for i in range(len(matrix)-3):
13         for j in range(3,len(matrix)):
14             if (matrix[i][j] == matrix[i+1][j] and matrix[i][j] == matrix[i+2][j] and matrix[i][j] == matrix[i+3][j]) or (matrix[i][j] == matrix[i+1][j-1] and matrix[i][j] == matrix[i+2][j-2] and matrix[i][j] == matrix[i+3][j-3]):
15                 return True
16     return False

The Hamming Distance

由于n和m的范围是0-10^6,所以需要用32位二进制。

1 def checkio(n, m):
2     x = n ^ m
3     st = bin(x & 0b11111111111111111111111111111111)
4     count = 0
5     for i in st:
6         count += 1 if i == "1" else 0
7     return count

Brackets

1 def checkio(Expression):
2     x = "".join(a for a in Expression if a in "{}()[]")
3     while ("()" in x) or ("[]" in x) or ("{}" in x):
4         x = x.replace("()", "")
5         x = x.replace("{}", "")
6         x = x.replace("[]", "")
7     return len(x) == 0

Roman Numerals

 1 def checkio(data):
 2     Roman_Numerals = (
 3     (1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'),
 4     (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I'))
 5     res = ""
 6     for i, j in Roman_Numerals:
 7         while data >= i:
 8             res += j
 9             data -= i
10     return res

The Longest Palindromic

我的思路就是把字符串截取得到各种长度的子字符串,然后判断截取到的字符串是不是回文字符串,如果是就保存在data字典里,键是该字符串,值是字符串的长度。对于得到的data字典根据字符串长度进行排序,然后用一个max_list存放最长长度的字符串,因为可能有多个字符串的长度相等,所以用一个列表来存放,最后返回距离开头较近的子字符串。

 1 def longest_palindromic(text):
 2     import re
 3     data = {}
 4     for i in range(1, len(text)+1):
 5         for j in range(0, i):
 6             if is_palindrome(text[j:i]):
 7                 data[text[j:i]] = len(text[j:i])
 8     s = sorted(data.items(), key=lambda x: x[1], reverse=True)
 9     max_list = []
10     for i in s:
11         max_list.append(i[0]) if i[1] == s[0][1] else 0
12 
13     if len(max_list) == 1:
14         return max_list[0]
15     else:
16         m = {}
17         for i in max_list:
18             m[i] = re.search(i, text).span()[0]
19         return sorted(m.items(), key=lambda x: x[1])[0][0]
20 
21 
22 def is_palindrome(text):
23     flag = 0
24     for i in range(len(text) // 2):
25         if text[i] != text[len(text) - i - 1]:
26             flag = 1
27             break
28     if flag:
29         return False
30     else:
31         return True

Reverse Roman Numerals

因为题目给出了数字的范围是1-4000,所以如果在这个范围内有一个数字对应的罗马字符串和给出的字符串相同,就返回该数字。

 1 def checkio(n):
 2     roman_numerals = {1000: 'M', 900: 'CM', 500: 'D', 400: 'CD', 100: 'C', 90: 'XC',
 3                       50: 'L', 40: 'XL', 10: 'X', 9: 'IX', 5: 'V', 4: 'IV', 1: 'I'
 4                       }
 5     roman_string = ""
 6     for key in sorted(roman_numerals.keys(), reverse=True):
 7         while n >= key:
 8             roman_string += roman_numerals[key]
 9             n -= key
10     return roman_string
11 
12 
13 def reverse_roman(n):
14     for i in range(1, 4000):
15         if checkio(i) == n:
16             return i

Date and Time Converter

 1 Month = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June',
 2          7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'}
 3 
 4 
 5 def date_time(time: str) -> str:
 6     # replace this for solution
 7     day = str(int(time[:2]))
 8     month = Month[int(time[3:5])]
 9     year = time[6:10] + " year"
10     hour = str(int(time[11:13])) + " hours" if int(time[11:13]) != 1 else str(int(time[11:13])) + " hour"
11     minute = str(int(time[14:16])) + " minutes" if int(time[14:16]) != 1 else str(int(time[14:16])) + " minute"
12     result = day + " " + month + " " + year + " " + hour + " " + minute
13     return result

Time Converter (12h to 24h)

 1 def time_converter(time):
 2     if time == '12:00 a.m.':
 3         return "00:00"
 4     elif time == '12:00 p.m.':
 5         return "12:00"
 6     else:
 7         if time[2] == ':':
 8             if int(time[:2]) < 12:
 9                 return str(int(time[:2]) + 12) + time[2:5] if 'p' in time else time[:5]
10             else:
11                 return time[:5]
12         else:
13             return str(int(time[:1]) + 12) + time[1:4] if 'p' in time else '0' + time[:4]

Multicolored Lamp

 1 colors = ['Green', 'Red', 'Blue', 'Yellow']
 2 
 3 
 4 class Lamp:
 5     def __init__(self):
 6         self.count = 0
 7 
 8     def light(self):
 9         if self.count == 4:
10             self.count = 0
11             color=colors[self.count]
12         else:
13             color=colors[self.count]
14             self.count += 1
15         return color

Army Battles

这个题目要注意到的是在士兵对拼时如果一方生命值<=0,则需要让新的士兵加入到战争中,而且新的士兵是和上场战斗中活下来的士兵对拼。

 1 class Army():
 2     def __init__(self):
 3         self.health = 0
 4         self.attack = 0
 5         self.num = 0
 6 
 7     def add_units(self, x, num):
 8         self.health = x().health
 9         self.attack = x().attack
10         self.num = num
11 
12 
13 class Battle:
14     def fight(self, army1, army2):
15         x2 = y2 = 0
16         while army1.num > 0 and army2.num > 0:
17             x1 = army1.health if x2 == 0 else x2
18             y1 = army2.health if y2 == 0 else y2
19             while True:
20                 y1 -= army1.attack
21                 if y1 <= 0:
22                     x2 = x1
23                     y2 = 0
24                     army2.num -= 1
25                     break
26                 x1 -= army2.attack
27                 if x1 <= 0:
28                     y2 = y1
29                     x2 = 0
30                     army1.num -= 1
31                     break
32         if army1.num:
33             return True
34         else:
35             return False
36 
37 
38 class Warrior:
39     health = 50
40     attack = 5
41     is_alive = True
42 
43 
44 class Knight(Warrior):
45     health = 50
46     attack = 7
47     is_alive = True

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:python3中urllib库的request模块详解

下一篇:Python字典基本操作