Simple Function vs Lambda
-
Code now defines a standard function with parameters, directly returning the formatted string based on the inputs.
-
Code is now called with a single set of parentheses, and arguments are passed in the standard way.
-
Readability dramatically improved
Testing
-
In the original setup, testing required understanding that the function produced a callable object, resulting in a syntax that could be confusing (Greeting(args)()).
-
Tests now call Greeting like any other function.
-
Anyone reading the tests can now quickly understand how the function is supposed to be used
def Greeting(name, rank=None, formal=False): greeting_style = "Hello" if formal else "Hey" rank_part = f"{rank} " if rank and formal else "" punctuation = '.' if formal else '!' return f"{greeting_style}, {rank_part}{name}{punctuation}"
Greeting=lambda name,rank=None,formal=False: lambda:f'He{["y","llo"][formal]}, {[str(rank)+" ",""][not rank or not formal]}{name}{chr(33+formal*13)}'- def Greeting(name, rank=None, formal=False):
- greeting_style = "Hello" if formal else "Hey"
- rank_part = f"{rank} " if rank and formal else ""
- punctuation = '.' if formal else '!'
- return f"{greeting_style}, {rank_part}{name}{punctuation}"
import codewars_test as test from solution import Greeting import random # Working solution / random generator def get_greeting(name, rank=None, formal=False): print(f'Hello,{" " + rank if rank is not None else ""} {name}.' if formal else f'Hey, {name}!') return f'Hello,{" " + rank if rank is not None else ""} {name}.' if formal else f'Hey, {name}!' def get_person(): names = [ 'Lisa', 'Skylar', 'Dylan', 'Harper', 'Susan', 'Kenneth', 'Quinn', 'Kevin', 'Morgan', 'Jordan', 'Finley', 'Karen', 'Michael', 'Emerson', 'Daniel', 'Avery', 'William', 'Michelle', 'Justice', 'David', 'Donald', 'Richard', 'Jennifer', 'Robert', 'Payton', 'John', 'James', 'Ariel', 'Skyler', 'Dorothy', 'Charles', 'Paul', 'Drew', 'Rory', 'Steven', 'Riley', 'Reese', 'Robin', 'Cameron', 'Mark', 'Jamie', 'Sarah', 'Jessica', 'Nancy', 'Anthony', 'Brian', 'Sandra', 'George', 'Helen', 'Melissa', 'Dakota', 'Mary', 'Alexis', 'Peyton', 'Alex', 'Charlie', 'Matthew', 'Patricia', 'Christopher', 'Edward', 'Elizabeth', 'Amanda', 'Sawyer', 'Margaret', 'Donna', 'Emily', 'Thomas', 'Bailey', 'Hayden', 'Rowan', 'Harley', 'Kai', 'Carol', 'Laura', 'Linda', 'Casey', 'Parker', 'Andrew', 'Joseph', 'Reagan', 'Emery', 'Phoenix', 'Taylor', 'Betty' ] titles = [ 'Duchess', 'Ambassador', 'Mistress', 'Executive', 'Sultan', 'Pharaoh', 'Baron', 'Mayor', 'Magistrate', 'Sergeant', 'Doctor', 'Sir', 'Lord', 'Vice President', 'Baroness', 'Cardinal', 'Officer', 'Archbishop', 'Duke', 'Agent', 'Madam', 'Queen', 'Minister', 'King', 'Captain', 'Pope', 'Master', 'Admiral', 'Princess', 'Lieutenant', 'Director', 'President', 'Governor', 'Commander', 'Prince', 'Detective', 'Professor', 'Sheikh', 'Bishop', 'Chancellor', 'Countess', 'Empress', 'Chief', 'Senator', 'Counselor', 'Emperor', 'Judge', 'General', 'Count' ] rand_num = random.randrange(1, 4) if rand_num == 1: return random.choice(names) if rand_num == 2: rand_name = random.choice(names) title_or_formal = random.choice([(random.choice(titles)), (random.randrange(1, 10) % 2 == 0)]) return rand_name, title_or_formal rand_name = random.choice(names) rand_title = random.choice(titles) form = random.randrange(1, 10) % 2 == 0 return rand_name, rand_title, form # START OF TESTING @test.describe("Example") def test_group(): @test.it("Test case") def test_case(): # Corrected function calls test.assert_equals(Greeting('John'), 'Hey, John!') test.assert_equals(Greeting('Churchill', 'Sir', True), 'Hello, Sir Churchill.') test.assert_equals(Greeting('Einstein', 'Proffessor', False), 'Hey, Einstein!') test.assert_equals(Greeting('Jane', formal=True), 'Hello, Jane.') @test.describe("Random Tests") def random_stuff(): for _ in range(100): person = get_person() @test.it(f"Testing: {person}") def test_random(): if isinstance(person, tuple): if len(person) == 2: if isinstance(person[1], str): test.assert_equals(Greeting(person[0], rank=person[1]), get_greeting(person[0], rank=person[1])) else: test.assert_equals(Greeting(person[0], formal=person[1]), get_greeting(person[0], formal=person[1])) else: test.assert_equals(Greeting(*person), get_greeting(*person)) else: test.assert_equals(Greeting(person), f'Hey, {person}!')
- import codewars_test as test
- from solution import Greeting
- import random
- # Working solution / random generator
- def get_greeting(name, rank=None, formal=False):
- print(f'Hello,{" " + rank if rank is not None else ""} {name}.' if formal else f'Hey, {name}!')
- return f'Hello,{" " + rank if rank is not None else ""} {name}.' if formal else f'Hey, {name}!'
- def get_person():
- names = [
- 'Lisa', 'Skylar', 'Dylan', 'Harper', 'Susan', 'Kenneth', 'Quinn', 'Kevin', 'Morgan', 'Jordan', 'Finley',
- 'Karen', 'Michael', 'Emerson', 'Daniel', 'Avery', 'William', 'Michelle', 'Justice', 'David', 'Donald',
- 'Richard', 'Jennifer', 'Robert', 'Payton', 'John', 'James', 'Ariel', 'Skyler', 'Dorothy', 'Charles', 'Paul',
- 'Drew', 'Rory', 'Steven', 'Riley', 'Reese', 'Robin', 'Cameron', 'Mark', 'Jamie', 'Sarah', 'Jessica', 'Nancy',
- 'Anthony', 'Brian', 'Sandra', 'George', 'Helen', 'Melissa', 'Dakota', 'Mary', 'Alexis', 'Peyton', 'Alex',
- 'Charlie', 'Matthew', 'Patricia', 'Christopher', 'Edward', 'Elizabeth', 'Amanda', 'Sawyer', 'Margaret',
- 'Donna', 'Emily', 'Thomas', 'Bailey', 'Hayden', 'Rowan', 'Harley', 'Kai', 'Carol', 'Laura', 'Linda', 'Casey',
- 'Parker', 'Andrew', 'Joseph', 'Reagan', 'Emery', 'Phoenix', 'Taylor', 'Betty'
- ]
- titles = [
- 'Duchess', 'Ambassador', 'Mistress', 'Executive', 'Sultan', 'Pharaoh', 'Baron', 'Mayor', 'Magistrate',
- 'Sergeant', 'Doctor', 'Sir', 'Lord', 'Vice President', 'Baroness', 'Cardinal', 'Officer', 'Archbishop',
- 'Duke', 'Agent', 'Madam', 'Queen', 'Minister', 'King', 'Captain', 'Pope', 'Master', 'Admiral', 'Princess',
- 'Lieutenant', 'Director', 'President', 'Governor', 'Commander', 'Prince', 'Detective', 'Professor',
- 'Sheikh', 'Bishop', 'Chancellor', 'Countess', 'Empress', 'Chief', 'Senator', 'Counselor', 'Emperor', 'Judge',
- 'General', 'Count'
- ]
- rand_num = random.randrange(1, 4)
- if rand_num == 1:
- return random.choice(names)
- if rand_num == 2:
- rand_name = random.choice(names)
- title_or_formal = random.choice([(random.choice(titles)), (random.randrange(1, 10) % 2 == 0)])
- return rand_name, title_or_formal
- rand_name = random.choice(names)
- rand_title = random.choice(titles)
- form = random.randrange(1, 10) % 2 == 0
- return rand_name, rand_title, form
- # START OF TESTING
- @test.describe("Example")
- def test_group():
- @test.it("Test case")
- def test_case():
print(Greeting('John').__dict__)test.assert_equals(Greeting('John')(), 'Hey, John!')test.assert_equals(Greeting('Churchill','Sir', True)(), 'Hello, Sir Churchill.')test.assert_equals(Greeting('Einstein', 'Proffessor', False)(), 'Hey, Einstein!')test.assert_equals(Greeting('Jane', formal=True)(), 'Hello, Jane.')- # Corrected function calls
- test.assert_equals(Greeting('John'), 'Hey, John!')
- test.assert_equals(Greeting('Churchill', 'Sir', True), 'Hello, Sir Churchill.')
- test.assert_equals(Greeting('Einstein', 'Proffessor', False), 'Hey, Einstein!')
- test.assert_equals(Greeting('Jane', formal=True), 'Hello, Jane.')
- @test.describe("Random Tests")
- def random_stuff():
- for _ in range(100):
- person = get_person()
- @test.it(f"Testing: {person}")
- def test_random():
- if isinstance(person, tuple):
- if len(person) == 2:
- if isinstance(person[1], str):
test.assert_equals(Greeting(person[0], rank=person[1])(), get_greeting(person[0], rank=person[1]))- test.assert_equals(Greeting(person[0], rank=person[1]), get_greeting(person[0], rank=person[1]))
- else:
test.assert_equals(Greeting(person[0], formal=person[1])(), get_greeting(person[0], formal=person[1]))- test.assert_equals(Greeting(person[0], formal=person[1]), get_greeting(person[0], formal=person[1]))
- else:
test.assert_equals(Greeting(*person)(), get_greeting(*person))- test.assert_equals(Greeting(*person), get_greeting(*person))
- else:
test.assert_equals(Greeting(person)(), f'Hey, {person}!')- test.assert_equals(Greeting(person), f'Hey, {person}!')