0% found this document useful (0 votes)
129 views12 pages

Content

The document contains 8 coding questions related to data analysis and manipulation in Python. For each question, it provides sample code to perform a task such as finding the smallest or most common element in a list, checking if a list is a palindrome, mapping and filtering a nested list, sorting and summing employee work hours data, calculating country medal points from Olympic data, computing means and maximum sales from quarterly sales data, and finding highest employment, second largest values, and plotting employment rates.

Uploaded by

Prashanth Shetty
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)
129 views12 pages

Content

The document contains 8 coding questions related to data analysis and manipulation in Python. For each question, it provides sample code to perform a task such as finding the smallest or most common element in a list, checking if a list is a palindrome, mapping and filtering a nested list, sorting and summing employee work hours data, calculating country medal points from Olympic data, computing means and maximum sales from quarterly sales data, and finding highest employment, second largest values, and plotting employment rates.

Uploaded by

Prashanth Shetty
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/ 12

10/04/2021 Q1.

ipynb - Colaboratory

1. Write a python function that returns the index of the smallest element in a list of integers. If
the number of such elements is greater than 1, return the smallest index

def smallest_element(list_of_int):

#get the minimum element


min_element = min(list_of_int)
#get the index of the min element
ind_of_min = list_of_int.index(min_element)
return ind_of_min

def testSmallest_element():

print("Testing smallest_element()....")
assert(smallest_element([1,2,3,4]) == 0)
assert(smallest_element([0,-1,2,90,0]) == 1)
assert(smallest_element([88, 99, 0,0,0,1,23,0]) == 2)
print("Passed..")

testSmallest_element()

Testing smallest_element()....
Passed..

https://colab.research.google.com/drive/1KgI6iOV4Tv40aKXz4fyIVlIkWBynh3eV#scrollTo=aOqNtyinnnIy&printMode=true 1/2
10/04/2021 Q2.ipynb - Colaboratory

2. Write the python function mostCommonName, that takes a list of names (such as ["Jane",
"Aaron", "Cindy", "Aaron"], and returns the most common name in this list (in this case,
"Aaron"). If there is more than one such name, return a set of the most common names. So
mostCommonName(["Jane", "Aaron", "Jane", "Cindy", "Aaron"]) returns the set {"Aaron", "Jane"}.
If the set is empty, return None. Also, treat names case sensitive, so "Jane" and "JANE" are
different names.

def mostCommonName(names):

#output set
output = set()
#variable to hold the max occurences
max = 0
#Create a dictionary to keep the counts of occurences
count = {}
#count the occurences
for name in names:
if name in count.keys():
count[name] += 1
else:
count[name] = 1
if count[name] > max:
max = count[name]
#add to the set whichever has max occurences
for name in count:
if count[name] == max:
output.add(name)

if len(output) == 0:
return None
return output

def testMostCommonName():

print("Testing testMostCommonName()..")
assert(mostCommonName(["Aaron"]) == {"Aaron"})
assert(mostCommonName(["Jane", "Jane", "Cindy"]) == {"Jane"})
assert(mostCommonName([]) == None)
assert(mostCommonName(["Jane", "Aaron", "Jane", "Cindy", "Aaron"]) == {"Jane","Aaron"})
print("Passed..")

testMostCommonName()

Testing testMostCommonName()..
Passed..

https://colab.research.google.com/drive/1aGPyERmHch0VBBtcskSiUqUHEDRApjeY#scrollTo=UjWC3D_2oFAG&printMode=true 1/2
10/04/2021 Q3.ipynb - Colaboratory

3. Write the python function isPalindromicList(a) that takes a list and returns True if it is the
same forwards as backwards and False otherwise.

def isPalindromicList(a):

length = len(a)
mid = length//2 - 1
flag = True

for i in range(0,mid+1):
if a[i] != a[length - i - 1]:
flag = False
break

return flag

def testisPalindromicList():

print("Testing isPalindromicList()...")
assert(isPalindromicList([0,1,0]) == True)
assert(isPalindromicList([1,0,0,0]) == False)
assert(isPalindromicList(["mam","jam","mam"]) == True)
print("Passed..")

testisPalindromicList()

Testing isPalindromicList()...
Passed..

https://colab.research.google.com/drive/1aEGpvzQraRsZ8iybPiM2mCawaNLdaahb#scrollTo=oxYWKJszodBq&printMode=true 1/2
11/04/2021 Q4.ipynb - Colaboratory

4. Imagine an accounting routine used in a book shop. It works on a list with sublists, which look
like this:

Write a Python program using lambda and map functions, which returns a list with 2-tuples.
Each tuple consists of an order number and the product of the price per items and the
quantity. The price of the product should be increased by 10, if the total value of the order is
smaller than 1.

account = [ [34587,"Learning Python, Mark Lutz",4,40.95 ],


[98762,"Programming Python, Marl Lutz",5,56.8],
[77226, "Head First Python, Paul Barry", 3, 32.95] ,
[88112,"Einfuhrung in Python3,Bernd Klein", 3 , 24.99]]

print(list(map(lambda x: (x[0],x[-1]*x[-2]) if x[-2]*x[-1] >= 1


else (x[0],x[-1] * (x[-2] + 10)) ,account)))

[(34587, 163.8), (98762, 284.0), (77226, 98.85000000000001), (88112, 74.97)]

https://colab.research.google.com/drive/1_b74d6qsRGkMuVugYx6EX7J5H5wbTLb6#scrollTo=pFnaQ90jovGc&printMode=true 1/2
11/04/2021 Q5.ipynb - Colaboratory

5. Suppose the weekly hours for all employees are stored in a table. Each row records an
employee’s seven-day work hours with seven columns. For example, the following table stores
the work hours for eight employees. Write a python program that displays employees and their
total hours in decreasing order of the total hours.

emp_table = [
[2,4,3,4,5,8,8],
[7,3,4,3,3,4,4],
[3,3,4,3,3,2,2],
[9,3,4,7,3,4,1],
[3,5,4,3,6,3,8],
[3,4,4,6,3,4,4],
[3,7,4,8,3,8,4],
[6,3,5,9,2,7,9]]
emp_total_hrs = []
emp_id = 0
for item in emp_table:
s = sum(item)
emp_total_hrs.append([emp_id, s])
emp_id += 1
print(emp_total_hrs)

[[0, 34], [1, 28], [2, 20], [3, 31], [4, 32], [5, 28], [6, 37], [7, 41]]
https://colab.research.google.com/drive/1ZTMUOND8_4CQtDeac1XqMF2pDcwdYxC9#scrollTo=IC6b_puupDfz&printMode=true 1/2
10/04/2021 Q6.ipynb - Colaboratory

. Imagine a point system in which each country is awarded 4 points for each gold medal, 2
points for each silver medal, and one point for each bronze medal. Create a new dataframe
called 'olympic_points_df' that includes the following with the given data i. column called
'country_name' with the country name ii. column called 'points' with the total number of points
the country earned.

countries = [C1.', 'C2', 'C3', 'C4, 'C5', 'C6', 'C7'] gold = [13, 11, 10, 9, 8, 8, 6] silver = [11, 5, 10, 7, 7, 6, 3]
bronze = [9, 10, 5, 12, 9, 5, 2]

import numpy as np
import pandas as pd

countries = ['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7']


gold = [13, 11, 10, 9, 8, 8, 6]
silver = [11, 5, 10, 7, 7, 6, 3]
bronze = [9, 10, 5, 12, 9, 5, 2]

points = np.dot([4,2,1],[gold,silver,bronze])
olympic_points_df = pd.DataFrame({'country_name':countries,'Points':points})
olympic_points_df

country_name Points

0 C1 83

1 C2 64

2 C3 65

3 C4 62

4 C5 55

5 C6 49

6 C7 32

https://colab.research.google.com/drive/1KzuaF5DF-vjCrlRaFivhH8zTlKzwXBBH#scrollTo=vPNAbmrVpcBQ&printMode=true 1/2
11/04/2021 Q7.ipynb - Colaboratory

7. Create a new dataframe that includes the following with the given data and compute the
following. i. Find mean value for each category and quarter separately. ii. Find the quarter
during which the highest sales was made.

import pandas as pd

quarter = [1,2,3,4]
electronics = [2000,5800,20000,1400]
clothing = [4000,2500,16000,3700]
kitchen = [5000,5400,7000,1700]
furniture = [4400, 3000,3600,2000]

data = pd.DataFrame({'Quarter': quarter,'Electronics':electronics,


'Clothing':clothing,'Kitchen':kitchen,
'Furniture':furniture})
#Quarter-wise mean
print(data.iloc[:,1:].mean(axis=1))
print()
#Category-wise mean
print(data.mean(axis=0)[1:])
print()
data = data.set_index('Quarter')
print(data.sum(axis=1).idxmax())

0 3850.0
1 4175.0
2 11650.0
3 2200.0
dtype: float64

Electronics 7300.0
Clothing 6550.0
Kitchen 4775.0
Furniture 3250.0
dtype: float64

https://colab.research.google.com/drive/1iDDdrCZpAGHqi6t9PdemIjNrx2xeWTpF#scrollTo=WlPhvqyUp0iT&printMode=true 1/2
10/04/2021 Q8.ipynb - Colaboratory

. Create a new dataframe that includes the following with the given data and compute the
following. i. Find the name of the country with the highest employment in the given
employment data. ii. Find the second-largest value of each column of the input DataFrame. iii.
Create a histogram plot of the employment rate in the decreasing order and group by
countries.

import pandas as pd
import matplotlib.pyplot as plt

data = {
'Countries':['C1','C2','C3','C4','C5','C6','C7','C8'],
'Employment': [55.7,51.4,50.5,75.69,61.5,60.4,70.4,56.79],
'Male':[40,43,33,47,65,57,33,57],
'Female':[23,33,45,48,33,28,55,33]
}

df = pd.DataFrame(data)

#highest employment rate


print(df.iloc[df['Employment'].idxmax()].Countries)
print()

#second highest
def second_highest(col_name):
res = df[col_name].nlargest(2)
print(col_name, " ", res.iloc[1])

cols = ['Employment','Male','Female']
for col in cols:
second_highest(col)
print()
#histogram
graph = df.sort_values(by=['Employment'],ascending=False)
plt.bar('Countries','Employment',data=graph)

https://colab.research.google.com/drive/1Pwv1zK1KN0VcCUEy6QTNJWRVxsl9TYq2#scrollTo=cc22S6ctqLKx&printMode=true 1/2
10/04/2021 Q8.ipynb - Colaboratory

C4

Employment 70.4
Male 57
Female 48

<BarContainer object of 8 artists>

 0s completed at 08:12

https://colab.research.google.com/drive/1Pwv1zK1KN0VcCUEy6QTNJWRVxsl9TYq2#scrollTo=cc22S6ctqLKx&printMode=true 2/2
10/04/2021 Q9.ipynb - Colaboratory

9. Using Numpy and Matplotlib implement the following. i. Create a color plot of sin(x) sin(y) on
the interval [−π,π]×[−π,π]. ii. Plot the function f(x,y)=x2−y2 on the domain [−2,2]×[−2,2]

import numpy as np
import matplotlib.pyplot as plt

#i
x,y = np.meshgrid(np.linspace(-np.pi,np.pi,100), np.linspace(-np.pi, np.pi,100))
f = np.sin(x) * np.sin(y)

plt.figure()
plt.xlim(-np.pi, np.pi)
plt.ylim(-np.pi, np.pi)
plt.pcolormesh(x,y,f, cmap = 'RdBu')
plt.show()

#ii

x,y = np.meshgrid(np.linspace(-2,2,100),np.linspace(-2,2,100))
f = x**2 - y**2

plt.figure()
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.pcolormesh(x,y,f, cmap = 'RdBu')
plt.show()

https://colab.research.google.com/drive/1x6AYarPMD_nYetdpcVra3gbaQwDHN5HK#scrollTo=pL2G63oDJlHF&printMode=true 1/2
10/04/2021 Q9.ipynb - Colaboratory

 0s completed at 19:51

https://colab.research.google.com/drive/1x6AYarPMD_nYetdpcVra3gbaQwDHN5HK#scrollTo=pL2G63oDJlHF&printMode=true 2/2
10/04/2021 Q10.ipynb - Colaboratory

10. Using Numpy and Matplotlib implement the following. i. Create a random 3×5 array using the
np.random.rand(3, 5) function and compute the sum of all the entries, the sum of the rows
and the sum of the columns. ii. Create a random 5×5 array using the function
np.random.rand(5, 5) and sort the rows according to the second column.

import numpy as np
#i
arr = np.random.rand(3,5)
#sum of all entries
print(arr.sum())
print()

#sum of all rows


print(arr.sum(axis=1))
print()

#sum of all cols


print(arr.sum(axis=0))
print()

#ii
arr = np.random.rand(5,5)
sorted(arr,key=lambda x: x[:][1])

7.323431598383239

[2.5469491 2.15687879 2.61960372]

[1.02148616 1.56264834 1.60644303 2.29902002 0.83383404]

[array([0.79841633, 0.00133204, 0.07125248, 0.99905941, 0.99381835]),


array([0.17798405, 0.31340893, 0.97125743, 0.08660958, 0.78592336]),
array([0.11861389, 0.71176921, 0.00507073, 0.07789945, 0.42851813]),
array([0.111753 , 0.8236556 , 0.76839122, 0.74097859, 0.95633174]),
array([0.6920631 , 0.88273103, 0.87744826, 0.49164971, 0.39363325])]

https://colab.research.google.com/drive/1J7fRrtwfz-P3MzuUrPSH5jNyFreac4sU#scrollTo=rtV5Vn7hqcNP&printMode=true 1/2

You might also like