• Write a program to accept two strings and find one is substrings of
another or not?
PROGRAM:-
MainString=input("Enter string:")
sub_str=input("Enter word:")
if(MainString.find(sub_str)==-1):
print("Substring not found in string!")
else:
print("Substring in string!")
Same question framed with more difficulty level as follows:
Write program to find all possible substrings for an inputted string.
PROGRAM:-
str = input("Enter string:")
n = len(str)
arr = []
for i in range(0,n):
for j in range(i,n):
arr.append(str[i:(j+1)])
print("All subsets for given string are: ")
for i in arr:
print(i)
• Write a program to confirm how many Armstrong/prime no’s are
present in a given list of 2 to 100.
Hint for Armstrong no:
d11 =d1
d1d2 = d12 + d22
d1d2d3 = d13 + d23 + d33
d1d2d3d4 = d14 + d24 + d34 + d44
PROGRAM:-
lower = 2
upper = 100
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
print("Armstrong numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
# order of number
order = len(str(num))
# initialize sum
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
if num == sum:
print(num)
• Write a program to confirm how many strings are there in a given
list, which are palindrome or starts with vowel.
PROGRAM:-
def palindrome(s):
v = []
for i in s:
if vowel(i):v.append(i)
if len(v)== 0: print("-1")
else:
x = v[::-1]
f = 1
for i in range(len(x)):
if x[i]!= v[i]:
f = 0
break
if f == 1: print("YES")
else: print("NO")
s = input("Enter string:")
palindrome(s.strip())
• Write are different ways of swapping the values of two variables
without using third variable:
One solution:
X, y= y,x
PROGRAM:-
x = 10
y = 50
x, y = y, x
print("After Swapping: x =", x, " y =", y);
Second solution:
X = x+y
y=x-y
x= x-y
PROGRAM:-
x = 10
y = 50
x = x + y
y = x - y
x = x - y
print("After Swapping: x =", x, " y =", y);
Third solution:
X = x^y
Y=x^y
X=x^y
PROGRAM:-
x = 10
y = 50
x = x ^ y
y = x ^ y
x = x ^ y
print("After Swapping: x =", x, " y =", y);
Fourth solution:
X = x*y
Y=x/y
X=x/y
PROGRAM:-
x = 10
y = 50
x = x + y
y = x - y
x = x – y
print("After Swapping: x =", x, " y =", y);
• Write a program to find how many are the leap years in range of
1300-2000.
For example: 1300, 1700, 1900 are not a leap year.
PROGRAM:-
year = int(input("Enter a year: "))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
• Find maximum and minimum marks scored by the same student
in any of these courses m1,m2,m3,m4,m5.
Solution: one way to go by using min and max comparison.
Second way by using sorting
PROGRAM:-
def getMinMax(low, high, arr):
arr_max = arr[low]
arr_min = arr[low]
# If there is only one element
if low == high:
arr_max = arr[low]
arr_min = arr[low]
return (arr_max, arr_min)
# If there is only two element
elif high == low + 1:
if arr[low] > arr[high]:
arr_max = arr[low]
arr_min = arr[high]
else:
arr_max = arr[high]
arr_min = arr[low]
return (arr_max, arr_min)
else:
# If there are more than 2 elements
mid = int((low + high) / 2)
arr_max1, arr_min1 = getMinMax(low, mid, arr)
arr_max2, arr_min2 = getMinMax(mid + 1, high, arr)
return (max(arr_max1, arr_max2), min(arr_min1, arr_min2))
sub1=int(input("Enter marks of the first subject: "))
sub2=int(input("Enter marks of the second subject: "))
sub3=int(input("Enter marks of the third subject: "))
sub4=int(input("Enter marks of the fourth subject: "))
sub5=int(input("Enter marks of the fifth subject: "))
arr =[sub1,sub2,sub3,sub4,sub5]
high = len(arr) - 1
low = 0
arr_max, arr_min = getMinMax(low, high, arr)
print('Minimum number is ', arr_min)
print('Maximum number is ', arr_max)
• To find second highest score or 3rd highest score done by him in
any of his courses in the above question.
PROGRAM:-
sub1=int(input("Enter marks of the first subject: "))
sub2=int(input("Enter marks of the second subject: "))
sub3=int(input("Enter marks of the third subject: "))
sub4=int(input("Enter marks of the fourth subject: "))
sub5=int(input("Enter marks of the fifth subject: "))
arr =[sub1,sub2,sub3,sub4,sub5]
b = sorted(list(set(a)),reverse=True)
print("2nd highest score: ",b[2])
print("3rd highest score: ",b[1])
• Programs for printing patterns in Python
• Simple pyramid pattern
Program:
height = int(input())
for i in range(1,height+1):
print(('* '*i))
• Simple pyramid pattern After 180 degree rotation
Program:
height = int(input())
for i in range(1,height+1):
print(('* '*i).rjust(height*2))
• Printing Triangle
Program:
height = int(input())
for i in range(1,height+1):
print(('* '*i).center(height*2))
• Number Pattern like pyramid
Program:
height = int(input())
for i in range(1,height+1):
[print(j,end=' ') for j in range(1,i+1)]
print('\r')
• Numbers pattern without re assigning the values means Number pattern with
distinct values like pyramid.
Program:
height = int(input())
num = 1
for i in range(1,height+1):
for _ in range(1,i+1):
print(num, end=' ')
num += 1
print('\r')
• Character Pattern like pyramid
Program:
height = int(input())
s = 'A'
for i in range(1,height+1):
print(((s+' ')*i))
s = chr(ord(s)+1)
• Star character like diamond.
Program:
height = int(input())
for i in range(1,height+1):
print(('* '*i).center(height*2))
for i in range(height,0,-1):
print(('* '*i).center(height*2))
• Continuous Character pattern like diamond
Program:
height = int(input())
s = 'A'
for i in range(1,height+1):
ns = ''
for j in range(1,i+1):
ns += s
s = chr(ord(s)+1)
print(' '.join(ns).center(height*2))
for i in range(height,0,-1):
ns = ''
for j in range(1,i+1):
ns += s
s = chr(ord(s)+1)
print(' '.join(ns).center(height*2))