Write the below functions in two ways: One with split and another with the csv module.

In the media folder, there is another piece of census data that includes city information, state-city-populations.csv. The first few lines of this file look like this:

```
SUMLEV,STATE,COUNTY,PLACE,COUSUB,CONCIT,FUNCSTAT,NAME,STNAME,

CENSUS2010POP,ESTIMATESBASE2010,POPESTIMATE2010,POPESTIMATE2011,

POPESTIMATE2012,POPESTIMATE2013
040,01,000,00000,00000,00000,A,Alabama,Alabama,4779736,4779758,4785570,

4801627,4817528,4833722 162,01,000,00124,00000,00000,A,Abbeville
city,Alabama,2688,2688,2683,2690,2658,2651
162,01,000,00460,00000,00000,A,Adamsville
city,Alabama,4522,4522,4519,4496,4474,4448 1
```

1. Write the findCityPopulation function that returns the population in 2013 (last field) of an input city.
2. Write a function findLargestCityInState and findSmallestCityInState to find the city with the largest and the smallest population (respectively) within a given state.
Note: The findLargestCityInState functions are made more difficult as a row expressing the population of the state begins each section. Therefore, without checking for this an answer will likely find that the largest city in Alabama is Alabama. However it’s easy enough to avoid, as each state section has a first row value of 040. Additionally, since the list also contains towns and counties, to avoid getting one of those, its necessary to check that the potential city has the word “city” in it.
3. Use both your state and city functions to answer some questions. Find the state with the largest population, and the state with the smallest population. Is the smallest city in the largest state larger or smaller than the smallest city in the smallest state? Is the largest city in the largest state larger or smaller than the largest city in the smallest state?


1. split
```
def findCityPopulation (cityName):
file = open(getMediaPath("state-city-populations.csv"),"rt")
lines = file.readlines()
file.close()
for index in range(0, len(lines)):
parts = lines[index].split(",")
if parts[7] ==cityName:
return int(parts[14])
return -1
```

csv module
```
from csv import *
def findCityPopulation (cityName):
file = open(getMediaPath("state-city-populations.csv"),"rb")
csvfile = reader(file)
for row in csvfile:
if row[7] ==cityName:
return int(row[14])
return -1
```

2. split
```
def findLargestCityInState (state):
file = open(getMediaPath("state-city-populations.csv"),"rt")
lines = file.readlines()
file.close()
maxState = "None"
maxPopulation = 0
for index in range(0, len(lines)):
parts = lines[index].split(",")
if parts[8] ==state and not parts[0]=="040":
thisPopulation = int(parts[14])
if thisPopulation>maxPopulation and "city" in parts[7]:
maxPopulation = thisPopulation
maxState = parts[7]
return maxState
def findSmallestCityInState (state):
file = open(getMediaPath("state-city-populations.csv"),"rt")
lines = file.readlines()
file.close()
minState = "None"
minPopulation = 0
for index in range(0, len(lines)):
parts = lines[index].split(",")
if parts[8] ==state and not parts[0]=="040":
thisPopulation = int(parts[14])
if (minPopulation==0 or thisPopulation minState = parts[7]
return minState
```

csv module
```
from csv import *
def findLargestCityInState (state):
file = open(getMediaPath("state-city-populations.csv"),"rb")
csvfile = reader(file)
maxState = "None"
maxPopulation = 0
for row in csvfile:
if row[8] == state and not row[0]=="040" and row[14].isdigit():
thisPopulation = int(row[14])
if thisPopulation>maxPopulation and "city" in row[7]:
maxPopulation = thisPopulation
maxState = row[7]
return maxState

def findSmallestCityInState (state):
file = open(getMediaPath("state-city-populations.csv"),"rb")
csvfile = reader(file)
minState = "None"
minPopulation = 0
for row in csvfile:
if row[8] == state and not row[0]=="040" and row[14].isdigit():
thisPopulation = int(row[14])
if (minPopulation==0 or thisPopulation minPopulation = thisPopulation
minState = row[7]
return minState
```

3. Largest State Population: California
Smallest State Population: Wyoming
Smallest City in California: Vernon city
Largest City in Wyoming: Cheyenne city
Is Vernon city (1955) larger or smaller than Cheyenne city (62448): Much smaller
Largest City in California: Los Angeles city
Is Los Angeles city (3884307) largeror smaller than Cheyenne city (62448): Much Larger

Computer Science & Information Technology

You might also like to view...

Which MouseEvent method can be used to determine if the Alt key is pressed?

a. isAltPressed. b. isAltDown. c. getClickCount. d. AltPressed.

Computer Science & Information Technology

In large organizations, ____ know operating systems and networks as well as how to interpret the information gleaned by the examiners.

A. forensic analysts B. forensic examiners C. application programmers D. incident managers

Computer Science & Information Technology

An index can slow down the creation and deletion of records because the data must be added to or deleted from the index

Indicate whether the statement is true or false

Computer Science & Information Technology

One of the challenges in combating cyberterrorism is that many of the prime targets are not owned and managed by the federal government.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology