CheckIO-ELECTRONIC STATION
- 题目1:Sum Numbers
- 题目2:Sort by Extension
- 题目3:Digits Multiplication
- 题目4:Acceptable Password II
- 题目5:Acceptable Password III
- 题目6:Acceptable Password IV
- 题目7:Acceptable Password V
- 题目8:Acceptable Password VI
- 题目9:All Upper II
- 题目10:Ascending List
- 题目11:Boolean Algebra
- 题目12:Brackets
- 题目13:Find Sequence
- 题目14:Surjection Strings
- 题目15:Mathematically Lucky Tickets
- 题目16:Similar Triangles
- 题目17:Verify Anagrams
- 题目18:What Is Wrong With This Family?
- 题目19:Inside Block
- 题目20:Can You Pass?
- 题目21:Unix Match. Part 1
- 题目22:Unix Match. Part 2
有些题目尚未解锁,解锁之后再更新
题目1:Sum Numbers
判断所给单词在句子中是否是按顺序出现的。
回答:
def words_order(text: str, words: list) -> bool:
# text = 'hi world im here'
# words = ['world', 'here']
try:
order = []
[text.split().index(x) for x in words]
for x in words:
order.append(text.index(x))
if order == sorted(order) and len(set(words)) == len(words):
return True
else:
return False
except:
return False
题目2:Sort by Extension
以文件名的扩展名进行排序
from typing import List
def fun_special(files: List[str]) -> List[str]:
return sorted(files)
def fun_normal(files: List[str]) -> List[str]:
files = [[x.rsplit('.',1)[-2],x.rsplit('.',1)[-1]] for x in files]
files = sorted(files,key=lambda x:(x[1] , x[0]))
return ['.'.join(x) for x in files]
def sort_by_ext(files: List[str]) -> List[str]:
special = []
normal = []
for x in files:
if '' in x.rsplit('.',1):
special.append(x)
else:
normal.append(x)
return fun_special(special) + fun_normal(normal)
题目3:Digits Multiplication
所给数字中,计算所有除了0以为的数字的乘积
def checkio(number: int) -> int:
number = (str(number).replace('0',''))
a = 1
for x in number:
a *= int(x)
return a
题目4:Acceptable Password II
判断一个字符串有六个以上的字符,且有最少一个数字
import re
def is_acceptable_password(password: str) -> bool:
if bool(re.search(r'\d', password)):
return len(password)>6
return False
题目5:Acceptable Password III
判断一个字符串有六个以上的字符,且有最少一个数字,但不能全是数字
import re
def is_acceptable_password(password: str) -> bool:
if bool(re.search('[0-9]',password)) and not password.isdigit():
return len(password)>6
else:
return False
题目6:Acceptable Password IV
判断一个字符串有六个以上的字符,且有最少一个数字,但不能全是数字,当字符串长度大于9时,则返回True
import re
def is_acceptable_password(password: str) -> bool:
if bool(re.search('[0-9]',password)) and not password.isdigit():
return len(password)>6
return len(password)>9
题目7:Acceptable Password V
判断一个字符串有六个以上的字符,且有最少一个数字,但不能全是数字,当字符串长度大于9时,则返回True,但是整个字符串不能以任何形式出现password
import re
def is_acceptable_password(password: str) -> bool:
if bool(re.search('password', password, re.IGNORECASE)):
return False
else:
if bool(re.search('[0-9]',password)) and not password.isdigit():
return len(password)>6
return len(password)>9
题目8:Acceptable Password VI
判断一个字符串有六个以上的字符,且有最少一个数字,但不能全是数字,当字符串长度大于9时,则返回True,但是整个字符串不能以任何形式出现password,即使长度超过十,也需要有三个不同的字符
import re
def is_acceptable_password(password: str) -> bool:
# password = 'aaaaaa1'
if bool(re.search('password', password, re.IGNORECASE)):
return False
else:
if len(set(password)) < 3:
return False
if bool(re.search('[0-9]',password)) and not password.isdigit():
return len(password)>6
return len(password)>9
题目9:All Upper II
判断所给字符串是否全为大写
def is_all_upper(text: str) -> bool:
return text.isupper()
题目10:Ascending List
判断所给数列是否递增
from typing import Iterable
def is_ascending(items: Iterable[int]) -> bool:
return sorted(items) == items and len(set(items)) == len(items)
题目11:Boolean Algebra
未解锁
题目12:Brackets
未解锁
题目13:Find Sequence
未解锁
题目14:Surjection Strings
判断两个所给字符是否是相同的格式匹配
def isometric_strings(str1: str, str2: str) -> bool:
for x,y in zip(str1,str2):
str1 = str1.replace(x,y)
return str1 == str2
题目15:Mathematically Lucky Tickets
给出一串组合数字,随意组合判断是否有一种组合使其为100
!!!
from itertools import product
def possible_results(data):
yield int(data)
for i in range(1, len(data)):
for (x,y) in product(
possible_results(data[:i]), possible_results(data[i:])
):
yield from (x+y, x-y, x*y)
if y:
yield x/y
题目16:Similar Triangles
判断两个三角形是否为相似三角形
from typing import List, Tuple
Coords = List[Tuple[int, int]]
from itertools import combinations
import numpy as np
def side_lenght(coord):
a = []
for point in list(combinations(iter(coord),2)):
a.append(distance(point))
return a
def distance(point):
point = list(point)
d1 = np.sqrt(np.sum(np.square(np.array(point[0]) - np.array(point[1]))))
return d1
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
c1 = sorted(side_lenght(coords_1))
c2 = sorted(side_lenght(coords_2))
if c1[0]/c2[0] == c1[1]/c2[1] and c1[1]/c2[1] == c1[2]/c2[2]:
return True
return False
题目17:Verify Anagrams
两个字符串是否是同构异序词。即是否一个字符串可以由另一个字符串改变顺序得到。
def verify_anagrams(a, b):
a = a.upper().replace(' ','')
b = b.upper().replace(' ','')
return sorted(a) == sorted(b)
题目18:What Is Wrong With This Family?
判断所给家谱图是否正确
from collections import defaultdict
def is_family(tree):
father =[];
son =[];
for a in tree:
father.append(a[0]);
son.append(a[1]);
father = set(father)
##自己不能做自己爸爸的爸爸
for x in tree:
for y in tree:
if x[0]==y[1] and y[0]==x[1]:
return False
##自己不能做自己的爸爸
for x,y in tree:
if x==y:
return False
if len(son) != len(set(son)):
return False
for a in list(father):
if a in son:
father.remove(a)
if len(father) == 1:
return True
return False
其他人的答案
def is_family(tree):
anscestor = defaultdict(set)
for father, son in tree:
if father == son: return False
if father in anscestor[son]: return False
if son in anscestor[father]: return False
if anscestor[father] & anscestor[son]: return False
anscestor[son] |= {father} | anscestor[father]
adam = [person for person in anscestor if not anscestor[person]]
return len(adam) == 1
题目19:Inside Block
未解锁
题目20:Can You Pass?
未解锁
题目21:Unix Match. Part 1
判断所给unix格式的文件名是否和所给文件名相符合。
#checkio不支持fnmatch
from fnmatch import fnmatch
def unix_match(filename: str, pattern: str) -> bool:
return bool(fnmatch(filename,pattern))
使用正则化匹配
import re
def unix_match(filename: str, pattern: str) -> bool:
return bool(re.fullmatch(pattern.replace('.', '\.').replace('*', '.*').replace('?', '.'), filename))
题目22:Unix Match. Part 2
#checkio不支持fnmatch
from fnmatch import fnmatch
def unix_match(filename: str, pattern: str) -> bool:
return bool(fnmatch(filename,pattern))
其他人的答案
from re import match
def unix_match(filename: str, pattern: str) -> bool:
pattern = pattern.replace('.', '\\.').replace('?', '.').replace('*', '.*')
pattern = pattern.replace('[!', '[^').replace('[]', '[^.]')
pattern = pattern.replace('[^]', '\\[!\\]')
return match(pattern, filename) is not None