CheckIO GITHUB
题目1:The End of Other
from itertools import combinations
def checkio(words_set):
words_set = list(words_set)
for x,y in combinations(words_set,2):
if x == y[-len(x):] or y == x[-len(y):]:
return True
return False
import itertools
def checkio(words_set):
return any(a.endswith(b) or b.endswith(a) for a, b in itertools.combinations(words_set, 2))
题目2:The Cheapest Flight
from typing import List
import numpy as np
def cheapest_flight(costs: List, a: str, b: str) -> int:
length = []
for x in costs:
length += x[:2]
length = sorted(list(set(length)))
dictionary = {}
for shuzi , dizhi in enumerate(length):
dictionary[dizhi] = shuzi
now = a
destiction = b
flight = [10**5] * len(length)
flight[dictionary[a]] = 0
flight1 = []
while flight1 != flight:
flight1 = flight.copy()
for now in length:
for x in costs:
if now in x:
other = [i for i in x[:2] if i != now][0]
if flight[dictionary[now]] + x[-1] < flight[dictionary[other]]:
flight[dictionary[other]] = flight[dictionary[now]] + x[-1]
if flight[dictionary[b]] == 10**5:
return 0
return flight[dictionary[b]]
def dict_rever(dic,val):
return [key for key,value in dic.items() if value == val][0]
题目3:The Highest Building
未解锁
题目4:The Most Frequent Weekdays
取第一周和最后一周即可
import calendar
import datetime
def most_frequent_days(year):
firstweek = set(range(datetime.datetime(year,1,1).weekday(),7))
lastweek = set(range(datetime.datetime(year, 12, 31).weekday()+1))
return [calendar.day_name[day] for day in sorted((firstweek & lastweek)or (firstweek|lastweek))]
题目5:Create Intervals
未解锁
题目6:Merge Intervals
未解锁
题目7:Expand Intervals
from typing import Iterable
def expand_intervals(items: Iterable) -> Iterable:
new_items = []
for x in items:
new_items += expand_single(x)
return new_items
def expand_single(items):
return list(range(items[0],items[1]+1))
题目8:Long Repeat Inside
def repeat_inside(line):
max_line = ''
for i in range(2,len(line)+1):
for j in range(len(line)-i+1):
if repeat_string(line[j:j+i]):
if len(max_line) < i:
max_line = line[j:j+i]
print(max_line)
return max_line
def repeat_string(line):
for i in range(1,len(line)//2+1):
if len(line)%i == 0:
a = line[:i]
j = i
while j < len(line) and line[j:j+i] == a:
j += i
if j == len(line):
return True
return False
题目9:Painting Wall
##测试的时候会有一些较大的数据使list产生memoryerror
def checkio(required, operations):
length = len(operations)
op = 0
result = set()
while length != 0:
result.update(set(list_expand(operations[op])))
op += 1
if len(result)>=required:
return op
length -= 1
return -1
def list_expand(items):
return list(range(items[0],items[1]+1))
def checkio(required, operations):
length = len(operations)
section = []
op = 0
while length != 0:
op += 1
section.append(operations[op-1])
paint = paint_meter(paint_section(section))
print(section)
print(paint_section(section))
if paint >= required:
print(op)
return op
length -= 1
return -1
def paint_section(line):
line1 = []
rem = []
while len(line) != len(line1):
line1 = line.copy()
for i in range(len(line1)-1):
for j in range(i+1,len(line1)):
x,y = line1[i],line1[j]
if max(x) < min(y) or max(y) < min(x):
continue
else:
rem.append(i)
rem.append(j)
line.append([min(min(x),min(y)),max(max(x),max(y))])
for k in sorted(list(set(rem)))[::-1]:
del line[k]
rem = []
return line
def paint_meter(line):
meter =0
for x in line:
meter += abs(x[1]-x[0]+1)
return meter
题目10:Power Plants
题目11:
题目12:
题目13:
题目14:
题目15:
题目16:
题目17:
题目18:
题目19:
题目20:
题目21:
题目22:
题目23: