Question: I am beginning to learn how to code in python and I am having trouble implementing a function based on the specifications given. I get

I am beginning to learn how to code in python and I am having trouble implementing a function based on the specifications given. I get an error that says "The call fold_right(add, (), 3) returns (), not 3." Note that we are not allowed to use method calls.

Below is my code funcs.py:

def fold_right(f,seq,value): """ Returns the result of folding f right over seq, starting with value. To fold a function f from the right, we * Start with value in the accumulator * Iterate over the sequence right-to-left * At each step, apply f to the next element and the accumulator * After applying f, make the new value the accumulator Example: Suppose f is - (subtraction), seq is (1,2,3,4) and value is 0. Then the result is (1-(2-(3-(4-0)))) = -2 Parameter f: the function to fold Precondition: f is a function that takes two arguments of the same time, and returning a value of the same type Parameter seq: the sequence to fold Precondition: seq is a sequence (tuple, string, etc.) whose elements are the same type as that returned by f Parameter value: the initial starting value Precondition: value has the same type as the return type of f """ accum = value for item in seq: if f(item): accum = accum + ( item, ) return accum

THiS IS THE TEST.PY has all the test cases. My code is crashing in the 3 test case

import funcs import introcs

# Some f values to test def add(x,y): """ Returns the sum x+y This works on any types that support addition (numbers, strings, etc.) Parameter x: The first value to add Precondition: x supports addition and x is same type as y Parameter x: The second value to add Precondition: x supports addition and x is same type as y """ return x+y

def sub(x,y): """ Returns the difference x-y Parameter x: The value to subtract from Precondition: x is a number Parameter y: The value to subtract Precondition: y is a number """ return x-y

def remove(s1,s2): """ Returns a copy of s, with all characters in s2 removed. Examples: remove('abc','ab') returns 'c' remove('abc','xy') returns 'abc' remove('hello world','ol') returns 'he wrd' Parameter s1: the string to copy Precondition: s1 is a string Parameter s2: the characters to remove Precondition: s2 is a string """ result = '' for x in s1: if not x in s2: result = result + x return result

# Test test proceduresdef test_fold_right(): """ Test procedure for fold_right """ print('Testing fold_right()') # Tuple tests result = funcs.fold_right(add,(),3) introcs.assert_equals(3,result) result = funcs.fold_right(sub,(),2) introcs.assert_equals(2,result) result = funcs.fold_right(add,(4,),0) introcs.assert_equals(4,result) result = funcs.fold_right(sub,(4,),0) introcs.assert_equals(4,result) result = funcs.fold_right(sub,(4,),2) introcs.assert_equals(2,result) result = funcs.fold_right(add,(1,2,3,4),0) introcs.assert_equals(10,result) result = funcs.fold_right(sub,(1,2,3,4),0) introcs.assert_equals(-2,result) result = funcs.fold_right(sub,(1,-2,3,-4),0) introcs.assert_equals(10,result) # String tests result = funcs.fold_right(add,'','A') introcs.assert_equals('A',result) result = funcs.fold_right(add,'B','') introcs.assert_equals('B',result) result = funcs.fold_right(add,'bcdefg','a') introcs.assert_equals('bcdefga',result) result = funcs.fold_right(remove,'','a') introcs.assert_equals('a',result) result = funcs.fold_right(remove,'a','a') introcs.assert_equals('',result) result = funcs.fold_right(remove,'b','a') introcs.assert_equals('b',result) # This is counter-intuitive. Try it out in your head result = funcs.fold_right(remove,'abc','d') introcs.assert_equals('a',result) result = funcs.fold_right(remove,'aabc','d') introcs.assert_equals('',result)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!