码迷,mamicode.com
首页 > 其他好文 > 详细

Checkio代码闯关小计

时间:2018-04-16 20:42:32      阅读:430      评论:0      收藏:0      [点我收藏+]

标签:闯关

首字母大写并增加标点关卡(Correct Sentence):

def correct_sentence(text: str) -> str:
"""
returns a corrected sentence which starts with a capital letter
and ends with a dot.
"""

your code here

text=text[0].upper()+text[1:]
if text[-1]!=‘.‘:
    text=text+"."
return text

return text

if name == ‘main‘:
print("Example:")
print(correct_sentence("greetings, friends"))

# These "asserts" are used for self-checking and not for an auto-testing
assert correct_sentence("greetings, friends") == "Greetings, friends."
assert correct_sentence("Greetings, friends") == "Greetings, friends."
assert correct_sentence("Greetings, friends.") == "Greetings, friends."
assert correct_sentence("hi") == "Hi."
print("Coding complete? Click ‘Check‘ to earn cool rewards!")

截取第一个单词(First Word)
import re
def first_word(text: str) -> str:
"""
returns the first word in a given text.
"""

your code here

a = re.compile(r".*?([a-zA-Z‘]+)")

return a.match(text).group(1)

if name == ‘main‘:
print("Example:")
print(first_word("Hello world"))

# These "asserts" are used for self-checking and not for an auto-testing
assert first_word("Hello world") == "Hello"
assert first_word(" a word ") == "a"
assert first_word("don‘t touch it") == "don‘t"
assert first_word("greetings, friends") == "greetings"
assert first_word("... and so on ...") == "and"
assert first_word("hi") == "hi"
print("Coding complete? Click ‘Check‘ to earn cool rewards!")

第二个相同字符引索(Second Index)
def second_index(text: str, symbol: str):
"""
returns the second index of a symbol in a given text
"""

your code here

a=text.find(symbol, text.find(symbol) + 1)#find函数的嵌套使用查询第二个字符
if a==-1:
    a=None
return a

if name == ‘main‘:
print(‘Example:‘)
print(second_index("sims", "s"))

# These "asserts" are used for self-checking and not for an auto-testing
assert second_index("sims", "s") == 3, "First"
assert second_index("find the river", "e") == 12, "Second"
assert second_index("hi", " ") is None, "Third"
assert second_index("hi mayor", " ") is None, "Fourth"
assert second_index("hi mr Mayor", " ") == 5, "Fifth"
print(‘You are awesome! All tests are done! Go Check it!‘)

最大值的键输出(Best Stock)
def best_stock(data):

your code here

new_dict = {v: k for k, v in data.items()}#将键与值反转存入新的字典

return new_dict[ max(data.values())]#输出最大data的值并作为新的字典的键输出值

if name == ‘main‘:
print("Example:")
print(best_stock({
‘CAC‘: 10.0,
‘ATX‘: 390.2,
‘WIG‘: 1.2
}))

# These "asserts" are used for self-checking and not for an auto-testing
assert best_stock({
    ‘CAC‘: 10.0,
    ‘ATX‘: 390.2,
    ‘WIG‘: 1.2
}) == ‘ATX‘, "First"
assert best_stock({
    ‘CAC‘: 91.1,
    ‘ATX‘: 1.01,
    ‘TASI‘: 120.9
}) == ‘TASI‘, "Second"
print("Coding complete? Click ‘Check‘ to earn cool rewards!")

字符出现频率(Popular Words)
import re
def popular_words(text, words):

your code here

dic1={}

for k in range(len(words)):
    i = 0
    a= re.finditer(words[k],text.lower())#返回全部相匹配字符串
    for match in a:

        i=i+1

    dic1[words[k]] = i#存入字典以及i(字符频率)
    k +=1

return dic1

if name == ‘main‘:
print("Example:")
print(popular_words(‘‘‘
When I was One,
I had just begun.
When I was Two,
I was nearly new.
‘‘‘, [‘i‘, ‘was‘, ‘three‘]))

# These "asserts" are used for self-checking and not for an auto-testing
assert popular_words(‘‘‘

When I was One,
I had just begun.
When I was Two,
I was nearly new.
‘‘‘, [‘i‘, ‘was‘, ‘three‘]) == {
‘i‘: 4,
‘was‘: 3,
‘three‘: 0
}
print("Coding complete? Click ‘Check‘ to earn cool rewards!")

Checkio代码闯关小计

标签:闯关

原文地址:http://blog.51cto.com/12903345/2104119

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!