Skillrack Practice Problems 5

Solutions for Skillrack Code Test CSE1001 Practice Problems 5

8-Sept-2016 00:00 to  19-Sept-2016 08:00

Total time : 500 mins

Challenges : 5

Question 1 (Letter Identification)

Given three words, write an algorithm and the subsequent Python code to identify the following letters:

  1. Letters common to all the three words
  2. Letters in first two words but not in third word
  3. Letters in first word but not in second and third word
  4. Letters in all the three words

For example, if the words are apple, camel, element then letters in common to all the three words –  i, e

Letters in first two words but not in third word – a

Letters in first word but not in second and third word  – p

Letters in all the three words – a, p, p, l, e, c, m, n, t

Hint: Use sets in Python. While reading input, use rstrip() function to remove extra spaces

Input Format

First line contains word1

Second line contains word2

Third line contains word3

Output Format

List of letters common to all the three words in lexicographical order

List of letters common to first two words but not in third word in lexicographical order

List of letters in first word but not in second or third word in lexicographical order

List of all letters in the three words in lexicographical order

Solution

 s1=set(input().rstrip())

 s2=set(input().rstrip())

 s3=set(input().rstrip())

 s=list(s1&s2&s3)

 s.sort()

 print(s)

 s=list((s1&s2)-s3)

 s.sort()

 print(s)

 s=list(s1-(s2|s3))

 s.sort()

 print(s)

 s=list(s1|s2|s3)

 s.sort()

 print(s)

Question 2 (Minimum distance points)

Given ‘n’ points in an X-Y plane , write an algorithm and the subsequent Python code to determine the pair of points that are closer. Distance between two points (x1, y1) and (x2, y2) is determined using the formula.

Consider only two decimal places of distance for comparison. When there are multiple points with the same minimum distance print all points.

Input Format

Number of points

x coordinate of point1

y coordinate of point1

x coordinate of point2

y coordinate of point2

Output format

Point1 and Point2 that have minimum distance between them

Solution

n=int(input())
l=[]
min=999
for i in range(n) :
    x=int(input())
    y=int(input())
    l.append((x,y))
for i in range(0,n-1) :
    for j in range(i+1,n) :
        x1,y1=l[i]
        x2,y2=l[j]
        d=(((x2-x1)**2)+((y2-y1)**2))**(0.5)
        if d < min :
            min=d
            m=[]
            m.append((x1,y1))
            m.append((x2,y2))
        elif d==min :
            m.append((x1,y1))
            m.append((x2,y2))
for i in m :
    print(i)
 

Question 3 (Count Words)

Write an algorithm and write the Python code to count the number of unique words in a given passage. The paragraph may contain words with special characters such as ‘!’, ‘?’, ‘.’, ‘,’ , ‘:’ and ‘;’ and digits are not permitted. Special character must occur only at the end of a word in a passage that is Hello World! is valid but Hello !World or Hello Wor!ld is invalid. No two special character occur together. Print ‘Invalid input’ for such cases. Count words without special characters. Counting must be case insensitive. Print words in lower case and in sorted order. For example, given a passage ‘Programming is a skill in demand! Your programming skills are bankable assets.’ The output should be

{‘a’: 1,

‘are’: 1,

‘assets’: 1,

‘bankable’: 1,

‘demand’: 1,

‘in’: 1,

‘is’: 1,

‘programming’: 2,

‘skill’: 1,

‘skills’: 1,

‘your’: 1}

[Hint: use pprint function for printing dictionary in sorted order.

Syntax for pprint is

pprint(dictionary name)

Include the line “from pprint import pprint” in the top of your program]

Input format:

enter a paragraph

Output format:

Dictionary of words in the passage and their count

Boundary conditions:

One paragraph is entered at a strech

Solution

import pprint
w=str(input())
l=w.split()
t=[]
d={}
f=1
for i in l :
    k=''
    for j in i :
        k+=j.lower()
    t.append(k)
for i in t :
    if i.isalpha() :
        if i in d :
            d[i]+=1
        else :
            d[i]=1
    elif i[:len(i)-1].isalpha() :
        i=i[:len(i)-1]
        if i in d :
            d[i]+=1
        else :
            d[i]=1
    else :
        f=0
        break
