Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
68
Task/Bulls-and-cows/F-Sharp/bulls-and-cows-1.fs
Normal file
68
Task/Bulls-and-cows/F-Sharp/bulls-and-cows-1.fs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
open System
|
||||
|
||||
let generate_number targetSize =
|
||||
let rnd = Random()
|
||||
let initial = Seq.initInfinite (fun _ -> rnd.Next(1,9))
|
||||
initial |> Seq.distinct |> Seq.take(targetSize) |> Seq.toList
|
||||
|
||||
let countBulls guess target =
|
||||
let hits = List.map2 (fun g t -> if g = t then true else false) guess target
|
||||
List.filter (fun x -> x = true) hits |> List.length
|
||||
|
||||
let countCows guess target =
|
||||
let mutable score = 0
|
||||
for g in guess do
|
||||
for t in target do
|
||||
if g = t then
|
||||
score <- score + 1
|
||||
else
|
||||
score <- score
|
||||
score
|
||||
|
||||
let countScore guess target =
|
||||
let bulls = countBulls guess target
|
||||
let cows = countCows guess target
|
||||
(bulls, cows)
|
||||
|
||||
let playRound guess target =
|
||||
countScore guess target
|
||||
|
||||
let inline ctoi c : int =
|
||||
int c - int '0'
|
||||
|
||||
let lineToList (line: string) =
|
||||
let listc = Seq.map(fun c -> c |> string) line |> Seq.toList
|
||||
let conv = List.map(fun x -> Int32.Parse x) listc
|
||||
conv
|
||||
|
||||
let readLine() =
|
||||
let line = Console.ReadLine()
|
||||
if line <> null then
|
||||
if line.Length = 4 then
|
||||
Ok (lineToList line)
|
||||
else
|
||||
Error("Input guess must be 4 characters!")
|
||||
else
|
||||
Error("Input guess cannot be empty!")
|
||||
|
||||
let rec handleInput() =
|
||||
let line = readLine()
|
||||
match line with
|
||||
| Ok x -> x
|
||||
| Error s ->
|
||||
printfn "%A" s
|
||||
handleInput()
|
||||
|
||||
[<EntryPoint>]
|
||||
let main argv =
|
||||
let target = generate_number 4
|
||||
let mutable shouldEnd = false
|
||||
while shouldEnd = false do
|
||||
let guess = handleInput()
|
||||
let (b, c) = playRound guess target
|
||||
printfn "Bulls: %i | Cows: %i" b c
|
||||
if b = 4 then
|
||||
shouldEnd <- true
|
||||
else
|
||||
shouldEnd <- false
|
||||
0
|
||||
50
Task/Bulls-and-cows/F-Sharp/bulls-and-cows-2.fs
Normal file
50
Task/Bulls-and-cows/F-Sharp/bulls-and-cows-2.fs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
function get_digit( num as uinteger, ps as uinteger ) as uinteger
|
||||
return (num mod 10^(ps+1))\10^ps
|
||||
end function
|
||||
|
||||
function is_malformed( num as uinteger ) as boolean
|
||||
if num > 9876 then return true
|
||||
dim as uinteger i, j
|
||||
for i = 0 to 2
|
||||
for j = i+1 to 3
|
||||
if get_digit( num, j ) = get_digit( num, i ) then return true
|
||||
next j
|
||||
next i
|
||||
return false
|
||||
end function
|
||||
|
||||
function make_number() as uinteger
|
||||
dim as uinteger num = 0
|
||||
while is_malformed(num)
|
||||
num = int(rnd*9877)
|
||||
wend
|
||||
return num
|
||||
end function
|
||||
|
||||
randomize timer
|
||||
|
||||
dim as uinteger count=0, num=make_number(), guess=0
|
||||
dim as uinteger cows, bulls, i, j
|
||||
|
||||
while guess <> num
|
||||
count += 1
|
||||
do
|
||||
print "Guess a number. "
|
||||
input guess
|
||||
loop while is_malformed(guess)
|
||||
|
||||
cows = 0
|
||||
bulls = 0
|
||||
|
||||
for i = 0 to 3
|
||||
for j = 0 to 3
|
||||
if get_digit( num, i ) = get_digit( guess, j ) then
|
||||
if i= j then bulls += 1
|
||||
if i<>j then cows += 1
|
||||
end if
|
||||
next j
|
||||
next i
|
||||
print using "You scored # bulls and # cows."; bulls; cows
|
||||
wend
|
||||
|
||||
print using "Correct. That took you ### guesses."; count
|
||||
Loading…
Add table
Add a link
Reference in a new issue