[Python Programming Basics to Advanced] : Nested List or 2D List | Lab 19

preview_player
Показать описание
This Python programming playlist is designed to take beginners with zero programming experience to an expert level. The course covers installation, basic syntax, practical scenarios, and efficient logic building. The course material includes PDF handouts, review questions, and covers a wide range of topics, from data types to advanced functions like Lambda and Recursive functions, Generators, and JSON data parsing.

In this lesson we will learn about the nested list or two-dimensional list. It is a list having another list as its element. There will be many tasks we will be doing in this lesson. The timestamps are given here:

Finding the Zodiac Sign 02:29
Creating Flat list from Nested list 07:32
Displaying the inner list having the maximum sum 10:17
Split a flat list every Nth element 13:01
Merge the corresponding inner lists of two nested lists 16:08
Multiple Students vs Multiple Subjects marks record 22:11
Tic Tac Toe Game 29:38
Game of Life 44:12

The codes created in the lesson:
Multiple Students vs Multiple Subjects marks record:

Tic Tac Toe Game:

Game of Life:

Complete Playlist:

If you have the basic programming knowledge and interested to learn Object-Oriented Programming in Python, check out this playlist:

Lab Manual 19 can be downloaded from here:

Review Questions:
Given at 44:02

#PythonProgramming #python #pythontutorial
Рекомендации по теме
Комментарии
Автор

#Review Question
## Game of life
import random
def displayGrid():
'This funtion should display the grid in 5x5 shape.'
print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')
print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')
print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}')
print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')
print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')
def neighbors(r, c):
row=list(range(max(0, r-1), min(r+2, 5)))
col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in row:
for j in col:
if (i, j)!=(r, c):
n.append((grid[i][j]))
return n
## Main Program ##
# print(chr(9650)) #Alive
# print(chr(9679)) #Dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
count=0
g=int(input('Enter no of next generations: '))
while(True):
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c)
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])
# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)

print(f'{count}')
displayGrid()
count+=1
if count==g:
break

nukhbaiqbal
Автор

Ans:
import random
import time
def displayGrid():
'This funtion should display the grid in 5x5 shape.'
print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')
print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')
print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}')
print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')
print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')
def neighbors(r, c):
row=list(range(max(0, r-1), min(r+2, 5)))
col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in row:
for j in col:
if (i, j)!=(r, c):
n.append((grid[i][j]))
return n
## Main Program ##
# print(chr(9650)) @/alive
# print(chr(9679)) @/dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
# displayGrid()

# Make 6 random cell alive
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
Gen=0
# Next Generation Calculations
for Gen in range(20):
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c) # Get the list of all neighbors
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])
# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(1)


print(f'Generation no. {Gen+1}')
displayGrid()

hamzaubaid
Автор

Review Question 1:
import random
import time
def displayGrid():
'This funtion should display the grid in 5x5 shape.'
print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')
print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')
print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}')
print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')
print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')
def neighbors(r, c):
row=list(range(max(0, r-1), min(r+2, 5)))
col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in row:
for j in col:
if (i, j)!=(r, c):
n.append((grid[i][j]))
return n
## Main Program ##
# print(chr(9650)) @/alive
# print(chr(9679)) @/dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
# displayGrid()

# Make 6 random cell alive
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
Gen=0
# Next Generation Calculations
for Gen in range(20):
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c) # Get the list of all neighbors
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])
# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(1)


print(f'Generation no. {Gen+1}')
displayGrid()

shahzaibaliafaq
Автор

ANSWER:

import random
import time
def displayGrid():
'This funtion should display the grid in 5x5 shape.'
print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')
print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')
print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}')
print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')
print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')
def neighbors(r, c):
row=list(range(max(0, r-1), min(r+2, 5)))
col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in row:
for j in col:
if (i, j)!=(r, c):
n.append((grid[i][j]))
return n


#print(chr(9650)) #Alive
#print(chr(9679)) #Dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
#displayGrid()



n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
Gen=0


for Gen in range(20):
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c) # Get the list of all neighbors

if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])

for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(1)


print(f'Generation no. {Gen+1}')
displayGrid()

laveezayasin
Автор

# Review Question:-

