0% found this document useful (0 votes)
3K views9 pages

Quiz Coding Question 1

The document provides information about validating customer IDs for a pharmaceutical factory. It describes the requirements for a valid customer ID, including having groups of three characters separated by underscores, containing only numbers and letters, being 12 characters long excluding underscores, and not repeating any character more than three times. It provides a sample valid customer ID and the expected output. It then provides a Python function that takes a customer ID as input and returns "valid" or "invalid" based on whether it meets the requirements.

Uploaded by

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

Quiz Coding Question 1

The document provides information about validating customer IDs for a pharmaceutical factory. It describes the requirements for a valid customer ID, including having groups of three characters separated by underscores, containing only numbers and letters, being 12 characters long excluding underscores, and not repeating any character more than three times. It provides a sample valid customer ID and the expected output. It then provides a Python function that takes a customer ID as input and returns "valid" or "invalid" based on whether it meets the requirements.

Uploaded by

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

Question 1

Valid customer IDs


Simon runs a pharmaceutical factory. Only customers who have been pre-approved can enter the
manufacturing facility and do a quality check for their order.
The pre-approved customers get a customer ID which needs to be validated at entrance.
A valid customer ID:
-It will have groups of three characters separated by "_".
-Can only contain numbers 0-9 and characters a-z and A-Z.(other than "_")
-Contains 12 characters excluding the "_"
-A character cannot be repeated more than thrice.

You have to write Python code to check for valid customer IDs.
The input will have a customer ID and the output should say "valid" if the entered ID is valid otherwise it should
say "invalid".

Sample Input:
abc_123_2ac_Adf

Sample Output:
valid

def validitycheck(cid):

#req1: a-z and 0-9 only

if not (cid.replace("_","").isalnum()):

return ("invalid")

#req2 & 3 :4 groups, each group has three characters with a-z, A-Z and 0-9

groups=cid.split("_")

if len(groups)>4:

return ("invalid")

for group in groups:

if len(group)>3:

return ("invalid")

#Req 5: repeating characters check

from collections import Counter


counts=dict(Counter(cid))

for key in counts.keys():

if counts.get(key)>3:

return("invalid")

return("valid")

cid=input()

print(validitycheck(cid))

Question 2
Password Validation
Your company requires employees to set a strong password for their email accounts.
The characteristics of a strong password include:
1. it should be at least 8 characters long
2. it should have one lowercase alphabet.
3.it should have one uppercase alphabet
4. it should have at least one number(0-9)
5. it should have one special character( a special character is considered among the following: [@%$*])
If the input is a valid password then print "valid" or else print "invalid".

Sample input:
DataScience123
Sample Output:
Invalid

pwd=input()

#write your code here

import re

#password = "R@m@_f0rtu9e$"

flag = 0

while True:

if (len(pwd)<8):
flag = -1

break

elif not re.search("[a-z]", pwd):

flag = -1

break

elif not re.search("[A-Z]", pwd):

flag = -1

break

elif not re.search("[0-9]", pwd):

flag = -1

break

elif not re.search("[_@$]", pwd):

flag = -1

break

elif re.search("\s", pwd):

flag = -1

break

else:

flag = 0

print("Valid")

break

if flag ==-1:

print("Invalid")
Question 7

List Overlap
Write Python code to find elements common between the two lists.
The output list should exclude duplicate elements. i.e. if both lists have 1 twice then the output list should have
1 only once.
The input will contain two lines with two lists.
The output should contain a list of common elements between the two input lists.
Sample Input:
[1,2,3,4,5]
[4,5,6,7,8]
Sample Output:
[4, 5]

import ast,sys

input_str = sys.stdin.read()

inp = ast.literal_eval(input_str)

list1=inp[0]#first list

list2=inp[1]#second list

#write your code here

def intersection(list1, list2):

list3 = [value for value in list1 if value in list2]

return list3

print(intersection(list1, list2))

Question 5

2-Sample t-test
Perform 2-sample t-tests on given columns of the dataframe.
The input will contain the names of two columns to test in two lines and the output should have the p-value
obtained from the paired two sample tests.
Sample Input:
city-mpg
highway-mpg
Sample Output:
1.9665445899143185e-113

import pandas as pd

import scipy.stats

