Assignment 1
Set A]
1) Write a program which accepts 5 integer values and prints “DUPLICATES” if any of
the
values
entered are duplicates otherwise it prints “ALL UNIQUE”.
Example:
Let 5 integers are (32, 45, 90, 45, 6) then output “DUPLICATES” to be printed.
Ans=
from collections import Counter
l1=[1,2,3,4]
s=set()
for i in range(0,len(l1)):
s.add(l1[i])
if(len(l1)!=len(s)):
print("dublicates")
else:
print("All Unique")
2) Write a program which accepts an integer value as command line and print “Ok” if value is
between
1 to 50 (both inclusive) otherwise it prints ”Out of range”
n=int(input("enter the 1 to 50 :"))
if(n in range(1, 50)):
print("ok")
else:
print("number is not between 1 to 50")
3) Write a program which finds sum of
digits of a number.
Example n=125 then output is 8
(1+2+5).
Ans=
n=125
ans=0
rem=0
while n>0:
rem=n%10
ans=ans+rem
n=n//10
print(ans)
4) Write a program which prints Fibonacci series of a number.
Ans=
n=int(input("enter number :"))
n1=0
n2=1
print(n1)
print(n2)
for i in range(2,n+1):
n3=n1+n2
n1=n2
n2=n3
print(n3)
Set B]
1) Write a program which accept an integer value ‘n’ and display all prime numbers till ‘n’.
Ans=
low = int(input("Enter lower number"))
high = int(input("Enter higher number"))
for num in range(low,high+1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
2) Write a program to display following
pattern.
1 2 3 4
1 2 3
1 2
1
Ans=
n=int(input("enter num"))
for i in range(n,1,-1):
for j in range(1,i):
print(j,"",end="")
print()
3) Write a program that finds distance between 2 points (x1,y1) and (x2,y2) using the equation
Ans=
import math
x1=5
x2=10
y1=7
y2=16
form=math.sqrt((x2-x1)**2+(y2-y1))
print("ans",form)
Set C]
1) Write a binary search function
which searches an item in a sorted list. The function should
return the index of element to be searched in the list.
Ans=
from bisect import bisect_left
def BinarySearch(a, x):
i = bisect_left(a, x)
if i != len(a) and a[i] == x:
return i
else:
return -1
a = [1, 2, 4, 4, 8]
x = int(4)
res = BinarySearch(a, x)
if res == -1:
print(x, "is absent")
else:
print("First occurrence of", x, "is present at", res)
Other Posts :-
- After 12th Course and Career Options
- SoftwareTechIT YouTube Channel Videos Points And Videos
- Android Tutorial on Button Click Event And Toast Message with SoftwareTechIT
- Points And Videos learn android app development with softwaretechit
- Introduction To fragment and Fragment Life Cycle With Example
- Views & ViewGroups Learn With | SoftwareTechIT (English)
0 Comments