Practice Exercise - Session 1
Practice Exercise - Session 1
# Supress warnings
import warnings
warnings.filterwarnings("ignore")
[6]: df.head()
Spread in Runs Question 1: Analyse the spread of Runs scored by Virat in all his matches and
report the difference between the scores at the 50th percentile and the 25th percentile respectively.
a)16.5
b)22.5
c)26.5
1
d)32.5
[8]: df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 132 entries, 0 to 131
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Runs 132 non-null object
1 Mins 132 non-null object
2 BF 132 non-null int64
3 4s 132 non-null int64
4 6s 132 non-null int64
5 SR 132 non-null object
6 Pos 132 non-null int64
7 Dismissal 132 non-null object
8 Inns 132 non-null int64
9 Opposition 132 non-null object
10 Ground 132 non-null object
11 Start Date 132 non-null object
dtypes: int64(5), object(7)
memory usage: 12.5+ KB
[9]: df.describe()
[11]: df['Runs'].describe()
2
75% 80.250000
max 154.000000
Name: Runs, dtype: float64
[12]: 22.5
3
[16]: # Calculate the upper fence
Q1 = df['Runs'].quantile(0.25)
Q3 = df['Runs'].quantile(0.75)
IQR = Q3 - Q1
upper_fence = Q3 + 1.5 * IQR
print("Q1 = ",Q1)
print('Q3 =', Q3)
print("IQR = ", IQR)
print("Upper Fence =",upper_fence)
Q1 = 10.0
Q3 = 80.25
IQR = 70.25
Upper Fence = 185.625
[20]: 11 31
13 23
14 17
10 16
12 11
15 10
16 10
09 6
08 5
17 3
4
Name: Start Date, dtype: int64
[21]: # Lets us see "Virat has the highest run average in the year 2017"
pd.pivot_table(df, values = 'Runs', columns = ['Start Date'], aggfunc=np.mean)
# Statement II is incorrect
Start Date 15 16 17
Runs 30.4 73.9 61.666667
[22]: #Virat has the maximum score in a single match and the highest run average in␣
↪the year 2016.
0.0.5 Hence answer for Q3 is option (c) or option (a) which is partially correct
Maximum Frequency Q4:Plot a histogram for the Mins column with 15 bins. Among the three
ranges mentioned below, which one has the highest frequency?
A - [54.6,68)
B - [68,81.4)
C - [121.6,135)
a)A - [54.6,68)
b)B - [68,81.4)
c)C - [121.6,135)
d)None of the bin ranges have the same frequency
5
0.0.6 Option (d) is the correct answer
0.0.7 Coding Question :
1) Given a positive integer ‘n’ less than or equal to 26, you are required to print the below
pattern
Sample Input: 5
6
Sample Output : -e——–
[56]: n = int(input())
alpha="abcdefghijklmnopqrstuvwxyz"
s=""
l=[]
for i in range(n):
s="-".join(alpha[i:n])
l.append(s[::-1]+s[1:])
length=len(l[0])
for i in range(n-1,0,-1):
print(l[i].center(length,"-"))
for i in range(n):
print(l[i].center(length,"-"))
5
--------e--------
------e-d-e------
----e-d-c-d-e----
--e-d-c-b-c-d-e--
e-d-c-b-a-b-c-d-e
--e-d-c-b-c-d-e--
----e-d-c-d-e----
------e-d-e------
--------e--------
2) Given an integer, print whether it is Even or Odd.
7
Input: An integer
Output: ‘Even’ or ‘Odd’
Sample input: 3
Sample output: Odd
Sample input: 6
Sample output: Even
[63]: num=int(input())
if num%2==0:
print("Even")
else:
print("Odd")
4
Even
3) You’re trying to automate your alarm clock by writing a function for it. You’re given a day
of the week encoded as 1=Mon, 2=Tue, … 6=Sat, 7=Sun, and whether you are on vacation
as a boolean value (a boolean object is either True or False. Google “booleans python” to get
a better understanding). Based on the day and whether you’re on vacation, write a function
that returns a time in form of a string indicating when the alarm clock should ring.
When not on a vacation, on weekdays, the alarm should ring at “7:00” and on the weekends
(Saturday and Sunday) it should ring at “10:00”.
While on a vacation, it should ring at “10:00” on weekdays. On vacation, it should not ring on
weekends, that is, it should return “off”.
Input: The input will be a list of two elements. The first element will be an integer from 1 to
7, and the second element will be a boolean value.
Output: The output will be a string denoting the time alarm will ring or ‘off’
8
else:
if day_of_the_week in weekend:
return '10:00'
else:
return '07:00'
input_str = input("Enter a list of two elements [day_of_the_week,␣
↪is_on_vacation]: ")
input_list = eval(input_str)
if len(input_list) != 2 or not isinstance(input_list[0], int) or not␣
↪isinstance(input_list[1], bool):
else:
print(alarm_time(input_list[0], input_list[1]))
5
False
5) A pascal’s triangle is a very interesting mathematical concept. Each number here is a sum of
the two numbers directly above it. Following is an 8 level Pascal’s triangle:
You can read about Pascal’s triangle here. Your task is to print an nth level of Pascal’s triangle. The
input will contain an integer n. The output will contain 1 line of the list of numbers representing
the nth row of Pascal’s triangle.
Sample Input: 6 Sample Output:
9
[1, 5, 10, 10, 5, 1]
[81]: n=int(input())
def generate_pascals_triangle_row(n):
row = [1]
for k in range(1, n):
row.append(row[-1] * (n - k) // k)
return row
print(generate_pascals_triangle_row(n))
5
[1, 4, 6, 4, 1]
6) Given two strings, one of the strings will contain an extra character. Find the extra character.
The number of all the other characters in both the strings will be the same. Check the sample
input/output for more clarification.
The code will be case sensitive.
abcd
cedab
10
e
6) While extracting data from different sources, often numeric values come in string format and
with commas like 1,000 or 23,321 and also sometimes with spaces in start and beginning of
the string. For simplicity, we will consider only integer values imbedded with commas. You
will take the input and print the cleaned integer without commas and spaces.
Input: One line input of string, it will consist of only spaces commas and digits
Output: Cleaned number
3,213
3213
7) Write a program that computes the value of n+nn+nnn+nnnn+… nn…n ntimes with a given
number as the value of n.
For example, if n=3 , then you have to find the value of 3+33+333 if n=10, then you have to find
the value of 10 + 1010 + 101010 + 10101010 + 1010101010 + 101010101010 + 10101010101010 +
1010101010101010 +101010101010101010+ 10101010101010101010
Note: n will always be a positive number
[94]: n=int(input())
current_term = str(n)
total_sum = 0
for i in range(1, n + 1):
# Add the current term to the total sum
total_sum += int(current_term)
current_term += str(n)
print(total_sum)
3
369
[ ]:
11