Checkio python 练习之Elementary

本文介绍了Checkio,一个有趣的Python编程练习平台,涵盖多种编程任务,包括字符串操作、逻辑判断、数学问题等,旨在提升编程技能。
摘要由CSDN通过智能技术生成

来源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 here
    return "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 letter
        and ends with a dot.
    """
    # your code here
    if 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 here

    pat1 = "[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 here
    if text.count(symbol)<2:return None
    else: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 = 0
    if 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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值