You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
6.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

open System
type MyArray<'T> = {
Items: 'T[]
Length: int
}
module MyArray =
let create items = { Items = items; Length = Array.length items }
let empty<'T> = { Items = Array.empty<'T>; Length = 0 }
let append item arr =
let newItems = Array.append arr.Items [|item|]
{ Items = newItems; Length = newItems.Length }
let get index arr = arr.Items.[index]
let toArray arr = arr.Items
let reverseString (s: string) =
let charArray = s.ToCharArray()
Array.rev charArray |> System.String
let copyLastElement (arrayA: MyArray<int>) (arrayB: MyArray<int>) =
if arrayB.Length > 0 then
MyArray.append arrayB.Items.[arrayB.Length - 1] arrayA
else
arrayA
let mergeArrays (arrayA: MyArray<'T>) (arrayB: MyArray<'T>) =
let mergedItems = Array.append arrayA.Items arrayB.Items
{ Items = mergedItems; Length = mergedItems.Length }
let filterDivisibleBy3 (arr: MyArray<int>) =
let filtered = Array.filter (fun x -> x % 3 = 0) arr.Items
{ Items = filtered; Length = filtered.Length }
let arrayToNumber (arr: int[]) =
arr
|> Array.fold (fun acc digit -> acc * 10 + digit) 0
let calculateDifference (arrayA: int[]) (arrayB: int[]) =
let numA = arrayToNumber arrayA
let numB = arrayToNumber arrayB
abs (numA - numB)
let arrayUnion (arrayA: int[]) (arrayB: int[]) =
Array.concat [arrayA; arrayB]
|> Array.distinct
|> Array.sort
let arrayIntersection (arrayA: int[]) (arrayB: int[]) =
Set.intersect (Set.ofArray arrayA) (Set.ofArray arrayB)
|> Set.toArray
|> Array.sort
let arraySymmetricDifference (arrayA: int[]) (arrayB: int[]) =
let setA = Set.ofArray arrayA
let setB = Set.ofArray arrayB
Set.union (Set.difference setA setB) (Set.difference setB setA)
|> Set.toArray
|> Array.sort
let generateDivisibleBy13Or17 count =
let rec generate acc current found =
if found >= count then acc
elif current % 13 = 0 || current % 17 = 0 then
generate (current :: acc) (current + 1) (found + 1)
else
generate acc (current + 1) found
generate [] 1 0 |> List.rev |> List.toArray
let gcd a b =
let rec gcdRec a b =
if b = 0 then a
else gcdRec b (a % b)
gcdRec (abs a) (abs b)
let findRationalRoots (coefficients: int[]) =
if coefficients.Length < 2 then []
else
let constantTerm = coefficients.[coefficients.Length - 1]
let leadingCoeff = coefficients.[0]
let factors n =
[1..abs n] |> List.filter (fun x -> n % x = 0)
let pFactors = if constantTerm = 0 then [0] else factors constantTerm
let qFactors = if leadingCoeff = 0 then [1] else factors leadingCoeff
let possibleRoots =
[for p in pFactors do
for q in qFactors do
yield p, q
yield -p, q]
|> List.map (fun (p, q) -> float p / float q)
|> List.distinct
let evaluatePolynomial x =
coefficients
|> Array.mapi (fun i coeff -> float coeff * (x ** float (coefficients.Length - 1 - i)))
|> Array.sum
possibleRoots
|> List.filter (fun x -> abs (evaluatePolynomial x) < 1e-10)
[<EntryPoint>]
let main argv =
printfn "Задание 18: Работа с классом массив"
printfn "=====================================\n"
printfn "1. Обращение строки:"
let originalString = "Привет"
let reversedString = reverseString originalString
printfn " %s -> %s\n" originalString reversedString
printfn "2. Копирование последнего элемента:"
let arrayA = MyArray.create [|1; 2; 3|]
let arrayB = MyArray.create [|4; 5; 7|]
let resultArray = copyLastElement arrayA arrayB
printfn " A: %A" arrayA.Items
printfn " B: %A" arrayB.Items
printfn " A после копирования: %A\n" resultArray.Items
printfn "3. Объединение массивов:"
let merged = mergeArrays arrayA arrayB
printfn " Объединенный массив: %A\n" merged.Items
printfn "4. Фильтр элементов, делящихся на 3:"
let testArray = MyArray.create [|1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12|]
let filtered = filterDivisibleBy3 testArray
printfn " Исходный: %A" testArray.Items
printfn " Отфильтрованный: %A\n" filtered.Items
printfn "5. Разность чисел, представленных массивами:"
let numArray1 = [|1; 2; 3|] // 123
let numArray2 = [|4; 5|] // 45
let difference = calculateDifference numArray1 numArray2
printfn " Массив 1: %A (число: %d)" numArray1 (arrayToNumber numArray1)
printfn " Массив 2: %A (число: %d)" numArray2 (arrayToNumber numArray2)
printfn " Разность: %d\n" difference
printfn "6. Объединение множеств:"
let setA = [|1; 3; 5; 7|]
let setB = [|3; 4; 5; 8|]
let union = arrayUnion setA setB
printfn " Множество A: %A" setA
printfn " Множество B: %A" setB
printfn " Объединение: %A\n" union
printfn "7. Пересечение множеств:"
let intersection = arrayIntersection setA setB
printfn " Пересечение: %A\n" intersection
printfn "8. Симметрическая разность множеств:"
let symDiff = arraySymmetricDifference setA setB
printfn " Симметрическая разность: %A\n" symDiff
printfn "9. Первые 100 чисел, делящихся на 13 или 17:"
let divisibleNumbers = generateDivisibleBy13Or17 100
printfn " Первые 10: %A" (divisibleNumbers |> Array.take 10)
printfn " Последние 10: %A" (divisibleNumbers |> Array.skip 90)
printfn " Всего найдено: %d\n" divisibleNumbers.Length
printfn "10. Рациональные корни многочлена:"
printfn " Пример: - 5x + 6 = 0 (коэффициенты: [1; -5; 6])"
let polyCoeffs = [|1; -5; 6|]
let roots = findRationalRoots polyCoeffs
printfn " Рациональные корни: %A" roots
0