CheckIO Scientific Expedition


一些未解锁,持续更新

题目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

未解锁


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值