Python Exercise Sheet
I. Conditional Statements:
1. Given a positive integer x. Your task is to check, if it is even or odd
2. Given two integer variables a and b, and a boolean variable flag. The
task is to check the status and return accordingly.
Return True for the following cases:
a. Either a or b (not both) is non-negative and the flag is false.
b. Both a and b are negative and the flag is true.
c. Otherwise, return False.
3. You are given a string str, you need to return True if the words "cat"
and "hat" appear same number of times in str, otherwise return False.
Note: str contains only lowercase English alphabets.
4. Given a number x, the task is to print the numbers from x to 0 in
decreasing order in a single line.
5. You are given a number n. The number n can be negative or positive.
If n is negative, print numbers from n to 0 by adding 1 to n in the neg
function. If positive, print numbers from n-1 to 0 by subtracting 1
from n in the pos function.
II. Lists:
1. Given an array arr. Your task is to find the minimum and maximum
elements in the array.
Note: Return a Pair that contains two elements the first one will be a
minimum element and the second will be a maximum.
2. Given two arrays a[] and b[], the task is to find the number of
elements in the union between these two arrays.
The Union of the two arrays can be defined as the set containing
distinct elements from both arrays. If there are repetitions, then only
one element occurrence should be there in the union.
3. Given two sorted arrays arr1[] and arr2[]. Your task is to return the
intersection of both arrays.
Intersection of two arrays is said to be elements that are common in
both arrays. The intersection should not count duplicate elements.
Note: If there is no intersection then return an empty array.
III. Strings:
1. Python has a lot of string methods that can be used to manipulate the
strings like converting to lowercase, capitalizing, trimming the spaces
and so on. In this question, we'll take a look at inbuilt string methods
like title(), swapcase(), find(), strip(). You'll be given a string str and
x, you'll perform various operations on them.
2. Given a string S, the task is to determine whether the string starts and
ends with the characters 'gfg' (case insensitive). In order to complete
this task, you need to utilize the string functions [Link](), [Link](),
[Link]('string2'), and [Link]('string2'). By using these
functions, you can check if the given string S meets the specified
conditions of starting and ending with 'gfg'.
3. Given a string s. The task is to convert string characters to lowercase.
4. Given a string s, reverse the string without reversing its individual
words. Words are separated by spaces.
Note: The string may contain leading or trailing spaces, or multiple
spaces between two words. The returned string should only have a
single space separating the words, and no extra spaces should be
included.
5. Given a string of braces named bound_by, and a string named
tag_name. The task is to print a new string such that tag_name is in
the middle of bound_by.
IV. Functions:
1. Given a number N and power P, the task is to find the power of a
number ( i.e. NP ) using recursion.
2. Write a Python function to sum all the numbers in a list.
3. Write a Python function to calculate the factorial of a number (a non-
negative integer). The function accepts the number as an argument.
4. Write a Python function to check whether a number falls within a
given range.