Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // F# program to analyze Divvy daily ride data.
- //
- // << YOUR NAME HERE >>
- // U. of Illinois, Chicago
- // CS 341, Fall 2019
- // Project #04
- //
- #light
- module project04
- //
- // ParseLine and ParseInput
- //
- // Given a sequence of strings representing Divvy data,
- // parses the strings and returns a list of lists. Each
- // sub-list denotes one bike ride. Example:
- //
- // [ [15,22,141,17,5,1124]; ... ]
- //
- // The values are station id (from), station id (to), bike
- // id, starting hour (0..23), starting day of week (0 Sunday-6 Saturday)
- // and trip duration (secs),
- //
- let ParseLine (line:string) =
- let tokens = line.Split(',')
- let ints = Array.map System.Int32.Parse tokens
- Array.toList ints
- let rec ParseInput lines =
- let rides = Seq.map ParseLine lines
- Seq.toList rides
- //------------------- My Functions! You can't have them because they are mine --------------------------------------------
- //let bikenumber = getnth mylist 1
- let rec getNth transaction n =
- match transaction with
- | [] -> -999
- | element::[] ->
- if n=0
- then element
- elif n<0
- then -999
- else -999 // out of bounds
- | liststart::listend ->
- if n=0
- then liststart
- else getNth listend (n-1)
- let rec counter list =
- match list with
- | [] -> 0
- | e::[] -> 1
- | e::rest ->
- if ((getNth e 2) = (getNth (rest |> List.head) 2))
- then 0 + counter rest
- else 1 + counter rest
- let rec getNumBikeUsed ridedata desiredBike =
- match ridedata with
- | [] -> 0
- | e::[] ->
- if (getNth e 2)=desiredBike
- then 1
- else 0
- | liststart::listend ->
- if (getNth liststart 2)=desiredBike
- then 1 + getNumBikeUsed listend desiredBike
- else 0 + getNumBikeUsed listend desiredBike
- let rec getTimeonBike ridedata desiredBike =
- match ridedata with
- | [] -> 0
- | e::[] ->
- if (getNth e 2)=desiredBike
- then (getNth e 5)
- else 0
- | liststart::listend ->
- if (getNth liststart 2)=desiredBike
- then (getNth liststart 5) + getTimeonBike listend desiredBike
- else 0 + getTimeonBike listend desiredBike
- let rec getNumRidestoStation ridedata desiredStation =
- match ridedata with
- | [] -> 0
- | e::[] ->
- if (getNth e 1)=desiredStation
- then 1
- else 0
- | liststart::listend ->
- if (getNth liststart 1)=desiredStation
- then 1 + (getNumRidestoStation listend desiredStation)
- else 0 + (getNumRidestoStation listend desiredStation)
- let rec timeofRidestoStation ridedata desiredStation =
- match ridedata with
- | [] -> 0
- | e::[] ->
- if (getNth e 1)=desiredStation
- then (getNth e 5)
- else 0
- | liststart::listend ->
- if (getNth liststart 1)=desiredStation
- then (getNth liststart 5) + (timeofRidestoStation listend desiredStation)
- else 0 + (timeofRidestoStation listend desiredStation)
- let rec dayofRides ridedata desiredDay =
- match ridedata with
- | [] -> 0
- | e::[] ->
- if (getNth e 4)=desiredDay
- then 1
- else 0
- | e::rest ->
- if (getNth e 4)=desiredDay
- then 1 + dayofRides rest desiredDay
- else 0 + dayofRides rest desiredDay
- let rec getNumAsterisks totalUsesthatDay count=
- if (totalUsesthatDay) >= 10
- then
- 1+(getNumAsterisks (totalUsesthatDay-10) count)
- else 0
- let rec printNumAsterisks numberAterisks =
- if numberAterisks>0
- then
- printf "*"
- printNumAsterisks (numberAterisks-1)
- else printf ""
- let rec listEntry sortStationID sortBikeNumByInfo =
- match sortStationID with
- | [] -> []
- | e::[] ->
- [(getNth e 1); (getNumRidestoStation sortBikeNumByInfo (getNth e 1))]
- | listhead::listend ->
- let endsHead = (getNth listend 0)
- if ((getNth listhead 1) = (getNth endsHead 1))
- then [(getNth listhead 1); (getNumRidestoStation sortBikeNumByInfo (getNth listhead 1))]
- else listEntry listend sortBikeNumByInfo
- ////------------------- OK maybe the rest can be yours... --------------------------------------------
- [<EntryPoint>]
- let main argv =
- //
- // input file name, then input divvy ride data and build
- // a list of lists:
- //
- printf "filename> "
- let filename = System.Console.ReadLine()
- let contents = System.IO.File.ReadLines(filename)
- let ridedata = ParseInput contents
- let numRiders = List.length ridedata
- printfn ""
- printfn "# of rides: %A" numRiders
- printfn ""
- let sortBikeNumByInfo = List.sortBy (fun elem -> (getNth elem 2)) ridedata
- let BikeNum = counter sortBikeNumByInfo
- printfn "# of bikes: %A" BikeNum
- printfn ""
- printf "BikeID> "
- let stringBikeNumber = System.Console.ReadLine()
- let desiredBikeNumber = stringBikeNumber |> int
- let numRidersforBike = getNumBikeUsed sortBikeNumByInfo desiredBikeNumber
- printfn ""
- printfn "# of rides for BikeID %A" numRidersforBike
- printfn ""
- printf "Total time spent riding BikeID %A: " desiredBikeNumber
- let timeonBikeinMinutes = (getTimeonBike sortBikeNumByInfo desiredBikeNumber)/60
- let timeonBikeinSeconds = (getTimeonBike sortBikeNumByInfo desiredBikeNumber)%60
- printf "%A minutes " timeonBikeinMinutes
- printfn "%A seconds" timeonBikeinSeconds
- printfn""
- printf "Average time spent riding BikeID %A: " desiredBikeNumber
- let averageBikeTime = (float(getTimeonBike sortBikeNumByInfo desiredBikeNumber)/(float(numRidersforBike)))
- printfn "%.2f seconds" averageBikeTime
- printfn ""
- printf "StationID> "
- let stringStationID = System.Console.ReadLine()
- let desiredStationID = stringStationID |> int
- let numRidestoStation = getNumRidestoStation sortBikeNumByInfo desiredStationID
- printfn ""
- printf "# of rides to StationID %A: " desiredStationID
- printfn "%A" numRidestoStation
- printfn ""
- printf "Average time spent on trips leading to StationID %A: " desiredStationID
- let averageTime = (float((timeofRidestoStation sortBikeNumByInfo desiredStationID)) / float(numRidestoStation))
- printfn "%f seconds" averageTime
- printfn ""
- printfn "Number of Trips on Sunday: %A" (dayofRides sortBikeNumByInfo 0)
- printfn "Number of Trips on Monday: %A" (dayofRides sortBikeNumByInfo 1)
- printfn "Number of Trips on Tuesday: %A" (dayofRides sortBikeNumByInfo 2)
- printfn "Number of Trips on Wednesday: %A" (dayofRides sortBikeNumByInfo 3)
- printfn "Number of Trips on Thursday: %A" (dayofRides sortBikeNumByInfo 4)
- printfn "Number of Trips on Friday: %A" (dayofRides sortBikeNumByInfo 5)
- printfn "Number of Trips on Saturday: %A" (dayofRides sortBikeNumByInfo 6)
- printfn ""
- let numberSunday = dayofRides sortBikeNumByInfo 0
- let sunAskCount = 0
- let numAskSun = getNumAsterisks numberSunday sunAskCount
- printf "0: "
- let returnerSun = printNumAsterisks numAskSun
- printfn " %A" numberSunday
- let numberMonday = dayofRides sortBikeNumByInfo 1
- let monAskCount = 0
- let numAskMon = getNumAsterisks numberMonday monAskCount
- printf "1: "
- let returnerMon = printNumAsterisks numAskMon
- printfn " %A" numberMonday
- let numberTuesday = dayofRides sortBikeNumByInfo 2
- let tueAskCount = 0
- let numAskTue = getNumAsterisks numberTuesday tueAskCount
- printf "2: "
- let returnerTue = printNumAsterisks numAskTue
- printfn " %A" numberTuesday
- let numberWednesday = dayofRides sortBikeNumByInfo 3
- let wedAskCount = 0
- let numAskWed = getNumAsterisks numberWednesday wedAskCount
- printf "3: "
- let returnerWed = printNumAsterisks numAskWed
- printfn " %A" numberWednesday
- let numberThursday = dayofRides sortBikeNumByInfo 4
- let thuAskCount = 0
- let numAskThu = getNumAsterisks numberThursday thuAskCount
- printf "4: "
- let returnerThu = printNumAsterisks numAskThu
- printfn " %A" numberThursday
- let numberFriday = dayofRides sortBikeNumByInfo 5
- let friAskCount = 0
- let numAskFri = getNumAsterisks numberFriday friAskCount
- printf "5: "
- let returnerFri = printNumAsterisks numAskFri
- printfn " %A" numberFriday
- let numberSaturday = dayofRides sortBikeNumByInfo 6
- let satAskCount = 0
- let numAskSat = getNumAsterisks numberSaturday satAskCount
- printf "6: "
- let returnerSat = printNumAsterisks numAskSat
- printfn " %A" numberSaturday
- printfn""
- let sortStationID = List.sortBy (fun elem -> (getNth elem 1)) ridedata
- 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement