Last active
April 24, 2017 15:37
-
-
Save murasesyuka/e9ea8283869dc9b2a6c2ad8903368874 to your computer and use it in GitHub Desktop.
read chokudai book with F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // P.223 | |
| module ColorfulBoxesAndBalls = begin | |
| type intMin = int | |
| let getMatximum numRed numBlue onlyRed onlyBlue bothColors = | |
| let mutable ans = intMin.MinValue | |
| let chg = min numRed numBlue | |
| for i=0 to chg do | |
| let myscore = ((numRed - i) * onlyRed + | |
| (numBlue- i) * onlyBlue+ | |
| 2 * i * bothColors) | |
| ans <- max ans myscore | |
| done | |
| ans | |
| end | |
| printfn "colorful %b" ((ColorfulBoxesAndBalls.getMatximum 2 3 100 400 200 ) = 1400) | |
| printfn "colorful %b" ((ColorfulBoxesAndBalls.getMatximum 2 3 100 400 300 ) = 1600) | |
| printfn "colorful %b" ((ColorfulBoxesAndBalls.getMatximum 5 5 464 464 464 ) = 4640) | |
| printfn "colorful %b" ((ColorfulBoxesAndBalls.getMatximum 1 4 20 -30 -10 ) = -100) | |
| printfn "colorful %b" ((ColorfulBoxesAndBalls.getMatximum 9 1 -1 -10 4 ) = 0 ) | |
| // P. 255 | |
| module StockHistory = begin | |
| let maximumEarnings initialInvestment monthlyContibution (stockPrices: string []) = | |
| let mutable money = float initialInvestment | |
| let month = stockPrices.Length | |
| let corp = stockPrices.[0].Split(' ').Length | |
| let prices = (stockPrices | |
| |> Array.map (fun str -> str.Split(' ') ) | |
| |> Array.map (Array.map int)) | |
| let mutable max = 0.0 | |
| let mutable profit = 0.0 | |
| let proportion = Array.create (month-1) 1.0 | |
| let buy = Array.create (month-1) false | |
| let mutable buyed = 0.0 | |
| for i in [(month-2)..(-1)..0] do | |
| for j = 0 to (corp-1) do | |
| let p = (float prices.[month-1].[j]) / (float prices.[i].[j]) | |
| if (1.0 < p && max < p) then ( | |
| buy.[i] <- true | |
| max <- p | |
| proportion.[i] <- p | |
| ) | |
| for i = 0 to (buy.Length-1) do | |
| if buy.[i] then ( | |
| profit <- profit + money * proportion.[i] | |
| buyed <- buyed + money | |
| money <- 0.0 | |
| ) | |
| money <- money + monthlyContibution | |
| if profit > 0.0 then | |
| int (profit - buyed) | |
| else | |
| (int profit) | |
| end | |
| //printfn "stock %d" (StockHistory.maximumEarnings 1000 0.0 [|"10 20 30"; "15 24 32"|]) | |
| printfn "stock %b" ((StockHistory.maximumEarnings 1000 0.0 [|"10 20 30"; "15 24 32"|]) = 500) | |
| //printfn "stock %d" (StockHistory.maximumEarnings 1000 0.0 [|"10"; "9"|]) | |
| printfn "stock %b" ((StockHistory.maximumEarnings 1000 0.0 [|"10"; "9"|]) = 0) | |
| (* | |
| printfn "stock %d" (StockHistory.maximumEarnings 100 20.0 [|"40 50 60"; | |
| "37 48 55"; | |
| "100 48 50"; | |
| "105 48 47"; | |
| "110 50 52"; | |
| "110 50 52"; | |
| "110 51 54"; | |
| "109 49 53"|]) | |
| *) | |
| printfn "stock %b" ((StockHistory.maximumEarnings 100 20.0 [|"40 50 60"; | |
| "37 48 55"; | |
| "100 48 50"; | |
| "105 48 47"; | |
| "110 50 52"; | |
| "110 50 52"; | |
| "110 51 54"; | |
| "109 49 53"|] | |
| ) = 239) | |
| open System.Collections.Generic | |
| // P.246 | |
| module BatchSystem = | |
| let schedule (duration:int []) (user:string []) = | |
| let N = duration.Length | |
| let jobTime = new Dictionary<string, int>() | |
| Array.iter (fun x -> jobTime.[x] <-0 ) user | |
| for i = 0 to (N-1) do | |
| jobTime.[user.[i]] <- jobTime.[user.[i]] + duration.[i] | |
| let mutable nextUser = "" | |
| let doit = Array.create N false | |
| let ans = Array.create N 0 | |
| let ansCount = ref 0 | |
| while !ansCount < N do | |
| nextUser <- "" | |
| //printfn "%A" doit | |
| for n = 0 to (N-1) do | |
| if not doit.[n] && | |
| (nextUser = "" || jobTime.[user.[n]] < jobTime.[nextUser]) then | |
| nextUser <- user.[n] | |
| for n = 0 to (N-1) do | |
| if user.[n] = nextUser then | |
| doit.[n] <- true | |
| ans.[!ansCount] <- n | |
| ansCount := !ansCount + 1 | |
| ans | |
| //printfn "batch %A" (BatchSystem.schedule [|400;100;100;100|] [|"Danny";"Stella";"Stella";"Mac"|]) | |
| printfn "batch %b" ((BatchSystem.schedule [|400;100;100;100|] [|"Danny";"Stella";"Stella";"Mac"|]) = [|3;1;2;0|]) | |
| printfn "batch %b" ((BatchSystem.schedule [|200;200;200|] [|"Gri";"Sarah";"Warrick"|]) = [|0;1;2|]) | |
| printfn "batch %b" ((BatchSystem.schedule [|100;200;50|] [|"a";"b";"c"|]) = [|2;0;1|]) | |
| // P. 257 | |
| module AutoLone = begin | |
| let interestRate price monthlyPayment loanTerm = | |
| let mutable high = 100.0 | |
| let mutable low = 0.0 | |
| let mutable mid = 0.0 | |
| while (1e-9 < (high - low)) && (1e-9 < (high - low) / high) do | |
| let mutable balance = price | |
| mid <- ((high+low)/2.0) | |
| let rate = (mid/100.0/12.0) + 1.0 | |
| for j = 0 to (loanTerm-1) do | |
| balance <- balance * rate | |
| balance <- balance - monthlyPayment | |
| //printfn "ba %f ra %f hi %f lo %f %f" balance rate high low mid | |
| if 0.0 < balance then high <- mid else low <- mid | |
| mid | |
| end | |
| printfn "loan %f" (AutoLone.interestRate 6800.0 100.0 68) | |
| //printfn "loan %f" ((AutoLone.interestRate 6800 100 68) - 1.3322616182218813e-13) | |
| printfn "loan %f" (AutoLone.interestRate 2000.0 510.0 4) | |
| printfn "loan %f" (AutoLone.interestRate 15000.0 364.0 48) | |
| // P. 266 | |
| module CircleCountry = begin | |
| let inside x1 y1 x2 y2 r = | |
| let x = x1 - x2 | |
| let y = y1 - y2 | |
| ((x*x)+(y*y)) < (r*r) | |
| let leastBorders (x: int list) (y:int list) (r:int list) x1 y1 x2 y2 = | |
| let circles = List.zip3 x y r | |
| let mutable num = 0 | |
| for (x,y,r) in circles do | |
| if (inside x y x1 y1 r) <> (inside x y x2 y2 r) then num <- num+1 | |
| num | |
| end | |
| printfn "circles %b" ((CircleCountry.leastBorders [0] [0] [2] -5 1 5 1) = 0) | |
| printfn "circles %b" ((CircleCountry.leastBorders [0;-6;6] [0;1;2] [2;2;2] -5 1 5 1) = 2) | |
| printfn "circles %b" ((CircleCountry.leastBorders [1;-3;2;5;-4;12;12] | |
| [1;-1;2;5;5;1;1] | |
| [8;1;2;1;1;1;2] | |
| -5 1 12 1) = 3) | |
| printfn "circles %b" ((CircleCountry.leastBorders [-3;2;2;0;-4;12;12;12] | |
| [-1;2;3;1;5;1;1;1] | |
| [1;3;1;7;1;1;2;3] | |
| 2 3 13 2) = 5) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment