Advertisement
ElfikCo

PF - L1

Oct 10th, 2022
2,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.11 KB | Source Code | 0 0
  1. // Learn more about F# at http://fsharp.org
  2.  
  3. open System
  4.  
  5. let rec geoAvg (product: float, amount: int, sum: float) =
  6.     let s = System.Console.ReadLine()
  7.     let d = System.Convert.ToDouble(s)
  8.     if d <> 0.0 then geoAvg(d * product, amount + 1, sum + d)
  9.     else (product ** (1.0 / System.Convert.ToDouble(amount)), sum)
  10.  
  11. let rec middlePyramid (size: int, row: int, column: int) =
  12.     if row >= size then ()
  13.     else
  14.         if column < size * 2 then
  15.             if column < size - row || column > size + row then printf " "
  16.             else printf "*"
  17.             middlePyramid (size, row, column + 1)
  18.         else
  19.             printf "\n"
  20.             middlePyramid (size, row + 1, 0)
  21.  
  22.  
  23. let rec leftPyramid (size: int, row: int, column: int) =
  24.     if row > size then ()
  25.     else
  26.         if column < size then
  27.             if column < row then printf "*"
  28.             else printf " "
  29.             leftPyramid (size, row, column + 1)
  30.         else
  31.             printf "\n"
  32.             leftPyramid (size, row + 1, 0)
  33.  
  34. let rec rightPyramid (size: int, row: int, column: int) =
  35.     if row > size then ()
  36.     else
  37.         if column < size then
  38.             if column < size - row then printf " "
  39.             else printf "*"
  40.             rightPyramid (size, row, column + 1)
  41.         else
  42.             printf "\n"
  43.             rightPyramid (size, row + 1, 0)
  44.  
  45. let pyramid (size: int) =
  46.     if size > System.Console.WindowHeight || size > System.Console.WindowWidth then ()
  47.     else rightPyramid (size, 0, 0)
  48.  
  49. let rec dividers (target: int, current: int, result: int list) =
  50.     if target / 2 < current then result
  51.     elif target % current = 0 then dividers (target, current + 1, current::result)
  52.     else dividers (target, current + 1, result)
  53.  
  54. let dividersMain (target: int) =
  55.     dividers(target, 2, [])
  56.  
  57. let rec sorter (nums: float list, result: float list) =
  58.     if (nums.Length >= 2) then
  59.         let x1 = nums.[0]
  60.         let x2 = nums.[1]
  61.         let rest = nums.[2..]
  62.  
  63.         if x1 > x2 then
  64.             sorter(x2::rest, x1::result)
  65.         else
  66.             sorter(x1::rest, x2::result)
  67.     elif nums.Length = 1 then
  68.         result @ nums
  69.     else result
  70.  
  71. let checkIndex (list: float list, target: float) =
  72.     List.length (List.filter (fun x -> x < target) (list))
  73.  
  74. let rec sorter2 (nums: float list, result: float list) =
  75.     if (nums.Length >= 1) then
  76.         let x1 = nums.[0]
  77.         let rest = nums.[1..]
  78.         let idx = checkIndex (result, x1)
  79.         sorter2 (rest, result.[..idx] @ [x1] @ result.[idx + 1..])
  80.     else result
  81.  
  82.  
  83. let rec task10 current =
  84.     let k = current / 4
  85.     if k * 4 + 1 = current then true
  86.     elif k * 4 + 3 = current then false
  87.     elif current % 2 = 0 then task10 (current / 2)
  88.     else false
  89.  
  90. [<EntryPoint>]
  91. let main argv =
  92.     //printfn "%A" (geoAvg (1.0, 0, 0.0))
  93.     //pyramid (5)
  94.     //printf "%A" (dividersMain (100))
  95.     //printf "%b" (task10 17)
  96.     //printf "%b" (task10 19)
  97.     //printf "%b" (task10 20)
  98.     //printf "%b" (task10 3)
  99.     printf "%A" (sorter2 ([1.0; 2.0; 0.3; 4.0; 5.3; 3.3; 2.33; 2.32], []))
  100.     0 // return an integer exit code
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement