Thursday 15 September 2016

Skillrack Solutions VIT 19th September 2016


Program 1- Count Words

from pprint import pprint
import re
import sys
a=input()
a=a.lower()
for i in a:
    if i.isdigit():
        sys.exit()
if re.search(r' [?!.,:;]',a):
    print("Invalid input")
    sys.exit()
l=re.findall(r"[\w']+",a)
d={}
for i in l:
    d[i]=l.count(i)

pprint(d)



Program 2- Histogram

import sys
from pprint import pprint
a=input()
a=a.lower()
for i in a:
    if not i.isalpha():
        print("Invalid input")
        sys.exit()
b={}
for i in a:
    b.update({i:a.count(i)})
c={}
for k,v in b.items():
      c.setdefault(v, []).append(k)
for i in c.keys():
    c[i]=sorted(c[i])
pprint(b)

pprint(c)



Program 3- Minimum Distance

import math
n=int(input())
x=[]
y=[]
for i in range(n):
    x.append(int(input()))
    y.append(int(input()))
min=math.inf
for i in range(n-1):
    for j in range(i+1,n):
        dist=math.sqrt((x[i]-x[j])**2+(y[i]-y[j])**2)
        if dist<min:
            min=dist
for i in range(n-1):
    for j in range(i+1,n):
        dist=math.sqrt((x[i]-x[j])**2+(y[i]-y[j])**2)
        if dist==min:
            print((x[i],y[i]))
            print((x[j],y[j]))
            


Program 4- Letter Identification

w1=input().rstrip()
w2=input().rstrip()
w3=input().rstrip()
l1=[]
l2=[]
l3=[]
l4=[]
for i in w1:
    if i in w2 and i in w3:
        if i not in l1:
         l1.append(i)
for i in w1:
    if i in w2 and i not in w3:
        if i not in l2:
         l2.append(i)
for i in w1:
    if i not in w2 and i not in w3:
        if i not in l3:
         l3.append(i)
for i in w1:
    if i not in l4:
        l4.append(i)
for i in w2:
    if i not in l4:
        l4.append(i)
for i in w3:
    if i not in l4:
        l4.append(i)
print(sorted(l1))
print(sorted(l2))
print(sorted(l3))
print(sorted(l4))



Program 5- Rook and Queen

rr1=int(input())
rc1=int(input())
qr1=int(input())
qc1=int(input())
q=[]
r=[]
for i in range(1,9):
    if i!=rc1:
     r.append((rr1,i))
for i in range(1,9):
    if i!=rr1:
     r.append((i,rc1))
for i in range(1,9):
    if i!=qc1:
     q.append((qr1,i))
for i in range(1,9):
    if i!=qr1:
     q.append((i,qc1))
r1=qr1
c1=qc1
while(r1>1 and c1<8):
    q.append((r1-1,c1+1))
    r1-=1
    c1+=1
r1=qr1
c1=qc1
while(r1<8 and c1>1):
    q.append((r1+1,c1-1))
    r1+=1
    c1-=1
r1=qr1
c1=qc1
while(r1<8 and c1<8):
    q.append((r1+1,c1+1))
    r1+=1
    c1+=1
r1=qr1
c1=qc1
while(r1>1 and c1>1):
    q.append((r1-1,c1-1))
    r1-=1
    c1-=1
r=sorted(r)
for i in r:
    if i in q:
        print(i)


Try my app on play store and rate it!

No comments:

Post a Comment