Python daily exercise (23 Part Series)
1 Python Daily exercise 1: Generate a list of even number
2 Python daily exercise 2: string
… 19 more parts…
3 Python daily exercise 3: reverse a string
4 Python daily exercise 4: separate a string
5 Python Daily Exercise 5: Generate new list by removing unwanted object
6 Python Exercise 6: Remove Special character In String
7 Python exercise 7: Determine a string is a mixture of something
8 Python Exercise 8: generate a dictionary from different source
9 Python Exercise 9: find the key with the minimum value in a dictionary
10 Python Exercise 10: Generate a list based on odd or even index
11 Python Exercise 11: Count the number and generate new dictionary
12 Python exercise 12: find the hidden ball
13 Python Exercise 13: Scottish Screaming
14 Python Exercise 14: sum of fractions
15 Python Exercise 15 : Perform calculation on a string
16 Python Exercise 16: find the highest index alphabet
17 Python exercise 17: convert number to english word
18 Python exercise 18:Sales Season
19 Python Exercise 19:advanced list sort
20 Python Exercise 20:Find the pattern
21 Python Exercise 21: list comprehensions
22 Python exercise 22: ransomnote
23 Python Exercise 23: Two sum
Question
- Create a function that takes a list containing nested lists as an argument.
- Each sublist has 2 elements.
- The first element is the numerator and the second element is the denominator.
- Return the sum of the fractions rounded to the nearest whole number.
Examples
sum_fractions([[18, 13], [4, 5]]) 2
sum_fractions([[36, 4], [22, 60]]) 9
sum_fractions([[11, 2], [3, 4], [5, 4], [21, 11], [12, 6]]) 11
Enter fullscreen mode Exit fullscreen mode
My solution
- algorithm
>>separate the sublist in the nested list
iterate over the nested list
>>separate elementes in the sublist
iterate over the sublist
>>for each sublist, calculate the fraction
set the first element to the numerator
set the second element is the denominator
store the result to a variable called:total
>>return the variable total
Enter fullscreen mode Exit fullscreen mode
- code
def sum_fractions(nested_list:list):
total = 0
list_of_sum = list()
for sublist in nested_list:
division = int(sublist[0]) / int(sublist [1])
total += division
return round(total)
Enter fullscreen mode Exit fullscreen mode
Other solution
- unpacking the sublist by multiple assignment
- i.e. use numerator for the first element of the sublist
- use denominator for the second element of the sublist
- i.e. use numerator for the first element of the sublist
def sum_fractions(lst):
return round(sum(numerator/ denominator for numerator, denominator in lst))
Enter fullscreen mode Exit fullscreen mode
My reflection
- I have learned using multiple assignment instead of using index to access the value of a list.
Credit
- challenge find on edabit
Python daily exercise (23 Part Series)
1 Python Daily exercise 1: Generate a list of even number
2 Python daily exercise 2: string
… 19 more parts…
3 Python daily exercise 3: reverse a string
4 Python daily exercise 4: separate a string
5 Python Daily Exercise 5: Generate new list by removing unwanted object
6 Python Exercise 6: Remove Special character In String
7 Python exercise 7: Determine a string is a mixture of something
8 Python Exercise 8: generate a dictionary from different source
9 Python Exercise 9: find the key with the minimum value in a dictionary
10 Python Exercise 10: Generate a list based on odd or even index
11 Python Exercise 11: Count the number and generate new dictionary
12 Python exercise 12: find the hidden ball
13 Python Exercise 13: Scottish Screaming
14 Python Exercise 14: sum of fractions
15 Python Exercise 15 : Perform calculation on a string
16 Python Exercise 16: find the highest index alphabet
17 Python exercise 17: convert number to english word
18 Python exercise 18:Sales Season
19 Python Exercise 19:advanced list sort
20 Python Exercise 20:Find the pattern
21 Python Exercise 21: list comprehensions
22 Python exercise 22: ransomnote
23 Python Exercise 23: Two sum
暂无评论内容