Week 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

WEEK-7

1. Write a python script to find a given number is perfect or not, using python lists and
functions.

Program:

a=int(input(" Enter a number:"))


lst=[]
count=0
i=1
def perfect():
global count
for i in range(1,a):
if(a%i==0):
count=count+i
lst.append(i)
else:
continue
perfect()

if(count==a):
print(a,"is Perfect number")
print(lst)
else:
print(a,"is not a Perfect number")
Output:

Testcase 1:

Enter a number:24
24 is not a Perfect number

Testcase 2:

Enter a number:496
496 is Perfect number
[1, 2, 4, 8, 16, 31, 62, 124, 248]

Testcase 3:

Enter a number:28
28 is Perfect number
[1, 2, 4, 7, 14]
2. Write a python script to convert a given decimal number into binary number and
store it into a list.

Program;

n=int(input("Enter the number:"))


lst=[]
x=0
a=n
while(a!=0):
x=a%2
lst.append(x)
a=a//2
lst.append(a)
lst.reverse()
print("The demimal number into a binary number:",lst)

Output:

Testcase 1;
Enter the number:8
The demimal number into a binary number: [0, 1, 0, 0, 0]

Testcase 2:
Enter the number:11
The demimal number into a binary number: [0, 1, 0, 1, 1]

Testcase 3:
Enter the number:2
The demimal number into a binary number: [0, 1, 0]
3. Write a python script to find the 2’s complement of a given binary number using
list.

Program:

n=int(input("Range of a binary:"))
lst=[]
for i in range(0,n):
lst.append(int(input("enter a bits:")))
print(lst)
for j in range(0,n):
if lst[j]==0:
lst[j]=1
elif lst[j]==1:
lst[j]=0
print("1's compliment:",lst)
l=len(lst)
for k in range(l-1,-1,-1):
if lst[k]==1:
lst[k]=0
elif lst[k]==0:
lst[k]=1
break
print("2's compliment:",lst)
Output:

Testcase 1:
Range of a binary:4
enter a bits:1
enter a bits:0
enter a bits:1
enter a bits:0
[1, 0, 1, 0]
1's compliment: [0, 1, 0, 1]
2's compliment: [0, 1, 1, 0]

Testcase 2:

Range of a binary:3
enter a bits:0
enter a bits:0
enter a bits:0
[0, 0, 0]
1's compliment: [1, 1, 1]
2's compliment: [0, 0, 1]
4. Write a python script to concatenate two lists and eliminate the duplicates in the
result.

Program:

a=[1,2,3,4]
b=[3,4,5,6,7,8]
c=[]
for i in a:
c.append(i)
for j in b:
if j not in a:
c.append(j)
c.sort()
print("Concatenate of two lists:",c)

Output:

Testcase 1:

Concatenate of two lists: [1, 2, 3, 4, 5, 6, 7, 8]


Write a python script to simulate ‘stack’ operations using list functions.

Programe:
stack=[]
flag="yes"
def push(i):
global stack
stack.append(i)
def display():
global stack
for i in range(len(stack)-1,-1,-1):
print(stack[i])

while flag=="yes":
print("\n____STACK OPERATIONS____\n\n\n1.PUSH\n2.POP\n3.DISPLAY")
ch=int(input("Enter a choice:"))
if ch==1:
n=int(input("Enter a value"))
push(n)
print("Item is inserted:")
flag=input("\nDo you want to coutinue (yes/no):")
elif ch==2:
stack.pop()
elif ch==3:
display()
else:
print("\nEnter a correct choise:")
Output:
____STACK OPERATIONS____

1.PUSH
2.POP
3.DISPLAY
Enter a choice:1
Enter a value1
Item is inserted:

Do you want to coutinue (yes/no):yes

____STACK OPERATIONS____

1.PUSH
2.POP
3.DISPLAY
Enter a choice:1
Enter a value2
Item is inserted:

Do you want to coutinue (yes/no):yes

____STACK OPERATIONS____

1.PUSH
2.POP
3.DISPLAY
Enter a choice:1
Enter a value3
Item is inserted:

Do you want to coutinue (yes/no):yes

____STACK OPERATIONS____

1.PUSH
2.POP
3.DISPLAY
Enter a choice:1
Enter a value4
Item is inserted:

Do you want to coutinue (yes/no):yes

____STACK OPERATIONS____

1.PUSH
2.POP
3.DISPLAY
Enter a choice:3
4
3
2
1

____STACK OPERATIONS____

1.PUSH
2.POP
3.DISPLAY
Enter a choice:2

____STACK OPERATIONS____

1.PUSH
2.POP
3.DISPLAY
Enter a choice:3
3
2
1
6. Write a python script to simulate queue operations using lists.

Programe:

que=[]
flag="yes"
def insert(i):
global que
que.append(i)
def display():
global que
for i in range(0,len(que)):
print(que[i])

while flag=="yes":
print("____QUE OPERATIONS____\n\n\n1.INSERT\n2.DELETE\n3.DISPLAY")
ch=int(input("Enter a choice:"))
if ch==1:
n=int(input("Enter a value:"))
insert(n)
print("Item is inserted:")
flag=input("Do you want to coutinue (yes/no):")
elif ch==2:
que.pop(0)
elif ch==3:
display()
else:
print("Enter a correct choise:")
Output:

____QUE OPERATIONS____

1.INSERT
2.DELETE
3.DISPLAY
Enter a choice:1
Enter a value:1
Item is inserted:
Do you want to coutinue (yes/no):yes
____QUE OPERATIONS____

1.INSERT
2.DELETE
3.DISPLAY
Enter a choice:1
Enter a value:2
Item is inserted:
Do you want to coutinue (yes/no):yes
____QUE OPERATIONS____

1.INSERT
2.DELETE
3.DISPLAY
Enter a choice:1
Enter a value:3
Item is inserted:
Do you want to coutinue (yes/no):yes
____QUE OPERATIONS____

1.INSERT
2.DELETE
3.DISPLAY
Enter a choice:1
Enter a value:4
Item is inserted:
Do you want to coutinue (yes/no):yes
____QUE OPERATIONS____

1.INSERT
2.DELETE
3.DISPLAY
Enter a choice:3
1
2
3
4
____QUE OPERATIONS____

1.INSERT
2.DELETE
3.DISPLAY
Enter a choice:2
____QUE OPERATIONS____

1.INSERT
2.DELETE
3.DISPLAY
Enter a choice:3
2
3
4

You might also like