You are on page 1of 22

Name : Tushar Tomar Roll No.

: 2100270140060

ASSIGNMENT 1 (b)
Given a list A of numbers, you have to print those numbers which are not
multiples of 5.
def myCheck(num):

if (num%5)!=0:

print(num,end=’ ’)

list = [11,9,90,10,64,76,123,99,760,100,433,23]

print("From the given list of numbers,numbers which are not multiple of 5 ")

for i in list:

myCheck(i)

OUTPUT :
From the given list of numbers,numbers which are not multiple of 5
11 9 64 76 123 99 433 23

1
Name : Tushar Tomar Roll No. : 2100270140060

ASSIGNMENT 2 (a)
Illustrate lists and tuples functions
LISTS :

LI = [23,90,1,22]

print(id(LI))
print(type(LI))
print(len(LI))
LI[3]*=10
print(LI)
print(LI[-2])
print(LI[1:4:2])
print(LI[3:0:-1])
A=[32,22,33,7]
B=[66,55,88,45]
C=A+B
print(C)
C=A*2
print(C)
print(66 in B)
print(3 in A)
print(max(A))
print(min(A))
A.append(77)
print(A)
A.reverse()
print(A)
print(A.pop())
L1=list(('a',1,2))
print(L1)
L1.clear()
print(L1)
L1=[54,12,87,11]
print(L1)
L1.sort()
print(L1)

OUTPUT :

2895194936576
<class 'list'>
2
Name : Tushar Tomar Roll No. : 2100270140060

4
[23, 90, 1, 220]
1
[90, 220]
[220, 1, 90]
[32, 22, 33, 7, 66, 55, 88, 45]
[32, 22, 33, 7, 32, 22, 33, 7]
True
False
33
7
[32, 22, 33, 7, 77]
[77, 7, 33, 22, 32]
32
['a', 1, 2]
[]
[54, 12, 87, 11]
[11, 12, 54, 87]

3
Name : Tushar Tomar Roll No. : 2100270140060

TUPLES :
T=("A","B","C","D")
print(T)
print("Value in T[1]=",T[1])
print("Value in T[2]=",T[3])
print("Value in T[-2]=",T[-2])

T1=(1,2,3,4)
T2=('Tushar Tomar')

print(T1+T2)
print(len(T1))

T3=(T1,T2)
print(T3)

T4=print((' Tushar Tomar ',)*2)

T5=(2,3,4,5,3,4,7,8,2,8,9)

A=T5.count(2)
print(A)

OUTPUT :
('G', 'A', 'G', 'A', 'N')
Value in T[1]= A
Value in T[2]= A
Value in T[-2]= A
(1, 2, 3, 4, 'Tushar', 'Tomar')
4
((1, 2, 3, 4), ('Tushar', 'Tomar'))
('Tomar ', 'Tomar')
2

4
Name : Tushar Tomar Roll No. : 2100270140060

ASSIGNMENT 2 (b)
Given an integer number N, find whether it can be expressed as a sum of two
semi-primes or not (not necessarily distinct)
import math
number = int(input("Enter number : "))

def isprime(num):
for i in range(2,int(math.sqrt(num))+1):
if (num%i) == 0:
return False
return True

def is_semiPrime(num):
for i in range(2,num):
if num%i == 0:
if(isprime(i)) and isprime(num/i):
return True
else:
return False

count = 0
for i in range(4,number):
if(is_semiPrime(i) and is_semiPrime(number-i)):
count=count+1
break

if(count > 0):


print(number," is the sum of two Semi Prime numbers",i," and",number-i)
else:
print(number," is not the sum of two Semi Prime numbers")

OUTPUT :
Enter number : 43
43 is the sum of two Semi Prime numbers 4 and 39

5
Name : Tushar Tomar Roll No. : 2100270140060

ASSIGNMENT 3 (a)
Illustrate sets and dictionary functions
SETS :
x = {"Microsoft","Google","IBM"}
print(x)

x.add("Tushar Tomar ")


print(x)

y = x.copy()
print(y)

z = {"Google","Banana"}
print(z)

z1 = x.difference(z)
print(z1)

x.discard("IBM")
print(x)

z2 = x.intersection(z)
print(z2)

z3=x.intersection_update(z)
print(z3)

z4 = x.isdisjoint(z)
print(z4)

z5 = x.issubset(z)
print(z5)

z.pop()
print(z)
OUTPUT :
{'IBM', 'Google', 'Microsoft'}
{'IBM', 'NAME', 'Google', 'Microsoft'}
{'IBM', 'NAME', 'Google', 'Microsoft'}
{'Google', 'Apple'}
{'NAME', 'IBM', 'Microsoft'}
{'NAME', 'Google', 'Microsoft'}
{'Google'}
None
False

6
Name : Tushar Tomar Roll No. : 2100270140060

True
{'Apple'}

DICTIONARY :
st = {
"Name": " Tushar Tomar ",
"Roll no": "1",

x = st.items()
print(x)

x = st.keys()
print(x)

x = st.values()
print(x)

st.update({"college": "AKGEC"})
print(st)

st = ('b', 'k', 'p')


s=7

t_dict = dict.fromkeys(st, s)
print(t_dict)

OUTPUT :
dict_items([('Name', ' Tushar Tomar '), ('Roll no', '1')])
dict_keys(['Name', 'Roll no'])
dict_values([' Tushar Tomar ', '1'])
{'Name': ' Tushar Tomar ', 'Roll no': '1', 'college': 'AKGEC'}
{'b': 7, 'k': 7, 'p': 7}

7
Name : Tushar Tomar Roll No. : 2100270140060

ASSIGNMENT 3 (b)
Given a square matrix, you have to write a program to print it in a counter-
clockwise spiral form.
R=4
C=4

def counterClockspiral(m, n, arr) :


k = 0; l = 0

# k - starting row index


# m - ending row index
# l - starting column index
# n - ending column index
# i - iterator
cnt = 0

total = m * n

while (k < m and l < n) :


if (cnt == total) :
break

for i in range(k, m) :
print(arr[i][l], end = " ")
cnt += 1

l += 1

if (cnt == total) :
break

for i in range (l, n) :


print( arr[m - 1][i], end = " ")
cnt += 1

m -= 1

if (cnt == total) :
break

if (k < m) :
for i in range(m - 1, k - 1, -1) :
print(arr[i][n - 1], end = " ")
cnt += 1
n -= 1

8
Name : Tushar Tomar Roll No. : 2100270140060

if (cnt == total) :
break

if (l < n) :
for i in range(n - 1, l - 1, -1) :
print( arr[k][i], end = " ")
cnt += 1

k += 1

arr = [ [ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16 ] ]

counterClockspiral(R, C, arr)

OUTPUT :
1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7

9
Name : Tushar Tomar Roll No. : 2100270140060

ASSIGNMENT 4

Pandas:

Import pandas as pd
df = pd.read_csv("20191226-items.csv")

-df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 720 entries, 0 to 719
Data columns (total 10 columns):
# Column Non-Null Count Dtype
0 asin 720 non-null object
1 brand 716 non-null object
2 title 720 non-null object
3 url 720 non-null object
4 image 720 non-null object
5 rating 720 non-null float64
6 reviewUrl 720 non-null object
7 totalReviews 720 non-null int64
8 price 720 non-null float64
9 originalPrice 720 non-null float64
dtypes: float64(3), int64(1), object(6)
memory usage: 56.4+ KB

df.head()
asin brand ... price originalPrice
0 B0000SX2UC NaN ... 0.00 0.0
1 B0009N5L7K Motorola ... 49.95 0.0
2 B000SKTZ0S Motorola ... 99.99 0.0
3 B001AO4OUC Motorola ... 0.00 0.0
4 B001DCJAJG Motorola ... 149.99 0.0
[5 rows x 10 columns]

df.tail()
asin brand ... price originalPrice
715 B07ZPKZSSC Apple ... 949.00 0.00
716 B07ZQSGP53 Xiaomi ... 150.96 0.00
717 B081H6STQQ Sony ... 948.00 0.00
718 B081TJFVCJ Apple ... 478.97 0.00
719 B0825BB7SG Samsung ... 139.00 139.26

[5 rows x 10 columns]

df.index
RangeIndex(start=0, stop=720, step=1)

10
Name : Tushar Tomar Roll No. : 2100270140060

df.columns
Index(['asin', 'brand', 'title', 'url', 'image', 'rating', 'reviewUrl',
'totalReviews', 'price', 'originalPrice'],
dtype='object')

df.size
7200

df.shape
(720, 10)

df.ndim
2

df.at[2,'price']
99.99

df.iat[2,1]
'Motorola'

df.loc[:,'brand']
0 NaN
1 Motorola
2 Motorola
3 Motorola
4 Motorola
...
715 Apple
716 Xiaomi
717 Sony
718 Apple
719 Samsung
Name: brand, Length: 720, dtype: object

df.head(0)
Empty DataFrame
Columns: [asin, brand, title, url, image, rating, reviewUrl, totalReviews, price, originalPrice]
Index: []

df.head(1)
asin brand ... price originalPrice
0 B0000SX2UC NaN ... 0.0 0.0

[1 rows x 10 columns]

df.dtypes
asin object

11
Name : Tushar Tomar Roll No. : 2100270140060

brand object
title object
url object
image object
rating float64
reviewUrl object
totalReviews int64
price float64
originalPrice float64
dtype: object

df.describe()
rating totalReviews price originalPrice
count 720.000000 720.000000 720.000000 720.000000
mean 3.713750 105.679167 234.948931 63.690778
std 0.716014 167.601101 200.008441 172.405370
min 1.000000 1.000000 0.000000 0.000000
25% 3.300000 7.000000 97.512500 0.000000
50% 3.800000 32.000000 188.975000 0.000000
75% 4.100000 122.250000 336.725000 0.000000
max 5.000000 983.000000 999.990000 999.990000

numpy:

import numpy as np
arr = np.array([1, 32, 13, 54, 15,6])
print(arr)
print(type(arr))
print(arr.ndim)
print(arr[3])
print(arr[2:4])
print(arr[1:4:2])
print(arr.dtype)
x = arr.copy()
arr[0] = 42
print(arr)
print(x)
print(arr.shape)
newarr = arr.reshape(3, 2)
print(newarr)
x = np.where(arr == 4)

12
Name : Tushar Tomar Roll No. : 2100270140060

print(x)
print(np.sort(arr))

OUTPUT:
[ 1 32 13 54 15 6]
<class 'numpy.ndarray'>
1
54
[13 54]
[32 54]
int32
[42 32 13 54 15 6]
[ 1 32 13 54 15 6]
(6,)
[[42 32]
[13 54]
[15 6]]
(array([], dtype=int64),)
[ 6 13 15 32 42 54]

13
Name : Tushar Tomar Roll No. : 2100270140060

ASSIGNMENT 5(a)
capacity = (7,8,5)
x = capacity[0]
y = capacity[1]
z = capacity[2]
memory = {}
outcome = []
def getstates(state):
a = state[0]
b = state[1]
c = state[2]
if(a==6 and b==6):
outcome.append(state)
return True
if((a,b,c) in memory):
return False
memory[(a,b,c)] = 1
if(a&gt;0):
if(a+b&lt;=y):
if( getstates((0,a+b,c)) ):
outcome.append(state)
return True
else:
if( getstates((a-(y-b), y, c)) ):
outcome.append(state)
return True

if(a+c&lt;=z):

if( getstates((0,b,a+c)) ):
outcome.append(state)
return True
else:
if( getstates((a-(z-c), b, z)) ):
outcome.append(state)
return True

if(b&gt;0):

14
Name : Tushar Tomar Roll No. : 2100270140060

if(a+b&lt;=x):
if( getstates((a+b, 0, c)) ):
outcome.append(state)
return True
else:
if( getstates((x, b-(x-a), c)) ):
outcome.append(state)
return True

if(b+c&lt;=z):
if( getstates((a, 0, b+c)) ):
outcome.append(state)
return True
else:
if( getstates((a, b-(z-c), z)) ):
outcome.append(state)
return True

if(c&gt;0):

if(a+c&lt;=x):
if( getstates((a+c, b, 0)) ):
outcome.append(state)
return True
else:
if( getstates((x, b, c-(x-a))) ):
outcome.append(state)
return True

if(b+c&lt;=y):
if( getstates((a, b+c, 0)) ):
outcome.append(state)
return True
else:
if( getstates((a, y, c-(y-b))) ):
outcome.append(state)
return True

return False

15
Name : Tushar Tomar Roll No. : 2100270140060

initial_state = (7,0,0)
print(&quot;Starting work...\n&quot;)
getstates(initial_state)
outcome.reverse()
for i in outcome:
print(i)

16
Name : Tushar Tomar Roll No. : 2100270140060

ASSIGNMENT 5(b)
import random
board = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
player = 1

Win = 1
Draw = -1
Running = 0
Stop = 1
Game = Running
Mark = 'X'

def DrawBoard():
print(" %c | %c | %c " % (board[1],board[2],board[3]))
print("___|___|___")
print(" %c | %c | %c " % (board[4],board[5],board[6]))
print("___|___|___")
print(" %c | %c | %c " % (board[7],board[8],board[9]))
print(" | | ")

def CheckPosition(x):
if(board[x] == ' '):
return True
else:
return False

def CheckWin():
global Game
#Horizontal winning condition
if(board[1] == board[2] and board[2] == board[3] and board[1] != ' '):
Game = Win
elif(board[4] == board[5] and board[5] == board[6] and board[4] != ' '):
Game = Win
elif(board[7] == board[8] and board[8] == board[9] and board[7] != ' '):
Game = Win
elif(board[1] == board[4] and board[4] == board[7] and board[1] != ' '):
Game = Win
17
Name : Tushar Tomar Roll No. : 2100270140060

elif(board[2] == board[5] and board[5] == board[8] and board[2] != ' '):


Game = Win
elif(board[3] == board[6] and board[6] == board[9] and board[3] != ' '):
Game=Win
elif(board[1] == board[5] and board[5] == board[9] and board[5] != ' '):
Game = Win
elif(board[3] == board[5] and board[5] == board[7] and board[5] != ' '):
Game=Win
elif(board[1]!=' ' and board[2]!=' ' and board[3]!=' ' and board[4]!=' ' and
board[5]!=' ' and board[6]!=' ' and board[7]!=' ' and board[8]!=' ' and board[9]!
=' '):
Game=Draw
else:
Game=Running

print("Player 1 [X] --- Player 2 [O]\n")


while(Game == Running):
DrawBoard()
if(player % 2 != 0):
print("Player 1's chance")
Mark = 'X'
choice = int(input("Enter the position between [1-9] where you want to
mark : "))

else:
print("Computer chance")
Mark = 'O'
li= []
for i in range(1,len(board)):
if board[i] == ' ':
li.append(i)
choice = random.choice(li)
if(CheckPosition(choice)):
board[choice] = Mark
player+=1
CheckWin()

DrawBoard()
18
Name : Tushar Tomar Roll No. : 2100270140060

if(Game==Draw):
print("Game Draw")
elif(Game==Win):
player-=1
if(player%2!=0):
print("Player 1 Won")
else:
print("computer WON")

ASSIGNMENT 6(a)

19
Name : Tushar Tomar Roll No. : 2100270140060

num_nodes = int(input("Enter the number of nodes"))


graph = {}
li = []
for i in range(0,num_nodes):
print("Enter the number of links with node ",i,end = ' ')
link = int(input())
for j in range(0,link):
num = (input("Enter the node links with"))
li.append(num)
graph[str(i)] = li
li = []

visited = []
queue = []

def bfs(visited, graph, node):


visited.append(node)
queue.append(node)

while queue:
m = queue.pop(0)
print (m, end = " ")

for neig in graph[m]:


if neig not in visited:
visited.append(neig)
queue.append(neig)

print("BFS IS")
bfs(visited, graph, '0')

OUTPUT:

Enter the number of nodes4


20
Name : Tushar Tomar Roll No. : 2100270140060

Enter the number of links with node 03


Enter the node links with1
Enter the node links with2
Enter the node links with3
Enter the number of links with node 11
Enter the node links with3
Enter the number of links with node 21
Enter the node links with3
Enter the number of links with node 31
Enter the node links with1
BFS IS
0123

21
Name : Tushar Tomar Roll No. : 2100270140060

ASSIGNMENT 6(b)

num_nodes = int(input("Enter the number of nodes"))


graph = {}
li = []
for i in range(0,num_nodes):
print("Enter the number of links with node ",i,end = ' ')
link = int(input())
for j in range(0,link):
num = (input("Enter the node links with"))
li.append(num)
graph[str(i)] = li
li = []
visited = list()
def dfs(visited, graph, node):
if node not in visited:
print (node,end=' ')
visited.append(node)
for neig in graph[node]:
dfs(visited, graph, neig)
print("DFS IS")
dfs(visited, graph, '0')

OUTPUT:
Enter the number of nodes4
Enter the number of links with node 03
Enter the node links with1
Enter the node links with2
Enter the node links with3
Enter the number of links with node 11
Enter the node links with3
Enter the number of links with node 21
Enter the node links with3
Enter the number of links with node 31
Enter the node links with0
DFS IS
0132

22

You might also like