当前位置: 代码迷 >> 综合 >> Checkio python 练习之Elementary
  详细解决方案

Checkio python 练习之Elementary

热度:19   发布时间:2023-12-18 23:15:55.0

来源https://py.checkio.org 一个特别赞的游戏小岛练习网站

1.Say hi--------------------------------------------- ----------------------------------------

Input: Two arguments. String and positive integer.

Output: String.

Example:

say_hi("Alex", 32) == "Hi. My name is Alex and I'm 32 years old"
say_hi("Frank", 68) == "Hi. My name is Frank and I'm 68 years old"

def say_hi(name, age):"""Hi!"""# your code herereturn "Hi. My name is " + name + " and I'm " + str(age) + " years old"

2.Correct Sentence--------------------------------------------- ----------------------------------------

Pay attention to the fact that not all of the fixes is necessary. If a sentence already ends with a dot then adding another one will be a mistake.

Input: A string.

Output: A string.

Example:

correct_sentence("greetings, friends") == "Greetings, friends."
correct_sentence("Greetings, friends") == "Greetings, friends."
correct_sentence("Greetings, friends.") == "Greetings, friends."

def correct_sentence(text: str) -> str:"""returns a corrected sentence which starts with a capital letterand ends with a dot."""# your code hereif text[-1]=='.':return text[0].title()+text[1:]else:return text[0].title()+text[1:]+'.'

3.First word--------------------------------------------- ----------------------------------------

When solving a task pay attention to the following points:

  • There can be dots and commas in a string.
  • A string can start with a letter or, for example, a dot or space.
  • A word can contain an apostrophe and it's a part of a word.
  • The whole text can be represented with one word and that's it.

Input: A string.

Output: A string.

Example:

first_word("Hello world") == "Hello"
first_word("greetings, friends") == "greetings"

import re
def first_word(text: str) -> str:"""returns the first word in a given text."""# your code herepat1 = "[a-zA-Z']+"result = re.search(pat1,text)result = str(result.group())return result

4.Second index--------------------------------------------- ----------------------------------------

Input: Two strings.

Output: Int or None

Example:

second_index("sims", "s") == 3
second_index("find the river", "e") == 12
second_index("hi", " ") is None

def second_index(text: str, symbol: str):"""returns the second index of a symbol in a given text"""# your code hereif text.count(symbol)<2:return Noneelse:return text.find(symbol,text.find(symbol)+1)

5.Between Markers--------------------------------------------- ----------------------------------------

Input: Three arguments. All of them are strings. The second and third arguments are the initial and final markers.

Output: A string.

Example:

between_markers('What is >apple<', '>', '<') == 'apple'
between_markers('No[/b] hi', '[b]', '[/b]') == 'No'

def between_markers(text: str, begin: str, end: str) -> str:       if begin in text:start = text.index(begin) + len(begin)else:start = 0if end in text:finish = text.index(end)else:finish = len(text)return (text[start:finish])

6.Best stock------------------------------------------- ----------------------------------------

You are given the current stock prices. You have to find out which stocks cost more.

Input: The dictionary where the market identifier code is a key and the value is a stock price.

Output: A string and the market identifier code.

Example:

best_stock({
            
    'CAC': 10.0,
    'ATX': 390.2,
    'WIG': 1.2