Skillrack Practice Problems 6

Solutions for Skillrack Code Test CSE1001 Practice Problems 6

22-Sept-2016 00:00 to  1-Oct-2016 00:00

Total time : 600 mins

Challenges : 5

Question 1 (Adam number)

A number ‘n’ is said to be Adam number if reverse of the square of the number ‘n’ is reverse of square of ‘n’. For example, 12 is an Adam number.

Reverse of 12 = 21

Square of 21 = 441 which is reverse of square of 12 (i.e) 144

Write a function to reverse a number

Input Format:

The first line will contain the number.

Output Format:

Print Adam number or Not an Adam number

Solution

def rev(x) :
    y=0
    while x>0 :
        y*=10
        y+=x%10
        x=x//10
    return(y)
n1=int(input())
n2=rev(n1)
if n2**2==rev(n1**2) :
    print('Adam number')
else :
    print('Not an Adam number')

Input

INPUT:
n1 - Given number

Output

OUTPUT:
if n2**2==rev(n1**2) :
    print('Adam number')
else :
    print('Not an Adam number')

Processing

def rev(x) :
    y=0
    while x>0 :
        y*=10
        y+=x%10
        x=x//10
    return(y)

Pseudocode

BEGIN

Read n
def rev(x) :
    y=0
    while x>0 :
        y*=10
        y+=x%10
        x=x//10
    return(y)
n2=rev(n1)
if n2**2==rev(n1**2) :
    print('Adam number')
else :
    print('Not an Adam number')

END

 

Question 2 (Pangram)

Pangrams are words or sentences containing every letter of the English alphabet at least once. Write an algorithm and  a subsequent Python code to check whether a string is pangram or not. Write a function to check if a given string is pangram. For example, “The quick brown fox jumps over the lazy dog” is a panagram. Input format First line contains the string to be checked Output Format Print Pangram or Not pangram

Solution

n=input().rstrip()
l=[]
for i in n :
    if i.isalpha() :
        i=i.lower()
        if i not in l :
            l.append(i)
if len(l)==26 :
    print('Pangram')
else :
    print('Not pangram')

Input

INPUT:
n - Given string

Output

OUTPUT:
if len(l)==26 :
    print('Pangram')
else :
    print('Not pangram')

Processing

for i in n :
    if i.isalpha() :
        i=i.lower()
        if i not in l :
            l.append(i)

Pseudocode

BEGIN

Read n
for i in n :
    if i.isalpha() :
        i=i.lower()
        if i not in l :
            l.append(i)
if len(l)==26 :
    print('Pangram')
else :
    print('Not pangram')

END

Question 3 (Isomorphic Numbers)

Given ‘n’ integers, write an algorithm and the subsequent Python code to print all numbers that are isomorphic to the first number. Two numbers are said to be isomorphic if they have the same number of digits in it and the sets of positions  having the same  digits areequal.. For example:

12321 is isomorphic to 83538 .
Both the  numbers are of length five.  In 12321, positions 2 and 4 have the same digit 2, Positions 1 and 5 have same digit 1. Hence the set of positions having the same digit are {{1,5},{2,4}. Similarly , for the number 83538, the set of positions having the same digit is {{1,5}, {2,4}}.

1232 is not isomorphic to 2342 because set of places having same digits for 1232 is {{2, 4}} and for 2342 is {{1, 4}} (digits are numbered from left to right starting from 1).
12 not isomorphic to 10.
Write a function to check whether two  numbers are isomorphic. If none of the numbers are isomorphic then print ‘No isomorphic’.

Input Format

First line contains the number of elements, n

Next ‘n’ line contains the numbers

Output Format

Print first number in the first line

Next few lines contain the numbers that are isomorphic to first number

Solution

def iso(a) :
    t=[]
    w=0
    e=a
    while a>0 :
        t.append(a%10)
        a=a//10
        w+=1
    f=1
    g=1
    if q==w :
        for i in range(0,q-1) :
            for j in range(i+1,q) :
                if p[i]==p[j]  :
                    g=0
                    if t[i]!=t[j] :
                        f=0
                        break
    else :
        f=0
    if g==0 and l[0]==e :
        f=1
    elif g==1 :
        f=0
    return(f)
n=int(input())
l=[]
p=[]
y=[]
q=0
for i in range(n) :
    x=int(input())
    l.append(x)
led=l[0]
while led>0 :
    p.append(led%10)
    led=led//10
    q+=1
for z in range(1,n) :
    a=l[z]
    f=iso(a)
    if f==1 :
        y.append(l[z])
if len(y)>0 :
    print(l[0])
    for i in y :
        print(i)
else :
    print('No isomorphic')

Input

INPUT:
n - Number of terms

List of numbers
for i in range(n) :
    x=int(input())
    l.append(x)
led=l[0]

Output

OUTPUT:
if len(y)>0 :
    print(l[0])
    for i in y :
        print(i)
else :
    print('No isomorphic')

Processing

def iso(a) :
    t=[]
    w=0
    e=a
    while a>0 :
        t.append(a%10)
        a=a//10
        w+=1
    f=1
    g=1
    if q==w :
        for i in range(0,q-1) :
            for j in range(i+1,q) :
                if p[i]==p[j]  :
                    g=0
                    if t[i]!=t[j] :
                        f=0
                        break
    else :
        f=0
    if g==0 and l[0]==e :
        f=1
    elif g==1 :
        f=0
    return(f)

Pseudocode

BEGIN

Read n and create list of entries

def iso(a) :
    t=[]
    w=0
    e=a
    while a>0 :
        t.append(a%10)
        a=a//10
        w+=1
    f=1
    g=1
    if q==w :
        for i in range(0,q-1) :
            for j in range(i+1,q) :
                if p[i]==p[j]  :
                    g=0
                    if t[i]!=t[j] :
                        f=0
                        break
    else :
        f=0
    if g==0 and l[0]==e :
        f=1
    elif g==1 :
        f=0
    return(f)

while led>0 :
    p.append(led%10)
    led=led//10
    q+=1
for z in range(1,n) :
    a=l[z]
    f=iso(a)
    if f==1 :
        y.append(l[z])
if len(y)>0 :
    print(l[0])
    for i in y :
        print(i)
else :
    print('No isomorphic')

END

Question 4 (Automorphic Numbers)

Automorphic numbers are numbers having “n” digits such that the  last n digits of the square of the number will be the number itself). Write an algorithm and the subsequent Python code to check if the given number is automorphic. For example, 25 is a 2 digit automorphic number with a square of 625 and 376  with its  square  141376, is a 3 digit automorphic number.

