SlideShare uma empresa Scribd logo
1 de 5
Baixar para ler offline
Can you fix this code? It should also work with any data file.
The data file is:
4
4
1
0
3
4
5
6
7
8
9
10
11
12
13
14
15
16
The expected outcome should look like this:
Sim City land values:
1 0 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Calculated Sim City land values:
1 4 3 4
5 6 7 8
9 10 11 12
13 14 15 16
But instead looks like this:
Sim City land values:
4
4
1
0
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Calculated Sim City land values:
4
4
1
0
3
4
5
6
7
8
9
10
11
12
13
14
15
16
My code:
import copy
def create_grid(filename: str) -> list[list[int]]:
"""
Create a grid of land values from a file
"""
with open(filename) as file:
lines = file.readlines()
grid = [[int(value) for value in line.strip().split()] for line in lines]
return grid
def display_grid(grid: list[list[int]]) -> None:
"""
Display a grid of land values
"""
for row in grid:
for value in row:
print(f"{value:8}", end="")
print()
def find_neighbor_values(grid: list[list[int]], row: int, col: int) -> list[int]:
"""
Find the neighbors of a cell
"""
neighbors = []
num_rows = len(grid)
num_cols = len(grid[0])
# Check cells to the left and right
for c in range(col-1, col+2):
if c >= 0 and c < num_cols and c != col:
neighbors.append(grid[row][c])
# Check cells above and below
for r in range(row-1, row+2):
if r >= 0 and r < num_rows and r != row:
neighbors.append(grid[r][col])
return neighbors
from copy import deepcopy
def fill_gaps(grid: list[list[int]]) -> list[list[int]]:
"""
Fill the gaps in the grid
Creates a new grid with the same dimensions as the original grid
Calls find_neighbor_values() to find the neighbors of each cell
Do NOT modify the original grid!
"""
new_grid = deepcopy(grid)
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == -1:
neighbors = find_neighbor_values(grid, i, j)
total = 0
count = 0
for value in neighbors:
if value != -1:
total += value
count += 1
if count > 0:
new_grid[i][j] = total // count
return new_grid
def main() -> None:
"""
Main program.
"""
grid = create_grid("data_0.txt")
print("Sim City land values:")
display_grid(grid)
print("nCalculated Sim City land values:")
new_grid = fill_gaps(grid)
display_grid(new_grid)
if __name__ == "__main__":
main()

Mais conteúdo relacionado

Semelhante a Can you fix this code It should also work with any data file. The.pdf

SATySFiのこれからの課題たち
SATySFiのこれからの課題たちSATySFiのこれからの課題たち
SATySFiのこれからの課題たちT. Suwa
 
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docxchristiandean12115
 
100 c interview questions answers
100 c interview questions answers100 c interview questions answers
100 c interview questions answersSareen Kumar
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using PythonNishantKumar1179
 
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfCountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfAggarwalelectronic18
 
How to generate a 100+ page website using parameterisation in R
How to generate a 100+ page website using parameterisation in RHow to generate a 100+ page website using parameterisation in R
How to generate a 100+ page website using parameterisation in RPaul Bradshaw
 
Introduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordIntroduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordLakshmi Sarvani Videla
 
Python Cheat Sheet 2.0.pdf
Python Cheat Sheet 2.0.pdfPython Cheat Sheet 2.0.pdf
Python Cheat Sheet 2.0.pdfRahul Jain
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentationManchireddy Reddy
 
Ee693 sept2014midsem
Ee693 sept2014midsemEe693 sept2014midsem
Ee693 sept2014midsemGopi Saiteja
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxrohinitalekar1
 
openCypher Technology Compatibility Kit (TCK)
openCypher Technology Compatibility Kit (TCK)openCypher Technology Compatibility Kit (TCK)
openCypher Technology Compatibility Kit (TCK)openCypher
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdfMariappanR3
 

Semelhante a Can you fix this code It should also work with any data file. The.pdf (20)

R Functions in Dataframe.pptx
R Functions in Dataframe.pptxR Functions in Dataframe.pptx
R Functions in Dataframe.pptx
 
SATySFiのこれからの課題たち
SATySFiのこれからの課題たちSATySFiのこれからの課題たち
SATySFiのこれからの課題たち
 
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
100 c interview questions answers
100 c interview questions answers100 c interview questions answers
100 c interview questions answers
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfCountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
 
How to generate a 100+ page website using parameterisation in R
How to generate a 100+ page website using parameterisation in RHow to generate a 100+ page website using parameterisation in R
How to generate a 100+ page website using parameterisation in R
 
Bcsl 033 solve assignment
Bcsl 033 solve assignmentBcsl 033 solve assignment
Bcsl 033 solve assignment
 
Introduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordIntroduction to Data Science With R Lab Record
Introduction to Data Science With R Lab Record
 
Python Cheat Sheet 2.0.pdf
Python Cheat Sheet 2.0.pdfPython Cheat Sheet 2.0.pdf
Python Cheat Sheet 2.0.pdf
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentation
 
Doc 20180130-wa0006
Doc 20180130-wa0006Doc 20180130-wa0006
Doc 20180130-wa0006
 
Data import-cheatsheet
Data import-cheatsheetData import-cheatsheet
Data import-cheatsheet
 
Ee693 sept2014midsem
Ee693 sept2014midsemEe693 sept2014midsem
Ee693 sept2014midsem
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
1.2 matlab numerical data
1.2  matlab numerical data1.2  matlab numerical data
1.2 matlab numerical data
 
Computer Science Assignment Help
Computer Science Assignment Help Computer Science Assignment Help
Computer Science Assignment Help
 
openCypher Technology Compatibility Kit (TCK)
openCypher Technology Compatibility Kit (TCK)openCypher Technology Compatibility Kit (TCK)
openCypher Technology Compatibility Kit (TCK)
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
 

Mais de sales88

Caso de empresa Hamburguesa In-N-Out el valor del cliente a la an.pdf
Caso de empresa Hamburguesa In-N-Out el valor del cliente a la an.pdfCaso de empresa Hamburguesa In-N-Out el valor del cliente a la an.pdf
Caso de empresa Hamburguesa In-N-Out el valor del cliente a la an.pdfsales88
 
CASO 6.2 Algunos ciudadanos preocupados hicieron una cita para reu.pdf
CASO 6.2 Algunos ciudadanos preocupados hicieron una cita para reu.pdfCASO 6.2 Algunos ciudadanos preocupados hicieron una cita para reu.pdf
CASO 6.2 Algunos ciudadanos preocupados hicieron una cita para reu.pdfsales88
 
Caso de EstudioLiderazgo y Gesti�nLaura es directora asocia.pdf
Caso de EstudioLiderazgo y Gesti�nLaura es directora asocia.pdfCaso de EstudioLiderazgo y Gesti�nLaura es directora asocia.pdf
Caso de EstudioLiderazgo y Gesti�nLaura es directora asocia.pdfsales88
 
Caso de estudio Hacer que las alianzas estrat�gicas y las redes f.pdf
Caso de estudio Hacer que las alianzas estrat�gicas y las redes f.pdfCaso de estudio Hacer que las alianzas estrat�gicas y las redes f.pdf
Caso de estudio Hacer que las alianzas estrat�gicas y las redes f.pdfsales88
 
CASO DE ESTUDIO La cl�nica Mayo es uno de los nombres m�s respetad.pdf
CASO DE ESTUDIO La cl�nica Mayo es uno de los nombres m�s respetad.pdfCASO DE ESTUDIO La cl�nica Mayo es uno de los nombres m�s respetad.pdf
CASO DE ESTUDIO La cl�nica Mayo es uno de los nombres m�s respetad.pdfsales88
 
CASO DE ESTUDIO La �pera de Sydney es uno de los edificios ic�nico.pdf
CASO DE ESTUDIO La �pera de Sydney es uno de los edificios ic�nico.pdfCASO DE ESTUDIO La �pera de Sydney es uno de los edificios ic�nico.pdf
CASO DE ESTUDIO La �pera de Sydney es uno de los edificios ic�nico.pdfsales88
 
Caso de estudio Despu�s de luchar con la deuda y la fuerte compete.pdf
Caso de estudio Despu�s de luchar con la deuda y la fuerte compete.pdfCaso de estudio Despu�s de luchar con la deuda y la fuerte compete.pdf
Caso de estudio Despu�s de luchar con la deuda y la fuerte compete.pdfsales88
 
Caso cl�nicoSDRAPregunta 1.Dados sus s�ntomas de fatiga, disne.pdf
Caso cl�nicoSDRAPregunta 1.Dados sus s�ntomas de fatiga, disne.pdfCaso cl�nicoSDRAPregunta 1.Dados sus s�ntomas de fatiga, disne.pdf
Caso cl�nicoSDRAPregunta 1.Dados sus s�ntomas de fatiga, disne.pdfsales88
 
Caso 21-3 Orden de prueba de deterioro Five Star Hotel Corporati.pdf
Caso 21-3 Orden de prueba de deterioro Five Star Hotel Corporati.pdfCaso 21-3 Orden de prueba de deterioro Five Star Hotel Corporati.pdf
Caso 21-3 Orden de prueba de deterioro Five Star Hotel Corporati.pdfsales88
 
Caso 6.2 Seguro Mar�timo Cl�usula Inchmaree Un barco pesquero c.pdf
Caso 6.2 Seguro Mar�timo Cl�usula Inchmaree Un barco pesquero c.pdfCaso 6.2 Seguro Mar�timo Cl�usula Inchmaree Un barco pesquero c.pdf
Caso 6.2 Seguro Mar�timo Cl�usula Inchmaree Un barco pesquero c.pdfsales88
 
Caso 3.1 Firma de contadores Moss y McAdams Bruce Palmer hab�a t.pdf
Caso 3.1 Firma de contadores Moss y McAdams Bruce Palmer hab�a t.pdfCaso 3.1 Firma de contadores Moss y McAdams Bruce Palmer hab�a t.pdf
Caso 3.1 Firma de contadores Moss y McAdams Bruce Palmer hab�a t.pdfsales88
 
Caso 2 (TV de Alta Definici�n La Gran Alianza) (1) Seg�n el caso .pdf
Caso 2 (TV de Alta Definici�n La Gran Alianza) (1) Seg�n el caso .pdfCaso 2 (TV de Alta Definici�n La Gran Alianza) (1) Seg�n el caso .pdf
Caso 2 (TV de Alta Definici�n La Gran Alianza) (1) Seg�n el caso .pdfsales88
 
Caso 1 (8 puntos) Miguel y Cinthia Leatch viven en Covington, Ten.pdf
Caso 1 (8 puntos) Miguel y Cinthia Leatch viven en Covington, Ten.pdfCaso 1 (8 puntos) Miguel y Cinthia Leatch viven en Covington, Ten.pdf
Caso 1 (8 puntos) Miguel y Cinthia Leatch viven en Covington, Ten.pdfsales88
 
Caso 1 Agmmaglobulinemia ligada al X 1. Bill fue testamento duran.pdf
Caso 1 Agmmaglobulinemia ligada al X 1. Bill fue testamento duran.pdfCaso 1 Agmmaglobulinemia ligada al X 1. Bill fue testamento duran.pdf
Caso 1 Agmmaglobulinemia ligada al X 1. Bill fue testamento duran.pdfsales88
 
Caso 1 Felipe R�os y Tiffany De Los Rios married filling jointl.pdf
Caso 1 Felipe R�os  y Tiffany De Los Rios married filling jointl.pdfCaso 1 Felipe R�os  y Tiffany De Los Rios married filling jointl.pdf
Caso 1 Felipe R�os y Tiffany De Los Rios married filling jointl.pdfsales88
 
Case studyData Protect and PrivacyHuman beings value their priva.pdf
Case studyData Protect and PrivacyHuman beings value their priva.pdfCase studyData Protect and PrivacyHuman beings value their priva.pdf
Case studyData Protect and PrivacyHuman beings value their priva.pdfsales88
 
CASE STUDY [30 Marks] Former Tongaat Hulett bosses in court for frau.pdf
CASE STUDY [30 Marks] Former Tongaat Hulett bosses in court for frau.pdfCASE STUDY [30 Marks] Former Tongaat Hulett bosses in court for frau.pdf
CASE STUDY [30 Marks] Former Tongaat Hulett bosses in court for frau.pdfsales88
 
case study Private Practice Implements Safeguards for Waiting .pdf
case study Private Practice Implements Safeguards for Waiting .pdfcase study Private Practice Implements Safeguards for Waiting .pdf
case study Private Practice Implements Safeguards for Waiting .pdfsales88
 
Case Study Liberty and the Elderly Patient Ronald is 71 years old..pdf
Case Study Liberty and the Elderly Patient Ronald is 71 years old..pdfCase Study Liberty and the Elderly Patient Ronald is 71 years old..pdf
Case Study Liberty and the Elderly Patient Ronald is 71 years old..pdfsales88
 
Case Study AMr. P tripped and broke her left hip while attempting.pdf
Case Study AMr. P tripped and broke her left hip while attempting.pdfCase Study AMr. P tripped and broke her left hip while attempting.pdf
Case Study AMr. P tripped and broke her left hip while attempting.pdfsales88
 

Mais de sales88 (20)

Caso de empresa Hamburguesa In-N-Out el valor del cliente a la an.pdf
Caso de empresa Hamburguesa In-N-Out el valor del cliente a la an.pdfCaso de empresa Hamburguesa In-N-Out el valor del cliente a la an.pdf
Caso de empresa Hamburguesa In-N-Out el valor del cliente a la an.pdf
 
CASO 6.2 Algunos ciudadanos preocupados hicieron una cita para reu.pdf
CASO 6.2 Algunos ciudadanos preocupados hicieron una cita para reu.pdfCASO 6.2 Algunos ciudadanos preocupados hicieron una cita para reu.pdf
CASO 6.2 Algunos ciudadanos preocupados hicieron una cita para reu.pdf
 
Caso de EstudioLiderazgo y Gesti�nLaura es directora asocia.pdf
Caso de EstudioLiderazgo y Gesti�nLaura es directora asocia.pdfCaso de EstudioLiderazgo y Gesti�nLaura es directora asocia.pdf
Caso de EstudioLiderazgo y Gesti�nLaura es directora asocia.pdf
 
Caso de estudio Hacer que las alianzas estrat�gicas y las redes f.pdf
Caso de estudio Hacer que las alianzas estrat�gicas y las redes f.pdfCaso de estudio Hacer que las alianzas estrat�gicas y las redes f.pdf
Caso de estudio Hacer que las alianzas estrat�gicas y las redes f.pdf
 
CASO DE ESTUDIO La cl�nica Mayo es uno de los nombres m�s respetad.pdf
CASO DE ESTUDIO La cl�nica Mayo es uno de los nombres m�s respetad.pdfCASO DE ESTUDIO La cl�nica Mayo es uno de los nombres m�s respetad.pdf
CASO DE ESTUDIO La cl�nica Mayo es uno de los nombres m�s respetad.pdf
 
CASO DE ESTUDIO La �pera de Sydney es uno de los edificios ic�nico.pdf
CASO DE ESTUDIO La �pera de Sydney es uno de los edificios ic�nico.pdfCASO DE ESTUDIO La �pera de Sydney es uno de los edificios ic�nico.pdf
CASO DE ESTUDIO La �pera de Sydney es uno de los edificios ic�nico.pdf
 
Caso de estudio Despu�s de luchar con la deuda y la fuerte compete.pdf
Caso de estudio Despu�s de luchar con la deuda y la fuerte compete.pdfCaso de estudio Despu�s de luchar con la deuda y la fuerte compete.pdf
Caso de estudio Despu�s de luchar con la deuda y la fuerte compete.pdf
 
Caso cl�nicoSDRAPregunta 1.Dados sus s�ntomas de fatiga, disne.pdf
Caso cl�nicoSDRAPregunta 1.Dados sus s�ntomas de fatiga, disne.pdfCaso cl�nicoSDRAPregunta 1.Dados sus s�ntomas de fatiga, disne.pdf
Caso cl�nicoSDRAPregunta 1.Dados sus s�ntomas de fatiga, disne.pdf
 
Caso 21-3 Orden de prueba de deterioro Five Star Hotel Corporati.pdf
Caso 21-3 Orden de prueba de deterioro Five Star Hotel Corporati.pdfCaso 21-3 Orden de prueba de deterioro Five Star Hotel Corporati.pdf
Caso 21-3 Orden de prueba de deterioro Five Star Hotel Corporati.pdf
 
Caso 6.2 Seguro Mar�timo Cl�usula Inchmaree Un barco pesquero c.pdf
Caso 6.2 Seguro Mar�timo Cl�usula Inchmaree Un barco pesquero c.pdfCaso 6.2 Seguro Mar�timo Cl�usula Inchmaree Un barco pesquero c.pdf
Caso 6.2 Seguro Mar�timo Cl�usula Inchmaree Un barco pesquero c.pdf
 
Caso 3.1 Firma de contadores Moss y McAdams Bruce Palmer hab�a t.pdf
Caso 3.1 Firma de contadores Moss y McAdams Bruce Palmer hab�a t.pdfCaso 3.1 Firma de contadores Moss y McAdams Bruce Palmer hab�a t.pdf
Caso 3.1 Firma de contadores Moss y McAdams Bruce Palmer hab�a t.pdf
 
Caso 2 (TV de Alta Definici�n La Gran Alianza) (1) Seg�n el caso .pdf
Caso 2 (TV de Alta Definici�n La Gran Alianza) (1) Seg�n el caso .pdfCaso 2 (TV de Alta Definici�n La Gran Alianza) (1) Seg�n el caso .pdf
Caso 2 (TV de Alta Definici�n La Gran Alianza) (1) Seg�n el caso .pdf
 
Caso 1 (8 puntos) Miguel y Cinthia Leatch viven en Covington, Ten.pdf
Caso 1 (8 puntos) Miguel y Cinthia Leatch viven en Covington, Ten.pdfCaso 1 (8 puntos) Miguel y Cinthia Leatch viven en Covington, Ten.pdf
Caso 1 (8 puntos) Miguel y Cinthia Leatch viven en Covington, Ten.pdf
 
Caso 1 Agmmaglobulinemia ligada al X 1. Bill fue testamento duran.pdf
Caso 1 Agmmaglobulinemia ligada al X 1. Bill fue testamento duran.pdfCaso 1 Agmmaglobulinemia ligada al X 1. Bill fue testamento duran.pdf
Caso 1 Agmmaglobulinemia ligada al X 1. Bill fue testamento duran.pdf
 
Caso 1 Felipe R�os y Tiffany De Los Rios married filling jointl.pdf
Caso 1 Felipe R�os  y Tiffany De Los Rios married filling jointl.pdfCaso 1 Felipe R�os  y Tiffany De Los Rios married filling jointl.pdf
Caso 1 Felipe R�os y Tiffany De Los Rios married filling jointl.pdf
 
Case studyData Protect and PrivacyHuman beings value their priva.pdf
Case studyData Protect and PrivacyHuman beings value their priva.pdfCase studyData Protect and PrivacyHuman beings value their priva.pdf
Case studyData Protect and PrivacyHuman beings value their priva.pdf
 
CASE STUDY [30 Marks] Former Tongaat Hulett bosses in court for frau.pdf
CASE STUDY [30 Marks] Former Tongaat Hulett bosses in court for frau.pdfCASE STUDY [30 Marks] Former Tongaat Hulett bosses in court for frau.pdf
CASE STUDY [30 Marks] Former Tongaat Hulett bosses in court for frau.pdf
 
case study Private Practice Implements Safeguards for Waiting .pdf
case study Private Practice Implements Safeguards for Waiting .pdfcase study Private Practice Implements Safeguards for Waiting .pdf
case study Private Practice Implements Safeguards for Waiting .pdf
 
Case Study Liberty and the Elderly Patient Ronald is 71 years old..pdf
Case Study Liberty and the Elderly Patient Ronald is 71 years old..pdfCase Study Liberty and the Elderly Patient Ronald is 71 years old..pdf
Case Study Liberty and the Elderly Patient Ronald is 71 years old..pdf
 
Case Study AMr. P tripped and broke her left hip while attempting.pdf
Case Study AMr. P tripped and broke her left hip while attempting.pdfCase Study AMr. P tripped and broke her left hip while attempting.pdf
Case Study AMr. P tripped and broke her left hip while attempting.pdf
 

Último

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Último (20)

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

Can you fix this code It should also work with any data file. The.pdf

  • 1. Can you fix this code? It should also work with any data file. The data file is: 4 4 1 0 3 4 5 6 7 8 9 10 11 12 13 14 15 16 The expected outcome should look like this: Sim City land values: 1 0 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Calculated Sim City land values: 1 4 3 4 5 6 7 8 9 10 11 12 13 14 15 16 But instead looks like this: Sim City land values: 4
  • 2. 4 1 0 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Calculated Sim City land values: 4 4 1 0 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  • 3. My code: import copy def create_grid(filename: str) -> list[list[int]]: """ Create a grid of land values from a file """ with open(filename) as file: lines = file.readlines() grid = [[int(value) for value in line.strip().split()] for line in lines] return grid def display_grid(grid: list[list[int]]) -> None: """ Display a grid of land values """ for row in grid: for value in row: print(f"{value:8}", end="") print() def find_neighbor_values(grid: list[list[int]], row: int, col: int) -> list[int]: """ Find the neighbors of a cell """ neighbors = [] num_rows = len(grid) num_cols = len(grid[0]) # Check cells to the left and right for c in range(col-1, col+2): if c >= 0 and c < num_cols and c != col: neighbors.append(grid[row][c]) # Check cells above and below for r in range(row-1, row+2): if r >= 0 and r < num_rows and r != row: neighbors.append(grid[r][col])
  • 4. return neighbors from copy import deepcopy def fill_gaps(grid: list[list[int]]) -> list[list[int]]: """ Fill the gaps in the grid Creates a new grid with the same dimensions as the original grid Calls find_neighbor_values() to find the neighbors of each cell Do NOT modify the original grid! """ new_grid = deepcopy(grid) for i in range(len(grid)): for j in range(len(grid[i])): if grid[i][j] == -1: neighbors = find_neighbor_values(grid, i, j) total = 0 count = 0 for value in neighbors: if value != -1: total += value count += 1 if count > 0: new_grid[i][j] = total // count return new_grid def main() -> None: """ Main program. """ grid = create_grid("data_0.txt") print("Sim City land values:") display_grid(grid) print("nCalculated Sim City land values:") new_grid = fill_gaps(grid) display_grid(new_grid)
  • 5. if __name__ == "__main__": main()