Whats up, Title or World in Python


The problem

Outline a technique hiya that returns “Whats up, Title!” to a given title, or says Whats up, World! if title is just not 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).


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

The answer in Python code

Choice 1:

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

Choice 2:

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

Choice 3:

def hiya(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 + "!"

Check circumstances to validate our answer

import take a look at
from answer import hiya

@take a look at.describe("Mounted Exams")
def fixed_tests():
    @take a look at.it('Primary Check Circumstances')
    def basic_test_cases():
        checks = (
            ("John", "Whats up, John!"),
            ("aLIce", "Whats up, Alice!"),
            ("", "Whats up, World!"),
        for inp, exp in checks:
            take a look at.assert_equals(hiya(inp), exp)

        take a look at.assert_equals(hiya(), "Whats up, World!")

@take a look at.describe("Random Exams")
def random_tests():

    from random import randint, selection
    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 part of(c.decrease() if randint(0, 200) % 3 else c.higher() for c in selection(NAMES))
    reference = lambda n='', d='World': "Whats up, %s!" % (n or d).title()
    for _ in vary(100):
        test_case = create_test_case()
        @take a look at.it(f"testing for hiya({test_case})")
        def test_case():
            take a look at.assert_equals(hiya(test_case), reference(test_case))


Leave a Reply