|
|
|
@ -0,0 +1,121 @@
|
|
|
|
|
open System
|
|
|
|
|
|
|
|
|
|
let countSquareElements (lst: int list) : int =
|
|
|
|
|
let squares = lst |> List.map (fun x -> x * x)
|
|
|
|
|
lst |> List.filter (fun x -> List.contains x squares) |> List.length
|
|
|
|
|
|
|
|
|
|
let countSquareElementsWithDetails (lst: int list) : int * (int * int) list =
|
|
|
|
|
let squares = lst |> List.map (fun x -> x * x)
|
|
|
|
|
let squareElements = lst |> List.filter (fun x -> List.contains x squares)
|
|
|
|
|
let pairs = squareElements |> List.map (fun x ->
|
|
|
|
|
let root = lst |> List.find (fun y -> y * y = x)
|
|
|
|
|
(x, root))
|
|
|
|
|
(List.length squareElements, pairs)
|
|
|
|
|
|
|
|
|
|
let findSquareRoots (lst: int list) (element: int) : int list =
|
|
|
|
|
lst |> List.filter (fun x -> x * x = element)
|
|
|
|
|
|
|
|
|
|
let getAllSquarePairs (lst: int list) : (int * int) list =
|
|
|
|
|
let squares = lst |> List.map (fun x -> x * x)
|
|
|
|
|
lst
|
|
|
|
|
|> List.filter (fun x -> List.contains x squares)
|
|
|
|
|
|> List.collect (fun square ->
|
|
|
|
|
lst
|
|
|
|
|
|> List.filter (fun root -> root * root = square)
|
|
|
|
|
|> List.map (fun root -> (square, root)))
|
|
|
|
|
|
|
|
|
|
let getSquareStatistics (lst: int list) : Map<int, int list> =
|
|
|
|
|
let squares = lst |> List.map (fun x -> x * x)
|
|
|
|
|
lst
|
|
|
|
|
|> List.filter (fun x -> List.contains x squares)
|
|
|
|
|
|> List.map (fun square ->
|
|
|
|
|
let roots = lst |> List.filter (fun root -> root * root = square)
|
|
|
|
|
(square, roots))
|
|
|
|
|
|> Map.ofList
|
|
|
|
|
|
|
|
|
|
let hasSquareElements (lst: int list) : bool =
|
|
|
|
|
let squares = lst |> List.map (fun x -> x * x)
|
|
|
|
|
lst |> List.exists (fun x -> List.contains x squares)
|
|
|
|
|
|
|
|
|
|
let filterSquareElements (lst: int list) : int list =
|
|
|
|
|
let squares = lst |> List.map (fun x -> x * x)
|
|
|
|
|
lst |> List.filter (fun x -> List.contains x squares)
|
|
|
|
|
|
|
|
|
|
let filterNonSquareElements (lst: int list) : int list =
|
|
|
|
|
let squares = lst |> List.map (fun x -> x * x)
|
|
|
|
|
lst |> List.filter (fun x -> not (List.contains x squares))
|
|
|
|
|
|
|
|
|
|
let partitionBySquares (lst: int list) : int list * int list =
|
|
|
|
|
let squares = lst |> List.map (fun x -> x * x)
|
|
|
|
|
lst |> List.partition (fun x -> List.contains x squares)
|
|
|
|
|
|
|
|
|
|
let countUniqueSquareElements (lst: int list) : int =
|
|
|
|
|
let squares = lst |> List.map (fun x -> x * x)
|
|
|
|
|
lst |> List.filter (fun x -> List.contains x squares) |> List.distinct |> List.length
|
|
|
|
|
|
|
|
|
|
let getSquareFrequency (lst: int list) : Map<int, int> =
|
|
|
|
|
let squares = lst |> List.map (fun x -> x * x)
|
|
|
|
|
lst
|
|
|
|
|
|> List.filter (fun x -> List.contains x squares)
|
|
|
|
|
|> List.groupBy id
|
|
|
|
|
|> List.map (fun (key, values) -> (key, List.length values))
|
|
|
|
|
|> Map.ofList
|
|
|
|
|
|
|
|
|
|
[<EntryPoint>]
|
|
|
|
|
let main argv =
|
|
|
|
|
let testList1 = [1; 2; 3; 4; 5; 9; 16; 25]
|
|
|
|
|
let testList2 = [2; 4; 8; 16; 32]
|
|
|
|
|
let testList3 = [1; 4; 9; 16; 25; 36]
|
|
|
|
|
let testList4 = [3; 6; 9; 12; 15]
|
|
|
|
|
let testList5 = [0; 1; 2; 4; 8; 16]
|
|
|
|
|
let testList6 = []
|
|
|
|
|
let testList7 = [5; 10; 15; 20]
|
|
|
|
|
let testList8 = [-2; -1; 0; 1; 2; 4]
|
|
|
|
|
|
|
|
|
|
printfn "Тест 1: %A" testList1
|
|
|
|
|
printfn "Количество элементов-квадратов: %d" (countSquareElements testList1)
|
|
|
|
|
let (count1, pairs1) = countSquareElementsWithDetails testList1
|
|
|
|
|
printfn "Детали: количество = %d, пары (квадрат, корень) = %A" count1 pairs1
|
|
|
|
|
printfn "Статистика квадратов: %A" (getSquareStatistics testList1)
|
|
|
|
|
printfn ""
|
|
|
|
|
|
|
|
|
|
printfn "Тест 2: %A" testList2
|
|
|
|
|
printfn "Количество элементов-квадратов: %d" (countSquareElements testList2)
|
|
|
|
|
printfn "Все пары (квадрат, корень): %A" (getAllSquarePairs testList2)
|
|
|
|
|
printfn ""
|
|
|
|
|
|
|
|
|
|
printfn "Тест 3: %A" testList3
|
|
|
|
|
printfn "Количество элементов-квадратов: %d" (countSquareElements testList3)
|
|
|
|
|
printfn "Элементы-квадраты: %A" (filterSquareElements testList3)
|
|
|
|
|
printfn "Элементы-не квадраты: %A" (filterNonSquareElements testList3)
|
|
|
|
|
printfn ""
|
|
|
|
|
|
|
|
|
|
printfn "Тест 4: %A" testList4
|
|
|
|
|
printfn "Количество элементов-квадратов: %d" (countSquareElements testList4)
|
|
|
|
|
printfn "Есть ли квадраты: %b" (hasSquareElements testList4)
|
|
|
|
|
printfn ""
|
|
|
|
|
|
|
|
|
|
printfn "Тест 5: %A" testList5
|
|
|
|
|
printfn "Количество элементов-квадратов: %d" (countSquareElements testList5)
|
|
|
|
|
let (squares5, nonSquares5) = partitionBySquares testList5
|
|
|
|
|
printfn "Разделение: квадраты = %A, не квадраты = %A" squares5 nonSquares5
|
|
|
|
|
printfn ""
|
|
|
|
|
|
|
|
|
|
printfn "Тест 6 (пустой список): %A" testList6
|
|
|
|
|
printfn "Количество элементов-квадратов: %d" (countSquareElements testList6)
|
|
|
|
|
printfn ""
|
|
|
|
|
|
|
|
|
|
printfn "Тест 7: %A" testList7
|
|
|
|
|
printfn "Количество элементов-квадратов: %d" (countSquareElements testList7)
|
|
|
|
|
printfn ""
|
|
|
|
|
|
|
|
|
|
printfn "Тест 8 (с отрицательными числами): %A" testList8
|
|
|
|
|
printfn "Количество элементов-квадратов: %d" (countSquareElements testList8)
|
|
|
|
|
printfn "Уникальные элементы-квадраты: %d" (countUniqueSquareElements testList8)
|
|
|
|
|
printfn "Частота квадратов: %A" (getSquareFrequency testList8)
|
|
|
|
|
printfn ""
|
|
|
|
|
|
|
|
|
|
printfn "Поиск корней для элемента 16 в списке %A: %A" testList1 (findSquareRoots testList1 16)
|
|
|
|
|
printfn "Поиск корней для элемента 4 в списке %A: %A" testList8 (findSquareRoots testList8 4)
|
|
|
|
|
|
|
|
|
|
0
|