df=pd.read_csv("https://media-
doselect.s3.amazonaws.com/generic/K9WgyRZ75q4Pkdp38AQabgE0X/Automobile_data.csv")

col1=input()

col2=input()

print(scipy.stats.ttest_rel(df[col1],df[col2]).pvalue)

Question 6
t-test on a column
Perform a t-test on a given column of a given data frame.
The input will contain the column name and the value of x, the mean value to test in two lines respectively. The
output should contain the p-value obtained.
Sample Input:
CGPA
8
Sample Output:
1.6070878523226457e-62

import pandas as pd

import scipy.stats

df=pd.read_csv("https://media-
doselect.s3.amazonaws.com/generic/5J7nrZegVWOORbGr4M7KVPXE5/Admission_Predict.csv")

col=input()

x=float(input())

print(scipy.stats.ttest_1samp(df[col], x).pvalue)
Question 9
Survival of?
Given is a dataframe with data of passengers of the ship Titanic. The dataframe looks as follows:

PassengerId Survived Pclass \


0 1 0 3
1 2 1 1
2 3 1 3
3 4 1 1
4 5 0 3

Name Sex Age SibSp \


0 Braund, Mr. Owen Harris male 22.0 1
1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1
2 Heikkinen, Miss. Laina female 26.0 0
3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1
4 Allen, Mr. William Henry male 35.0 0

Parch Ticket Fare Cabin Embarked


0 0 A/5 21171 7.2500 NaN S
1 0 PC 17599 71.2833 C85 C
2 0 STON/O2. 3101282 7.9250 NaN S
3 0 113803 53.1000 C123 S
4 0 373450 8.0500 NaN S

Here, the "Survived" column has "1" if the passenger has survived. Otherwise, it contains "0". The Pclass
column indicates the class the passenger was travelling in(1st class, 2nd class and so on).

Write a Pandas program to create a Pivot table and find survival rate by the given column on various classes
according to Pclass.
Sample Input:
Sex
Sample Output:
Pclass 1 2 3
Sex
female 0.968085 0.921053 0.500000
male 0.368852 0.157407 0.135447

import pandas as pd

df=pd.read_csv("https://media-
doselect.s3.amazonaws.com/generic/pLMXoA0GZNAPMRNrdnn88pOxb/train.csv")
col=input()

print(df.pivot_table('Survived', index=[col], columns='Pclass'))

Question 8
Sorting based on one column
Given a dataframe, you have to sort the rows based on values of one column.
Note: Sorting should be in descending order of values of given column
The input will contain a column name. The output should contain the first five rows of the dataframe.
The output will contain the first n rows of the sorted dataframe.
Sample Input:
TOEFL Score
Sample Output:

Serial No. GRE Score TOEFL Score University Rating SOP LOR CGPA \
25 26 340 120 5 4.5 4.5 9.60
97 98 331 120 3 4.0 4.0 8.96
81 82 340 120 4 5.0 5.0 9.50
202 203 340 120 5 4.5 4.5 9.91
203 204 334 120 5 4.0 5.0 9.87

Research Chance of Admit


25 1 0.94
97 1 0.86
81 1 0.96
202 1 0.97
203 1 0.97

import pandas as pd

col=input()

df=pd.read_csv("https://media-
doselect.s3.amazonaws.com/generic/RM8r5NBrJdA4QeVZXvwbjokwv/Admission_Predict.csv")

print(df.sort_values(by=[col],ascending=False).head())

Question 4
Flatten a dictionary
Consider a nested dictionary as follows:
{'Fruit': 1, 'Vegetable': {'Cabbage': 2, 'Cauliflower': 3}, 'Spices': 4}
Your task is to flatten a nested dictionary and join the nested keys with the "_" character. For the above
dictionary, the flattened dictionary would be as follows:
{'Fruit': 1, 'Vegetable_Cabbage': 2, 'Vegetable_Cauliflower': 3, 'Spices': 4}

The input will have a nested dictionary.


The output should have two lists. The first list will have keys and the second list should have values. Both lists
should be sorted.
Sample Input:
{'Fruit': 1, 'Vegetable': {'Cabbage': 2, 'Cauliflower': 3}, 'Spices': 4}
Sample Output:
['Fruit', 'Spices', 'Vegetable_Cabbage', 'Vegetable_Cauliflower']
[1, 2, 3, 4]

import ast,sys

input_str = sys.stdin.read()

input_dict = dict(ast.literal_eval(input_str))

def flatten_dict(dd, separator='_', prefix=''):

#complete this function

return { prefix + separator + k if prefix else k : v

for kk, vv in dd.items()

for k, v in flatten_dict(vv, separator, kk).items()

} if isinstance(dd, dict) else { prefix : dd }

out1=list(flatten_dict(input_dict).keys())

out2=list(flatten_dict(input_dict).values())

out1.sort()

out2.sort()

print(out1)

print(out2)

Question 3
Divide the dataframe
Write a Python program to slice a dataframe in a given ratio.
For example: if the dataframe has 160 rows and you have to slice it in a ratio of 1:3, then the first part will have
the first 40 rows(0-39) and the second part will have the next 120 rows(40-159).
The input will have two lines with the ratio to separate the dataframe in. For example, for 1:3, the input will be
as follows:
1
3
The output should contain the summary statistics(df.describe()) of both resulting dataframes in the respective
order.
Note: You can assume that the given ratio will split the dataframe exactly into two non-fractional parts.
Sample Input:
10
30
Sample Output:

import pandas as pd

df=pd.read_csv("https://media-
doselect.s3.amazonaws.com/generic/A0zOxQvk78ONwRgLZ1WYJOxWq/titaniMod2.csv")

a=int(input())

b=int(input())

c=int(a/(a+b)*len(df))

print(df[:c].describe())

print(df[c:].describe())

You might also like