0% found this document useful (0 votes)
9 views10 pages

A2a - Accumulate Evens

The document outlines various programming tasks related to loops in Python, including functions to accumulate even numbers, calculate products, compute averages, and analyze lists. Each task specifies the function's purpose, expected inputs, and outputs, along with test cases for validation. The tasks utilize both for and while loops, with specific error handling for invalid inputs.

Uploaded by

hrbharath07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

A2a - Accumulate Evens

The document outlines various programming tasks related to loops in Python, including functions to accumulate even numbers, calculate products, compute averages, and analyze lists. Each task specifies the function's purpose, expected inputs, and outputs, along with test cases for validation. The tasks utilize both for and while loops, with specific error handling for invalid inputs.

Uploaded by

hrbharath07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

A2 - Loops

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

A2b - Product of first n numbers

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

A2d - Average of a list

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

list_average [1, 2] 1.5

list_average [1, 2, 3] 2.0

A2e - Sum of series

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

e_value 100 2.718281828459045

A2f - Sum of multiples

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
'''

def sum_multiples(n, k):


pass

Test Cases
Function Inputs Output

sum_multiples 2, 5 15

sum_multiples 5, 5 75

sum_multiples 5, 1 15

A2g - Product and Average of multiples

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
'''

def product_average_multiples(n, k):


pass

Test Cases
Function Inputs Output

product_average_mulitples 2, 5 [50, 7.5]

product_average_mulitples 5, 5 [375000, 15.0]

product_average_mulitples 5, 1 [120, 3.5]

A2h - Return the first letters of a string in a list

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

first_letters [‘aircraft’, ‘bull’, ‘cream’] ‘abc’

first_letters [‘NITK’, ‘Surathkal’, ‘575025’] ‘NS5’

first_letters [‘NITK’, ‘Surathkal’, ‘575025’, ‘‘ ] ValueError

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
'''

def relations(n, list1):


pass
Test Cases
Function Inputs Output

relations 7, [10, 21, -14, 32, 74, 0] [False, True, True, False, False, True]

relations 0, [21, -14, 0] [False, False, True]

Use while loops for the following questions.

A2j - First even index

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

first_even_index [10, 21, -14, 32, 74, 0] 0

first_even_index [21, -14, 32, 74, 0] 1

A2k - First negative value and index

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

first_negative_value_index [10, 21, -14, 32, 74, 0] [-14, 2]

first_negative_value_index [21, 14, 32, 74, 0] ValueError


A2l - First space in a string

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

first_space “First space in this string” 5

first_space “NoSpaceHere” ValueError

A2m - Count evens

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

count_evens [10, 21, -14, 32, 74, 0] 4

count_evens [21, -15, 33, 1] ValueError

A2n - Count negatives, zeroes and positives

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 [10, 21, -14, 32, 74, 0] [1, 1, 4]

count_all [-21, -15, -33, -1] [4, 0, 0]

count_all [] ValueError

A2o - Count floats and others

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 [10.9, 21.0, -14.2, “name”, [1.2, 3.4], -2] [3, 3]

count_floats_others [-21, -15, -33, -1] [0, 4]

count_floats_others [] ValueError

A2p - Sums of positives and negatives

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 [10.9, 21.0, -14.2, “name”, [1.2, 3.4], -2] ValueError

sums [-21, -15, -33, -1] [0, -70]

sums [] [0, 0]

sums [21, -15, -33, 1] [22, -48]

A2q - Min of positives

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 [10.9, 21.0, -14.2, “name”, [1.2, 3.4], -2] ValueError

min_positive [-21, -15, -33, -1] ValueError

min_positive [] ValueError

min_positive [21, -15, -33, 1] 1

A2r - Max of negatives

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 [10.9, 21.0, -14.2, “name”, [1.2, 3.4], -2] ValueError

max_negative [-21, -15, -33, -1] ValueError

max_negative [] ValueError

max_negative [21, -15, -33, 1] -15

A2s - Min of positives and Max of negatives

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 [10.9, 21.0, -14.2, “name”, [1.2, 3.4], -2] ValueError

min_max [-21, -15, -33, -1] ValueError

min_max [] ValueError

min_max [21, -15, -33, 1] [1, -15]

A2t - Count the digits

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 10.9 ValueError

count_digits -21 2

count_digits 0 1

count_digits int(9999e10) 14

A2u - Reverse the digits

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 10.9 ValueError

reverse_digits -21 -12

reverse_digits 0 0

reverse_digits int(9999e10) 9999

*****

You might also like