Expert answer:anybody familiar with python

Solved by verified expert:all info you need is in the file copy the tester file into a .py file to test it
l7.docx

l7teaster.docx

Unformatted Attachment Preview

1. We can define functions to name a block of code, and then feed it inputs and get an
output. Functions also allow us to give default values for parameters, which is useful
but needs special attention. We will write functions that are more than just sideeffect-free input-output factories.
Notes


Don’t call input() or print() anywhere. 
We need you to figure out the “signature” (top line) of some functions, so we
don’t explicitly show 
you the parameters list those times. Part of the goal is learning how to choose
when to use default 
parameters, and what default values to use. 

You’re also learning when to modify the original or not; test cases should
target this fact. 
Turning It In 
Add a comment at the top of the file that indicates your name, userID, G#, lab
section, a description of your collaboration partners, as well as any other
details you feel like sharing. Once you are done, run the testing script once
more to make sure you didn’t break things while adding these comments. If
all is well, go ahead and turn in just your one .py file you’ve been working on,
named with our usual convention (see above), over on BlackBoard to the
correct lab assignment. We have our own copy of the testing file that we’ll
use, so please don’t turn that in (or any other extra files), as it will just slow
us down. 
Tasks
Implement each of these four functions.
Returning multiple values: tupling up the results.
def rank3(x,y,z, ascending=True): Given three integers, return them in order
in a tuple of length three. An optional fourth argument (ascending) states whether
output should be sorted ascending (argument is True) or descending (argument is
False).
o Examples:
▪rank3(5, 3, 4)
▪rank3(5, 3, 4, False) ▪rank3(6, 8, 6)
→ (3,4,5) → (5,4,3) → (6,6,8)
Allowing defaults, modifying a list in-place:
def remove(val, xs, limit=None): Remove multiple copies of val from xs
(directly modify the list value that xs refers to). You may only remove up to the first
limit occurrences of val. If limit==3, and xs had ten copies of val in it, then
you’d only remove the first three and leave the last seven in place. When
limit==None, there’s truly no limit (and we remove all occurrences of val). Return
None, because the work happened in-place. Negative or zero limit: no removals.
o hint: if you need to traverse a list from front to back, but you don’t always want to
go to the next index location, while loops can be very useful – we don’t have to step
forward each iteration.
o Note – some test cases are multiple lines (which we’ve avoided so far). You might
need to notice the line number of a failed test, and go look at the code of that
particular testing function, to see what’s being attempted. This is a good habit to get
into for projects.
o Examples:

▪ >>> xs = [1,2,1,3,1,4,1,5,1]
>>> remove(1,xs) >>> xs
[2, 3, 4, 5]

▪ >>> xs = [1,2,1,3,1,4,1,5,1] >>> remove(1,xs,3)
>>> xs
[2, 3, 4, 1, 5, 1]
(see more on the next page)
Guess the Signature
Designing a function’s signature is an important skill. We haven’t gotten a lot of
practice yet, as we absolutely need your functions to work with our testers, and that
has so far meant that we tell you exactly how to interface with the testing scripts.
But now, we will describe the signature, and you have to get it correct based upon
the needs of the function (how it’s supposed to behave).
filter_chars: Write the definition for the filter_chars function. It accepts a
message as the first argument (call it msg), and an optional argument called keeps
as the second argument. Dig through msg, and any character in msg that is also
present in keeps gets kept; all others are ignored/skipped. Return this possiblyshorter string.
o Strings can’t be modified, so we can’t modify the message. But we can create a
new string that only contains the parts we wanted to keep.
o When keeps isn’t provided ,all upper and lower case letters should be kept. o
Hint: the in keyword is your friend.
o Examples:
▪filter_chars(“hi
▪filter_chars(“hi
▪filter_chars(“hi
▪filter_chars(“hi
there!
there!
there!
there!
:)”)
:)”, “ether”)
:)”, “ie:”)
:)”,”1234567890″) →
Using a default value that is complex: Watch out!
→→→
‘hithere’ ‘hthere’ ‘iee:’

relocate_evens: The first argument, data, contains integers. We will be
removing any evens we find in data, and appending them to the second argument,
new_home. A reference to the list which received any evens must be returned. When
no second argument is given, a list of all the relocated evens should be created and
returned.
o Careful!What default value do you want to use for new_home?What happens when
we call the function multiple times in the same coding session? (try it out)
o Go ahead and use methods like .insert(), .pop(), .append()
o Examples:

▪ >>> xs = [1,2,3,4,5]
>>> relocate_evens(xs) [2,4]
>>> xs
[1,3,5]

▪ >>> vals = [6,7,8,9,10]
>>> sanctuary = [2,4]
>>> relocate_evens(vals,sanctuary)
[2,4,6,8,10]
>>> sanctuary # same as what relocate_evens returned [2,4,6,8,10]
>>> vals # all evens have been relocated out of here. [7,9]
# Based on testing harness dated 2017-06-02.
# STUDENTS: TO USE:
#
# The following command will test all test cases on your file:
#
# python3
#
#
# You can also limit the tester to only the functions you want tested.
# Just add as many functions as you want tested on to the command line at the end.
# Example: to only run tests associated with func1 and func2, run this command:
#
# python3 func1 func2
#
# You really don’t need to read the file any further, except that when
# a specific test fails, you’ll get a line number – and it’s certainly
# worth looking at those areas for details on what’s being checked. This would
# all be the indented block of code starting with “class AllTests”.
# INSTRUCTOR: TO PREPARE:
# – add test cases to class AllTests. The test case functions’ names must
# be precise – to test a function named foobar, the test must be named “test_foobar_#”
# where # may be any digits at the end, such as “test_foobar_13”.
# – any extra-credit tests must be named “test_extra_credit_foobar_#”
#
# – name all required definitions in REQUIRED_DEFNS, and all extra credit functions
# in EXTRA_CREDIT_DEFNS. Do not include any unofficial helper functions. If you want
# to make helper definitions to use while testing, those can also be added there for
# clarity.
#
# – to run on either a single file or all .py files in a folder (recursively):
# python3
# python3
# python3 .
# current directory
#
# A work in progress by Mark Snyder, Oct. 2015.
# Edited by Yutao Zhong, Spring 2016.
# Edited by Raven Russell, Spring 2017.
# Edited by Mark Snyder, June 2017.
import unittest
import shutil
import sys
import os
import time
#import subprocess
import importlib
############################################################################
############################################################################
# BEGIN SPECIALIZATION SECTION (the only part you need to modify beyond
# adding new test cases).
# name all expected definitions; if present, their definition (with correct
# number of arguments) will be used; if not, a decoy complainer function
# will be used, and all tests on that function should fail.
REQUIRED_DEFNS = [ “rank3”, “remove”,”filter_chars”,”relocate_evens” ]
# for method names in classes that will be tested
SUB_DEFNS = [ ]
# definitions that are used for extra credit
EXTRA_CREDIT_DEFNS = [ ]
# how many points are test cases worth?
weight_required = 4
weight_extra_credit = 1
# don’t count extra credit; usually 100% if this is graded entirely by tests.
# it’s up to you the instructor to do the math and add this up!
# TODO: auto-calculate this based on all possible tests.
total_points_from_tests = 100
# how many seconds to wait between batch-mode gradings?
# ideally we could enforce python to wait to open or import
# files when the system is ready but we’ve got a communication
# gap going on.
DELAY_OF_SHAME = 1
# set it to true when you run batch mode…
CURRENTLY_GRADING = False
# what temporary file name should be used for the student?
# This can’t be changed without hardcoding imports below, sorry.
# That’s kind of the whole gimmick here that lets us import from
# the command-line argument without having to qualify the names.
RENAMED_FILE = “student”
# END SPECIALIZATION SECTION
############################################################################
############################################################################
# enter batch mode by giving a directory to work on as the only argument.
BATCH_MODE = len(sys.argv)==2 and (sys.argv[1] in [“.”] or os.path.isdir(sys.argv[1]))
# This class contains multiple “unit tests” that each check
# various inputs to specific functions, checking that we get
# the correct behavior (output value) from completing the call.
class AllTests (unittest.TestCase):
########################################################################
####
def test_rank3_1 (self): self.assertEqual (rank3(5,3,4),(3,4,5))
def test_rank3_2 (self): self.assertEqual (rank3(1,3,1),(1,1,3))
def test_rank3_3 (self): self.assertEqual (rank3(2,4,3,False), (4,3,2))
def test_rank3_4 (self): self.assertEqual
(rank3(z=100,x=30,ascending=False,y=200),(200,100,30))
def test_rank3_5 (self): self.assertEqual (rank3(40,z=30,y=20,ascending=True),(20,30,40))
#
def test_rank3_6 (self): self.assertEqual (rank3(1,2,3),(1,2,3))
#
def test_rank3_7 (self): self.assertEqual (rank3(10,5,1),(1,5,10))
#
def test_rank3_8 (self): self.assertEqual (rank3(1,3,3,False),(3,3,1))
def test_remove_1 (self):
xs=[1,2,1,3,1,4,1,5,1]
remove(1,xs)
self.assertEqual (xs,[2,3,4,5])
def test_remove_2 (self):
xs=[1,2,1,3,1,4,1,5,1]
remove(1,xs,3)
self.assertEqual (xs,[2,3,4,1,5,1])
def test_remove_3 (self):
xs=[1,2,1,3,1,4,1,5,1]
remove(1,xs,-5)
self.assertEqual (xs,[1,2,1,3,1,4,1,5,1])
def test_remove_4 (self):
xs=[9,2,9,9,3,9,9,9,4,9,9,9,9,5,9,9,9,9,9,6]
remove(9,xs)
self.assertEqual (xs,[2,3,4,5,6])
def test_remove_5 (self):
xs=[1,2,1,3,1,4,1,5,1]
remove(1,xs,50)
self.assertEqual (xs,[2,3,4,5])
def test_remove_6 (self):
xs=[1,2,1,3,1,4,1,5,1]
remove(1,xs,0)
self.assertEqual (xs,[1,2,1,3,1,4,1,5,1])
def test_remove_7 (self):
xs=[1,2,1,3,1,4,1,5,1]
ans = remove(1,xs,5)
self.assertEqual (xs,[2,3,4,5])
self.assertEqual(ans, None)
#
#
#
#
def test_remove_8 (self):
xs=[1,2,1,3,1,4,1,5,1]
remove(1234,xs)
self.assertEqual (xs,[1,2,1,3,1,4,1,5,1])
def test_filter_chars_1 (self): self.assertEqual (filter_chars(“hi there! :)”),”hithere”)
def test_filter_chars_2 (self): self.assertEqual (filter_chars(“hi there! :)”,”ether”),”hthere”)
def test_filter_chars_3 (self): self.assertEqual (filter_chars(“hi there! :)”,”ie:”),”iee:”)
def test_filter_chars_4 (self): self.assertEqual (filter_chars(“hi there! :)”,”123″),””)
def test_filter_chars_5 (self): self.assertEqual (filter_chars(“abcd”,”abcdef”),”abcd”)
def test_filter_chars_6 (self): self.assertEqual (filter_chars(keeps=”aeiou”,msg=”pleased to
meet you”),”eaeoeeou”)
# #17. check that it removes the 2 and 4. Also, check that it returns the list of [2,4].
def test_relocate_evens_1 (self):
# part 1
xs = [1,2,3,4,5]
ans = relocate_evens(xs)
self.assertEqual (xs,[1,3,5])
# part 2
xs = [1,2,3,4,5]
ans = relocate_evens(xs)
self.assertEqual (ans,[2,4])
# #19 (50%). check that a given sanctuary is actually returned (same id)
# #20 (50%). check that an explicit place to put evens is used (should see the preserved
original values as part of the result).
def test_relocate_evens_2 (self):
vals = [6,7,8,9,10]
sanctuary = [2,4]
ans = relocate_evens(vals,sanctuary)
self.assertEqual (id(sanctuary), id(ans)) # part 1
self.assertEqual (sanctuary,[2,4,6,8,10]) # part 2
# #21. check that the answer given used the provided sanctuary (should see the
preserved original values as part of the returned answer)
def test_relocate_evens_3 (self):
vals = [6,7,8,9,10]
sanctuary = [2,4]
ans = relocate_evens(vals,sanctuary)
self.assertEqual (ans,[2,4,6,8,10])
# #22. check that the first argument is updated to remove; should have only odds left over.
def test_relocate_evens_4 (self):
vals = [6,7,8,9,10]
sanctuary = [2,4]
ans = relocate_evens(vals,sanctuary)
self.assertEqual (vals,[7,9])
# #23. check that we can remove all items from a list (if they’re all even), and that they’ll all
go to the destination.
def test_relocate_evens_5 (self):
vals = [2,4,6,8,10]
ans = relocate_evens(vals)
self.assertEqual (vals,[])
self.assertEqual (ans,[2,4,6,8,10])
# part 2:
# check that when there are no evens, we still have all the others left over.
# (a): check all values are still in original.
# (b): check none moved to sanctuary.
vals = [1,3,5,7,9]
ans = relocate_evens(vals)
self.assertEqual (vals,[1,3,5,7,9])
self.assertEqual (ans,[])
# #25. check that two consecutive calls with no second argument are successfully
unaffected by each other.
def test_relocate_evens_6 (self):
xs = [1,2,3,4,5]
ans1 = relocate_evens(xs)
self.assertEqual (ans1,[2,4])
self.assertEqual (xs,[1,3,5])
ys = [6,7,8,9,10]
# checking that the next call with no new_home value is
# safely unaffected by previous call.
ans2 = relocate_evens(ys)
self.assertEqual (ans2,[6,8,10])
self.assertEqual (ys,[7,9])
# enforcing that that parameters were named data and new_home by providing them out
of order as keyword arguments.
def test_relocate_evens_7 (self):
vals = [6,7,8,9,10]
sanctuary = [2,4]
ans = relocate_evens(new_home = sanctuary, data=vals)
self.assertEqual (vals,[7,9])
########################################################################
####
# This class digs through AllTests, counts and builds all the tests,
# so that we have an entire test suite that can be run as a group.
class TheTestSuite (unittest.TestSuite):
# constructor.
def __init__(self,wants):
self.num_req = 0
self.num_ec = 0
# find all methods that begin with “test”.
fs = []
for w in wants:
for func in AllTests.__dict__:
# append regular tests
# drop any digits from the end of str(func).
dropnum = str(func)
while dropnum[-1] in “1234567890”:
dropnum = dropnum[:-1]
if dropnum==(“test_”+w+”_”) and (not
(dropnum==(“test_extra_credit_”+w+”_”))):
fs.append(AllTests(str(func)))
if dropnum==(“test_extra_credit_”+w+”_”) and not BATCH_MODE:
fs.append(AllTests(str(func)))
#
print(“TTS ====> “,list(map(lambda f: (f,id(f)),fs)))
# call parent class’s constructor.
unittest.TestSuite.__init__(self,fs)
class TheExtraCreditTestSuite (unittest.TestSuite):
# constructor.
def __init__(self,wants):
# find all methods that begin with “test_extra_credit_”.
fs = []
for w in wants:
for func in AllTests.__dict__:
if str(func).startswith(“test_extra_credit_”+w):
fs.append(AllTests(str(func)))
#
print(“TTS ====> “,list(map(lambda f: (f,id(f)),fs)))
# call parent class’s constructor.
unittest.TestSuite.__init__(self,fs)
# all (non-directory) file names, regardless of folder depth,
# under the given directory ‘dir’.
def files_list(dir):
#
#
this_file = __file__
if dir==”.”:
dir = os.getcwd()
info = os.walk(dir)
filenames = []
for (dirpath,dirnames,filez) in info:
print(dirpath,dirnames,filez)
if dirpath==”.”:
continue
for file in filez:
if file==this_file:
continue
filenames.append(os.path.join(dirpath,file))
print(dirpath,dirnames,filez,”n”)
return filenames
def main():
if len(sys.argv)<2: raise Exception("needed student's file name as command-line argument:" +"nt"python3 testerX.py gmason76_2xx_Px.py"""")

How it works

  1. Paste your instructions in the instructions box. You can also attach an instructions file
  2. Select the writer category, deadline, education level and review the instructions 
  3. Make a payment for the order to be assignment to a writer
  4.  Download the paper after the writer uploads it 

Will the writer plagiarize my essay?

You will get a plagiarism-free paper and you can get an originality report upon request.

Is this service safe?

All the personal information is confidential and we have 100% safe payment methods. We also guarantee good grades

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more

Order your essay today and save 20% with the discount code ESSAYHELP