Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Learn more about F# at http://fsharp.org
- // See the 'F# Tutorial' project for more help.
- // Link to Solution can be found here - http://xxx
- type MyRecord = {
- IP : string
- MAC : string
- FriendlyName : string
- ID : int }
- type GunState =
- {
- X: double
- Y: double
- Speed: double
- ExpectedDistance: double
- Name: string
- }
- type ShotResult =
- | ShotSuccess
- | ShotFailure of double
- | ShotImpossible
- type GunResult =
- {
- Name: string
- Result: ShotResult
- }
- open System
- open System.IO
- [<EntryPoint>]
- let main argv =
- // Koan
- // type MyRecord =
- // { IP : string
- // MAC : string
- // FriendlyName : string
- // ID : int }
- //
- // let IsMatchByName record1 name =
- // match record with
- // | { FriendlyName = nameFound; ID = _; IP = _ } if nameFound = name yield Some((id,ip))
- // | _ -> None
- //
- // let checkmatch input =
- // match input with
- // | x, y -> printfn "%A" x
- // | None -> printfn "%A" "Sorry no match"
- let IsMatchByName record name =
- match record with
- | { FriendlyName = nameFound; ID = _; IP = _ } -> if nameFound = name then Some((record.ID, record.IP))
- else None
- | _ -> None
- let checkmatch input =
- match input with
- | Some((x, y)) -> printfn "%A" x
- | None -> printfn "%A" "Sorry no match"
- // let record = {IP ="127.0.0.1"; MAC ="FF:FF:FF:FF:FF:FF"; FriendlyName = "Home";ID = 229229}
- let record = {IP = "10.1.1.1"; MAC = "FF:FF:FF:FF:FF:FF"; FriendlyName = "ServerFailure";ID = 0}
- printf "Enter name "
- let name = Console.ReadLine()
- let m = IsMatchByName record name
- checkmatch m
- // Problem
- let angle_of_reach d v =
- let x = 9.81 * d / Math.Pow(v, 2.0)
- if Math.Abs(x) <= 1.0 then
- Some (0.5 * Math.Asin(x))
- else
- None
- let distance_travelled angle v = Math.Pow(v, 2.0) * Math.Sin(2.0 * angle) / 9.81
- let angle x y = Math.Atan(y / x)
- let GetFile =
- Console.Write("Enter the full path to the name of the input file: ")
- Console.ReadLine()
- try
- use input =
- new StreamReader(match argv.Length with
- | 0 -> GetFile
- | _ -> argv.[0])
- // By using sequences file and memory access will be efficient
- let data = seq {while not input.EndOfStream do
- let raw = input.ReadLine()
- let values = raw.Split(',')
- yield {
- X = double values.[0]
- Y = double values.[1]
- Speed = double values.[2]
- ExpectedDistance = double values.[3]
- Name = values.[4]
- }
- }
- let results = seq {
- // Correction factor to deal with
- // accumulated floating point errors
- let epsilon = 0.005
- for gun in data do
- let angle = Math.Atan2(gun.Y, gun.X)
- let d = distance_travelled angle gun.Speed
- if Math.Abs(d - gun.ExpectedDistance) < epsilon then
- yield {
- Name = gun.Name
- Result = ShotSuccess
- }
- else
- yield {
- Name = gun.Name
- Result = match angle_of_reach gun.ExpectedDistance gun.Speed with
- | Some expected_angle -> ShotFailure expected_angle
- | None -> ShotImpossible
- }
- }
- for r in results do
- let msg =
- match r.Result with
- | ShotFailure expected_angle -> "failed since angle should be " + expected_angle.ToString()
- | ShotSuccess _ -> "hits the target"
- | ShotImpossible -> "target is too far"
- printfn "Gun %s %s" r.Name msg
- Console.ReadKey()
- 0
- with
- | :? System.IO.FileNotFoundException ->
- Console.Write("File Not Found. Press a key to exit")
- Console.ReadKey()
- -1
- | _ ->
- Console.Write("Something else happened")
- Console.ReadKey()
- -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement