Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. //
  2. // F# program to analyze Divvy daily ride data.
  3. //
  4. // << YOUR NAME HERE >>
  5. // U. of Illinois, Chicago
  6. // CS 341, Fall 2019
  7. // Project #04
  8. //
  9.  
  10. #light
  11.  
  12. module project04
  13.  
  14. //
  15. // ParseLine and ParseInput
  16. //
  17. // Given a sequence of strings representing Divvy data,
  18. // parses the strings and returns a list of lists. Each
  19. // sub-list denotes one bike ride. Example:
  20. //
  21. // [ [15,22,141,17,5,1124]; ... ]
  22. //
  23. // The values are station id (from), station id (to), bike
  24. // id, starting hour (0..23), starting day of week (0 Sunday-6 Saturday)
  25. // and trip duration (secs),
  26. //
  27. let ParseLine (line:string) =
  28. let tokens = line.Split(',')
  29. let ints = Array.map System.Int32.Parse tokens
  30. Array.toList ints
  31.  
  32. let rec ParseInput lines =
  33. let rides = Seq.map ParseLine lines
  34. Seq.toList rides
  35.  
  36. //------------------- My Functions! You can't have them because they are mine --------------------------------------------
  37. //let bikenumber = getnth mylist 1
  38. let rec getNth transaction n =
  39. match transaction with
  40. | [] -> -999
  41. | element::[] ->
  42. if n=0
  43. then element
  44. elif n<0
  45. then -999
  46. else -999 // out of bounds
  47. | liststart::listend ->
  48. if n=0
  49. then liststart
  50. else getNth listend (n-1)
  51.  
  52. let rec counter list =
  53. match list with
  54. | [] -> 0
  55. | e::[] -> 1
  56. | e::rest ->
  57. if ((getNth e 2) = (getNth (rest |> List.head) 2))
  58. then 0 + counter rest
  59. else 1 + counter rest
  60.  
  61. let rec getNumBikeUsed ridedata desiredBike =
  62. match ridedata with
  63. | [] -> 0
  64. | e::[] ->
  65. if (getNth e 2)=desiredBike
  66. then 1
  67. else 0
  68. | liststart::listend ->
  69. if (getNth liststart 2)=desiredBike
  70. then 1 + getNumBikeUsed listend desiredBike
  71. else 0 + getNumBikeUsed listend desiredBike
  72.  
  73. let rec getTimeonBike ridedata desiredBike =
  74. match ridedata with
  75. | [] -> 0
  76. | e::[] ->
  77. if (getNth e 2)=desiredBike
  78. then (getNth e 5)
  79. else 0
  80. | liststart::listend ->
  81. if (getNth liststart 2)=desiredBike
  82. then (getNth liststart 5) + getTimeonBike listend desiredBike
  83. else 0 + getTimeonBike listend desiredBike
  84.  
  85. let rec getNumRidestoStation ridedata desiredStation =
  86. match ridedata with
  87. | [] -> 0
  88. | e::[] ->
  89. if (getNth e 1)=desiredStation
  90. then 1
  91. else 0
  92. | liststart::listend ->
  93. if (getNth liststart 1)=desiredStation
  94. then 1 + (getNumRidestoStation listend desiredStation)
  95. else 0 + (getNumRidestoStation listend desiredStation)
  96.  
  97. let rec timeofRidestoStation ridedata desiredStation =
  98. match ridedata with
  99. | [] -> 0
  100. | e::[] ->
  101. if (getNth e 1)=desiredStation
  102. then (getNth e 5)
  103. else 0
  104. | liststart::listend ->
  105. if (getNth liststart 1)=desiredStation
  106. then (getNth liststart 5) + (timeofRidestoStation listend desiredStation)
  107. else 0 + (timeofRidestoStation listend desiredStation)
  108.  
  109. let rec dayofRides ridedata desiredDay =
  110. match ridedata with
  111. | [] -> 0
  112. | e::[] ->
  113. if (getNth e 4)=desiredDay
  114. then 1
  115. else 0
  116. | e::rest ->
  117. if (getNth e 4)=desiredDay
  118. then 1 + dayofRides rest desiredDay
  119. else 0 + dayofRides rest desiredDay
  120.  
  121. let rec getNumAsterisks totalUsesthatDay count=
  122. if (totalUsesthatDay) >= 10
  123. then
  124. 1+(getNumAsterisks (totalUsesthatDay-10) count)
  125. else 0
  126.  
  127. let rec printNumAsterisks numberAterisks =
  128. if numberAterisks>0
  129. then
  130. printf "*"
  131. printNumAsterisks (numberAterisks-1)
  132. else printf ""
  133.  
  134. let rec listEntry sortStationID sortBikeNumByInfo =
  135. match sortStationID with
  136. | [] -> []
  137. | e::[] ->
  138. [(getNth e 1); (getNumRidestoStation sortBikeNumByInfo (getNth e 1))]
  139. | listhead::listend ->
  140. let endsHead = (getNth listend 0)
  141. if ((getNth listhead 1) = (getNth endsHead 1))
  142. then [(getNth listhead 1); (getNumRidestoStation sortBikeNumByInfo (getNth listhead 1))]
  143. else listEntry listend sortBikeNumByInfo
  144.  
  145. ////------------------- OK maybe the rest can be yours... --------------------------------------------
  146.  
  147. [<EntryPoint>]
  148. let main argv =
  149. //
  150. // input file name, then input divvy ride data and build
  151. // a list of lists:
  152. //
  153. printf "filename> "
  154. let filename = System.Console.ReadLine()
  155. let contents = System.IO.File.ReadLines(filename)
  156. let ridedata = ParseInput contents
  157.  
  158.  
  159. let numRiders = List.length ridedata
  160. printfn ""
  161. printfn "# of rides: %A" numRiders
  162. printfn ""
  163.  
  164.  
  165. let sortBikeNumByInfo = List.sortBy (fun elem -> (getNth elem 2)) ridedata
  166. let BikeNum = counter sortBikeNumByInfo
  167. printfn "# of bikes: %A" BikeNum
  168. printfn ""
  169.  
  170. printf "BikeID> "
  171. let stringBikeNumber = System.Console.ReadLine()
  172. let desiredBikeNumber = stringBikeNumber |> int
  173. let numRidersforBike = getNumBikeUsed sortBikeNumByInfo desiredBikeNumber
  174. printfn ""
  175. printfn "# of rides for BikeID %A" numRidersforBike
  176. printfn ""
  177.  
  178. printf "Total time spent riding BikeID %A: " desiredBikeNumber
  179. let timeonBikeinMinutes = (getTimeonBike sortBikeNumByInfo desiredBikeNumber)/60
  180. let timeonBikeinSeconds = (getTimeonBike sortBikeNumByInfo desiredBikeNumber)%60
  181. printf "%A minutes " timeonBikeinMinutes
  182. printfn "%A seconds" timeonBikeinSeconds
  183.  
  184. printfn""
  185. printf "Average time spent riding BikeID %A: " desiredBikeNumber
  186. let averageBikeTime = (float(getTimeonBike sortBikeNumByInfo desiredBikeNumber)/(float(numRidersforBike)))
  187. printfn "%.2f seconds" averageBikeTime
  188.  
  189.  
  190. printfn ""
  191. printf "StationID> "
  192. let stringStationID = System.Console.ReadLine()
  193. let desiredStationID = stringStationID |> int
  194. let numRidestoStation = getNumRidestoStation sortBikeNumByInfo desiredStationID
  195. printfn ""
  196. printf "# of rides to StationID %A: " desiredStationID
  197. printfn "%A" numRidestoStation
  198. printfn ""
  199.  
  200. printf "Average time spent on trips leading to StationID %A: " desiredStationID
  201. let averageTime = (float((timeofRidestoStation sortBikeNumByInfo desiredStationID)) / float(numRidestoStation))
  202. printfn "%f seconds" averageTime
  203. printfn ""
  204.  
  205. printfn "Number of Trips on Sunday: %A" (dayofRides sortBikeNumByInfo 0)
  206. printfn "Number of Trips on Monday: %A" (dayofRides sortBikeNumByInfo 1)
  207. printfn "Number of Trips on Tuesday: %A" (dayofRides sortBikeNumByInfo 2)
  208. printfn "Number of Trips on Wednesday: %A" (dayofRides sortBikeNumByInfo 3)
  209. printfn "Number of Trips on Thursday: %A" (dayofRides sortBikeNumByInfo 4)
  210. printfn "Number of Trips on Friday: %A" (dayofRides sortBikeNumByInfo 5)
  211. printfn "Number of Trips on Saturday: %A" (dayofRides sortBikeNumByInfo 6)
  212. printfn ""
  213.  
  214. let numberSunday = dayofRides sortBikeNumByInfo 0
  215. let sunAskCount = 0
  216. let numAskSun = getNumAsterisks numberSunday sunAskCount
  217. printf "0: "
  218. let returnerSun = printNumAsterisks numAskSun
  219. printfn " %A" numberSunday
  220.  
  221. let numberMonday = dayofRides sortBikeNumByInfo 1
  222. let monAskCount = 0
  223. let numAskMon = getNumAsterisks numberMonday monAskCount
  224. printf "1: "
  225. let returnerMon = printNumAsterisks numAskMon
  226. printfn " %A" numberMonday
  227.  
  228. let numberTuesday = dayofRides sortBikeNumByInfo 2
  229. let tueAskCount = 0
  230. let numAskTue = getNumAsterisks numberTuesday tueAskCount
  231. printf "2: "
  232. let returnerTue = printNumAsterisks numAskTue
  233. printfn " %A" numberTuesday
  234.  
  235. let numberWednesday = dayofRides sortBikeNumByInfo 3
  236. let wedAskCount = 0
  237. let numAskWed = getNumAsterisks numberWednesday wedAskCount
  238. printf "3: "
  239. let returnerWed = printNumAsterisks numAskWed
  240. printfn " %A" numberWednesday
  241.  
  242. let numberThursday = dayofRides sortBikeNumByInfo 4
  243. let thuAskCount = 0
  244. let numAskThu = getNumAsterisks numberThursday thuAskCount
  245. printf "4: "
  246. let returnerThu = printNumAsterisks numAskThu
  247. printfn " %A" numberThursday
  248.  
  249. let numberFriday = dayofRides sortBikeNumByInfo 5
  250. let friAskCount = 0
  251. let numAskFri = getNumAsterisks numberFriday friAskCount
  252. printf "5: "
  253. let returnerFri = printNumAsterisks numAskFri
  254. printfn " %A" numberFriday
  255.  
  256. let numberSaturday = dayofRides sortBikeNumByInfo 6
  257. let satAskCount = 0
  258. let numAskSat = getNumAsterisks numberSaturday satAskCount
  259. printf "6: "
  260. let returnerSat = printNumAsterisks numAskSat
  261. printfn " %A" numberSaturday
  262.  
  263. printfn""
  264. let sortStationID = List.sortBy (fun elem -> (getNth elem 1)) ridedata
  265.  
  266. 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement