Question: USING PYTHON, please complete the template below to solve these problems: Right-mouse click and save the file as project4.py. #PROBLEM 1 def chop(l1,w): list1 =
USING PYTHON, please complete the template below to solve these problems:
Right-mouse click and save the file as project4.py.
#PROBLEM 1 def chop(l1,w): list1 = l1 width = w #YOUR CODE GOES HERE (indented) return list1 #END YOUR CODE #PROBLEM 2 def remove(l1,l2): string1 = l1 string2 = l2 #YOUR CODE GOES HERE (indented) return string1 #END YOUR CODE
| Problem 1: | Write code that takes a non-empty list of integers from the user, as well as a width. Return a new list where the original list has been converted into a two dimensional list with the specified width, padded with zeroes at the end as needed. You may not use any built-in functions/methods besides len() and .append(). (This was a homework problem) |
| Problem 2: | Write code that takes two strings from the user, and returns what is left over if the second string is removed from the first. The second string to be removed is guaranteed to be less than four letters long. |
Test cases for problem 1:
chop [] 2 [[0, 0]] chop [1] 2 [[1, 0]] chop [1,2,3] 3 [[1, 2, 3]] chop [1,2,3,4,5,6,7] 3 [[1, 2, 3], [4, 5, 6], [7, 0, 0]] Test cases for problem 2: remove bird cat bird remove bird i brd remove bird bi rd remove birdbirdbikd ir bdbdbikd
Please use this driver to test your code and make sure that it works as expected. Thanks
By right-mouse-click to save the file driver to the same directory as your project4.py. You can then run your code against our test cases through the terminal with the command" python driver4_sample.py
Driver is below:
import subprocess from project4 import * file = open("tests4_sample.txt") contents = file.readlines() file.close() source = open("project4.py") lines = source.readlines() source.close() ctr = 0 bad = False for line in lines: if (".index(" in line or ".rindex(" in line or ".remove(" in line or ".count(" in line or ".find(" in line or ".slpit(" in line or ".rsplit(" in line or ".rindex(" in line or ".join(" in line or ".split(" in line or ".replace(" in line): print "ERROR: on line " +str(ctr) + ": "+ line + " you are using a forbidden function or method. You are only allowed to use len(...) and/or .append(...) for this assignment." bad = True failed = False ctr = 0 while ctr + 1< len(contents) and not bad: pieces = contents[ctr].split(" ") if pieces[0] == "chop": result = chop(eval(pieces[1]),int(pieces[2])) expected = contents[ctr+1].rstrip() elif pieces[0] == "remove": result = remove(pieces[1],pieces[2][:-1]) expected = contents[ctr+1].rstrip() if (expected == str(result)): print "passed test" else: print "For input " + contents[ctr].rstrip() + ", we expected '" + expected + "', but you got '" + str(result) + "'" failed = True ctr = ctr + 2 if not failed and not bad: print "You passed all tests! Great work! PLEASE REMOVE ALL debug PRINT STATEMENTS FROM YOUR CODE before submitting to Marmoset." result = subprocess.check_output("curl -k https://cs.gmu.edu/~kdobolyi/sparc/process.php?user=sparc_JjNcihjCCN7CpKRh-sample_ass4_1-COMPLETED", shell=True) else: print "At least one test case did not pass yet." result = subprocess.check_output("curl -k https://cs.gmu.edu/~kdobolyi/sparc/process.php?user=sparc_JjNcihjCCN7CpKRh-sample_ass4_1-PROGRESS", shell=True)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