## Game of Life
import random, time
def displayGrid():
'This funtion should display the grid in 5x5 shape.'
for i in range(len(grid)):
for j in range(len(grid)):
if (j<(len(grid)-1)):
print(f'{grid[i][j]} ', end='')
else:
print(grid[i][j])

def neighbors(r, c):
#Approach 1
r1=r-1
r2=r+2
c1=c-1
c2=c+2
if (r1<0):
r1=0
if (r2>4):
r2=5
if (c1<0):
c1=0
if (c2>4):
c2=5
R=list(range(r1, r2))
C=list(range(c1, c2))

# Approach 2 (same but compact form)
# R=list(range(max(r-1, 0), min(r+2, 5)))
# C=list(range(max(c-1, 0), min(c+2, 5)))

# In Both Approaches r and c must be positive indices.
# In case of negative index like c=-1 in this scenario we first have to convert it to corresponding positive index ie:- c=4
# This I have done just after calculating c from the slot

n=[]
for i in R:
for j in C:
if (i, j)!=(r, c):
n.append(grid[i][j])
return n

## Main Program ##
# print(chr(9650)) #Alive
# print(chr(9679)) #Dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
# displayGrid()

# Make 6 random cell alive
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
# A little additional fix for neighbors(r, c) function
if (c==-1):
c=4
r=abs(i-1)//5
grid[r][c]=chr(9650)
print('Initial Grid')
displayGrid()

for count in range(20):
# Next Generation Calculations
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c) # Get the list of all neighbors
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])

# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(2)


print(f'Generation no {count+1}')
displayGrid()
print('The Program has ended.\n\nThank You!')

abdul-hadi
Автор

QUESTION:

import random
import time
def displayGrid():
'This funtion should display the grid in 5x5 shape.'
print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')
print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')
print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}')
print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')
print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')
def neighbors(r, c):
row=list(range(max(0, r-1), min(r+2, 5)))
col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in row:
for j in col:
if (i, j)!=(r, c):
n.append((grid[i][j]))
return n
## Main Program ##
# print(chr(9650)) @/alive
# print(chr(9679)) @/dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
# displayGrid()
# Make 6 random cell alive
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
Gen=0
# Next Generation Calculations
for Gen in range(20):
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c) # Get the list of all neighbors
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])
# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(1)

print(f'Generation no. {Gen+1}')
displayGrid()

areebaazhar
Автор

Task)
import random, time

def displayGrid():
'This funtion should display the grid in 5x5 shape.'
print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')
print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')
print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}')
print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')
print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')

def neighbors(r, c):
row=list(range(max(0, r-1), min(r+2, 5)))
col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in row:
for j in col:
if (i, j)!=(r, c):
n.append((grid[i][j]))
return n

## Main Program ##

grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
n=list(range(1, 26))
s=random.sample(n, 6)

for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
Gen=0

# Next Generation Calculations

for Gen in range(20):
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c)
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])

# Cell status update

for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:



print(f'Generation no. {Gen+1}')
displayGrid()

hishamawan
Автор

##Game of life



import random, time
def displayGrid():
a=0
for i in range(1, 6):
print(f'{grid[a][0]} {(grid[a][1])} {grid[a][2]} {grid[a][3]} {grid[a][4]}')
a+=1
def neighbors(r, c):
row=list(range(max(0, r-1), min(r+2, 5)))
col=list(range(max(0, c-1), min(c+2, 5)))
s=[]
for i in row:
for j in col:
if (i, j)!=(r, c):
s.append((grid[i][j]))
return s




#Main Program


# print(chr(9650)) #Alive
# print(chr(9679)) #Dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
for i in range(1, 20):
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c)
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])
# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(2)

displayGrid()

muhammad-ahmad
Автор

#REVIEW_QUESTION
## Game of Life
import random, time
def displayGrid():
'This funtion should display the grid in 5x5 shape.'
print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')
print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')
print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}')
print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')
print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')
def neighbors(r, c):
Row=list(range(max(0, r-1), min(r+2, 5)))
Col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in Row:
for j in Col:
if (i, j)!=(r, c):
n.append((grid[i][j]))
return n

## Main Program ##
# print(chr(9650)) #Alive
# print(chr(9679)) #Dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
# displayGrid()

# Make 6 random cell alive
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
print('INITIAL GRID:')
displayGrid()
for count in range(20):
# Next Generation Calculations
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c) # Get the list of all neighbors
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])

# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(1.5)


print(f'GENERATION # {count+1}:')
displayGrid()

print('PROGRAM HAS ENDED.')
print('THANK YOU!!!')

noorulhuda
Автор

#Review Question

import random

def displayGrid():

'This funtion should display the grid in 5x5 shape.'

print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')

print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')

print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}')

print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')

print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')

def neighbors(r, c):

row=list(range(max(0, r-1), min(r+2, 5)))

col=list(range(max(0, c-1), min(c+2, 5)))

n=[]

for i in row:

for j in col:

if (i, j)!=(r, c):

n.append((grid[i][j]))

return n

## Main Program ##

# print(chr(9650)) @/alive

# print(chr(9679)) @/dead

grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]

n=list(range(1, 26))

s=random.sample(n, 6)

for i in s:

c=(i%5)-1

r=abs(i-1)//5

grid[r][c]=chr(9650)

displayGrid()

count=0

g=int(input('Enter no of next generations: '))

while(True):

getDie=[]

getAlive=[]

for r in range(5):

for c in range(5):

n=neighbors(r, c)

# Conditions when cell will change the status

if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):

getDie.append([r, c])

elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):

getDie.append([r, c])

elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):

getAlive.append([r, c])

# Cell status update

for i in getDie:

grid[i[0]][i[1]]=chr(9679)

for i in getAlive:

grid[i[0]][i[1]]=chr(9650)



print(f'{count}')

displayGrid()

count+=1

if count==g:

break

zainulhassan
Автор

import random, time
def displayGrid():
'This funtion should display the grid in 5x5 shape.'
for x in range(len(grid)):
for y in range(len(grid)):
if(y<len(grid)-1):
print(f'{grid[x][y]} ', end='')
else:
print(grid[x][y])

def neighbors(r, c):
Row=list(range(max(0, r-1), min(r+2, 5)))
Col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in Row:
for j in Col:
if (i, j)!=(r, c):
n.append((grid[i][j]))
return n

## Main Program ##
# print(chr(9650)) #Alive
# print(chr(9679)) #Dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
# displayGrid()

# Make 6 random cell alive
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
for count in range(20):
# Next Generation Calculations
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c) # Get the list of all neighbors
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])

# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(1.5)


displayGrid()

amaanmajid
Автор

mport random, time
def displayGrid():
'This funtion should display the grid in 5x5 shape.'
for x in range(len(grid)):
for y in range(len(grid)):
if(y<len(grid)-1):
print(f'{grid[x][y]} ', end='')
else:
print(grid[x][y])

def neighbors(r, c):
Row=list(range(max(0, r-1), min(r+2, 5)))
Col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in Row:
for j in Col:
if (i, j)!=(r, c):
n.append((grid[i][j]))
return n

## Main Program ##
# print(chr(9650)) #Alive
# print(chr(9679)) #Dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
# displayGrid()

# Make 6 random cell alive
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
for count in range(20):
# Next Generation Calculations
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c) # Get the list of all neighbors
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])

# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(1.5)


displayGrid()

akaabdullahmateen
Автор

#Review_Question
#Game Of Life#

import random
def displayGrid():
'This function should display the grid in 5x5 shape.'
print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')
print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')
print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}')
print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')
print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')

def neighbors(r, c):
Row=list(range(max(0, r-1), min(r+2, 5)))
Col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in Row:
for j in Col:
if(i, j)!=(r, c):
n.append((grid[i][j]))
return n

## Main Program ##
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
# displayGrid()

n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
count=0
G=int(input("Enter the value of next generations: "))

while(True):
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c)

if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])
# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)

print(f'{count}')
displayGrid()
count+=1
if count==G:
break

talhakamboh
Автор

2019-MC-13
REVIEW QUESTION 1

import random
import time
def displayGrid():
'This funtion should display the grid in 5x5 shape.'
print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')
print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')
print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}')
print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')
print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')
def neighbors(r, c):
row=list(range(max(0, r-1), min(r+2, 5)))
col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in row:
for j in col:
if (i, j)!=(r, c):
n.append((grid[i][j]))
return n
## Main Program ##
# print(chr(9650)) #Alive
# print(chr(9679)) #Dead
per=eval(input('Enter no. of Genrations you want it to run for: '))
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
# displayGrid()

# Make 6 random cell alive
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
Gen=0
# Next Generation Calculations
for Gen in range(per):
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c) # Get the list of all neighbors
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])
# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(1)


print(f'Generation no. {Gen+1}')
displayGrid()

dotienv
Автор

#REVIEW QUESTION

#GAME OF LIFE

import random
def displayGrid():
'This funtion should display the grid in 5x5 shape.'
print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')
print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')
print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}')
print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')
print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')

def neighbors(r, c):
Row=list(range(max(0, r-1), min(r+2, 5)))
Col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in Row:
for j in Col:
if(i, j)!=(r, c):
n.append((grid[i][j]))
return n

## Main Program ##
# print(chr(9650)) #Alive
# print(chr(9679)) #Dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
# displayGrid()

# Make 6 random cell alive
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
count=0
G=int(input("Enter the number of Next Generations: "))
# Next Generation Calculations
while(True):
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c)
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])
# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)

print(f'{count}')
displayGrid()
count+=1
if count==G:
break
print("The code End.")
print("Thank You!")

rajahaseebahmad
Автор

@/review_question
## Game of Life

import random, time

def displayGrid():

'This funtion should display the grid in 5x5 shape.'

print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')

print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')

print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}')

print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')

print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')

def neighbors(r, c):

Row=list(range(max(0, r-1), min(r+2, 5)))

Col=list(range(max(0, c-1), min(c+2, 5)))

n=[]

for i in Row:

for j in Col:

if (i, j)!=(r, c):

n.append((grid[i][j]))

return n



## Main Program ##

# print(chr(9650)) @/alive

# print(chr(9679)) @/dead

grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]

# displayGrid()



# Make 6 random cell alive

n=list(range(1, 26))

s=random.sample(n, 6)

for i in s:

c=(i%5)-1

r=abs(i-1)//5

grid[r][c]=chr(9650)

print('INITIAL GRID:')

displayGrid()

for count in range(20):

# Next Generation Calculations

getDie=[]

getAlive=[]

for r in range(5):

for c in range(5):

n=neighbors(r, c) # Get the list of all neighbors

# Conditions when cell will change the status

if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):

getDie.append([r, c])

elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):

getDie.append([r, c])

elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):

getAlive.append([r, c])



# Cell status update

for i in getDie:

grid[i[0]][i[1]]=chr(9679)

for i in getAlive:

grid[i[0]][i[1]]=chr(9650)

time.sleep(1.5)





print(f'GENERATION # {count+1}:')

displayGrid()



print('PROGRAM HAS ENDED.')

print('THANK YOU!!!')

aliraza-zlft
Автор

Review Question 1:
import random
import time
def displayGrid():
'This funtion should display the grid in 5x5 shape.'
print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')
print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')
print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}')
print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')
print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')
def neighbors(r, c):
row=list(range(max(0, r-1), min(r+2, 5)))
col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in row:
for j in col:
if (i, j)!=(r, c):
n.append((grid[i][j]))
return n
## Main Program ##
# print(chr(9650)) #Alive
# print(chr(9679)) #Dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
# displayGrid()

# Make 6 random cell alive
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
Gen=0
# Next Generation Calculations
for Gen in range(20):
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c) # Get the list of all neighbors
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])
# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(1)


print(f'Generation no. {Gen+1}')
displayGrid()

ahmadlatif
Автор

# Review Question:-

## Game of Life
import random, time
def displayGrid():
'This funtion should display the grid in 5x5 shape.'
for i in range(len(grid)):
for j in range(len(grid)):
if (j<(len(grid)-1)):
print(f'{grid[i][j]} ', end='')
else:
print(grid[i][j])

def neighbors(r, c):
#Approach 1
r1=r-1
r2=r+2
c1=c-1
c2=c+2
if (r1<0):
r1=0
if (r2>4):
r2=5
if (c1<0):
c1=0
if (c2>4):
c2=5
R=list(range(r1, r2))
C=list(range(c1, c2))

# Approach 2 (same but compact form)
# R=list(range(max(r-1, 0), min(r+2, 5)))
# C=list(range(max(c-1, 0), min(c+2, 5)))

# In Both Approaches r and c must be positive indices.
# In case of negative index like c=-1 in this scenario we first have to convert it to corresponding positive index ie:- c=4
# This I have done just after calculating c from the slot

n=[]
for i in R:
for j in C:
if (i, j)!=(r, c):
n.append(grid[i][j])
return n

## Main Program ##
# print(chr(9650)) #Alive
# print(chr(9679)) #Dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
# displayGrid()

# Make 6 random cell alive
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
# A little additional fix for neighbors(r, c) function
if (c==-1):
c=4
r=abs(i-1)//5
grid[r][c]=chr(9650)
print('Initial Grid')
displayGrid()

for count in range(20):
# Next Generation Calculations
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c) # Get the list of all neighbors
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])

# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(2)


print(f'Generation no {count+1}')
displayGrid()
print('The Program has ended.\n\nThank You!')

mechalahore
Автор

#Review Question
## Game of life

import random, time
def displayGrid():

'This funtion should display the grid in 5x5 shape.'
print(f'{grid[0][0]} {grid[0][1]} {grid[0][2]} {grid[0][3]} {grid[0][4]}')
print(f'{grid[1][0]} {grid[1][1]} {grid[1][2]} {grid[1][3]} {grid[1][4]}')
print(f'{grid[2][0]} {grid[2][1]} {grid[2][2]} {grid[2][3]} {grid[2][4]}'
print(f'{grid[3][0]} {grid[3][1]} {grid[3][2]} {grid[3][3]} {grid[3][4]}')
print(f'{grid[4][0]} {grid[4][1]} {grid[4][2]} {grid[4][3]} {grid[4][4]}')
def neighbors(r, c):
Row=list(range(max(0, r-1), min(r+2, 5)))
Col=list(range(max(0, c-1), min(c+2, 5)))
n=[]
for i in Row:
for j in Col:
if (i, j)!=(r, c):
n.append((grid[i][j]))
return n
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
print('INITIAL GRID:')
displayGrid()
for count in range(20):
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c) # Get the list of all neighbors
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(1.5)

print(f'GENERATION # {count+1}:')
displayGrid()
print('Game end.')
print('thanku!!!')

irfanshehzada
Автор

##Game of life
import random, time
def displayGrid():
a=0
for i in range(1, 6):
print(f'{grid[a][0]} {(grid[a][1])} {grid[a][2]} {grid[a][3]} {grid[a][4]}')
a+=1
def neighbors(r, c):
row=list(range(max(0, r-1), min(r+2, 5)))
col=list(range(max(0, c-1), min(c+2, 5)))
s=[]
for i in row:
for j in col:
if (i, j)!=(r, c):
s.append((grid[i][j]))
return s
##Main Program
# print(chr(9650)) #Alive
# print(chr(9679)) #Dead
grid=a=[[chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5, [chr(9679)]*5]
n=list(range(1, 26))
s=random.sample(n, 6)
for i in s:
c=(i%5)-1
r=abs(i-1)//5
grid[r][c]=chr(9650)
displayGrid()
for i in range(1, 20):
getDie=[]
getAlive=[]
for r in range(5):
for c in range(5):
n=neighbors(r, c)
# Conditions when cell will change the status
if(grid[r][c]==chr(9650) and n.count(chr(9650))<2):
getDie.append([r, c])
elif(grid[r][c]==chr(9650) and n.count(chr(9650))>3):
getDie.append([r, c])
elif(grid[r][c]==chr(9679) and n.count(chr(9650))==3):
getAlive.append([r, c])
# Cell status update
for i in getDie:
grid[i[0]][i[1]]=chr(9679)
for i in getAlive:
grid[i[0]][i[1]]=chr(9650)
time.sleep(2)

displayGrid()

saifullahafzal