Akshay Pythonlab 3-11

You might also like

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

In [1]:

#Program 1

list1=[3, 1, 7, 8, 4, 2]

maxl = minl = list1[0]


for num in list1:
if num > maxl:
maxl = num
if num < minl:
minl = num

print("Max Element : ", maxl)


print("Min Element : ", minl)

Max Element : 8
Min Element : 1

In [2]:
#Program 2

def removedup(list2):
if not list2:
print("Input list is empty!")
unique_list = []
for item in list2:
if item not in unique_list:
unique_list.append(item)
return unique_list

list2 = [2, 3, 5, 8, 2, 5, 9, 3]
result = removedup(list2)
print(result)

[2, 3, 5, 8, 9]

In [3]:
#Program 3

def maximize(prices):
min_price = prices[0]
max_profit = 0

for item in prices:


if item < min_price:
min_price = item
else:
profit = item - min_price
if profit > max_profit:
max_profit = profit
return max_profit

prices = [10, 20, 30]


max_profit = maximize(prices)
print("Maximum Profit = ", max_profit)

Maximum Profit = 20

In [8]:
#Program 4

def subset(list3, target):


def rsubset(c_index, c_subset, c_sum):
if c_sum == target:
subsets.append(c_subset[:])
return
if c_index == len(list3) or c_sum > target:
return
for i in range(c_index, len(list3)):
c_subset.append(list3[i])
rsubset(i + 1, c_subset, c_sum + list3[i])
c_subset.pop()

subsets = []
rsubset(0, [], 0)
return subsets

list3 = [2, 4, 6, 8, 16]


print(list3)
target = int(input("\nEnter the target sum: "))
result = subset(list3, target)
print(result)

[2, 4, 6, 8, 16]

Enter the target sum: 16


[[2, 6, 8], [16]]

In [86]:
#Program 5

list4 = [10, 2,30,40, 5, 6]


n = len(list4)
dp = [1] * n

for i in range(n):
for j in range(i):
if list4[i] > list4[j]:
dp[i] = max(dp[i], dp[j] + 1)

out =max(dp)
print("Length of the longest increasing subsequence = ",out)

Length of the longest increasing subsequence = 3

In [94]:
#Program 6

def color_chessboard(n, m):


chessboard = []
row = []
colors = ['B', 'W']

for i in range(n):
for j in range(m):
color = colors[(i + j) % 2]
row.append(color)
chessboard.append(row)
return chessboard

n = 4
m = 4

for row in chessboard:


print(" ".join(row))

B W B W
W B W B
B W B W
W B W B

In [25]:
# Program 7

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


print("\n",matrix[0],"\n",matrix[1],"\n",matrix[2] )

def spiral(matrix):
result = []
top = 0
left = 0
bottom = len(matrix) - 1
right = len(matrix[0]) - 1

while top <= bottom and left <= right:


for i in range(left, right + 1):
result.append(matrix[top][i])
top += 1

for i in range(top, bottom + 1):


result.append(matrix[i][right])
right -= 1

if top <= bottom:


for i in range(right, left - 1, -1):
result.append(matrix[bottom][i])
bottom -= 1

if left <= right:


for i in range(bottom, top - 1, -1):
result.append(matrix[i][left])
left += 1
return result

result = spiral(matrix)
print('')
print(result)

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

[1, 2, 3, 6, 9, 8, 7, 4, 5]

In [44]:
# Program 8

even = 0
odd = 0
num = [1, 2, 3, 4, 5, 6, 7]
for item in num:
if item % 2 == 0:
even = even + 1
else:
odd = odd + 1
print("Even numbers:", even)
print("Odd numbers:", odd)

Even numbers: 3
Odd numbers: 4

In [87]:
# Program 9

for i in range(1, 51):


if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz

In [40]:
# Program 10

age = float(input("Enter dog's age in human years : "))

if age <= 2:
newage = age * 10.5
else:
newage = 2 * 10.5 + (age - 2) * 4

print("Dog's age in dog years = ", newage)

Enter dog's age in human years : 5


Dog's age in dog years = 33.0

You might also like