Help with Python?
0
If anyone is handy with python, I could certainly use some help with my homework. I'm not asking for answers (but if you have them I will take them), but I do need help understanding somethings.
Specifically, I do not understand anything about arrays. I just missed the class for it and using the online manual for python isn't getting my head wrapped around how to do the following problem:
"Write a python program to print the first 10 Fibonacci number in reverse order. Not that it is easy to use an array to store the numbers in ascending order and then print the array in reverse. The Fibonacci series is as follows:
1, 1, 2, 3, 5, 8 ......
Note that the first two numbers are 1's. The following numbers are the sum of the previous two numbers."
So yeah? If you can shed light on this problem I would be oh so very grateful.
*note: I also have to make a Visuallogic flowchart for this problem, but I doubt anyone knows how to use VisualLogic. BUT IF YOU DO CALL MEH.
Specifically, I do not understand anything about arrays. I just missed the class for it and using the online manual for python isn't getting my head wrapped around how to do the following problem:
"Write a python program to print the first 10 Fibonacci number in reverse order. Not that it is easy to use an array to store the numbers in ascending order and then print the array in reverse. The Fibonacci series is as follows:
1, 1, 2, 3, 5, 8 ......
Note that the first two numbers are 1's. The following numbers are the sum of the previous two numbers."
So yeah? If you can shed light on this problem I would be oh so very grateful.
*note: I also have to make a Visuallogic flowchart for this problem, but I doubt anyone knows how to use VisualLogic. BUT IF YOU DO CALL MEH.
0
Tegumi
"im always cute"
Well, what you could do is have 3 integers which you add together to get your fibonacci sums (well 2, then set it to another) and then write to an array. After you do this 11 times you can just print the array in reverse.
ie.
int a = 0; int b = 1; int c;
(loop to iterate 11 times)
{
a + b = c;
(throw c into an array)
a = b;
b = c;
}
(print array in reverse)
Note I don't actually know Python so I tried to make that as generalist as possible.
ie.
int a = 0; int b = 1; int c;
(loop to iterate 11 times)
{
a + b = c;
(throw c into an array)
a = b;
b = c;
}
(print array in reverse)
Note I don't actually know Python so I tried to make that as generalist as possible.
0
That helps tegumi. I'm getting an error with my code haha, but it kinda gives me an idea oh how to do it...
0
Fruid
Lurker of Threads
I don't know how to write in Python, but it helps to think of Fibonacci numbers recursively.
Here's how I'd do it:
Here's how I'd do it:
Spoiler:
0
Not to discredit fellow Fakku members, but in the future, Stack Overflow might be a better place to ask coding questions.
If you still need help, this should work:
#!/usr/bin/python
fibonacci = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
reverse = []
for num in reversed(fibonacci):
>>>>reverse.append(num)
print reverse
The forum keeps stripping my indents, so I tricked it above. If you copy and paste that code, remove the angle brackets that I put in there.
If you still need help, this should work:
#!/usr/bin/python
fibonacci = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
reverse = []
for num in reversed(fibonacci):
>>>>reverse.append(num)
print reverse
The forum keeps stripping my indents, so I tricked it above. If you copy and paste that code, remove the angle brackets that I put in there.
0
Tegumi
"im always cute"
tehnores wrote...
Not to discredit fellow Fakku members, but in the future, Stack Overflow might be a better place to ask coding questions.If you still need help, this should work:
#!/usr/bin/python
fibonacci = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
reverse = []
for num in reversed(fibonacci):
>>>>reverse.append(num)
print reverse
The forum keeps stripping my indents, so I tricked it above. If you copy and paste that code, remove the angle brackets that I put in there.
What's the point if you just hard-code the numbers into the array?
0
I assumed the point of the assignment was to reverse an array. It's easy to generate the Fibonacci numbers, though.
#!/usr/bin/python
def fib(n):
>>>>fiblist = []
>>>>for x in xrange(0, n + 1):
>>>>>>>>if x < 2:
>>>>>>>>>>>>fiblist.append(x)
>>>>>>>>else:
>>>>>>>>>>>>fiblist.append(fiblist[-1] + fiblist[-2])
>>>>return fiblist
fibonacci = fib(10)
reverse = []
for num in reversed(fibonacci):
>>>>reverse.append(num)
print reverse
#!/usr/bin/python
def fib(n):
>>>>fiblist = []
>>>>for x in xrange(0, n + 1):
>>>>>>>>if x < 2:
>>>>>>>>>>>>fiblist.append(x)
>>>>>>>>else:
>>>>>>>>>>>>fiblist.append(fiblist[-1] + fiblist[-2])
>>>>return fiblist
fibonacci = fib(10)
reverse = []
for num in reversed(fibonacci):
>>>>reverse.append(num)
print reverse
0
tehnores wrote...
I assumed the point of the assignment was to reverse an array. It's easy to generate the Fibonacci numbers, though.#!/usr/bin/python
def fib(n):
>>>>fiblist = []
>>>>for x in xrange(0, n + 1):
>>>>>>>>if x < 2:
>>>>>>>>>>>>fiblist.append(x)
>>>>>>>>else:
>>>>>>>>>>>>fiblist.append(fiblist[-1] + fiblist[-2])
>>>>return fiblist
fibonacci = fib(10)
reverse = []
for num in reversed(fibonacci):
>>>>reverse.append(num)
print reverse
Could you clarify on this a little? Also, this code gives me an output of [0] =)
Edit:
Thanks to everyone's help, this is what I got in the end. It took a lot of thinking and soul searching to come up with this =P
My Python Code wrote...
---------------------------------------------------
#Fibonacci numbers printed in reverse order#
#python version 2.7#
endNumber = int(raw_input("Enter how many Fibonacci numbers to print: "))
def fib(n):
>>>>if n < 2:
>>>>>>>>return n
>>>>return fib(n-2) + fib(n-1)
fibonacci= map(fib, range(0, endNumber))
print fibonacci[::-1]
----------------------------------------------------
This SHOULD be good enough for the assignment, but I do have a habit of overlooking things just when I think I have them figured out. My teacher did say it could be done in only five lines, but meh.
Thanks for your guys help =)
0
Have you tested your code with quantities larger than 30? I don't know how the map function works internally, but when I enter something like 100 in your program, it takes a crazy amount of time to complete. Might just be me though.
0
sciwhiz wrote...
Have you tested your code with quantities larger than 30? I don't know how the map function works internally, but when I enter something like 100 in your program, it takes a crazy amount of time to complete. Might just be me though.Yeah I noticed that too. I am not sure if I am running into a memory limit for storing intergers or what. The problem is the calculations have to constantly add numbers and recall numbers, so I think I might try using two loops to come up with a different way of doing this.
I also have to do this problem in VisualLogic which... I don't think it is possible to translate this into visualLogic.
0
Weird. I tested it with Python 2.7.3 on Debian Linux and it worked fine. Did you copy and paste my code? If you did, you need to replace the angle brackets I put in there to show the indents with tab indents. I suppose it doesn't matter too much now, though, since you've got your own code working. I'm glad you got it figured out on your own!
0
tehnores wrote...
Weird. I tested it with Python 2.7.3 on Debian Linux and it worked fine. Did you copy and paste my code? If you did, you need to replace the angle brackets I put in there to show the indents with tab indents. I suppose it doesn't matter too much now, though, since you've got your own code working. I'm glad you got it figured out on your own!I did delete the angle brackets. I dunno... I just get a return of "[0]".
EDIT:
Okay, I see what happened. It wasn't indented right, my fault XP
