Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Computer Science - Coding in Python Converting a decimal to a fraction Although computers generally represent numbers as decimals, it is often convenient to express

Computer Science - Coding in Python

Converting a decimal to a fraction

Although computers generally represent numbers as decimals, it is often convenient to express values as fractions. If we desire to convert a decimal to a fraction, we need to follow a two-step process:

Starting with the number itself, multiply by ten repeatedly until no values remain after the decimal point. This is the numerator. Keep the multiplier as the denominator.

Simplify the fraction.

For instance, for 0.75:

Multiply by ten repeatedly:

0.751010=750.751010=75

Thus the numerator is 75 and the denominator is 1010=1001010=100.

Simplify the fraction:

Find the factors of the numerator and the denominator.

75?{1,3,5,25,75}100?{1,2,4,5,10,20,25,50,100}75?{1,3,5,25,75}100?{1,2,4,5,10,20,25,50,100}

Find the greatest common factor of each and divide both by it.

7525=310025=47525=310025=4

0.75?340.75?34

Write a function factors( n ) which accepts a number n and returns a list containing all of the factors of that number. (This is a common task. Write your own codedon't just google it!)

Write a function fraction( n ) which accepts a decimal number n and returns a tuple ( numerator,denominator ) of the resulting fraction representation.

Your submission should include two functions factors( n ) and fraction( n ).

STARTER CODE:

def factors( n ): pass

def fraction( n ): n_str = str( n ) decimal_part = n_str[ n_str.find( '.' )+1: ] # 1. Multiply by ten repeatedly (to make all of decimal positive). numer = n * 10 ** ??? denom = ???

# 2. Find factors. numer_factors = ??? denom_factors = ??? factor = 1 # ??? find greatest common factor of both numerator and denominator # ??? divide both by GCF before returning them return ( numer,denom )

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications Dasfaa 2023 International Workshops Bdms 2023 Bdqm 2023 Gdma 2023 Bundlers 2023 Tianjin China April 17 20 2023 Proceedings Lncs 13922

Authors: Amr El Abbadi ,Gillian Dobbie ,Zhiyong Feng ,Lu Chen ,Xiaohui Tao ,Yingxia Shao ,Hongzhi Yin

1st Edition

3031354141, 978-3031354144

More Books

Students also viewed these Databases questions

Question

Name is needed for identifying organisms ?

Answered: 1 week ago

Question

Q.1. Taxonomic classification of peafowl, Tiger and cow ?

Answered: 1 week ago

Question

7. Identify six intercultural communication dialectics.

Answered: 1 week ago