Skip to content

Commit

Permalink
update csv and program with column headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzeysinay committed Apr 11, 2024
1 parent e88314a commit 0fdd16e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions data.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CITY_ID,POPULATION,LONGITUDE,LATITUDE
1,2201670,36.985420,35.325020
2,610484,37.762990,38.277298
3,714523,38.756217,30.537846
Expand Down
21 changes: 11 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,37 @@

def read_csv_data(csv_file):

data = []
with open(csv_file, mode='r') as file:
csv_reader = csv.reader(file, quoting=csv.QUOTE_NONNUMERIC)
data = list(csv_reader)
csv_reader = csv.DictReader(file)
for row in csv_reader:
data.append({key: float(value) for key, value in row.items()})
return data

def calculate_latitude(data, total_population):

latitude = 0
for row in data:
latitude += (row[1] * row[2] / total_population)
latitude += (row['POPULATION'] * row['LATITUDE'] / total_population)
return latitude

def calculate_longitude(data, total_population):

longitude = 0
for row in data:
longitude += (row[1] * row[3] / total_population)
longitude += (row['POPULATION'] * row['LONGITUDE'] / total_population)
return longitude

def main():
input_csv_file = "data.csv"

data = read_csv_data(input_csv_file)

total_population = sum(row[1] for row in data)

latitude = calculate_latitude(data, total_population)
print("Latitude:", latitude)
total_population = sum(row['POPULATION'] for row in data)

longitude = calculate_longitude(data, total_population)
print("Longitude:", longitude)
latitude = calculate_latitude(data, total_population)
print("Latitude:", latitude)

main()

0 comments on commit 0fdd16e

Please sign in to comment.