Whats up, Title or World in Python

The problem

Outline a way hi there that returns “Whats up, Title!” to a given title, or says Whats up, World! if title will not be given (or handed as an empty String).

Assuming that title is a String and it checks for person typos to return a reputation with a primary capital letter (Xxxx).


hi there "john"   => "Whats up, John!"
hi there "aliCE"  => "Whats up, Alice!"
hi there          => "Whats up, World!" <em># title not given</em>
hi there ""       => "Whats up, World!" <em># title is an empty String</em>

The answer in Python code

Possibility 1:

def hi there(title=''):
    return f"Whats up, {title.title() or 'World'}!"

Possibility 2:

def hi there(title=''):
    return "Whats up, {}!".format(title.title() if title else 'World')

Possibility 3:

def hi there(title = ""):
    nameNow = ""
    if title == "":
        return "Whats up, World!"
    j = 0
    for i in title:
        if j == 0:
            temp1 = i.higher()
            nameNow = nameNow + temp1
            j += 1
            temp1 = i.decrease()
            nameNow = nameNow + temp1
    return "Whats up, " + nameNow + "!"

Take a look at instances to validate our answer

import check
from answer import hi there

@check.describe("Mounted Exams")
def fixed_tests():
    @check.it('Primary Take a look at Circumstances')
    def basic_test_cases():
        exams = (
            ("John", "Whats up, John!"),
            ("aLIce", "Whats up, Alice!"),
            ("", "Whats up, World!"),
        for inp, exp in exams:
            check.assert_equals(hi there(inp), exp)

        check.assert_equals(hi there(), "Whats up, World!")

@check.describe("Random Exams")
def random_tests():

    from random import randint, alternative
    NAMES = [
        "James", "Christopher", "Ronald", "Mary", "Lisa", "Michelle",
        "John", "Daniel", "Anthony", "Patricia", "Nancy", "Laura",
        "Robert", "Paul", "Kevin", "Linda", "Karen", "Sarah", "Michael",
        "Mark", "Jason", "Barbara", "Betty", "Kimberly", "William", "Donald",
        "Jeff", "Elizabeth", "Helen", "Deborah", "David", "George", "Jennifer",
        "Sandra", "Richard", "Kenneth", "Maria", "Donna", "Charles", "Steven",
        "Susan", "Carol", "Joseph", "Edward", "Margaret", "Ruth", "Thomas",
        "Brian", "Dorothy", "Sharon", ""
    def create_test_case():
        return "".be a part of(c.decrease() if randint(0, 200) % 3 else c.higher() for c in alternative(NAMES))
    reference = lambda n='', d='World': "Whats up, %s!" % (n or d).title()
    for _ in vary(100):
        test_case = create_test_case()
        @check.it(f"testing for hi there({test_case})")
        def test_case():
            check.assert_equals(hi there(test_case), reference(test_case))

Leave a Reply

Your email address will not be published. Required fields are marked *