In Python
Use the Design Recipe to write a function factorial that when given a positive integer calculates the factorial. If given a negative integer or not an integer), the function should return None. If given 0, it should return 1.
Include a docstring!
Note: You may assume all arguments passed to this function will be numeric.
Write three assertEqual statements to test your function. To test and receive credit for your assertEqual statements, copy them into Part E and click 'Evaluate'.
def factorial(n):
"""
if n is negative, then return None
if n is zero, then return 1
if n is positive, then return factorial of n
:param n:
:return: factorial of number n
"""
if n < 0:
return None
elif n == 0:
return 1
else:
return n * factorial(n-1)
assert factorial(5) == 120
assert factorial(0) == 1
assert factorial(-2) is None
In Python Use the Design Recipe to write a function factorial that when given a positive...
CODE IN PYTHON 17. Use the Design Recipe to define a function called black_jack which consumes to 2 int values greater than 0. Return whichever value is nearest to 21 without going over. Return 0 if they both go over. Include a docstring! Write three assertEqual statements to test your function.
Python Use the Design Recipe to write a function called running_average that repeatedly asks the user to input integers at the keyboard until they type the word done. Return the average of the values they entered. You may assume the user will only enter integers or "done". Include a docstring!
Given the following tests, deduce what the function make_string_sandwich does: assertEqual(make_string_sandwich("bbb", "a"), "bbbaabbb") assertEqual(make_string_sandwich("12","3t"), "123t3t12") assertEqual(make_string_sandwich("Z",""), "ZZ") Use the Design Recipe to write the make_string_sandwich function. Include a Docstring. Enter 3 other assertEqual conditions to test the make_string_sandwich function. PYTHON. Please show steps thanks
Problem 20 [ 2 points ] Use the Design Recipe to write a function, print_histogram that consumes a list of numbers and prints a histogram graph using asterisks to represent each number in the list. Use one output line per number in the list. You may assume that only integers are passed to this function. Your function should ignore negative values. Include a docstring! Test Result print_histogram([ 0, 2, 4, 1]) ** **** * print_histogram([10, 5, 3, -1, 8]) **********...
Python Language
The Blip Blob New Year celebration started on a Tuesday. Use the Design Recipe to write a function called blip_blop_day that consumes an int representing the number of days that have passed since their celebration start day and returns name of the current day. For example, if 2 days have passed, then 'Thursday' should be returned. Include a docstring! For example: Test Result 2 Thursday 4 Saturday Monday Tuesday 6 7 8 Wednesday
PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a function yesOrNo which has no parameters. When called, it gets input from the user until the user types either 'yes' or 'no', at which point the function should return True if the user typed 'yes' and False if the user typed 'no'. Any other entries by the user are ignored and another value must be input. For example: Test Input Result print(yesOrNo()) hello blank...
1. Write a Python function to count how many python function definitions in a given Python program. 2. Write a Python function to return the number of word wrapped lines of given number of characters in a given text file. Inside your function you must do it in a single line of code except docstring and comments.
Write a python program to compute the factorial of a given non-negative integer n. If the user inputs any integer outside the required, print “Error! Try again!”
USING PYTHON! The second function you will write should be called ‘varOrd’. Your function should take two (2) arguments, an integer and a string. The function should return one (1) integer calculated as follows. If the input integer is 1, the function should return the sum of the return value of the ord() function on each character of the string (e.g., varOrd(1, ‘cat’) should return the result of ord(‘c’) + ord(‘a’) + ord(‘t’)). If the input integer is 2, the...
Python Language !
Use the Design Recipe to define a function save_history which consumes two parameters, a nested list and an int representing the current landing attempt. The function should open a new file named LandingNN.csv' where NN is two digits representing the current landing attempt. The first line should contain the number of sublists in the nested list. Each sublists should be written to the file on its own line with its values separated by commas. This function should...