Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am beginning to learn how to code and I am having trouble implementing a function based on the specifications given. I pass 2 test

I am beginning to learn how to code and I am having trouble implementing a function based on the specifications given. I pass 2 test cases and failing the other's and dont really understand why it passes 2 and then fails the third test case. I get a type error problem and I am wondering if it because the tuple in the test case has 1 element instead of 2?

Below is my code funcs.py:

def fold_left(f,seq,value): """ Returns the result of folding f left over seq, starting with value. To fold a function f from the left, we * Start with value in the accumulator * Iterate over the sequence normally * At each step, apply f to the accumulator and the next element * 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 ((((0-1)-2)-3)-4) = -10 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 """ #start with the value in the accumulator accumulator = value #Iterate over the sequence normally for item in seq: #apply f to the accumulator and the next element After applying f, make the new value the accumulator if f(item): accumulator = accumulator + (item, ) return accumulator

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 procedures def test_fold_left(): """ Test procedure for fold_left """ print('Testing fold_left()') # Tuple tests result = funcs.fold_left(add,(),3) introcs.assert_equals(3,result) result = funcs.fold_left(sub,(),2) introcs.assert_equals(2,result) result = funcs.fold_left(add,(4,),0) introcs.assert_equals(4,result) result = funcs.fold_left(sub,(4,),0) introcs.assert_equals(-4,result) result = funcs.fold_left(sub,(4,),2) introcs.assert_equals(-2,result) result = funcs.fold_left(add,(1,2,3,4),0) introcs.assert_equals(10,result) result = funcs.fold_left(sub,(1,2,3,4),0) introcs.assert_equals(-10,result) result = funcs.fold_left(sub,(-1,-2,-3,-4),0) introcs.assert_equals(10,result) # String tests result = funcs.fold_left(add,'','A') introcs.assert_equals('A',result) result = funcs.fold_left(add,'B','') introcs.assert_equals('B',result) result = funcs.fold_left(add,'bcdefg','a') introcs.assert_equals('abcdefg',result) result = funcs.fold_left(remove,'','a') introcs.assert_equals('a',result) result = funcs.fold_left(remove,'a','a') introcs.assert_equals('',result) result = funcs.fold_left(remove,'b','a') introcs.assert_equals('a',result) result = funcs.fold_left(remove,'abcef','abcefhijklimnop') introcs.assert_equals('hijklimnop',result)

if __name__ == '__main__': test_fold_left() print('Module funcs is working correctly')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions