Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let getSubsets list =
- let rec generateSubsets list acc compacc =
- seq {
- match list with
- | [] -> yield acc
- | head::tail ->
- yield! generateSubsets tail (head::acc) compacc
- yield! generateSubsets tail acc (head::compacc)
- }
- (generateSubsets list [] []) |> Seq.toArray
- let getResults list =
- let getCalcString calcstr op head =
- if calcstr = "" then
- head
- else
- calcstr + " " + op + " " + head
- let rec loop list result calcstr =
- seq {
- match list with
- | [] -> yield (result, calcstr)
- | head::tail ->
- let headstr = head.ToString()
- yield! loop tail (result + head) (getCalcString calcstr "+" headstr)
- yield! loop tail (result - head) (getCalcString calcstr "-" headstr)
- yield! loop tail (head - result) (getCalcString headstr "-" calcstr)
- yield! loop tail (result * head) (getCalcString calcstr "*" headstr)
- if head <> 0 then
- yield! loop tail (result / head) (getCalcString calcstr "/" headstr)
- if result <> 0 then
- yield! loop tail (head / result) (getCalcString headstr "/" calcstr)
- }
- let run list =
- match list with
- | [] -> Seq.empty
- | head::tail -> loop tail head (head.ToString())
- run list
- // only works for sets with 3 numbers or larger
- let calculate subsets =
- if subsets |> Array.length < 8 then
- ()
- else
- for i in 1..subsets.Length/2 do
- (getResults subsets.[i])
- |> Seq.iter(fun (result, calcstr) ->
- (getResults subsets.[subsets.Length-1-i])
- |> Seq.iter(fun (result2, calcstr2) ->
- if result = result2 then
- printfn "%s = %s "calcstr calcstr2))
- calculate (getSubsets [1;2;3;6])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement