How to sum "any three elements" in a tuple in F #

I recently started learning how to program in F #, and I have a task that gives me serious headaches.

I need to make a function that takes two arguments, an integer and a five-element set of integers and returns true if the sum of any three elements of the tuple is greater than the first argument, otherwise false.

I started developing my code this way

{
let t3 = (1, 2, 3, 4, 5)
let intVal = 1
let check intVal t3 = 
for t3
    if (*sum of any three elements*) > intVal then true
    else false
}

but at this moment I am stuck and do not know how to act.

+3
source share
3 answers

An easy way to define is to sort the tuple elements and compare them with the sum of the last three elements (ascending sorting):

let inline isAnyThreeGreaterThan2 limit (x1, x2, x3, x4, x5) = 
    [x1;x2;x3;x4;x5] |> List.sort |> Seq.skip 2 |> Seq.sum > limit

Example:

isAnyThreeGreaterThan2 15 (1, 2, 5, 5, 5) |> printfn "%A"
isAnyThreeGreaterThan2 14 (1, 2, 5, 5, 5) |> printfn "%A"
isAnyThreeGreaterThan2 15 (1, 2, 5, 5, 6) |> printfn "%A"
isAnyThreeGreaterThan2 15 (1, 2, 3, 4, 5) |> printfn "%A"
isAnyThreeGreaterThan2 12 (1, 2, 3, 4, 5) |> printfn "%A"
isAnyThreeGreaterThan2 11 (1, 2, 3, 4, 5) |> printfn "%A"

Print

false
true
true
false
false
true

Link

https://dotnetfiddle.net/7XR1ZA

+6
source

, , , , - ,

(1,2,3,4,5) 
|> Microsoft.FSharp.Reflection.FSharpValue.GetTupleFields 
|> Array.toList 
//Implementing this is left as and exercise to the reader
|> combinations 3 
//converts the obj list as a int list and then sums the elements
|> List.map (fun x -> x |> List.map unbox<int> |> List.sum) 
//Verifies if any sum is greater than intVal
|> List.exists (fun x -> x > intVal)
+4

Something like this should do this:

let cross3 l1 l2 l3 =
    [
        for x in l1 do
            for y in l2 do
                for z in l3 do
                yield x, y, z ]

module Tuple3 =
    let distinct (x, y, z) =
        let l = [x; y; z]
        l |> List.distinct |> List.length = l.Length

    let snd (x, y, z) = snd x, snd y, snd z

    let inline sum (x, y, z) = x + y + z

let inline isAnyThreeGreaterThan limit (x1, x2, x3, x4, x5) =
    let l = [x1; x2; x3; x4; x5] |> List.indexed
    let legalCombinations =
        cross3 l l l
        |> List.filter Tuple3.distinct
        |> List.map Tuple3.snd
    legalCombinations |> List.exists (fun t3 -> Tuple3.sum t3 > limit)

Since this is an assignment, I will leave it as an exercise to understand what is happening, but here is an example of an FSI session:

> isAnyThreeGreaterThan 15 (1, 2, 5, 5, 5);;
val it : bool = false
> isAnyThreeGreaterThan 14 (1, 2, 5, 5, 5);;
val it : bool = true
> isAnyThreeGreaterThan 15 (1, 2, 5, 5, 6);;
val it : bool = true
> isAnyThreeGreaterThan 15 (1, 2, 3, 4, 5);;
val it : bool = false
> isAnyThreeGreaterThan 12 (1, 2, 3, 4, 5);;
val it : bool = false
> isAnyThreeGreaterThan 11 (1, 2, 3, 4, 5);;
val it : bool = true
+1
source

Source: https://habr.com/ru/post/1608822/


All Articles