Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1.  
  2. File=[]
  3. Line=[]
  4. def readFile():
  5. with open("C:/Users/Saarangan/Documents/APExam.txt", "r") as in_file:
  6. next(in_file)
  7. for line in in_file:
  8. #This is getting rid of the white space.
  9. line=line.rstrip()
  10. Line=line.split(',')
  11. #A second list is created so I can change the strings to integers.
  12. Line2=Line[1:]
  13. del Line[1:]
  14. # This for loop is used to place the integers back into the original list in place of the strings.
  15. for i in Line2:
  16. Line.append(int(i))
  17. #This combines the lines together each as an element in a larger list
  18. File.append(Line)
  19.  
  20. #use the split function to break each line into separate fields.
  21. return File
  22.  
  23. readFile()
  24.  
  25. #This function sorts the file through bubblesort. The parameter given determines what the list is being sorted by.
  26. def bubble_sort(x):
  27. length = len(File)-1
  28. sorted = False
  29.  
  30. while not sorted:
  31. sorted = True
  32. for i in range(length):
  33. if File[i][x] > File[i+1][x]:
  34. sorted = False
  35. File[i], File[i+1] = File[i+1], File[i]
  36.  
  37. bubble_sort(1)
  38. #This funtion runes the bubble sort and then prints out the 5 highest and 5 lowest places for students taking the test.
  39. def Question1(x):
  40.  
  41. bubble_sort(x)
  42.  
  43. print "Highest Number of Students Taking Test"
  44. for b in range(43,48):
  45. print File[b][0]
  46. print File[b][1]
  47. print "Lowest Number of Students Taking Test"
  48. for a in range(0,5):
  49. print File[a][0]
  50. print File[a][1]
  51.  
  52. Question1(1)
  53.  
  54. #This function finds which states has 0 female participation.
  55. def file_search(File,y):
  56. for i in range(len(File)):
  57. if File[i][3]==y:
  58. return File[i][0]
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. #This function runs the bubble sort then prints out the top 3 places with the highest femal percentage.
  66. def Question2(x):
  67.  
  68. bubble_sort(x)
  69. print "Highest Percentage of Females Taking Test"
  70. for c in range(45,48):
  71. print File[c][0]
  72. print File[c][3]
  73.  
  74. print "This state had zero female participation."
  75. print file_search(File,0)
  76.  
  77. Question2(3)
  78.  
  79.  
  80. #Question3
  81. #This function finds a state with a higher percentage than the one inputted and returns it.
  82. def percentage_search(x):
  83. if 0<=x<=100:
  84. for i in range(len(File)):
  85. if File[i][3]>x:
  86. print ("This state had higher than " + str(percent) + "% female participants.")
  87. return File[i][0]
  88.  
  89. if 0>x:
  90. print "Error, next time please input a number between 0 and 100."
  91. return ' '
  92. if 100<x:
  93. print "Error, next time please input a number between 0 and 100."
  94. return ' '
  95.  
  96.  
  97. percent = input("Please input a percentage.")
  98.  
  99. print percentage_search(percent)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement