[Tutor] fractions from Fractions

2018-02-05 Thread Rex Florian via Tutor
I have written a program to generate the array that describes the finite simple 
continued fraction from any fractional input.  The input is any fraction, 
num/den, and the output is the array.  The program works and I am happy with 
the work.

The problem is one of the PyCharm problems and when I use the check feature it 
tells me my answer is incorrect.  I think I know the source of the trouble.  
The problem asks that my input format be "A single line with an fraction in the 
numerator/denominator format.

My input is obtained as seen in the code.  I have googled fractions in Python.  
I have tried importing fractions from Fraction but seem to be in over my head.  
What am I missing?



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=' ')


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] fractions from Fractions

2018-02-05 Thread Alan Gauld via Tutor
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?

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

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

> I think I know the source of the trouble.  

Would you like to tell us?

> I have tried importing fractions from Fraction 

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

> 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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] fractions from Fractions

2018-02-05 Thread Steven D'Aprano
Hi Rex,

My comments below.


On Sun, Feb 04, 2018 at 11:11:23PM -0500, Rex Florian via Tutor wrote:

> I have written a program to generate the array that describes the 
> finite simple continued fraction from any fractional input.  The input 
> is any fraction, num/den, and the output is the array.  The program 
> works and I am happy with the work.

Have you tried it with a proper fraction, like 1/2 or 4/11? As continued 
fractions, they ought to print out:

0 2# 1/2 = 0 + 1/2
0 2 1 3# 4/11 = 0 + 1/(2 + 1/(1 + 1/3))

but it doesn't.

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

I'm afraid I have no idea what you are talking about. What are the 
PyCharm problems, and what's "check feature"?


> I think I know the source 
> of the trouble.  The problem asks that my input format be "A single 
> line with an fraction in the numerator/denominator format.

Is it possible that you are supposed to write a function?

def contfract(fraction):
# code as before


And then call it like this?


frac = input("Enter a fraction like num/den ")
contfract(frac)




-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor