Skip to content

Commit 759aca1

Browse files
committed
extra function exercises included
1 parent 363ff3f commit 759aca1

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

043_assessment_exercise.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#These are the functions I was requried to create for the Maker's Assessment:
2+
3+
#Function to convert a number of minutes into an hours:minutesds style format.
4+
5+
def time_conversion(num):
6+
7+
hours = num // 60
8+
minutees = num % 60
9+
return f"{hours}:{minutees}"
10+
11+
#test cases
12+
13+
print(time_conversion(126))
14+
print(time_conversion(45))
15+
print(time_conversion(200))
16+
17+
#Function to find the longest word in a string. The function should also remove punctuaction and if multiple words have the same length, it shoukd return the first one.
18+
19+
def longest_word(sen):
20+
words = sen.split()
21+
cleaned_words = ["". join(char for char in word if char.isalnum()) for word in words]
22+
return max(cleaned_words, key=len)
23+
24+
#test cases
25+
26+
print(longest_word("fun&!! time"))
27+
print(longest_word("I love dogs and cats"))
28+
print(longest_word("The longest word here is..."))
29+
30+
#Functions to return the sum of numbers in a list up to a given number
31+
#two approaches: one incorparting a loop method and another using mathematics
32+
33+
def sum_to_num(num):
34+
return sum(range(1, num + 1))
35+
36+
def sum_to_num_formula(num):
37+
return num * (num + 1) // 2
38+
39+
print(sum_to_num(10))
40+
print(sum_to_num_formula(20))
41+
print(sum_to_num(100))
42+
print(sum_to_num_formula(200))

0 commit comments

Comments
 (0)