DEV Community

HHMathewChan
HHMathewChan

Posted on • Originally published at rebirthwithcode.tech

5 2

Python Exercise 14: sum of fractions

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
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

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (2)

Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli

Fun little problem! Here's my solution, using the way we were taught to add fractions at school 😋 (I added in fraction reduction for extra spice!)

from math import gcd

def reduce_fraction(fraction):
    hcf = gcd(fraction[0], fraction[1])
    return [fraction[0]//hcf, fraction[1]//hcf]

def sum_of_fractions(fractions):
    total = fractions.pop()
    while len(fractions) > 0:
        next = fractions.pop()
        bottom = total[1] * next[1]
        topLeft = next[1] * total[0];
        topRight = next[0] * total[1]
        total = [topLeft + topRight, bottom]
    return reduce_fraction(total)

test = [[11, 2], [3, 4], [5, 4], [21, 11], [12, 6]]

fraction_sum = sum_of_fractions(test[:])

result = round(fraction_sum[0]/fraction_sum[1])

print(f'The sum of {test} fractions is {fraction_sum}, which is {result} to the nearest integer')

Enter fullscreen mode Exit fullscreen mode
Collapse
 
mathewchan profile image
HHMathewChan

Interesting method, just learn a syntax(test[:]) from you. Also good exercise for me to read long code from others.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs