Wednesday, February 21, 2024
HomeSoftware EngineeringHowdy, Title or World in Python

Howdy, Title or World in Python


The problem

Outline a technique whats up that returns “Howdy, Title!” to a given title, or says Howdy, World! if title just isn’t given (or handed as an empty String).

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

Examples:

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

The answer in Python code

Choice 1:

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

Choice 2:

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

Choice 3:

def whats up(title = ""):
    nameNow = ""
    if title == "":
        return "Howdy, World!"
    j = 0
    for i in title:
        if j == 0:
            temp1 = i.higher()
            nameNow = nameNow + temp1
            j += 1
            cross
        else:
            temp1 = i.decrease()
            nameNow = nameNow + temp1
            cross
        cross
    return "Howdy, " + nameNow + "!"
    cross

Check circumstances to validate our resolution

import check
from resolution import whats up

@check.describe("Fastened Checks")
def fixed_tests():
    @check.it('Fundamental Check Instances')
    def basic_test_cases():
        
        checks = (
            ("John", "Howdy, John!"),
            ("aLIce", "Howdy, Alice!"),
            ("", "Howdy, World!"),
        )
        
        for inp, exp in checks:
            check.assert_equals(whats up(inp), exp)

        check.assert_equals(whats up(), "Howdy, World!")

@check.describe("Random Checks")
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 a part of(c.decrease() if randint(0, 200) % 3 else c.higher() for c in selection(NAMES))
    
    reference = lambda n='', d='World': "Howdy, %s!" % (n or d).title()
    
    for _ in vary(100):
        test_case = create_test_case()
        @check.it(f"testing for whats up({test_case})")
        def test_case():
            check.assert_equals(whats up(test_case), reference(test_case))
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments