计算机代写|COMP 202 Fall 2022 Assignment 3

这是一篇来自加拿大的关于解决一些函数相关问题的计算机代写

 

Part 1 (0 points): Warm-up

Do NOT submit this part, as it will not be graded. However, doing these exercises might help you to do the second part of the assignment, which will be graded. If you have difffficulties with the questions of Part 1, then  we suggest that you consult the TAs during their offffice hours; they can help you and work with you through  the warm-up questions. You are responsible for knowing all of the material in these questions.

Warm-up Question 1 (0 points)

Write a function split_at_space that takes as input a string of words with spaces in between each word and returns a list of the words in the string. Do not use the string method split to do this.

>>> split_at_space(‘aardvark butterfly carrot’)

[‘aardvark’, ‘butterfly’, ‘carrot’]

Warm-up Question 2 (0 points)

Write a function generate_random_list which takes an input an integer n and returns a list containing n random integers between 0 and 100 (both included). Use the random module to do this.

Warm-up Question 3 (0 points)

Write a function sum_numbers which takes as input a list of integers and returns the sum of the numbers in the list. Do not use the built-in function sum to do this.

Warm-up Question 4

(0 points)

Write a function same_elements which takes as input a two dimensional list and returns True if all the elements in each sublist are the same and False otherwise. For example,

>>> same_elements([[1, 1, 1], [‘a’, ‘a’], [6]])

True

>>> same_elements([[1, 6, 1], [6, 6]])

False

Warm-up Question 5 (0 points)

Write a function flatten_list which takes as input a two dimensional list and returns a one dimensional

list containing all the elements of the sublists. For example,

>>> flatten_list([[1, 2], [3], [‘a’, ‘b’, ‘c’]])

[1, 2, 3, ‘a’, ‘b’, ‘c’]

>>> flatten_list([[]])

[]

Part 2

The questions in this part of the assignment will be graded.

The main learning objectives for this assignment are:

  • Apply what you have learned about lists and nested lists.
  • Understand how to raise exceptions in cases of invalid inputs.
  • Solidify your understanding of working with loops and strings.
  • Apply what you have learned about file IO and string manipulation.
  • Understand how to write a docstring and use doctest, in particular when working with files.
  • Practice making copies of lists so that inputs are not modified.

Note that this assignment is designed for you to be practicing what you have learned in our  lectures up to and including Lecture 22 (File IO II). For this reason, you are NOT allowed to  use anything seen after that lecture or not seen in class at all. You will be heavily penalized  if you do so.

For full marks, in addition to the points listed on page 1, make sure to add the appropriate documentation string (docstring) to all the functions you write. The docstring must contain the following:

  • The type contract of the function.
  • A description of what the function is expected to do.
  • At least three (3) examples of calls to the function. You are allowed to use at most one example per function from this PDF.

Examples

For each question, we provide several examples of how your code should behave. All examples are given as if you were to call the functions from the shell.

When you upload your code to codePost, some of these examples will be run automatically to test that your code outputs the same as given in the example. However, it is your responsibility to make sure  your code/functions work for any inputs, not just the ones shown in the examples. When the time comes to grade your assignment, we will run additional, private tests that will use inputs not seen in the examples. You should make sure that your functions work for all the different possible scenarios. Also, when testing your code, know that mindlessly plugging in various different inputs is not enough—it’s not the quantity of tests that matters, it’s having tests that cover all of the possible scenarios, and that requires thinking about possible scenarios.

Furthermore, please note that your code files should not contain any function calls in the main body  of the program (i.e., outside of any functions). Code that contains function calls in the main body will  automatically fail the tests on codePost and thus be heavily penalized. It is OK to place function calls in the main body of your code for testing purposes, but if you do so, make certain that you remove them before submitting. You can also test your functions by calling them from the shell. Please review what you have learned in our lecture on Modules if you’d like to add code to your modules which executes only when you run your files.

Safe Assumptions

For all questions on this assignment, you can safely assume that the type of the inputs (both to the functions and those provided to the program by the user) will always be correct. For example, if a function takes as input a string, you can assume that a string will always be provided to it during testing. The same goes for user input. At times you will be required to do some input validation, but this requirement will always be clearly stated. Otherwise, your functions should work with any possible input that respect the function’s description. For example, if the description says that the function takes as input a positive integer, then it should work with all integers greater than 0. If it mentions an integer, then it should work for any integer.

Make sure to test your functions for edge cases!

Code Repetition

One of the main principles of software development is DRY: Don’t Repeat Yourself. One of the main ways we can avoid repeating ourselves in code is by writing functions, then calling the functions when necessary, instead of repeating the code contained within them. Please pay careful attention in the questions of this assignment to not repeat yourself, and instead call previously-defined functions whenever appropriate. As always, you can also add your own helper functions if need be, with the intention of reducing code repetition as much as possible.