Input Format

First line contains the number to be checked

Output Format

Print Automorphic or Not automorphic

Solution

n1=int(input())
n2=n1**2
f=1
while n1 > 0 :
    if n1%10==n2%10 :
        n1=n1//10
        n2=n2//10
    else :
        f=0
        break
if f==1 :
    print('Automorphic')
else :
    print('Not automorphic')

Input

INPUT:
n1 - Given number

Output

OUTPUT:
if f==1 :
    print('Automorphic')
else :
    print('Not automorphic')

Processing

while n1 > 0 :
    if n1%10==n2%10 :
        n1=n1//10
        n2=n2//10
    else :
        f=0
        break

Pseudocode

BEGIN

Read n1
n2=n1**2
f=1
while n1 > 0 :
    if n1%10==n2%10 :
        n1=n1//10
        n2=n2//10
    else :
        f=0
        break
if f==1 :
    print('Automorphic')
else :
    print('Not automorphic')

END

Question 5 (Elizabeth’s Walk)

Elizabeth visits her friend Andrew and then returns home by the same route. She always walks 2 kilometers per hour (km/h) when walking uphill, 6 km/h when walking downhill, and 3 km/h when walking on level ground. If the path from Elizabeth’s home to Andrew’s home consists of ‘x’ meter in the level ground. ‘y’ meter in the uphill, ‘z’ meter in the  downhill and Elizabeth starts from home by 6 a.m. Write an algorithm and the subsequent Python code to determine when Elizabeth will reach Andrew’s home and when she will reach her home back if she spends ‘m1’ minutes in Andrew’s home. For example, if x is 1000, ‘y’ is 500, ‘z’ is 300  and m1 is ’30’ minutes, then Elizabeth takes 38 min to reach Andrew’s home so she will reach Andrew’s home by 6 hour 38 min. Elizabeth will take 34 min to walk from Andrew’s home to her home and time when will reach her home back is 7 hour 42 min.

Input Format

First line contains the distance ‘x’ in level ground

Next line contains the distance ‘y’ in uphill

Next line contains the distance ‘z’ in downhill

Next line contains the value for ‘ml’ minutes spent at Andrew’s home

Output Format

Print time by which Elizabeth will reach Andrew’s home. Print hours and minutes separated by a space

Print time by which Elizabeth will reach back her home. Print hours and minutes separated by a space

Solution

x=int(input())
y=int(input())
z=int(input())
ml=int(input())
t=(x/0.834)+(y/0.556)+(z/1.667)
t1=int(round(t/60,0))
t2=int(round((t+(ml*60)+(x/0.834)+(y/1.667)+(z/0.556))/60,0))
x=t1//60
print(6+x,t1-(60*x))
x=t2//60
print(6+x,t2-(60*x))

Input

INPUT:
x - Distance on level ground
y - Distance uphill
z - Distance downhill
ml - Minutes spent at Andrew's home

Output

OUTPUT:
x=t1//60
print(6+x,t1-(60*x))
x=t2//60
print(6+x,t2-(60*x))

Processing

t=(x/0.834)+(y/0.556)+(z/1.667)
t1=int(round(t/60,0))
t2=int(round((t+(ml*60)+(x/0.834)+(y/1.667)+(z/0.556))/60,0))

Pseudocode

BEGIN

Read x, y, z and ml
t=(x/0.834)+(y/0.556)+(z/1.667)
t1=int(round(t/60,0))
t2=int(round((t+(ml*60)+(x/0.834)+(y/1.667)+(z/0.556))/60,0))
x=t1//60
print(6+x,t1-(60*x))
x=t2//60
print(6+x,t2-(60*x))

END

 

Advertisements

One thought on “Skillrack Practice Problems 6

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s