-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfunctionslab.py
More file actions
72 lines (52 loc) · 2.26 KB
/
functionslab.py
File metadata and controls
72 lines (52 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 1) Write a `split_name` function that takes a string and returns a dictionary with first_name and last_name
def split_name(name):
names = name.split()
first_name = names[0]
last_name = names[-1]
return {
'first_name': first_name,
'last_name': last_name,
}
assert split_name("Kevin Bacon") == {
"first_name": "Kevin",
"last_name": "Bacon",
}, f"Expected {{'first_name': 'Kevin', 'last_name': 'Bacon'}} but received {split_name('Kevin Bacon')}"
# 1) Write a `split_name` function that takes a string and returns a dictionary with first_name and last_name
def split_name(name):
first_name, last_name = name.split(maxsplit=1)
return {
'first_name': first_name,
'last_name': last_name,
}
assert split_name("Kevin Bacon") == {
"first_name": "Kevin",
"last_name": "Bacon",
}, f"Expected {{'first_name': 'Kevin', 'last_name': 'Bacon'}} but received {split_name('Kevin Bacon')}"
# 2) Ensure that `split_name` can handle multi-word last names
assert split_name("Victor Von Doom") == {
"first_name": "Victor",
"last_name": "Von Doom",
}, f"Expected {{'first_name': 'Victor', 'last_name': 'Von Doom'}} but received {split_name('Victor Von Doom')}"
# 3) Write an `is_palindrome` function to check if a string is a palindrome (reads the same from left-to-right and right-to-left)
def is_palindrome(item):
item = str(item)
return item == item[::-1]
assert is_palindrome("radar") == True, f"Expected True but got {is_palindrome('radar')}"
assert is_palindrome("stop") == False, f"Expected False but got {is_palindrome('stop')}"
# 4) Make `is_palindrome` work with numbers
assert is_palindrome(101) == True, f"Expected True but got {is_palindrome(101)}"
assert is_palindrome(10) == False, f"Expected False but got {is_palindrome(10)}"
# 5) Write a `build_list` function that takes an item and a number to include in a list
def build_list(item, count=1):
items = []
for _ in range(count):
items.append(item)
return items
assert build_list("Apple", 3) == [
"Apple",
"Apple",
"Apple",
], f"Expected ['Apple', 'Apple', 'Apple'] but received {repr(build_list('Apple', 3))}"
assert build_list("Orange") == [
"Orange"
], f"Expected ['Orange'] but received {repr(build_list('Orange'))}"