if f==1 :
    pprint.pprint(d)
else :
   print('Invalid input')

Question 4 (Word histogram)

Histogram is a graphical representation drawn based on the frequency of occurrence of things. Histogram for a word is drawn based on the number of occurrences of each character in the word. An inverted dictionary is used to maintain the list of characters that has same frequency. Write an algorithm and the subsequent Python code to compute and print the frequency of occurrence of character as a dictionary (ch:count). Make the entire process to be case insensitive. For example, if the input word is ‘parrot’, then the dictionary with characters and frequency of occurrence is

{‘a’: 1, ‘o’: 1, ‘p’: 1, ‘r’: 2, ‘t’: 1}

and the inverted dictionary is

{1: [‘a’, ‘o’, ‘p’, ‘t’], 2: [‘r’]}

Print dictionary in sorted order by key. Sort the values of each key in the inverted dictionary.

[Hint: use pprint function for printing dictionary in sorted order.

Syntax for pprint is

pprint(dictionary name)

Include the line “from pprint import pprint” in the top of your program

Check for boundary conditions and print ‘Invalid input’ if conditions are not met.

Input Format:

First line contains the input word

Output Format:

Dictionary of characters in the word as keys and count as values in sorted order by key.

Inverted dictionary in sorted order by key.

Boundary Conditions:

Given word should only contain alphabets

Solution

import pprint
d={}
f={}
w=input()
if w.isalpha() :
    for i in w :
        if i.isupper() :
            i=i.lower()
        if i in d :
            d[i]+=1
        else :
            d[i]=1
    for i in d :
        if d[i] in f :
            f[d[i]].append(i)
        else :
            f[d[i]]=[i]
    for i in f :
        f[i].sort()
    pprint.pprint(d)
    pprint.pprint(f)
else :
    print('Invalid input')

Question 5 (Rook and a Queen)

Given the position of a Rook and a queen in a chess board (8X8 board),  write an algorithm and the subsequent Python code to determine the common positions where both rook and queen can be placed in the next move. Rook can move through any number of cells,  either horizontally or vertically. Queens can move through any number of cells,  either horizontally, vertically or diagonally.  Each cell in the chess board may be represented as a 2-tuple (row,col). For example, if the current position of the rook is (3,1) then the next possible position of the rook may be either in the same column {(2,1),(1,1),(4,1),(5,1),(6,1),(7,1),(8,1)} or in the same row {(3,2),(3,3),(3,4),(3,5),(3,6),(3,7),(3,8)}. If the queen is in the position (5,3) then it can be placed in the same row {(5,1),(5,2),(5,4),(5,5),(5,6),(5,7),(5,8)} or same column {(1,3),(2,3),(3,3),(4,3),(6,3),(7,3),(8,3)} or along the diagonal of the current position {(6,4),(7,5),(8,6),(4,2),(5,1),(6,2),(7,1),(4,4),(3,5),(2,6),(1,7)}. Then the common cells for next move are {(3,3), (5,1), (7,1)}.

The output is a set of common board positions where both queen and rook can be placed. The positions must be printed in sorted order, sort it by row. When rows are same,  sort it by column.

(Hint: Use built-in function to sort the values)

Input Format

Row position of rook

Column position of rook

Row position of queen

Column position of queen

Output Format

Common position1

Common position2

Solution

r={0}
r2={0}
xr=int(input())
yr=int(input())
xq=int(input())
yq=int(input())
s1=xq+yq
a=xq-yq
b=yq-xq
for i in range(1,9) :
    r2.add((xr,i))
    r2.add((i,yr))
r2=r2-r
r2=r2-{(xr,yr)}
q={0}
q2={0}
for i in range(1,9) :
    for j in range(1,9) :
        if i+j==s1 :
            q2.add((i,j))
        elif xq>yq :
            if i-j==a :
                q2.add((i,j))
        elif yq>xq :
            if j-i==b :
                q2.add((i,j))
for i in range(1,9) :
    q2.add((xq,i))
    q2.add((i,yq))
q2=q2-q
q2=q2-{(xq,yq)}
a=r2&q2
c=list(a)
c.sort()
for i in c :
    print(i)
Advertisements

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