Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. # function: getLeadingDigit(num) --> splice 1st char off,
  2. # function: calcFreq(dataList) --> use counted()
  3. # function: calcStats() --> convert frequency / per number to percentage
  4. # --> write to calcDataList
  5. # function: printTable() --> write calcDataList: leading digit, frequency(%)
  6. import random
  7. dataList = []
  8. firstDigitList = []
  9.  
  10. with open("data.txt", "r") as data:
  11. for line in data:
  12. dataList.append(int(line.strip("\n")))
  13.  
  14. def getLeadingDigit(num):
  15. return int(str(num)[0])
  16.  
  17. def leadingDigitList(dataList):
  18. firstDigitList = []
  19. for item in dataList:
  20. firstDigitList.append(getLeadingDigit(item))
  21. return firstDigitList
  22.  
  23. def calcFreq(firstDigitList):
  24. for item in range(1, 10):
  25. print(item, firstDigitList.count(item))
  26.  
  27. # print(calcFreq(leadingDigitList(dataList)))
  28.  
  29. def calcStats(firstDigitList):
  30. percentageFreq = 0
  31. sum = 0
  32. percentageList = []
  33. for item in range(1, 10):
  34. sum += firstDigitList.count(item)
  35. print("The total sum is:", sum)
  36.  
  37. #sum = sum(firstDigitList.count(item))
  38. for item in range(1, 10):
  39. percentageFreq = (((firstDigitList.count(item)) / sum) * 100)
  40. print(item, str(percentageFreq) + "%")
  41. percentageList.append(int(round(percentageFreq)))
  42. #print(percentageList)
  43. return percentageList
  44.  
  45. #print("\n")
  46. #print("Percentage frequency values list, rounded to the nearest whole number, is:", calcStats(leadingDigitList(dataList)))
  47.  
  48.  
  49. def printTable():
  50. print("The frequency of 1-9 values in data.txt is:")
  51. print(calcFreq(leadingDigitList(dataList)), "\n")
  52. print("List of frequence percentage (1-9), rounded to the nearest whole number, is:", calcStats(leadingDigitList(dataList)))
  53.  
  54. def main():
  55. print("This is output from main:")
  56. printTable()
  57.  
  58. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement