Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- File=[]
- Line=[]
- def readFile():
- with open("C:/Users/Saarangan/Documents/APExam.txt", "r") as in_file:
- next(in_file)
- for line in in_file:
- #This is getting rid of the white space.
- line=line.rstrip()
- Line=line.split(',')
- #A second list is created so I can change the strings to integers.
- Line2=Line[1:]
- del Line[1:]
- # This for loop is used to place the integers back into the original list in place of the strings.
- for i in Line2:
- Line.append(int(i))
- #This combines the lines together each as an element in a larger list
- File.append(Line)
- #use the split function to break each line into separate fields.
- return File
- readFile()
- #This function sorts the file through bubblesort. The parameter given determines what the list is being sorted by.
- def bubble_sort(x):
- length = len(File)-1
- sorted = False
- while not sorted:
- sorted = True
- for i in range(length):
- if File[i][x] > File[i+1][x]:
- sorted = False
- File[i], File[i+1] = File[i+1], File[i]
- bubble_sort(1)
- #This funtion runes the bubble sort and then prints out the 5 highest and 5 lowest places for students taking the test.
- def Question1(x):
- bubble_sort(x)
- print "Highest Number of Students Taking Test"
- for b in range(43,48):
- print File[b][0]
- print File[b][1]
- print "Lowest Number of Students Taking Test"
- for a in range(0,5):
- print File[a][0]
- print File[a][1]
- Question1(1)
- #This function finds which states has 0 female participation.
- def file_search(File,y):
- for i in range(len(File)):
- if File[i][3]==y:
- return File[i][0]
- #This function runs the bubble sort then prints out the top 3 places with the highest femal percentage.
- def Question2(x):
- bubble_sort(x)
- print "Highest Percentage of Females Taking Test"
- for c in range(45,48):
- print File[c][0]
- print File[c][3]
- print "This state had zero female participation."
- print file_search(File,0)
- Question2(3)
- #Question3
- #This function finds a state with a higher percentage than the one inputted and returns it.
- def percentage_search(x):
- if 0<=x<=100:
- for i in range(len(File)):
- if File[i][3]>x:
- print ("This state had higher than " + str(percent) + "% female participants.")
- return File[i][0]
- if 0>x:
- print "Error, next time please input a number between 0 and 100."
- return ' '
- if 100<x:
- print "Error, next time please input a number between 0 and 100."
- return ' '
- percent = input("Please input a percentage.")
- print percentage_search(percent)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement