Python
-
Upload
vivek-pandey -
Category
Documents
-
view
208 -
download
0
Transcript of Python
OPERATING SYSTEMS LABORATORY RECORD
2. Write a program that displays all three digit numbers that are equal to the sum of the cubes of their digits. These are called Armstrong numbers.
AIM:
ALGORITHM:
CODE:
i = 100while i < 1000: n = i a = n % 10 n = n / 10 b = n % 10 n = n / 10 c = n % 10 n = n / 10 if a ** 3 + b ** 3 + c ** 3 == i: print i i = i + 1
OUTPUT:
153370371
407
3. Write a program to accept a 4-digit number and display the progression to Kaprekar’s constant.
AIM:
ALGORITHM:
CODE:
n = input()
def swap(a,b):t = aa = bb = t
def kaprekar(n,count):
N = na = n % 10n = n / 10
b = n % 10n = n / 10
c = n % 10n = n / 10
d = n % 10n = n / 10
if(a == b and b == c and c == d):print " All digits are same"exit()
i = 1while i <= 5:
if a > b:t = aa = bb = t
if b > c:t = bb = cc = t
if c > d:t = cc = dd = t
i = i + 1
A = 0D = 0
A = 1000 * a + 100 * b + 10 * c + dD = 1000 * d + 100 * c + 10 * b + a
D = D - Aif D == N:
print count
else:kaprekar(D,count + 1)
count = 0kaprekar(n,0)
INPUT: 3524
OUTPUT:
5432 – 2345 = 30878730 – 0378 = 8352
8532 – 2358 = 6174
4. Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares. For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).
AIM:
ALGORITHM:
i = 32while i < 100: n = i * i Flag = True while Flag and n > 0: d = n % 10 if d % 2 == 1: Flag = False n = n / 10 if Flag is True: print i ** 2 i = i + 1
OUTPUT:
4624
6084
6400
8464
5. The aliquot of a number is defined as: the sum of the proper of the number.For example, the aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.Write a function that returns the aliquot number of a given number.
AIM:
ALGORITHM:
CODE:
n = input()
i = 1sum = 0
while i * i < n:if n % i == 0:
sum = sum + isum = sum + n / i
i = i + 1
if i * i == n:sum = sum + i
sum = sum - nprint sum
INPUT: 1234
OUTPUT: 620
6. A pair of numbers (a, b) is said to be amicable if the aliquot number of a is b and the aliquot number of b is a.
AIM:
ALGORITHM:
CODE:
def aliquot(n):sum = 0i = 1while i * i < n:
if n % i == 0:
sum = sum + isum = sum + n / i
i = i + 1
if i * i == n:sum = sum + i
sum = sum - nreturn sum
a = 1list = [0]empty = []i = 1
while a < 100000:list.append(aliquot(a))a = a + 1
i = 1while i < 100000:
if list[i] < 100000 and list[i] > 0 and i == list[list[i]] and i < list[i]:
print i,list[i]i = i + 1
OUTPUT:
220 284
1184 1210
2620 2924
5020 5564
6232 6368
10744 10856
12285 14595
17296 18416
63020 76084
66928 66992
67095 71145
69615 87633
79750 88730
7. Write a program to display the following pyramid. The number of lines has to be a parameterobtained from the user. The pyramid must appear aligned to the left edge of the screen.12 43 6 94 8 12 165 10 15 20 25
AIM:
ALGORITHM:
CODE:
n = input()
i = 1while i <= n:
list = []j = 1while j <= i:
list.append(i * j)j = j + 1
s = ''for k in list:
s = s + str(k) + ' 'print s
i = i + 1
OUTPUT:
12 43 6 94 8 12 165 10 15 20
8. Given a string of the form "4-7, 9, 12, 15" find the numbers missing in this list for a given range.
AIM:
ALGORITHM:
CODE:
s = raw_input()
list = s.split(',')final = []
print list
i = 0while i < len(list):
list[i] = list[i].strip()i = i + 1
for i in list:temp = itemp = temp.split('-')if len(temp) == 1:
final.append(int(temp[0]))else:
start = int(temp[0])end = int(temp[1])j = startwhile j <= end:
final.append(j)j = j + 1
INPUT: 1, 2-7, 10-12, 16-19, 18-24
OUTPUT: ['1', ' 2-7', ' 10-12', ' 16-19', ' 18-24']
9. Round Robin scheduling
AIM:
ALGORITHM:
CODE:
print " Enter total processes- "
n = input()
process = []sum = 0
print " Enter the burst times of processes- "
i = 0while i < n:
process.append([])a = input()process[i].append(a)sum = sum + ai = i + 1
print " Enter arrival times of processes one by one- "
i = 0while i < n:
a = input()process[i].append(a)process[i].append(i)i = i + 1
print " Enter time slice "s = input()
t = []i = 0while i < 100:
t.append(-1)i = i + 1
cur = 0idx = -1while len(process) > 0:
idx = (idx + 1) % len(process)while cur < process[idx][1]:
idx = (idx + 1) % len(process)m = min( process[idx][0], s )#print idx,mi = 0while i < m:
t[cur + i] = process[idx][2] + 1i = i + 1
cur = cur + mprocess[idx][0] = process[idx][0] - mif process[idx][0] == 0:
del process[idx]idx = (idx - 1)if idx < 0:
idx = len(process) - 1
cur = 0print "Time Process ID "while t[cur] != -1:
start = curidx = t[cur]while t[cur] == idx:
cur = cur + 1print start," - ",cur," ", idx
INPUT:
Enter total processors- 3
Enter burst times sone by one- 2 4 10
Enter arrival times one by one- 0 1 2
Enter time slice- 3
OUTPUT:
Time Process ID
0 - 2 1
2 - 5 2
5 - 8 3
8 - 9 2
9 - 16 3
10.SJF scheduling
AIM:
ALGORITHM:
CODE:print " Enter total processes- "
n = input()
process = []
print " Enter the burst times of processes- "
i = 0while i < n:
process.append([])a = input()process[i].append(a)i = i + 1
print " Enter arrival times of processes one by one- "
i = 0while i < n:
a = input()process[i].append(a)process[i].append(i)i = i + 1
t = []i = 0while i < 100:
t.append(-1)i = i + 1
process.sort()
i = 0
while i < n:idx = process[i][2]cur = process[i][1]burst = process[i][0]while burst > 0:
if t[cur] == -1:t[cur] = idx + 1burst = burst - 1
cur = cur + 1i = i + 1
cur = 0print "Time Process ID "while t[cur] != -1:
start = curidx = t[cur]while t[cur] == idx:
cur = cur + 1print start," - ",cur," ", idx
INPUT:
Enter total processes- 3
Enter the burst times of processes- 2 4 10
Enter arrival times of processes one by one- 0 1 2
OUTPUT:
Time Process ID
0 - 2 1
2 - 6 2
6 - 16 3
11.SRTF scheduling
AIM:
ALGORITHM:
CODE:print " Enter total processes- "
n = input()
process = []
print " Enter the burst times of processes- "
i = 0while i < n:
process.append([])a = input()process[i].append(a)i = i + 1
print " Enter arrival times of processes one by one- "
i = 0while i < n:
a = input()process[i].append(a)process[i].append(i)i = i + 1
t = []i = 0while i < 100:
t.append(-1)i = i + 1
process.sort()
i = 0while i < n:
idx = process[i][2]cur = process[i][1]burst = process[i][0]while burst > 0:
if t[cur] == -1:t[cur] = idx + 1burst = burst - 1
cur = cur + 1i = i + 1
cur = 0print "Time Process ID "while t[cur] != -1:
start = curidx = t[cur]while t[cur] == idx:
cur = cur + 1print start," - ",cur," ", idx
INPUT:
Enter total processes- 3
Enter the burst times of processes- 2 4 10
Enter arrival times of processes one by one- 0 1 2
OUTPUT:
Time Process ID
0 - 2 1
2 - 6 2
6 - 16 3
12.FIRST FIT MEMORY PARTITIONING
AIM:
ALGORITHM:
CODE:
print "Enter total partitions- "n = input()
p = []flag = []process = []M = []
print "Enter partition sizes one by one- "for i in range(n): a = input() p.append(a) flag.append(False)
print "Enter total processes- "N = input()
print "Enter process sizes one by one"for i in range(N): a = input() process.append(a)
i = 0while i < N: print process[i] i = i + 1
#First fit
i = 0while i < N: c = process[i] j = 0 while j < n: if p[j] >= c and flag[j] is False: flag[j] = True M[i] = j break j = j + 1 i = i + 1
i = 0while i < N: print "Process ", i, " allocated to partition of size ", p[M[i]] i = i + 1
INPUT:
Enter total partitions- 6
Enter partition sizes one by one- 400 300 200 500 600 600
Enter total processes- 5
Enter process sizes one by one- 212 314 545 416 276
OUTPUT:
First Fit:
Process 0 allocated to partition of size 400
Process 1 allocated to partition of size 500
Process 2 allocated to partition of size 600
Process 3 allocated to partition of size 600
Process 4 allocated to partition of size 300
Memory wasted in partition of size 400 is 188
Memory wasted in partition of size 300 is 24
Memory wasted in partition of size 200 is 200
Memory wasted in partition of size 500 is 186
Memory wasted in partition of size 600 is 55
Memory wasted in partition of size 600 is 184
13.BEST FIT MEMORY PARTITIONING
AIM:
ALGORITHM:
CODE:
print "Enter total partitions- "n = input()
p = []flag = []process = []
M = []Wastage = []
print "Enter partition sizes one by one- "for i in range(n): a = input() p.append(a) flag.append(False) Wastage.append(a)
print "Enter total processes- "N = input()
print "Enter process sizes one by one"for i in range(N): a = input() process.append(a) M.append(-1)
i = 0while i < n: print p[i] i = i + 1
print " "
i = 0while i < N: print process[i] i = i + 1
#Best fit
print " "print "Best Fit:"
i = 0while i < N: c = process[i] best = 1000000 M[i] = -1 j = 0 while j < n: if p[j] >= c and flag[j] is False and p[j] - c < best: M[i] = j best = p[j] - c j = j + 1 flag[M[i]] = True Wastage[M[i]] = p[M[i]] - c i = i + 1
i = 0while i < N: if M[i] == -1: print "Process ",i," not allocated to any partition " else:
print "Process ", i, " allocated to partition of size ", p[M[i]] i = i + 1
sum = 0print " "i = 0while i < n: print "Memory wasted in partition of size ", p[i], " is ", Wastage[i] sum = sum + Wastage[i] i = i + 1
print "\nTotal wastage: ", sum
INPUT:
Enter total partitions- 6
Enter partition sizes one by one- 400 300 200 500 600 600
Enter total processes- 5
Enter process sizes one by one- 212 314 545 416 276
OUTPUT:
Best Fit:
Process 0 allocated to partition of size 300
Process 1 allocated to partition of size 400
Process 2 allocated to partition of size 600
Process 3 allocated to partition of size 500
Process 4 allocated to partition of size 600
Memory wasted in partition of size 400 is 86
Memory wasted in partition of size 300 is 88
Memory wasted in partition of size 200 is 200
Memory wasted in partition of size 500 is 84
Memory wasted in partition of size 600 is 55
Memory wasted in partition of size 600 is 324
Total wastage: 837
14.WORST FIT MEMORY PARTITIONING
AIM:
ALGORITHM:
CODE:
print "Enter total partitions- "n = input()
p = []flag = []process = []M = []Wastage = []
print "Enter partition sizes one by one- "for i in range(n): a = input() p.append(a)
flag.append(False) Wastage.append(a)
print "Enter total processes- "N = input()
print "Enter process sizes one by one"for i in range(N): a = input() process.append(a) M.append(-1)
i = 0while i < n: print p[i] i = i + 1
print " "
i = 0while i < N: print process[i] i = i + 1
#Worst fit
print " "print "First Fit:"
i = 0while i < N: c = process[i] M[i] = -1 j = 0 worst = 0 while j < n: if flag[j] is False and p[j] > p[worst]: worst = j j = j + 1 if p[worst] >= c: M[i] = worst Wastage[worst] = p[worst] - c flag[worst] = True i = i + 1
i = 0while i < N: if M[i] == -1: print "Process ",i," not allocated to any partition " else: print "Process ", i, " allocated to partition of size ", p[M[i]] i = i + 1
sum = 0print " "i = 0while i < n: print "Memory wasted in partition of size ", p[i], " is ", Wastage[i] sum = sum + Wastage[i] i = i + 1
print "\nTotal wastage: ", sum
INPUT:
Enter total partitions- 6
Enter partition sizes one by one- 400 300 200 500 600 600
Enter total processes- 5
Enter process sizes one by one- 212 314 545 416 276
OUTPUT:
First Fit:
Process 0 allocated to partition of size 600
Process 1 allocated to partition of size 600
Process 2 not allocated to any partition
Process 3 allocated to partition of size 500
Process 4 allocated to partition of size 400
Memory wasted in partition of size 400 is 124
Memory wasted in partition of size 300 is 300
Memory wasted in partition of size 200 is 200
Memory wasted in partition of size 500 is 84
Memory wasted in partition of size 600 is 388
Memory wasted in partition of size 600 is 286
Total wastage: 1382