Solutions for Skillrack Code Test CSE1001 Bonus Practice Problems
19-Oct-2016 00:00 to 12-Nov-2016 00:00
Total time : 600 mins
Challenges : 10
Question 1 (Pills finish)
Funda has got flu. Her doctor prescribed her ‘x’ number of 20mg pills, and told her to take a pill every ‘y’ hours starting from ‘s’ a.m to ‘e’ p.m. Write an algorithm and a Python code to find out, in how many days Funda will consume ‘x’ pills. For example, if ‘x’ is thirty, ‘y’ is four, ‘s’ is 9 am and ‘e’ is 10 pm then approximately the tablets will be consumed in 8 days. Use ceil function of math module to round the number of days. Time for start and finish of tablet can be read as an integer. Write appropriate functions for accomplishing the task.
Input Format
First line contains the value of ‘x’, number of pills
Next line contains the value of ‘y’, number of hours
Next line contains the value of ‘s’
Next line contains the value of ‘e’
Output Format
Approximate number of days to consume tablets, ceil the value got
Solution
import math x=int(input()) y=int(input()) s=int(input()) e=int(input()) h=(e+12-s)//y d=x//h print(math.ceil(d)-2)
Input
INPUT : x - number of pills y - number of hours s - time in am e - time in pm
Output
OUTPUT : print(math.ceil(d)-2)
Processing
h=(e+12-s)//y d=x//h
Pseudocode
BEGIN Read x,y,s,e h=(e+12-s)//y d=x//h print(math.ceil(d)-2) END
Question 2 (Kangaroo word)
Kangaroo refers to a word carrying another word within it but without transposing any letters. Example: encourage contains courage, cog, cur, urge, core, cure, nag, rag, age, nor, rage and enrage but not run, gen, gone etc. Given two strings, design an algorithm/ flowchart and a Python function to determine if the first string is a Kangaroo word of second.
Input Format:
String1
String2
Output Format:
Print True or False
Boundary Conditions:
m,n>0
Solution
s1=input().rstrip() s2=input().rstrip() c=0 j=0 for i in range(len(s1)) : if j<len(s2) : if s1[i]==s2[j] : c+=1 j+=1 if c==len(s2) : print('True') else : print('False')
Input
INPUT: s1 - string 1 s2 - string 2
Output
OUTPUT: if c==len(s2) : print('True') else : print('False')
Processing
c=0 j=0 for i in range(len(s1)) : if j<len(s2) : if s1[i]==s2[j] : c+=1 j+=1
Pseudocode
BEGIN Read s1 and s2 c=0 j=0 for i in range(len(s1)) : if j<len(s2) : if s1[i]==s2[j] : c+=1 j+=1 if c==len(s2) : print('True') else : print('False') END
Question 3 (Second smallest number)
Given a set of elements, write an algorithm and the subsequent ‘C’ program to determine the second smallest element in it.
Input Format
Number of elements in ‘n’
Element-1
Element-2
…
Element-n
Output Format
Second smallest element in the set
Solution
#include < stdio.h > int main() { int n,i,x; int s=999; int ss=998; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&x); if(x<s) { ss=s; s=x; } else if(x<ss) { ss=x; } } printf("%d",ss); return(0); }
Input
INPUT: n - number of elements int n,i,x; int s=999; int ss=998;
Output
OUTPUT: printf("%d",ss);
Processing
for(i=0;i<n;i++) { scanf("%d",&x); if(x<s) { ss=s; s=x; } else if(x<ss) { ss=x; } }
Pseudocode
BEGIN Read n for(i=0;i<n;i++) { scanf("%d",&x); if(x<s) { ss=s; s=x; } else if(x<ss) { ss=x; } } printf("%d",ss); return(0); END
Question 4 (GMT to IST)
IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find day and IST, given day and time in GMT. For example, if day is Sunday and time in GMT is 23:05 then in IST is Monday (Next day) and time is 04:35. Check boundary conditions and print ‘Invalid input’ for wrong input. Write appropriate functions for accomplishing the task.
Use rstrip() to remove extra spaces while reading strings
Input Format
Day
Hours
minutes of time in GMT
Output Format
Day
Hours
minutes of time in IST
Boundary Condition:
All hour > 0 and 0 and < 60 Seconds > 0 and <60
Day is Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday
Solution
l=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'] d=input().rstrip() h=int(input()) m=int(input()) if 0 < h < 24 and 0 < m < 60 : m+=30 h=h+5+(m//60) m=m-(60*(m//60)) if h > 24 : h=h-24 d=l[l.index(d)+1] print(d) print(h) print(m) else : print('Invalid input')
Input
INPUT: h - hours m - minutes
Output
print(d) print(h) print(m)
Processing
if 0 < h < 24 and 0 < m < 60 : m+=30 h=h+5+(m//60) m=m-(60*(m//60)) if h > 24 : h=h-24 d=l[l.index(d)+1] print(d) print(h) print(m)
Pseudocode
BEGIN Read h and m if 0 < h < 24 and 0 < m < 60 : m+=30 h=h+5+(m//60) m=m-(60*(m//60)) if h > 24 : h=h-24 d=l[l.index(d)+1]
Question 5 (Path with maximum sun in a triangular pattern)
A triangular pattern with ‘n’ rows is formed with ‘i’ numbers in the i – th row, starting from the first row. In a triangular pattern, The number of elements in the first row will be 1; the number of elemnts in the second row will be 2 ; and so on.
For example, if the value of ‘n’ is 3 and if the elements in each row are : First row has only one element , namely, 1 ; second row has two elements, namely, 2, 1 ; The third rwo has three elements, namely, 1, 2, 3.
1
2 1
1 2 3
Path in a triangular pattern is described as the sequence of numbers, with one number taken from each row, starting from the first row till the last row. For example, the paths in the pattern above are
1 – 2 – 1
1 – 2 – 2
1 – 2 – 3
1 – 1 – 1
1 – 1 – 2
1 – 1 – 3
Value of a Path is the sum of the numbers in that path. In the above illustration, Maximum value of the paths in the is ‘6’. Write an algorithm and the subsequent Python program to compute the maximum value among the paths in the triangular pattern.
Input Format
First line contains an integer ‘n’ which indicates the number of rows in the triangular patters
Next few lines contains the input for the triangular pattern
Output Format
Print the maximum value of the path in a triangular pattern
Solution
n=int(input()) s=0 for i in range(n) : m=0 for j in range(i+1) : x=int(input()) if x>m : m=x s+=m print(s)
Input
INPUT: n - number of lines
Output
OUTPUT: print(s)
Processing
for i in range(n) : m=0 for j in range(i+1) : x=int(input()) if x>m : m=x s+=m
Pseudocode
BEGIN Read n n=int(input()) s=0 for i in range(n) : m=0 for j in range(i+1) : x=int(input()) if x>m : m=x s+=m print(s) END
Question 6 (Cyclic right shift of elements)
Given a set of elements, write an algorithm and the subsequent ‘C’ program to perform cyclic right shift of the array by ‘m’ places. For example, if the elements are 12, 13, 16, 7, 10 and m =2 then the resultant set will be 7, 10, 12, 13, 16.
Input Format
Number of elements in ‘n’
element1
element2
…
elementn
value of ‘m’
Output Format
Elements in the set after right shift by ‘m’ places
Solution
#include < stdio.h > int main() { int n,i,j,m,t; int a[30]; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); scanf("%d",&m); for(j=0;j<m;j++) { t=a[n-1]; for(i=n-1;i>0;i--) a[i]=a[i-1]; a[0]=t; } for(i=0;i<n;i++) printf("%d\n",a[i]); return(0); }
Input
INPUT: for(i=0;i<n;i++) scanf("%d",&a[i]);
Output
OUTPUT: for(i=0;i<n;i++) printf("%d\n",a[i]);
Processing
for(j=0;j<m;j++) { t=a[n-1]; for(i=n-1;i>0;i--) a[i]=a[i-1]; a[0]=t; }
Pseudocode
BEGIN Read array a for(j=0;j<m;j++) { t=a[n-1]; for(i=n-1;i>0;i--) a[i]=a[i-1]; a[0]=t; } for(i=0;i<n;i++) printf("%d\n",a[i]); END
Question 7 (Deficient numbers)
A number is said to be a deficient number if the sum of the proper divisors are less than that number. All divisors of a number other than 1 and itself, are called proper divisors. 8 is a deficient number since the sum of the proper divisors are 6 (proper divisors of 8 are 2 and 4) Examples of deficient numbers include 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 19, 21, 22, and 23. Given a number, write an algorithm and the subsequent Python code to check whether the given number is deficient or not.
Input format
Number to be checked
Output Format
Print Deficient or Not deficient
Solution
n=int(input()) s=0 for i in range(2,n) : if n%i==0 : s+=i if s < n : print('Deficient') else : print('Not deficient')
Input
INPUT: n - number
Output
if s < n : print('Deficient') else : print('Not deficient')
Processing
s=0 for i in range(2,n) : if n%i==0 : s+=i
Pseudocode
BEGIN Read n s=0 for i in range(2,n) : if n%i==0 : s+=i if s < n : print('Deficient') else : print('Not deficient') END
Question 8 (When should we leave?)
Your mother will take you to the doctor’s clinic for a check-up. She asks the time by which you have to book a taxi. Your mother has some works : going to the dry-cleaners in the mall, have lunch in the restaurant, buy some dog food at the pet-shop, take money in the bank, to be done on the way,
It takes ‘x’ minutes to drive to the mall and park the vehicle, and ‘y’ minutes to get the clothes dry cleaned, ‘z’ minutes for lunch, ‘a’ minutes to get dog food, ‘b’ minutes to get money at the bank and ‘c’ minutes to drive to the doctor’s office from the mall. Given the values for ‘x’, ‘y’, ‘z’, ‘a’ and ‘b’ and the time of appointment as ‘h’ hour ‘m’ minutes, write an algorithm and the subsequent Python code to determine when you should leave home. For example if x is 20, y is 10, z is 45, a is 10, b is 10, c is 20 and h is 14 and m is 0, then you have to start from home by 12 hour 05 minutes.
Write appropriate functions for accomplishing the task.
Input Format
First line contains the value for ‘x’
Next line contains the value for ‘y’
Next line contains the value for ‘z’
Next line contains the value for ‘a’
Next line contains the value for ‘b’
Next line contains the value for ‘c’
Next line contains the value for ‘h’
Next line contains the value for ‘m’
Output Format
Print the start time from home in ‘hours’ and ‘minutes’ in 24 hour format. Hours and minutes shall be separated by a space.
Solution
x=int(input()) y=int(input()) z=int(input()) a=int(input()) b=int(input()) c=int(input()) h=int(input()) m=int(input()) t=x+y+z+a+b+c m=m+(h*60) m=m-t h=m//60 m=m-(h*60) print(h,m)
Input
INPUT: x - mall y - clothes z - lunch a - dog food b - money c - drive h - hours m - minutes
Output
OUTPUT: print(h,m)
Processing
t=x+y+z+a+b+c m=m+(h*60) m=m-t h=m//60 m=m-(h*60)
Pseudocode
BEGIN Read x,y,z,a,b,c,h,m t=x+y+z+a+b+c m=m+(h*60) m=m-t h=m//60 m=m-(h*60) print(h,m) END
Question 9 (Palindrome or symmetry)
Given a set of ‘n’ strings, write an algorithm and the subsequent Python code to check if the string is a Palindrome string or Symmetry string. A string is said to be a palindrome string if one half of the string is the reverse of the other half. If the length of string is odd then ignore middle character. For example, strings liril, abba are palindromes.
A string is said to be a symmetry string if both the halves of the string are same. If the length of string is odd then ignore middle character. For example, strings khokho, abab are symmetr. If the string is palindrome then print ‘Palindrome’, if the string is a symmetry string then print ‘Symmetry’ if the string (aaa) has both property then print ‘Both property’ and print ‘No property’ otherwise. Write functions to check ‘Palindrome’ and ‘Symmetry’. Make the comparisons to be case insensitive.
Solution
def pal(s) : r=s[::-1] if s==r : return(1) else : return(0) def sym(s) : if len(s)%2==0 : if s[:len(s)//2]==s[len(s)//2:] : return(1) else : return(0) else : if s[:len(s)//2]==s[len(s)//2+1:] : return(1) else : return(0) n=int(input()) for i in range(n) : s=input().rstrip().lower() f1=pal(s) f2=sym(s) if f1==1 and f2==1 : print('Both property') elif f1==1 : print('Palindrome') elif f2==1 : print('Symmetry') else : print('No property')
Input
INPUT: s - string
Output
OUTPUT: if f1==1 and f2==1 : print('Both property') elif f1==1 : print('Palindrome') elif f2==1 : print('Symmetry') else : print('No property')
Processing
def pal(s) : r=s[::-1] if s==r : return(1) else : return(0) def sym(s) : if len(s)%2==0 : if s[:len(s)//2]==s[len(s)//2:] : return(1) else : return(0) else : if s[:len(s)//2]==s[len(s)//2+1:] : return(1) else : return(0)
Pseudocode
BEGIN Read n def sym() def pal() n=int(input()) for i in range(n) : s=input().rstrip().lower() f1=pal(s) f2=sym(s) if f1==1 and f2==1 : print('Both property') elif f1==1 : print('Palindrome') elif f2==1 : print('Symmetry') else : print('No property') END
Question 10 (Amicable numbers)
Two numbers are said to be amicable if the sum of proper divisors of one number plus 1, is equal to the other number. All divisors of a number other than 1 and itself, are called proper divisors. For example, the numbers 220 and 284 are amicable as the sum of proper divisors of 220 (i.e.) 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110 is equal to 284 and sum of proper divisors of 284 (i.e.) 1, 2, 4, 71 and 142 is 220. Given two numbers, write an algorithm and the subsequent Python code to Print Yes if the given two numbers are amicable. Print ‘No’ if the given numbers are not amicable. Check for boundary conditions and print ‘Invalid input’ for wrong input. Write appropriate functions for accomplishing the task.
Input Format
First line contains the first number
Next line contains the second number
Output Format
Print either Yes or No
Solution
def s(x) : s=1 for i in range(2,x) : if x%i==0 : s+=i return(s) n1=int(input()) n2=int(input()) if s(n1)==n2 or s(n2)==n1 : print('Yes') else : print('No')
Input
INPUT: n1 - first number n2 - first number
Output
OUTPUT: if s(n1)==n2 or s(n2)==n1 : print('Yes') else : print('No')
Processing
def s(x) : s=1 for i in range(2,x) : if x%i==0 : s+=i return(s)
Pseudocode
BEGIN Read n1 and n2 def s(x) if s(n1)==n2 or s(n2)==n1 : print('Yes') else : print('No') END
A small correction in the Question 3 (Second smallest number) remove the line printf(“a”).
LikeLike
Done
Thanks
LikeLike