CheckIO Scientific Expedition
- 题目1:Absolute Sorting
- 题目2:Remove Accents
- 题目3:Goes Right After
- 题目4:The Hidden Word
- 题目5:Time Converter (24h to 12h)
- 题目6:Sum by Type
- 题目7:Bird Language
- 题目8:Common Words
- 题目9:Follow Instructions
- 题目10:Pangram
- 题目11:Caps Lock
- 题目12:The Most Wanted Letter
- 题目13:Letter Queue
- 题目14:Striped Words
- 题目15:Conversion from CamelCase
- 题目16:Conversion into CamelCase
- 题目17:Secret Message
- 题目18:Remove Brackets
- 题目19:YAML. Simple Dict
- 题目20:YAML. More Types
- 题目21:Morse Clock
- 题目22:I Love Python!
- 题目23:Call to Home
- 题目24:Cipher Map
一些未解锁,持续更新
题目1:Absolute Sorting
def checkio(numbers_array: tuple) -> list:
return sorted(numbers_array,key=lambda x:abs(x))
题目2:Remove Accents
未解锁
题目3:Goes Right After
def goes_after(word: str, first: str, second: str) -> bool:
try:
index = word.index(first)
return index+1 == word.index(second)
except:
return False
题目4:The Hidden Word
def checkio(text, word):
text = text.lower().replace(' ','').split('\n')
for colume, y in enumerate(text):
if y.find(word) != -1:
return [colume+1,y.find(word)+1,colume+1,y.find(word)+len(word)]
for row in range(len(text)-len(word)+1):
for colume in range(len(text[row])):
try:
if all(text[i][colume] == word[i-row] for i in range(row,row+len(word))):
return [row+1,colume+1,row+len(word),colume+1]
except:
break
题目5:Time Converter (24h to 12h)
def time_converter(time):
min,hour = time.split(':')[1] , int(time.split(':')[0])
return f"{(hour-1)%12+1}:{min} {'ap'[hour>11]}.m."
题目6:Sum by Type
from typing import Tuple
def sum_by_types(items: list) -> Tuple[str, int]:
string_sum = ''
string = [x for x in items if type(x)==str]
num = [x for x in items if type(x)==int]
for x in string:
string_sum += x
return (string_sum,sum(num))
题目7:Bird Language
VOWELS = "aeiouy"
def translate(phrase):
new_phrase = ''
skip = 0
for x in phrase:
if x in VOWELS and skip == 0:
new_phrase += x
skip = 3
elif x not in VOWELS and skip == 0 and x != ' ':
new_phrase += x
skip = 2
elif x == ' ':
new_phrase += x
skip += 1
skip -= 1
return new_phrase
题目8:Common Words
def checkio(line1: str, line2: str) -> str:
line1 = line1.split(',')
line2 = line2.split(',')
new_line = [x for x in line2 if x in line1]
return ','.join(sorted(new_line))
题目9:Follow Instructions
def follow(instructions):
forward = instructions.count('f')
left = instructions.count('l')
right = instructions.count('r')
back = instructions.count('b')
return (right-left,forward-back)
题目10:Pangram
def check_pangram(text):
text = ''.join(x.lower() for x in text if x.isalpha())
return len(set(text)) == 26
题目11:Caps Lock
def caps_lock(text: str) -> str:
new_text = ''
a = False
for x in text:
if x == 'a' and a == False:
a = True
continue
elif x == 'a' and a == True:
a = False
continue
if a == False:
new_text += x
if a == True:
new_text += x .upper()
new_text = new_text.replace('a','')
return new_text
题目12:The Most Wanted Letter
def checkio(text: str) -> str:
text = text.lower()
set_text = sorted(set(x.lower() for x in text if x.isalpha()))
max= 0
for x in set_text:
if max < text.count(x):
index = x
max = text.count(x)
return index
法二:使用collections
import collections
def checkio(text: str) -> str:
text = "Hello World!"
text = [i for i in sorted(text.lower()) if i.isalpha()]
text = collections.Counter(text)
return text.most_common()[0][0]
题目13:Letter Queue
未解锁
题目14:Striped Words
未解锁
题目15:Conversion from CamelCase
def from_camel_case(name):
name = ''.join(list('_'+x.lower() if x.isupper() else x for x in name))
return name.lstrip('_')
题目16:Conversion into CamelCase
def to_camel_case(name):
return ''.join(list(x.capitalize() for x in name.split('_')))
题目17:Secret Message
import re
def find_message(message: str) -> str:
return ''.join(re.findall('[A-Z]',message))
题目18:Remove Brackets
from itertools import combinations
BRACKETS = {'{':'}', '(':')', '[':']'}
def remove_brackets(a):
def filtered(excluded):
for i, c in enumerate(a):
if i not in excluded:
yield c
def valid(excluded):
expected = []
for c in filtered(excluded):
if c in BRACKETS:
expected.append(BRACKETS[c])
elif not(expected and expected.pop() == c):
return False
return not expected
for r in range(len(a) + 1):
for ex in combinations(range(len(a)), r):
if valid(ex):
return ''.join(filtered(ex))
def remove_brackets(text):
from itertools import combinations
brackets, size = ("()", "[]", "{}"), len(text)
if text in brackets:
return text
pairs = [(x, y) for x, y in combinations(range(size), 2)
if text[x]+text[y] in brackets]
rb = remove_brackets
result = [text[x]+rb(text[x+1:y])+text[y]+rb(text[y+1:])
for x, y in pairs]
return max(result[::-1], key=len, default='')
题目19:YAML. Simple Dict
def yaml(a):
dic = {}
a = list(x for x in sorted(a.split('\n')) if x != '')
for x in a:
try:
dic[x.split(': ')[0]] = int(x.split(': ')[1])
except:
dic[x.split(': ')[0]] = x.split(': ')[1]
return dic
题目20:YAML. More Types
from ast import literal_eval
def yaml(a):
# a = 'coding:'
dic = {}
a = list(x for x in sorted(a.split('\n')) if x != '')
#print(a)
for x in a:
try:
dic[x.split(': ')[0]] = int(x.split(': ')[1])
print(dic)
continue
except:
pass
try:
dic[x.split(': ')[0]] = literal_eval(x.split(': ')[1])
print(dic)
continue
except:
pass
try:
if x.split(': ')[1] == 'false':
dic[x.split(': ')[0]] = False
elif x.split(': ')[1] == 'true':
dic[x.split(': ')[0]] = True
elif x.split(': ')[1] == 'null':
dic[x.split(': ')[0]] = None
else:
dic[x.split(': ')[0]] = x.split(': ')[1]
print(dic)
continue
except:
dic[x.rstrip(':')] = None
return dic
题目21:Morse Clock
未解锁
题目22:I Love Python!
def i_love_python():
return "I love Python!"
题目23:Call to Home
未解锁
题目24:Cipher Map
未解锁