MacInsiders Logo

Similar Threads
Thread Thread Starter Forum Replies Last Post
Desktop Cool Speakers blackdragon General Discussion 0 06-23-2011 07:10 PM
Now this is COOL! Chad Computers & Tech 11 12-17-2010 02:56 AM
Cool Pictures of Old Hamilton danallan General Discussion 4 06-08-2010 11:13 PM
Motorcycles are still cool right? PaulTersigni Clubs & Groups 0 01-06-2010 06:31 PM

Code efficiency question + cool article

 
Old 02-02-2012 at 11:58 PM   #1
anonanon987
Power Abuser
Join Date: Apr 2011
Posts: 3,170

Thanked: 246 Times
Liked: 459 Times




Code efficiency question + cool article
Article: http://www.chacha.com/topic/solar-pa...r-panel-design
Code:
Code:
# Humza Tariq # Computer Science I # McMaster University # Program Description: This program is capable of doing two things: 1) Compute the Fibonacci terms till the nth number you desire # or it can compute the sum of all the Fibonacci terms you desire, or do both! def computeAllFib(n): list=[0,1,1] initial = 3 x = 1 y = 1 for i in range(initial,n+1): x,y=x+y,x list.append(x) return list[0:n+1] def findSum(n): x = computeAllFib(n) sum = 0 for i in x: sum = sum + i return sum def main(): choice = int(input("Enter '1' to compute all Fibonacci terms till the nth number,\nor '2' to compute the sum of all Fibonacci numbers till the nth one or anything else for both: ")) desiredNumber = int(input("What is the nth number?")) if choice == 1: c1 = computeAllFib(desiredNumber) print c1 elif choice == 2: c2 = findSum(desiredNumber) print c2 else: c1 = computeAllFib(desiredNumber) c2 = findSum(desiredNumber) print "The numbers are: "+str(c1)+".","The sum is "+str(c2)+"." main()
I made this little program using Python. Is there anything I can to do it to make it more efficient and not change the code substantially? If that isn't possible, what edits should I make to the code so it's still understandable to me and (much more) efficient. I also noticed..there isn't a code snippets sub-section anywhere on this forum which would be cool idea. Thanks in advance,

Humza
Old 02-03-2012 at 12:07 AM   #2
MacPack
Offical Deal Blogger
MacInsiders Staff
Join Date: Mar 2010
Posts: 1,016

Thanked: 82 Times
Liked: 160 Times




Did you guys learn about recursion?
__________________
Due to sig restrictions, see my about me for more info on MI DealsBlogger!
Old 02-03-2012 at 12:15 AM   #3
anonanon987
Power Abuser
Join Date: Apr 2011
Posts: 3,170

Thanked: 246 Times
Liked: 459 Times




Quote:
Originally Posted by MacPack View Post
Did you guys learn about recursion?
Barely, but I understand the concept from CS 1MA3 .



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



McMaster University News and Information, Student-run Community, with topics ranging from Student Life, Advice, News, Events, and General Help.
Notice: The views and opinions expressed in this page are strictly those of the student(s) who authored the content. The contents of this page have not been reviewed or approved by McMaster University or the MSU (McMaster Students Union). Being a student-run community, all articles and discussion posts on MacInsiders are unofficial and it is therefore always recommended that you visit the official McMaster website for the most accurate up-to-date information.

Copyright © MacInsiders.com All Rights Reserved. No content can be re-used or re-published without permission. MacInsiders is a service of Fullerton Media Inc. | Created by Chad
Originally Powered by vBulletin®, Copyright © 2019 MH Sub I, LLC dba vBulletin. All rights reserved. | Privacy | Terms