Question

By using Python, data class and Functional Programming(don't use while & for loops but you can...

By using Python, data class and Functional Programming(don't use while & for loops but you can use if statement)

Write a program that reads table with given columns from input stream. Columns are name, amount, debt. Then filter the table (condition: debt is equal to 0). After that increase debt by 42% then output the results.

Example.

User's Input:

4 - No. of People

Sam Hill 411889 36881

Angela Smith 121877 0

Nicholas Ander 783887 591951

Lance Rod 123543 0


Expected Output:

Sam Hill  411889 52371.02
Nicholas Ander 783887 840570.42

**Please also explain your answer**

Homework Answers

Answer #1
#A Class is created which binds the name, amount and dept together into a single object
class Data: 
    def __init__(self,name,amount,dept):
        self.name=name
        self.amount=amount
        self.dept=dept

#logic of the recurssive start function :
#1.the variable n holds the total no of inputs and if it becomes zero that means our input taking is done and we can further proceed to analyze the data
def start(n,objlist):
    if n==0: #if the n becomes 0 that means our inputs are succesfull and we proceed to the analyze func
        analyzeData(objlist,len(objlist),0,[])
    else:
        str=input() # we are taking the input in a single line as a string
        str=str.split(' ') #by using split function we create a list from the input by space , ex :- "abc fgb" becomes ["abc","fgh"]
        if str[0]=='' and str[1]=='' and str[2]=='': #if any of the params are empty then we call the function agian with same n means we are retaking the input
            print("Re-enter all the 3 values, Enter again")
            start(n,objlist)
        try: #we are checking if the amount (in str[1] and dept (in str[2]) is number or not)
            str[1]=float(str[1])
            str[2]=float(str[2])
            data=Data(str[0],str[1],str[2]) #here if the amount and dept are floats then we proceed to craete the data object
            objlist.append(data) #we add the object to the list
            start(n-1,objlist) #we call this function again but instead of "n" we pass "n-1" which makes sure this input was succesfull and we can proceed to the next input
        except:#if the amount or dept is not a number then the below statements are executed which re-takes the input
            print("The amount and dept should be number, Try Again")
            start(n,objlist)        

#this functions is also a recursive function we pass the length of the array as n and an index 0
#if the value of n and i becomes equal then our analysis is over
#after each analysis we increment i by 1
def analyzeData(objlist,n,i,result):
    if n==i:#once the calculation of all the objects are done we call the printOutput  function 
        print("Result : ")
        printOutput(result,len(result),0)
    else:
        if objlist[i].dept!=0: #if the dept is not 0 the we the calculation in the next line and we add the object to the new "result" list
            objlist[i].dept=objlist[i].dept*42/100+objlist[i].dept
            result.append(objlist[i])
        i=i+1
        analyzeData(objlist,n,i,result) #after 1 analysis or calculation we increment i with 1 and call the function again which will then do the calculation on the next object
            
def printOutput(result,n,i):#This function prints the results recursively
    if n==i:
        pass
    else:
        print(result[i].name,result[i].amount,result[i].dept)
        i=i+1
        printOutput(result,n,i)


# The Program starts from here 
# Note : Because in the question you have mentioned that I cannot use for and while loops
# I have used recursion throughout whenever looping was needed
print("Enter no of inputs : ")
n=int(input()) 
print("Enter name, amount and dept");
objlist=[] #empty list which will hold the objects in future
start(n,objlist) #the number of inputs and an empty array is passed in this function
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT