1.
Input:
The input will contain only one number.
Output:
Output the same number.
Example:
Input:
10
Output:
10
a= int(input())
print (a)
Input Format:
The first line of the input contains two numbers separated by a space.
Output Format:
Print the difference in single line
Example:
Input:
42
Output:
2
Explanation:
Since the difference of numbers 4 and 2 is 2, hence the output is 2.
a , b = input().split()
print(int(a)-int(b))
Instructor Solution:
Input Format:
The first line of input contains two numbers separated by a space
Output Format:
Print the larger number
Example:
Input:
23
Output:
3
a,b = input().split()
if int(a) > int(b):
print (int(a))
else:
print (int(b))
Instructor Solution
if(x>y):
print(x)
else:
print(y)
Given an array A of N numbers, you have to write a program which prints the sum of the elements
of array A with the corresponding elements of the reverse of array A.
If array A has elements [1,2,3], then reverse of the array A will be [3,2,1] and the resultant array
should be [4,4,4].
Input Format:
The first line of the input contains a number N representing the number of elements in array A.
The second line of the input contains N numbers separated by a space. (after the last elements, there
is no space)
Output Format:
Print the resultant array elements separated by a space. (no space after the last element)
Example:
Input:
4
2531
Output:
3883
Explanation:
Here array A is [2,5,3,1] os reverse of this array is [1,3,5,2] and hence the resultant array is [3,8,8,3]
size = int(input())
list1=[]
list1= input().split()
list2= list1[::-1]
list3=[]
for i in range(0, len(list1) ):
list3.append( int(list1[i]) + int(list2[i]) )
print(*list3,sep=' '),
Instructor Solution:
N = int(input())
A = [int(i) for i in input().split(" ")]
B = []
for i in range(len(A)-1, -1,-1):
B.append(A[i])
C = []
for i in range(len(B)):
C.append(A[i]+B[i])
for i in range(len(C)):
if(i==len(C)-1):
print(C[i])
else:
print(C[i],end=" ")
list1= []
list1 = input().split()
maxval=int((list1[0]))
minval=int((list1[0]))
for i in range( 1, len(list1)):
if (int(list1[i])) > maxval:
maxval = int(list1[i])
elif (int(list1[i])) < minval:
minval = int(list1[i])
print(maxval, minval),
Instructor Solution:
Instructor Solution:
for i in range(len(b)):
if(i==len(b)-1):
print(b[i],end="")
else:
print(b[i],end=" ")
Input Format:
The first line contains a number made up of 0's and 1's.
Output Format:
Print 'YES' or 'NO' accordingly without quotes.
Example:
Input:
101
Output:
YES
Explanation:
If you flip the middle digit from 0 to 1 then all the digits will become same. Hence output is YES.
inp = input()
count0 = count1=0
res1= "YES"
res2= "NO"
Instructor Solution:
A = input()
ls = []
li = str(A)
for j in li:
ls.append(int(j))
count_z = 0
count_o = 0
for k in ls:
if(k==1):
count_o += 1
if(k==0):
count_z += 1
else:
if((count_o == 0) or (count_z == 0)):
print("NO")
else:
print("NO")
Instructor Solution:
k = int(input())
fac = 1
for i in range(1,k+1):
if(k==0):
break
fac=fac*i
print(fac)
n= int(r)*int(c)
for i in range(1,n+1):
print(i,end=" ")
cnt=cnt+1
if cnt==int(c):
if i < n:
cnt=0
print()
elif i==n:
break
a,b=map(int,input().split())
count=1
m = []
for i in range(1,a+1):
l = []
for j in range(1,b+1):
l.append(count)
count+=1
m.append(l)
for i in range(a):
for j in range(b):
if(j==b-1):
print(m[i][j], end="")
else:
print(m[i][j], end=" ")
if(i!=a-1):
print()
import random
n=int(input())
list_1 = []
for i in range(n):
list_1.append(int (input()))
list_2=[]
while list_1:
minimum = list_1[0]
for x in list_1:
if x < minimum:
minimum = x
list_2.append(minimum)
list_1.remove(minimum)
sarr = [str(a) for a in list_2]
print(' '.join(sarr))
Instructor Solution:
sorted = True
while(sorted):
j = randint(0,n-1)
i = randint(0,n-1)
arr[i],arr[j] = arr[j],arr[i]
for k in range(0,n-1):
if (arr[k] > arr[k+1]):
sorted = False
if(sorted):
break
else:
sorted = True
for i in range(n):
if(i==n-1):
print(arr[i])
else:
print(arr[i],end=" ")
import math
N,V1,V2 = input().split()
n= int(N)
v1=int(V1)
v2=int(V2)
if ( (n>=1 and n<=200) and (v1>=1 and v1<=100) and (v2>=1 and v2<=100) ):
dist = int(math.sqrt(2*n))
if dist >= n:
print("Walk")
else:
print("Cab")
Instructor Solution:
n,v1,v2 = input().split()
n = int(n)
v1 = int(v1)
v2 = int(v2)
st_d = pow(2,1/2)*n
st_t = st_d/v1
el_d = 2*n
el_t = el_d/v2
if(st_t<el_t):
print("Walk")
else:
print("Cab")
Programming Assignment-2: End-Sort
Due on 2019-03-07, 23:59 IST
Given a list A of N distinct integer numbers, you can sort the list by moving an element to the end
of the list. Find the minimum number of moves required to sort the list using this method in
ascending order.
Input Format:
The first line of the input contains N distinct integers of list A separated by a space.
Output Format
Print the minimum number of moves required to sort the elements.
Example:
Input:
13245
Output:
3
Explanation:
In the first move, we move 3 to the end of the list. In the second move, we move 4 to the end of the
list, and finally, in the third movement, we move 5 to the end.
Instructor Solution:
arr = [int(x) for x in input().split()]
arr1 = sorted(arr)
count = 0
for i in range(len(arr)):
if arr[i] == arr1[count]:
count+=1
print(len(arr)-count)
Programming Assignment-3: Semi Primes
Due on 2019-03-07, 23:59 IST
A semiprime number is an integer which can be expressed as a product of two distinct primes. For
example 15 = 3*5 is a semiprime number but 9 = 3*3 is not .
Given an integer number N, find whether it can be expressed as a sum of two semi-primes or not
(not necessarily distinct).
Input Format:
The first line contains an integer N.
Output Format:
Print 'Yes' if it is possible to represent N as a sum of two semiprimes 'No' otherwise.
Example:
Input:
30
Output:
Yes
Explanation:
N = 30 can be expressed as 15+15 where 15 is a semi-prime number (5*3 = 15)
NOTE: N is less than equal to 200
def checkSemiprime(num):
factors=[]
primes=[]
factors=print_factors(num)
#print("The factors of",num,"are:")
#print(factors)
for i in factors:
x=isprime(i)
if (x==True) :
primes.append(i)
#print(primes)
product=[]
for i in range(0,len(primes)):
for j in range(i, len(primes)):
product.append(primes[i]*primes[j])
#print(product)
status = False
for i in range(0,len(primes)):
for j in range(i+1, len(primes)):
product.append(primes[i]*primes[j])
#print(product)
status = False
for i in range(0, len(product)):
if(product[i] == num or isequal(product[i],num) == True):
status = True
break
else:
status = False
return status
def isequal(x,y):
f = False
for i in range(1,int(y/2)):
if(x*i == y):
f = True
break
else:
f = False
return f
def isprime(x):
flag = False
cnt=0
for i in range(1,x+1):
if (x % i) == 0:
cnt = cnt +1
if cnt ==2:
flag = True
else:
flag = False
return flag
def print_factors(x):
fl=[]
for i in range(2, x ):
if x % i == 0:
fl.append(i)
return fl
n = int(input())
if checkSemiprime(n) == True:
print("Yes")
else:
print("No")
Instructor Solution:
li = [12, 16, 20, 21, 24, 25, 27, 28, 29, 30, 31, 32, 35, 36, 37, 39, 40, 41, 42, 43, 44, 45, 47, 48, 49,
50, 52, 53, 54, 55, 56, 57, 59, 60, 61, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144,
145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200]
N = int(input())
if (N in li):
print('Yes')
else:
print('No')
You decided to sort this playlist in increasing order of songs length. For example, if the lengths of
the songs in the playlist were {1, 3, 5, 2, 4} after sorting it becomes {1, 2, 3, 4, 5}.
Before the sorting, "Computing Paradox" was on the kth position (1-indexing is assumed for the
playlist) in the playlist.
Your task is to find the position of "Computing Paradox" in the sorted playlist.
Input Format:
The first line contains two numbers N denoting the number of songs in the playlist.
The second line contains N space separated integers A1, A2, A3,..., AN denoting the lengths of
songs.
The third line contains an integer k, denoting the position of "Computing Paradox" in the initial
playlist.
Output Format:
Output a single line containing the position of "Computing Paradox" in the sorted playlist.
Example:
Input:
4
1342
2
Output:
3
Explanation:
n = int(input())
arr = [int(i) for i in input().split()]
k = int(input())
ele = arr[k-1]
arr.sort()
x=arr.index(ele)
print(x+1)
Instructor Solution:
n=int(input())
a=[int(x) for x in input().split()]
k=int(input())
key=a[k-1]
a.sort()
for i in range(len(a)):
if key==a[i]:
print(i+1)
break
n = int(input())
squares = { x: x*x for x in range(1,n+1)}
print(squares)
Instructor Solution:
n=int(input())
d=dict()
for i in range(1,n+1):
d[i]=i*i
print(d)
def printDict():
n = int(input())
squares = { x: x*x for x in range(1,n+1)}
print(squares)
printDict()
Instructor Solution:
def printDict():
n = int(input())
d=dict()
for i in range(1,n+1):
d[i]=i**2
print(d)
printDict()
n=int(input())
mat = []
for i in range(n):
mat.append([int(j) for j in input().split()])
tr=[]
tr = [ [0 for j in range(len(mat[0])) ] for i in range(len(mat)) ]
for i in range(0,n):
for j in range(0,n):
if(i>j):
tr[i][j] = 0
else:
tr[i][j]=mat[i][j]
for i in range(0,n):
for j in range(0,n):
if(j<n-1):
print(tr[i][j],end=" "),
else:
print(tr[i][j],end="")
if(i!=n-1):
print()
Instructor Solution:
a = int(input())
m = []
for i in range(1,a+1):
l = list(map(int, input ().split ()))
m.append(l)
for i in range(a):
for j in range(a):
if(i<=j):
if(j==a-1):
print(m[i][j], end="")
else:
print(m[i][j], end=" ")
else:
if(j==a-1):
print(0, end="")
else:
print(0, end=" ")
if(i!=a-1):
print()
tr=[]
tr = [ [0 for j in range(len(mat[0])) ] for i in range(len(mat)) ]
for i in range(0,n):
for j in range(0,n):
tr[i][j]=mat[j][i]
for i in range(n):
for j in range(n):
if (mat[i][j] != tr[i][j]):
flag=False
break
else:
flag=True
if(flag==True):
print("YES",end=" ")
elif(flag==False):
print("NO",end=" ")
Instructor Solution:
a = int(input())
m = []
for i in range(1,a+1):
l = list(map(int, input ().split ()))
m.append(l)
if (isSymmetric(m, a)):
print("YES")
else:
print("NO")
n,m =(input().split())
r =int(n)
c =int(m)
a = []
for i in range(r):
a.append([int(j) for j in input().split()])
cnt=0
for i in range(0,r):
for j in range(0,c):
if(a[i][j] == 0 or a[i][j]==1):
cnt=cnt+1
num = r*c
if(cnt == num):
print("YES",end=" ")
else:
print("NO",end=" ")
Instructor Solution:
def isBinaryMatrix(mat,M,N):
for i in range(M):
for j in range(N):
# Returns false if element
# is other than 0 or 1.
if ((mat[i][j] == 0 or mat[i][j] == 1)==False):
return False;
Example:
If the input list is
12 24 35 24 88 120 155 88 120 155
Then the output should be
12 24 35 88 120 155
Explanation:
Third, seventh and ninth element of the list L has been removed because it was already present.
Input Format:
In one line take the elements of the list L with each element separated by a space.
Output Format:
Print the elements of the modified list in one line with each element separated by a space.
Example:
Input:
12 24 35 24
Output:
12 24 35
return repeated
res=Repeat(arr)
print (*res, end=" ")
Instructor Solution:
def removeDuplicate( li ):
newli=[]
seen = set()
for item in li:
if item not in seen:
seen.add( item )
newli.append(item)
return newli
li=[]
li= list(map(int, input ().split ()))
x = removeDuplicate(li)
for i in x:
print(i,end=" ")
def Repeat(x):
size = len(x)
repeated = []
for i in range(size):
count =0
k=i+1
for j in range(k, size):
if x[i] == x[j]:
count =count+1
if x[i] not in repeated:
repeated.append(x[i])
elif count==0 and x[i] not in repeated:
repeated.append(x[i])
return repeated
text = input()
inputsent = text.lower()
listaz = []
for i in range(26):
listaz.append(string.ascii_lowercase[i])
#print(listaz)
inputlist = []
for i in range(len(inputsent)):
if inputsent[i]!= ' ':
inputlist.append(inputsent[i])
#print(inputlist)
newinput=Repeat(inputlist);
#print(newinput)
newinput.sort()
#print(newinput)
if listaz == newinput:
print("YES",end=" ")
else:
print("NO",end=" ")
Instructor Solution:
def checkPangram(s):
List = []
# create list of 26 charecters and set false each entry
for i in range(26):
List.append(False)
if (checkPangram(sentence)):
print("YES")
else:
print("NO")
text = input()
strinput=text.lower()
def is_vow(c):
return ((c == 'a') or (c == 'e') or (c == 'i') or (c == 'o') or (c == 'u'))
def removeVowels(istr):
liststr=[]
liststr.append(istr[0])
for i in range(1,len(istr)):
if ((is_vow(istr[i - 1]) != True) or (is_vow(istr[i]) != True)):
liststr.append(istr[i])
return liststr
reslist =[]
reslist = removeVowels(strinput)
resstr = ''.join(reslist)
print (resstr)
Instructor Solution:
def is_vow(c):
# comparison of consecutive
# characters
if ((is_vow(str[i - 1]) != True) or
(is_vow(str[i]) != True)):
# Driver code
str= input()
removeVowels(str)
n = int(input())
a = []
for i in range(n):
row = input().split()
for i in range(len(row)):
row[i] = int(row[i])
a.append(row)
def counterClockspiralPrint(m, n, arr):
k = 0; l = 0
cnt = 0
total = m * n
while (k < m and l < n):
if (cnt == total) :
break
counterClockspiralPrint(n,n, a)
Instructor Solution:
# total number of
# elements in matrix
total = m * n
l += 1
if (cnt == total) :
break
m -= 1
if (cnt == total) :
break
if (cnt == total) :
break
k += 1
# Driver Code
num = int(input())
R = num
C = num
arr = []
for i in range(1,num+1):
l = list(map(int, input ().split ()))
arr.append(l)
counterClockspiralPrint(R, C, arr)
for i in range(len(s)):
Instructor Solution:
import re
def extractMax(input):
print(max(numbers))
S = input()
extractMax(S)
email = input()
comp =[]
for i in range(len(email)):
if email[i] == '@':
pos=i
break
Instructor Solution:
import re
emailAddress = input()
pat2 = "(\w+)@(\w+)\.(com)"
r2 = re.match(pat2,emailAddress)
print(r2.group(2))
s = input()
splchar = "[@_!#$%^&*()<>?/\|}{~:]"
for i in range(len(s)):
if s[i] in splchar:
flag = True
break
else:
flag = False
if flag == True:
print('YES')
else:
print('NO')
Instructor Solution:
import re
else:
print("YES")
S = input()
run(S)
Programming Assignment-2: Jumps
Due on 2019-04-11, 23:59 IST
One day Ajit got a strange feeling of jumping from one point to another. The jumping will be done
in one dimension only.
He will start from a point 0 and from there he will perform a lot of jumps. He can only jump in a
specific sequence:
1jump, 2jump, 3jump, 1jump, 2jump, 3jump, 1jump, and so on. (1>2>3>1>2>3>1.....)
1-jump means that if Ajit is at the point x, he will jump to the point x+1.
2-jumps mean that if Ajit is at the point x, he will jump to the point x+2.
3-jumps mean that if Ajit is at the point x, he will jump to the point x+3.
Before the start Ajit asks you: will he arrive at the point a after some number of jumps?
Input Format:
The first line contains a single number a denoting the point Ajit asks about.
Output Format:
Output "YES" without a quotes if Ajit can arrive at point a or "NO" without quotes
otherwise.
Example-1:
Input:
0
Output:
YES
Explanation:
He started at point 0
Example-2:
Input:
2
Output:
NO
Explanation:
From 0 he can take 1 jump to reach point 1 but after that he can take only 2 jumps which will lead
him to point 3.
Jump sequence (1>2).
NOTE: The value of a can be as large as 1018. Please make your program efficient or you may
receive run time error.
n = int(input())
i=0
j=[1,2,3]
x=0
while(x<=n):
x+=j[i]
if x==n:
print("YES")
break
if(i==2):
i=0
else:
i=i+1
if x!=n:
print("NO")
Instructor Solution:
x = int(input())
for i in range(len(res)-1):
print(res[i],end = ",")
Instructor Solution:
import math
c=50
h=30
value = []
items=[x for x in input().split(',')]
for d in items:
value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))
print (','.join(value))
import sys;
arr.sort()
if (n < m):
return -1
min_diff = sys.maxsize
first = 0
last = 0
i=0
while(i+m-1<n ):
min_diff = diff
first = i
last = i + m - 1
i+=1
print(findMinDiff(arr, n, m))
Instructor Solution:
import sys;
min_diff = diff
first = i
last = i + m - 1
i+=1
print(findMinDiff(A, n, m))
l = input().split(",")
l.sort()
print( ','.join(l))
Instructor Solution:
for i in range(len(values)):
if values[i] == 0:
values.append(values[i])
values.remove(values[i])
for i in range(len(values)-1):
print(values[i],end=" ")
print(values[len(values)-1],end=" ")
Instructor Solution:
def move_zero(num_list):
a = [0 for i in range(num_list.count(0))]
x = [ i for i in num_list if i != 0]
x.extend(a)
return(x)
res = move_zero(num_list)
for i in range(len(res)):
if(i==len(res)-1):
print(res[i],end="")
else:
print(res[i],end=" ")
Output:
Explanation:
inp = input()
input = inp.upper()
l =list(input)
onehole =['A','D','O','P','Q','R']
twohole = ['B']
cnt1=0
cnt2=0
total=0
for i in range(len(l)):
if l[i] in onehole:
cnt1 =cnt1+1
elif l[i] in twohole:
cnt2=cnt2+2
total =cnt1+cnt2
print(total,end=" ")
Instructor Solution:
list_st = []
zero_hole = ["C","E","F","G","H","I","J","K","L","M","N","S","T","U","V","W","X","Y","Z"]
one_hole = ["A","D","O","P","Q","R"]
two_hole = ["B"]
x = input()
c=0
for j in x:
if(j in one_hole):
c = c+1
if(j in two_hole):
c=c+2
print(c)
def fadedPalindrome(s):
hlen = len(s)
slist = []
slist.extend(s)
for i in range(hlen//2):
if slist[i] != '.' and slist[hlen-1-i] != '.' and slist[i] != slist[hlen-1-i]:
print(-1,end=" ")
return
for i in range(hlen):
if slist[i] == '.' and slist[hlen-1-i] != '.':
slist[i] = slist[hlen-1-i]
elif slist[i] == '.':
slist[i] = 'a'
print("".join(slist),end=" ")
s = input()
fadedPalindrome(s)
Instructor Solution:
numbers = []
li = []
x = input()
li = list(str(x))
if(len(li)%2 != 0):
if(li[int(len(li)/2)] == '.'):
li[int(len(li)/2)] = 'a'
s=0
p=len(li)-1
count = 0
flag = []
while((s < int(len(li)/2)) and (p >= int(len(li)/2))):
if(li[s]!='.' and li[p]!='.'):
if(li[s] == li[p]):
flag.append(1)
count+=1
s += 1
p -= 1
else:
break
else:
if(li[s]=='.' and li[p]!='.'):
li[s]=li[p]
count+=1
s += 1
p -= 1
else:
if(li[s]!='.' and li[p]=='.'):
li[p]=li[s]
count+=1
s += 1
p -= 1
else:
li[s] = 'a'
li[p] = 'a'
count+=1
s += 1
p -= 1
if(count == int(len(li)/2)):
print(''.join(li))
else:
print(-1)
st = input()
cntu=0
cntl=0
for i in range(len(st)):
if st[i].isupper():
cntu=cntu+1
elif st[i].islower():
cntl=cntl+1
print(str(cntu) + " " + str(cntl) ,end=" ")
Instructor Solution:
s = input()
d={"UPPER CASE":0, "LOWER CASE":0}
for c in s:
if c.isupper():
d["UPPER CASE"]+=1
elif c.islower():
d["LOWER CASE"]+=1
else:
pass
print(d['UPPER CASE'],d['LOWER CASE'])