Question

Your task is to: 1. Introduce function documentation (the triple-quoted string that precedes the definition of a Python funct# TODO: # 1. Write PyDoc for the function # 2 . Make any changes needed for readability def sum (x): return 0 else:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Changes done for the readability of code are:

  1. The given method sum() actually multiplies all the numbers passed as a list, but named as sum(). So change the method name to multiplyNumbers()
  2. The variable name running_total is not appropriate as the method does product but not sum of numbers. So change the variable name from running_total to running_product
  3. Added documentation for the method multiplyNumbers()

Program:

1#Change method name from sun to multiplyNumbers 2 def multiplyNumbers(x): 4 mulitplyNumbers(...) multiplyNumbers (iterable)

Code:

#Change method name from sum to multiplyNumbers
def multiplyNumbers(x):
   """
   mulitplyNumbers(...)
       multiplyNumbers(iterable) -> value
  
       Return the product of an iterable or sequence of numbers
       When the sequence is empty, returns 0.
   """
   if x == []:
       #empty list so return 0
       return 0
   else:
       #initial product is 1
       running_product = 1
       for i in x:
           #multiply all the numbers in the list
           running_product *= i
       #return product
       return running_product
      
a = [0,1,3,5]
b = [-1,3,6,23,-9]
c = [-1,-3,-5]
d = [0]
e = []

#Tests for multiplyNumbers
assert(multiplyNumbers(a) == 0)
assert(multiplyNumbers(b) == 3726)
assert(multiplyNumbers(c) == -15)
assert(multiplyNumbers(d) == 0)
assert(multiplyNumbers(e) == 0)
print("All tests passed")

Output:

File Edit View Search Terminal Help All tests passed (program exited with code: θ) Press return to continue

Add a comment
Know the answer?
Add Answer to:
Your task is to: 1. Introduce function documentation (the triple-quoted string that precedes the definition of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • Assignment 1) Your function will only write the Nth character of the array, supplied as a...

    Assignment 1) Your function will only write the Nth character of the array, supplied as a parameter by the caller. This is an example prototype (although you may want to change the return value type, based on the second twist, below). void writeArrayNthBackward(const char [], int, int, int); The first three arguments serve the same purpose as in the iterative example. The fourth argument (an int) supplies the N for the Nth character to print in the array. If n...

  • In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the...

    In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the code in main to create and sort the array of pointers. The places to make modifications are indicated by TODO: comments. You should not have to make modifications anywhere else. 3- what's big O and runtime for 100000 items. #include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <cstdlib> #include <cassert> using namespace std; // Set this to false to skip the...

  • 18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one...

    18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one part to lab lesson 11. The entire lab will be worth 100 points. Lab lesson 11 part 1 is worth 100 points For part 1 you will have 80 points if you enter the program and successfully run the program tests. An additional 20 points will be based on the style and formatting of your C++ code. Style points The 20 points for coding...

  • Please note that I cannot use the string library function and that it must be coded...

    Please note that I cannot use the string library function and that it must be coded in C89, thank you! Formatting: Make sure that you follow the precise recommendations for the output content and formatting: for example, do not change the text in the first problem from “Please enter a string of maximum 30 characters:” to “Enter string: ”. Your assignment will be auto-graded and any changes in formatting will result in a loss in the grade. 2.Comments: Header comments...

  • Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will...

    Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will call our version MyString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The MyString class will handle constructing strings, reading/printing, and accessing characters. In addition, the MyString object will have the ability to make a full deep-copy of itself...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • Lab 1.java only Goal: This lab will give you experience with defining and using classes and...

    Lab 1.java only Goal: This lab will give you experience with defining and using classes and fields, and with conditionals and recursive functions. Getting Started --------------- Read the Fraction.java class into a text editor and compile it filling in the command javac -g Fraction.java. The program should compile without errors. In a shell window, run the program using "java Fraction". The program should run, although it will print fractions in a non-reduced form, like 12/20. Part I: Constructors (1 point)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT