dacorest


Joined 1 year ago
Homeworks submitted:
Homework comments:
5
0

About Me

No description provided.

Classes

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming

Class status: Established
Role: Student
. 29% complete

Submitted Assignments

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 7, HW 1
#Name : Waz 
#Problem set 4

___________________Problem 1________________________

#plan
#make sure arguments are of right format
#declare variable for list & year counter
#create loop for counter

def nestEggFixed(salary, save, growthRate, years):
    """
    - salary: the amount of money you make each year.
    - save: the percent of your salary to save in the investment account each
      year (an integer between 0 and 100).
    - growthRate: the annual percent increase in your investment account (an
      integer between 0 and 100).
    - years: the number of years to work.
    - return: a list whose values are the size of your retirement account at
      the end of each year.
    """
    assert save >= 0 and save <=100, "Save should be between 0-100"
    assert growthRate >= 0 and growthRate <=100, "growthRate should be between 0-100"
    
    thelist = ['tobefilled'] #the list to be returned
    counter = 1 # counter for loop
    if years == 0:
        thelist[0]='Your retirement account is empty'
    elif years == 1:
        thelist[0]=salary*save*0.01
    else:
        thelist[0]=salary*save*0.01
        while counter<years:
            thelist.append(thelist[counter-1]*(1+0.01*growthRate)+salary*save*0.01)
            counter = counter + 1
    return thelist

    
    

def testNestEggFixed():
    salary     = 10000
    save       = 10
    growthRate = 15
    years      = 5
    savingsRecord = nestEggFixed(salary, save, growthRate, years)
    print savingsRecord
    # Output should have values close to:
    # [1000.0, 2150.0, 3472.5, 4993.375, 6742.3812499999995]
    
def testNestEggFixed2():
    salary     = 24000
    save       = 10
    growthRate = 7
    years      = 5
    savingsRecord = nestEggFixed(salary, save, growthRate, years)
    print savingsRecord
    # Output should have values close to:
    # [2400.0, 4968.0, 7715.76, 10655.863200000002, 13801.773624000003]

#_____________________ Problem 2__________________
#
def isbetween0to100(alist): #will be used for assertion
    """Test if elements of a list is between 0 and 100"""
    for i in alist:
        if i < 0 or i > 100:
            return "False"
        

def nestEggVariable(salary, save, growthRates):
    """
    - salary: the amount of money you make each year.
    - save: the percent of your salary to save in the investment account each
      year (an integer between 0 and 100).
    - growthRate: a list of the annual percent increases in your investment
      account (integers between 0 and 100).
    - return: a list of your retirement account value at the end of each year.
    """
    assert save >= 0 and save <=100, "Save should be between 0-100"
    assert isbetween0to100(growthRates)!="False", "growthRates should be between 0-100"

    thelist = ['tobefilled'] #the list to be returned
    counter = 1 # counter for loop
    if len(growthRates) == 0:
        thelist[0]='Your retirement account is empty'
    elif len(growthRates) == 1:
        thelist[0]=salary*save*0.01
    else:
        thelist[0]=salary*save*0.01
        while counter<len(growthRates):
            thelist.append(thelist[counter-1]*(1+0.01*growthRates[counter])+salary*save*0.01)
            counter = counter + 1
    return thelist



def testNestEggVariable():
    salary      = 10000
    save        = 10
    growthRates = [3, 4, 5, 0, 3]
    savingsRecord = nestEggVariable(salary, save, growthRates)
    print savingsRecord
    # Output should have values close to:
    # [1000.0, 2040.0, 3142.0, 4142.0, 5266.2600000000002]

def testNestEggVariable2():
    salary      = 60000
    save        = 15
    growthRates = [9,2,7,6,5]
    savingsRecord = nestEggVariable(salary, save, growthRates)
    print savingsRecord
    # Output should have values close to:
    # [9000.0, 18180.0, 28452.600000000002, 39159.75600000001, 50117.74380000001]


# _____________________________Problem 3_______________
#
def isbetween0to100(alist): #will be used for assertion
    """Test if elements of a list is between 0 and 100"""
    for i in alist:
        if i < 0 or i > 100:
            return "False"
   
def postRetirement(savings, growthRates, expenses):
    """
    - savings: the initial amount of money in your savings account.
    - growthRate: a list of the annual percent increases in your investment
      account (an integer between 0 and 100).
    - expenses: the amount of money you plan to spend each year during
      retirement.
    - return: a list of your retirement account value at the end of each year.
    """
    assert isbetween0to100(growthRates)!="False", "growthRates should be between 0-100"
        ### Why only 0-100? I'd think at the very least that negative growth would be possible, e.g. years 2007/8
    thelist = ['tobefilled'] #the list to be returned
    counter = 1 # counter for loop
    if len(growthRates) == 0:
        thelist[0]='Your retirement account is empty'
    elif len(growthRates) == 1:
        thelist[0]=savings * (1 + 0.01 * growthRates[0])- expenses 
    else:
        thelist[0]=savings * (1 + 0.01 * growthRates[0])- expenses 
        while counter<len(growthRates):
            thelist.append(thelist[counter-1]*(1+0.01*growthRates[counter])-expenses)
            counter = counter + 1
    return thelist
    
def testPostRetirement():
    savings     = 100000
    growthRates = [10, 5, 0, 5, 1]
    expenses    = 30000
    savingsRecord = postRetirement(savings, growthRates, expenses)
    print savingsRecord
    # Output should have values close to:
    # [80000.000000000015, 54000.000000000015, 24000.000000000015,
    # -4799.9999999999854, -34847.999999999985]
def testPostRetirement2():
    savings     = 50000
    growthRates = [3,9,4]
    expenses    = 2000
    savingsRecord = postRetirement(savings, growthRates, expenses)
    print savingsRecord
    # Output should have values close to:
    # [49500.0, 51955.000000000007, 52033.200000000012]
# _______________________Problem 4__________________
#
def nestEggVariable(salary, save, growthRates):
    """
    - salary: the amount of money you make each year.
    - save: the percent of your salary to save in the investment account each
      year (an integer between 0 and 100).
    - growthRate: a list of the annual percent increases in your investment
      account (integers between 0 and 100).
    - return: a list of your retirement account value at the end of each year.
    """
    assert save >= 0 and save <=100, "Save should be between 0-100"
    assert isbetween0to100(growthRates)!="False", "growthRates should be between 0-100"
    thelist = ['tobefilled'] #the list to be returned
    counter = 1 # counter for loop
    if len(growthRates) == 0:
        thelist[0]='Your retirement account is empty'
    elif len(growthRates) == 1:
        thelist[0]=salary*save*0.01
    else:
        thelist[0]=salary*save*0.01
        while counter<len(growthRates):
            thelist.append(thelist[counter-1]*(1+0.01*growthRates[counter])+salary*save*0.01)
            counter = counter + 1
    return thelist
def isbetween0to100(alist): #will be used for assertion
    """Test if elements of a list is between 0 and 100"""
    for i in alist:
        if i < 0 or i > 100:
            return "False"
   
def postRetirement(savings, growthRates, expenses):
    """
    - savings: the initial amount of money in your savings account.
    - growthRate: a list of the annual percent increases in your investment
      account (an integer between 0 and 100).
    - expenses: the amount of money you plan to spend each year during
      retirement.
    - return: a list of your retirement account value at the end of each year.
    """
    assert isbetween0to100(growthRates)!="False", "growthRates should be between 0-100"
   
    thelist = ['tobefilled'] #the list to be returned
    counter = 1 # counter for loop
    if len(growthRates) == 0:
        thelist[0]='Your retirement account is empty'
    elif len(growthRates) == 1:
        thelist[0]=savings * (1 + 0.01 * growthRates[0])- expenses 
    else:
        thelist[0]=savings * (1 + 0.01 * growthRates[0])- expenses 
        while counter<len(growthRates):
            thelist.append(thelist[counter-1]*(1+0.01*growthRates[counter])-expenses)
            counter = counter + 1
    return thelist

def findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates,
                    epsilon):
    """
    - salary: the amount of money you make each year.
    - save: the percent of your salary to save in the investment account each
      year (an integer between 0 and 100).
    - preRetireGrowthRates: a list of annual growth percentages on investments
      while you are still working.
    - postRetireGrowthRates: a list of annual growth percentages on investments
      while you are retired.
    - epsilon: an upper bound on the absolute value of the amount remaining in
      the investment fund at the end of retirement.
    """
    thesavingsbyyear =nestEggVariable(salary,save,preRetireGrowthRates)
    thesavings=thesavingsbyyear[-1]
    #i m about to try to find the value for expenses in postRetirement where
    # postRetirement(thesavings+epsilon,postRetireGrowthRates,expenses)[len(postRetireGrowthRates)-1]=0
    exphigh=thesavings
    explow=0
    guess=(exphigh+explow)/2
    limitcounter =0 
    limit = 150 #i know its high ;-)
    
    while postRetirement(thesavings+epsilon,postRetireGrowthRates,guess)[len(postRetireGrowthRates)-1]!=0:
        while postRetirement(thesavings+epsilon,postRetireGrowthRates,guess)[len(postRetireGrowthRates)-1]>0:
            print 'You have to spend more than ',guess,' annually. \n'
            explow = guess
            guess=(exphigh+explow)/2
            limitcounter = limitcounter + 1
            if limitcounter > limit:
                return "You passed the iteration limit"
        while postRetirement(thesavings+epsilon,postRetireGrowthRates,guess)[len(postRetireGrowthRates)-1]<0:
            print 'You have to spend less than ',guess,' annually. \n'
            exphigh = guess
            guess=(exphigh+explow)/2
            limitcounter = limitcounter + 1
            if limitcounter > limit:
                return "You passed the iteration limit"
    return guess
    
    
    

def testFindMaxExpenses():
    salary                = 10000
    save                  = 10
    preRetireGrowthRates  = [3, 4, 5, 0, 3]
    postRetireGrowthRates = [10, 5, 0, 5, 1]
    epsilon               = .01
    expenses = findMaxExpenses(salary, save, preRetireGrowthRates,
                               postRetireGrowthRates, epsilon)
    print expenses
    # Output should have a value close to:
    # 1229.95548986
def testFindMaxExpenses2():
    salary                = 60000
    save                  = 15
    preRetireGrowthRates  = [8,9,4,5,9,8,9,8,5,0,0,5]
    postRetireGrowthRates = [1,6,6,5,1,8,1]
    epsilon               = .001
    expenses = findMaxExpenses(salary, save, preRetireGrowthRates,
                               postRetireGrowthRates, epsilon)
    print expenses
    # Output should have a value close to:
    # 23616.7691323

dacorest 1 year ago
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 5, HW 1
# Name: Waz
#Problem set 3

_______Problem 1__________
from string import*
def countSubStringMatch(target,key):
   counter =0
   whurr = find(target,key)
   while whurr!= -1:
      counter += 1
      whurr = find(target, key, whurr+1)
   return counter

_______Problem 2_____________
from string import*

def countSubStringMatchExact(target,key):
   stpoints = ()
   whurr = find(target,key)
   while whurr!= -1:
      stpoints += (whurr,)
      whurr = find(target, key, whurr+1)
   return stpoints

_________Problem 3_____________
def constrainedMatchPair(firstMatch,secondMatch,length):
    result = ()
    for n in firstMatch:
        for k in secondMatch:
            if n + length+ 1 == k:
                result += (n,)
    return result

_________Problem 4___________
def subStringMatchOneSub(key,target):
    """search for all locations of key in target, with one substitution"""
    allAnswers = ()
    for miss in range(0,len(key)):
        # miss picks location for missing element
        # key1 and key2 are substrings to match
        key1 = key[:miss]
        key2 = key[miss+1:]
        print 'breaking key',key,'into',key1,key2
        # match1 and match2 are tuples of locations of start of matches
        # for each substring in target
        match1 = subStringMatchExact(target,key1)
        match2 = subStringMatchExact(target,key2)
        # when we get here, we have two tuples of start points
        # need to filter pairs to decide which are correct
        filtered = constrainedMatchPair(match1,match2,len(key1))
        allAnswers = allAnswers + filtered
        print 'match1',match1
        print 'match2',match2
        print 'possible matches for',key1,key2,'start at',filtered
    return allAnswers
        

dacorest 1 year ago
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 3, HW 1
#Name: Waz
# Problem set 2

# _____________Part 1_______
#to show the combos, let's create a function to do so
def showcombos(noofng):
   for a in range (0,noofng+1):
      for b in range (0,noofng+1):
         for c in range (0,noofng+1):
            if (6*a) + (9*b) + (20*c) == noofng:
               print a, "x6pcks + ",b,"x9pcks+ ",c, "x20pcks = ", noofng, "nuggets."
            c = c + 1
         b = b + 1
      a = a + 1
#let's show the results
print "\n Combos for 50 nuggets"
showcombos (50)
print "\n Combos for 51 nuggets"
showcombos (51)
print "\n Combos for 52 nuggets"
showcombos (52)
print "\n Combos for 53 nuggets"
showcombos (53)
print "\n Combos for 54 nuggets"
showcombos (54)
print "\n Combos for 55 nuggets"
showcombos (55)


____________Part 2_______

print "\n The reason that every number greater than x will generate a combo is \n because every number higher than x+5 equals to a number between x and x+5 plus \n a multiple of 6. That multiple will only augment 'a' but we will still have a combo"

___________Part3___________


def showcombos(noofng): #noofng = number of nuggets
   for a in range (0,noofng+1):
      for b in range (0,noofng+1):
         for c in range (0,noofng+1):
            if (6*a) + (9*b) + (20*c) == noofng:
               Hascombo = True
               return Hascombo
          
strt=50  #we are starting from 50 because we know anything >50 will have a combo.
while showcombos(strt)<> None:
    strt = strt - 1
    showcombos (strt)
print "Largest number of McNuggets that cannot be bought in exact quantity: ", strt

#------------Generalized version--------------------
def showcombos(noofng,x,y,z):  
   for a in range (0,noofng+1):
      for b in range (0,noofng+1):
         for c in range (0,noofng+1):
            if (x*a) + (y*b) + (z*c) == noofng:
               Hascombo = True
               return Hascombo
          
def findbiggest (strt, x,y,z): #x,y,z are the sizes of the packages & strt= where to start counting down
   while showcombos(strt,x,y,z)<> None:
       print strt, "has combos"
       strt -= 1
       showcombos (strt,x,y,z)
   print strt, " is the biggest number of nuggets that you can't\nhave the exact amount with that combo."

_________Part 4________________


def showcombos(noofng,x,y,z):  
   for a in range (0,noofng+1):
      for b in range (0,noofng+1):
         for c in range (0,noofng+1):
            if (x*a) + (y*b) + (z*c) == noofng:
               Hascombo = True
               return Hascombo
          
def findbiggestu200 (x,y,z): #x,y,z are the sizes of the packages 
   strt =200
   while showcombos(strt,x,y,z)<> None:
       strt -= 1
       showcombos (strt,x,y,z)
   print "Given package sizes ",x,",", y, "and ",z," the largest number of McNuggets that cannot be bought in exact quantity is: ", strt

dacorest 1 year ago
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
#problem set 1
#Name Waz


# Problem 1

#Write a program that computes and prints the 1000th prime number. 

import math
candidate = 3 #starting from 3
counter = 1 #counting no 2
while counter < 1000:
   divisor =3 #no need to start below 3
   while (divisor< math.sqrt(candidate) and candidate%divisor <> 0): # test4primality(credit to munia)
        divisor = divisor + 2
   if divisor > math.sqrt (candidate): #if we find a prime
        counter = counter +1 #up the count
   candidate= candidate + 2
print candidate-2 #to substract the last 2 added

#Problem 2

#Write a program that computes the sum of the logarithms of all the primes from 2 to some number n, #and print out the sum of the logs of the primes, the number n, and the ratio of these two quantities. #Test this for different values of n. 

import math
candidate = 3 #starting from 3
counter = 1 #counting no 2
n=int(raw_input("What is your n?"))
sumoflogs=math.log(2) #we start with 2, remember?
while candidate < n:
   divisor =3 #no need to start below 3
   while (divisor< math.sqrt(candidate) and candidate%divisor <> 0): # test4primality
        divisor = divisor + 2
   if divisor > math.sqrt (candidate): #if we find a prime
        sumoflogs = sumoflogs + math.log(candidate)
   candidate= candidate + 2
print sumoflogs
ratio = sumoflogs/candidate
print "Ratio is ", ratio

dacorest 1 year ago
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 1, HW 1
#problem set 0
#Name Waz
#time: 6mins


ln = raw_input('What is your last name?')
print
fn = raw_input('What is your first name?')
print
print 'Your name is '+ ' '+ fn +' '+ln


dacorest 1 year ago