[Tutor] Fwd: Re: fractions from Fractions

Alan Gauld alan.gauld at yahoo.co.uk
Tue Feb 6 04:26:55 EST 2018


Forwarding to list, please always use Reply ALL or Reply LIst to send
mail to the list.



-----Original Message-----
From: Alan Gauld via Tutor <tutor at python.org>
To: tutor <tutor at python.org>
Sent: Mon, Feb 5, 2018 4:50 am
Subject: Re: [Tutor] fractions from Fractions

On 05/02/18 04:11, Rex Florian via Tutor wrote:

> The problem is one of the PyCharm problems and when I
> use the check feature it tells me my answer is incorrect.

What input did you use and what output did you get?

Input 239/30  --->  7 1 29
Input 415/93  --->  4 2 6 7

So far as I can tell your algorithm is correct, although
I'm not sure why you limit it to values greater than 1?

The problem suggests to do so and the wiki on Finite simple continued
fractions showed examples also greater than one.  Upon consideration, i
suppose that being greater than one is not a requirement but really is
not of consequence to the insight I am seeking.

But is it perhaps the format of the output that Pycharm
objects to?

No, the format required is for the elements of the list to appear on one
line separated by a space.

> I think I know the source of the trouble.

Would you like to tell us?

My input is a string which I convert to num and den.  The problem ask
specifically that the input format be "A line with an fraction in the
"numerator/denominator" format and I am interpreting this to mean some
kind of application of the Fraction module.  But that is just a wild
guess and is what I was hoping I could get some insight on.

So, is there away with using the Fraction module to input a fraction
without having to go through the int conversions that my code employees?

> I have tried importing fractions from Fraction

And what happened?

Not much.  I just bungled something:
fraction = input(Fraction(numerator, denominator)
Please don't scold me.  It was late and I was fighting the flu and  ....
and ...

(I assume you mean you tried importing Fraction from fractions)
Although you don't really need it for this problem.

I agree I do not really need to for this problem but am just trying to
see if I could input a fraction that would not need to be processed like
my program does.

> fraction = input()
> # define list for holding coefficients
> coef = []
>
> # find the index where the / is
> slash = fraction.find('/')
> # extract the string num and den and convert them to integers
> num = int(fraction[0:slash])
> den = int(fraction[slash + 1:])
>
> while num > den and den != 0:
> mod = num % den
> a = num // den
> coef.append(a)
> num = den
> den = mod
> for i in coef:
> print(i, end=' ')


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - Tutor at python.org <mailto:Tutor at python.org>
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list