Find Sequence The Hamming Distance 由於n和m的範圍是0-10^6,所以需要用32位二進位。 Brackets Roman Numerals The Longest Palindromic 我的思路就是把字元串截取得到各種長度的子字元串,然後判斷截取到的字元串是不是 ...
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