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:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值