>>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
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!)
frommathimportgcddefreduce_fraction(fraction):hcf=gcd(fraction[0],fraction[1])return[fraction[0]//hcf,fraction[1]//hcf]defsum_of_fractions(fractions):total=fractions.pop()whilelen(fractions)>0:next=fractions.pop()bottom=total[1]*next[1]topLeft=next[1]*total[0];topRight=next[0]*total[1]total=[topLeft+topRight,bottom]returnreduce_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')
Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.
What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.
Top comments (2)
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!)
Interesting method, just learn a syntax(test[:]) from you. Also good exercise for me to read long code from others.