Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Help.
#1
This is for a friend who asked me to help. I know nothing about Python or Wing IDE (Which I'm guessing they use in that class).

$5 via PayPal to whoever does this for me. No joke. :O


Quote:CIS115 Introduction to Programming & Logic

Lists / Arrays

Design & write a python program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day and continues to double each day. The only input to the program should be the number of days to calculate. The day number, amount of pay for each day and the accumulated amount of pay should be stored in 3 parallel lists. The output should be displayed in a dollar amounts, not the number of pennies.

A minimum of three modules should be used for this problem. The program must also loop for additional input and provide for user termination of the program.

Code:
#!/usr/bin/env python

# Return the salary at any given day
# If given an empty list as second parameter,
# it would also store the total accumulated amount of pay
def salary(day, accumulated=None):
    if day == 1:
        if accumulated != None:
            accumulated.append(1)
        return 1
    n = 2 * salary(day - 1, accumulated)
    if accumulated != None: accumulated.append(n)
    return n

# Read from command line should be done here, as well as parsing
# e.g.
from sys import stdin
try:
    days = int(stdin.readline())
    listofdays = range(1,days)
    accumulated = []
    salary(days, accumulated)
except:
    print "Goodbye"

This seems really ghetto/wrong. I need some input.
Reply
#2
What version of Python? 2.6? 3.0? 3.1?
Reply
#3
Sh`it. She left, too. I would assume the latest version, but I can't even be sure.
Reply
#4
Here's a version I hacked up:

Code:
def CalculatePay(numberDays):
    payEachDay = [0] * numberDays
    accumPay = [0] * numberDays
    for eachDay in range(numberDays):
        if eachDay == 0:
            payEachDay[eachDay] = 1
            accumPay[eachDay] = 1
        else:
            payEachDay[eachDay] = payEachDay[eachDay - 1] * 2
            accumPay[eachDay] = sum(payEachDay)
    return payEachDay, accumPay
            
        


def Main():
    numberDays = []
    payEachDay = []
    accumPay = []
    inputSelection = 1
    while inputSelection != 0:
        inputSelection = input('Enter the amount of days to calculate\n' +
                               'If you want to quit, enter 0\n')
        if inputSelection > 0:
            if len(accumPay) >= inputSelection:
                print("You would make $%0.2f in %d days" % (float(accumPay[inputSelection-1]) / 100, inputSelection))
            else:
                numberDays = range(1, inputSelection + 1)
                payEachDay, accumPay = CalculatePay(inputSelection)
                print("You would make $%0.2f in %d days" % (float(accumPay[inputSelection-1]) / 100, inputSelection))                
    print("Goodbye.\n\n")      
            

Main()

Works for me. Even uses a quick caching system. Smile
Reply
#5
Lawd you were quick. Thanks. I donated $5. I hope that goes to you...D:
Reply
#6
I know that the assignment says to use three parallel lists, and that's partly the purpose, but I can't help but say that's such an inefficient way to calculate such a sum when it's simply equal to 2^n-1 where n is the number of days.
Reply
#7
When we used to assignments in my C++/C# classes, they used to be the exact same way. Very long and inefficient, and there were ways to cut out about 75% of the code that we were supposed to have in there. For the purpose of learning things, though, I suppose it was beneficial...
Reply


Forum Jump:


Users browsing this thread: