“””States state_abbreviation self.state_population = state_population self.region = region

“””States class provides the getter and setters of the declared variable provided in def _init_”””List = sortName = Falseclass State: def __init__(self, name, city, state_abbreviation, state_population, region, US_house_seats): self.name = name self.city = city self.state_abbreviation = state_abbreviation self.state_population = state_population self.region = region self.US_house_seats = US_house_seats#Name def get_name(self): return self.name def set_name(self, name): self.name = name#City def get_city(self): return self.city def set_city(self, city): self.city = city#State Abbreviation def get_state_abbreviation(self): return self.state_abbreviation def set_state_abbreviation(self, state_abbreviation): self.state_abbreviation = state_abbreviation#State Population def get_state_population(self): return str(self.state_population) def set_state_population(self, state_population): self.state_population = state_population#Region def get_region(self): return self.region def set_name(self, region): self.region = region#US House Seats def get_US_house_seats(self): return (self.US_house_seats).strip(‘
‘) def set_US_house_seats(self, US_house_seats): self.US_house_seats = US_house_seats# Organizing the given variables into there correct location def __str__(self): print(usState.get_name() + ” ” + usState.get_city() + ” ” + usState.get_state_abbreviation() + ” ” + str( usState.get_state_population()) + ” ” + usState.get_region() + ” ” + usState.get_US_house_seats())########################################################################################”””Reads the file provided and creates a global variablecalled List to keep track of the data with each change made.”””def readFile(): filename = raw_input(“Enter the file name: “) file = open(filename) content_list = file.readlines() file.close() global List for fileEntry in content_list1:: if fileEntry.rstrip() != “” and fileEntry.rstrip() is not None: data = fileEntry.split(“,”) List.append(State(data0, data1, data2, int(data3), data4, data5))############################################################################################”””Creates cases for each variable to better organize the data provided in the file.”””def printRecords(): #State Name name_width = max(len(state.get_name()) for state in List) + 2 if name_width < (len("State name") + 2): name_width = len("State name") + 2 city_width = max(len(state.get_city()) for state in List) + 2 #Capital City if city_width < (len("Capital City") + 2): city_width = len("Capital City") + 2 stAbbr_width = max(len(state.get_state_abbreviation()) for state in List) + 2 #State Abbreviation if stAbbr_width < (len("State Abbr") + 2): stAbbr_width = len("State Abbr") + 2 stPop_width = max(len(str(state.get_state_population())) for state in List) + 2 #State Population if stPop_width < (len("State Population") + 2): stPop_width = len("State Population") + 2 region_width = max(len(state.get_region()) for state in List) + 2 #Region if region_width < (len("Region") + 2): region_width = len("Region") + 2 house_width = max(len(state.get_US_house_seats()) for state in List) + 2 #US house Seats if house_width < (len("US House Seats") + 2): house_width = len("US House Seats") + 2 # Print in sequential order print "".join("State name".ljust(name_width)) + "".join("Capital City".ljust(city_width)) + "".join( "State Abbr".ljust(stAbbr_width)) + "".join("State Population".ljust(stPop_width)) + "".join( "Region".ljust(region_width)) + "".join("US House Seats".ljust(house_width)) #Creating spacing and padding between variables print "-" * (name_width + city_width + stAbbr_width + stPop_width + region_width + house_width) for usState in List: print "".join(usState.get_name().ljust(name_width)) + "".join(usState.get_city().ljust(city_width)) + "".join( usState.get_state_abbreviation().ljust(stAbbr_width)) + "".join( usState.get_state_population().ljust(stPop_width)) + "".join(usState.get_region().ljust(region_width)) + "".join( usState.get_US_house_seats().ljust(house_width))##########################################################################################################################"""Sorts array depending on the index of the elementThe function helps quick sort organize the data into and array. """def partition(arr, low, high): i = (low - 1) # index of smaller element pivot = arrhigh.get_name() # pivot for j in range(low, high): # If current element is smaller than or equal to pivot if arrj.get_name() <= pivot: # increment index of smaller element i = i + 1 arri, arrj = arrj, arri arri + 1, arrhigh = arrhigh, arri + 1 return (i + 1)#########################################################################################################"""Function picks an element as pivot and partitions the given array around the picked pivot."""def quickSort(array, low, high): if low < high: pivot = partition(array, low, high) # Separately sort elements before and after partition quickSort(array, low, pivot - 1) quickSort(array, pivot + 1, high)#####################################################################################################"""Function sorts by name usingmethods from quickSort function"""def sortByName(): quickSort(List, 0, len(List) - 1) global sortName sortName = True print "List is sorted by name: " print"************************"######################################################################################################"""Function is a linear time sorting algorithm that sorts in O(n+k)time when elements are in range from 1 to k."""def countingSort(array, exp1): n = len(array) # The output array elements that will be in a sorted array output = 0 * (n) # initialize count array as 0 count = 0 * (10) # Store count of occurrences in count for i in range(0, n): index = (arrayi.state_population / exp1) count(index) % 10 += 1 # Change counti to contain actual # position of this digit in output array for i in range(1, 10): counti += counti - 1 # Build the output array i = n - 1 while i >= 0: index = (arrayi.state_population / exp1) outputcount(index) % 10 – 1 = arrayi count(index) % 10 -= 1 i -= 1 # Copying the output array to arr, # so that arr now contains sorted numbers i = 0 for i in range(0, len(array)): arrayi = outputi##################################################################################################”””Radix Sort uses counting sort as a subroutine to sort.The sorting starts by analyzing each element from least to greatest.”””def radixSort(array): # Find the maximum number max1 = max(state.state_population for state in array) # Performing counting sort # where i is current digit number exp = 1 while max1 / exp > 0: countingSort(array, exp) exp *= 10##########################################################################”””Using radixsort the function can place the elements from least to greatest depending on there population size”””def sortByPopulation(): radixSort(List) global sortName sortName = False print “List is sorted by Population”def binarySearch(array, l, r, x): #base case if r >= l: mid = l + (r – l) / 2 # If element is present at the middle itself if arraymid.name == x: return mid # If element is smaller than mid, then the element will be in left subarray elif arraymid.name > x: return binarySearch(array, l, mid – 1, x) # Else place the element in the right subarray else: return binarySearch(array, mid + 1, r, x) else: # Element is not present in the array return -1###################################################################################################”””Function has both binary search and sequential search Binary Search:Finds the position of value targeted within the arraySequential Search:finds targeted value within a list”””def searchByName(): search = raw_input(“Enter the state name: “) if sortName is True: print “Binary Search” index = binarySearch(List, 0, len(List) – 1, search) if index == -1: print “Error state not found!” else: print ” State name: ” + Listindex.get_name() + ”
Capital City: ” + List index.get_city() + ”
State Abbr: ” + Listindex.get_state_abbreviation() + ”
State Population ” + Listindex.get_state_population() + ”
Region: ” + Listindex.get_region() + ”
US House Seats:” + Listindex.get_US_house_seats() else: found = 0 for state in List: if state.get_name() == search: found = 1 print ” State name: ” + state.get_name() + ”
Capital City: ” + state.get_city() + ”
State Abbr: ” + state.get_state_abbreviation() + ”
State Population ” + state.get_state_population() + ”
Region: ” + state.get_region() + ”
US House Seats:” + state.get_US_house_seats() break if found == 0: print “Error state not found”############################################################################################”””Prompts the user with a menu and list of choices”””def printMenu(): print “CA94640/5605 Project 1” print “Instructor : Xudong Liu” print “Name: Erica Arellano” readFile() while (True): print ”
1. Print a state report
2. Sort by State name
3. Sort by Population
4. Find and print a given state
5. Quit” print ”
There were ” + str(len(List)) + ” state records read.” print ”
Enter your Choice: ” option = input() if option == 1: printRecords() elif option == 2: sortByName() elif option == 3: sortByPopulation() elif option == 4: searchByName() elif option == 5: break else: print “Invalid choice enter 1-5 “printMenu()