A2a - Accumulate Evens
A2a - Accumulate Evens
Task: use for loop. Accumulate first n even numbers (2 is the first even number). Accumulate means
“compute the sum of”. The function accepts n as input.
accumulate_evens.py
'''
CS101. A2a - Accumulate Evens
'''
def accumulate_evens(n):
pass
Test Cases
Function Inputs Output
accumulate_evens 0 0
accumulate_evens 1 2
accumulate_evens 10 110
accumulate_evens -1 ValueError
Task: use for loop. Return the product of first n positive integers. The function accepts n as input.
prod_n.py
'''
CS101. A2b - Product of n
'''
def product_n(n):
pass
Test Cases
Function Inputs Output
product_n 0 0
product_n 1 1
product_n 5 120
product_n -1 ValueError
A2c - Product of first n Odd numbers
Task: use for loop. Return the product of first n odd integers. The function accepts n as input.
prod_n_odds.py
'''
CS101. A2c - Product of n odd numbers
'''
def product_n_odds(n):
pass
Test Cases
Function Inputs Output
product_n_odds 0 0
product_n_odds 1 1
product_n_odds 5 945
product_n_odds -1 ValueError
Task: use for loop. Return the average of elements in a list. The function accepts a list as input.
list_average.py
'''
CS101. A2d - List average
'''
def list_average(list1):
pass
Test Cases
Function Inputs Output
list_average [] 0.0
Task: use for loop. Return the sum of the series. The function accepts n as input. n is defined as per the
following formula:
1 1 1 1 1
𝑒 = 0!
+ 1!
+ 2!
+ 3!
+···+ 𝑛!
e_value.py
'''
CS101. A2e - e value
'''
def e_value(n):
pass
Test Cases
Function Inputs Output
e_value 1 2
e_value 2 2.5
e_value 10 2.7182818011463845
Task: use for loop. Return the sum of the first n multiples of k. The function accepts n and k as input.
Multiples of k are k, 2k, 3k, …, nk. Eg. n=2, k=5 returns 15, n=5, k=5 returns 75, n=5, k=1 returns 15.
Return ValueError if either n or k is negative.
sum_multiples.py
'''
CS101. A2f - sum of multiples
'''
Test Cases
Function Inputs Output
sum_multiples 2, 5 15
sum_multiples 5, 5 75
sum_multiples 5, 1 15
Task: use for loop. Return the product and average of the first n multiples of k. The function accepts n and k
as inputs. Return value is the list [product, average]. Eg. n=2, k=5 returns [50, 7.5], n=5, k=5 returns
[375000, 15.0], n=5, k=1 returns [120, 3.5]. Return ValueError if either n or k is negative.
product_average_multiples.py
'''
CS101. A2g - product and average of multiples
'''
Test Cases
Function Inputs Output
Task: Given a list of strings, return the first letters of each string as a string. Raise ValueError if an empty
string is encountered. Eg. input [‘aircraft’, ‘bull’, ‘cream’] should return ‘abc’.
first_letters.py
'''
CS101. A2h - first letters
'''
def first_letters(list1):
pass
Test Cases
Function Inputs Output
A2i - Relations
Task: Given an integer n and a list of integers, check if each item in the list is a multiple of n. Return a list of
True and False boolean values corresponding to each item. 0 is always a multiple. Eg. inputs: 7, [10, 21,
-14, 32, 74, 0], return should be: [False, True, True, False, False, True].
relations.py
'''
CS101. A2i - relations
'''
relations 7, [10, 21, -14, 32, 74, 0] [False, True, True, False, False, True]
Task: Given a list, find the index of the first even number. 0 is even.
first_even_index.py
'''
CS101. A2j - first even index
'''
def first_even_index(list1):
pass
Test Cases
Function Inputs Output
Task: Given a list, return the value and index of the first negative number in a list. 0 is even. Raise
ValueError if there are none.
first_negative_value_index.py
'''
CS101. A2k - first negative value index
'''
def first_negative_value_index(list1):
pass
Test Cases
Function Inputs Output
Task: Given a string, return the index of the first space (‘ ‘) character. Raise ValueError if there are none.
first_space.py
'''
CS101. A2l - first space in a string
'''
def first_space(str1):
pass
Test Cases
Function Inputs Output
Task: Use while loop. Given a list, return the number of even numbers in the list. 0 is even. Raise
ValueError if there are none.
count_evens.py
'''
CS101. A2m - count evens
'''
def count_evens(list1):
pass
Test Cases
Function Inputs Output
Task: Use while loop. Given a list, return the number of negative integers, zeroes, and positive integers in a
list. Raise ValueError if there are none.
count_all.py
'''
CS101. A2n - count negatives, zeros, positives
'''
def count_all(list1):
pass
Test Cases
Function Inputs Output
count_all [] ValueError
Task: Use while loop. Given a list, return the number of floats and others. Raise ValueError if there are
none.
count_floats_others.py
'''
CS101. A2o - count floats and others
'''
def count_floats_others(list1):
pass
Test Cases
Function Inputs Output
count_floats_others [] ValueError
Task: Use while loop. Given a list of integers, return the sums of positive and negative numbers. Raise
ValueError if there is a non-integer datatype in the list.
sums.py
'''
CS101. A2p - sums of positive and negative numbers
'''
def sums(list1):
pass
Test Cases
Function Inputs Output
sums [] [0, 0]
Task: Use while loop. Given a list of integers, return the minimum of positive numbers. Raise ValueError if
there are no positive integers or if there is a non-integer datatype in the list.
min_positive.py
'''
CS101. A2q - minimum of positive numbers
'''
def min_positive(list1):
pass
Test Cases
Function Inputs Output
min_positive [] ValueError
Task: Use while loop. Given a list of integers, return the maximum of negative numbers. Raise ValueError if
there are no negative numbers in the list or if there is a non-integer datatype in the list.
max_negative.py
'''
CS101. A2r - maximum of negative numbers
'''
def max_negative(list1):
pass
Test Cases
Function Inputs Output
max_negative [] ValueError
Task: Use while loop. Given a list of integers, return the minimum of positive numbers and max of negative
numbers. 0 is positive. Raise ValueError if there are no positive integers or if there are no negative integers
or if there is a non-integer datatype in the list.
min_max.py
'''
CS101. A2s - minimum of positives and maximum of negative numbers
'''
def min_max(list1):
pass
Test Cases
Function Inputs Output
min_max [] ValueError
Task: Use while loop. Given an integer, return the number of digits in it. Raise ValueError if input is a
non-integer datatype.
count_digits.py
'''
CS101. A2t - count digits
'''
def count_digits(n):
pass
Test Cases
Function Inputs Output
count_digits -21 2
count_digits 0 1
count_digits int(9999e10) 14
Task: Use while loop. Given an integer, return the reverse of the number. Preserve the sign. Raise
ValueError if input is a non-integer datatype.
reverse_digits.py
'''
CS101. A2u - reverse digits
'''
def reverse_digits(n):
pass
Test Cases
Function Inputs Output
reverse_digits 0 0
*****