Skip to content

Commit 2bcbc6d

Browse files
Fixed Formatting
1 parent e23a607 commit 2bcbc6d

16 files changed

+102
-85
lines changed

Algorithms/Program.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ module Program =
88
// for item in InsertionSort.Sort list do
99
// System.Console.WriteLine item
1010
Math.Fibonacci.PrintSerie 1 0
11-
11+
1212
0

Algorithms/Search/BinarySearch.fs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
namespace Algorithms.Search
2+
23
open System
34

45
module BinarySearch =
5-
let rec byRecursion (sortedData : IComparable[], item : int, left : int, right : int) =
6+
let rec byRecursion (sortedData: IComparable [], item: int, left: int, right: int) =
67

78
let middle = left + (right - left) / 2
89

910
match sortedData.[middle] with
10-
| s when s.CompareTo(sortedData.[middle]) > item -> byRecursion(sortedData, item, left, middle - 1)
11-
| s when s.CompareTo(sortedData.[middle]) < item -> byRecursion(sortedData, item, left, middle + 1)
11+
| s when s.CompareTo(sortedData.[middle]) > item -> byRecursion (sortedData, item, left, middle - 1)
12+
| s when s.CompareTo(sortedData.[middle]) < item -> byRecursion (sortedData, item, left, middle + 1)
1213
| _ -> middle
1314

1415
/// <summary>
@@ -20,7 +21,7 @@ module BinarySearch =
2021
/// <param name="sortedData">Sorted array to search in.</param>
2122
/// <param name="item">Item to search for.</param>
2223
/// <returns>Index of item that equals to item searched for or -1 if none found.</returns>
23-
let rec findIndex (sortedData : IComparable[], item : int) =
24+
let rec findIndex (sortedData: IComparable [], item: int) =
2425

2526
let left = 0
2627
let right = sortedData.Length - 1
@@ -29,6 +30,6 @@ module BinarySearch =
2930
let currentItem = sortedData.[middle]
3031

3132
match currentItem with
32-
| c when c.CompareTo(sortedData.[middle]) > item -> findIndex(sortedData, item)
33-
| c when c.CompareTo(sortedData.[middle]) < item -> findIndex(sortedData, item)
34-
| _ -> item
33+
| c when c.CompareTo(sortedData.[middle]) > item -> findIndex (sortedData, item)
34+
| c when c.CompareTo(sortedData.[middle]) < item -> findIndex (sortedData, item)
35+
| _ -> item

Algorithms/Sort/Bubble_Sort.fs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
namespace Algorithms.Sort
22

33
module BubbleSort =
4-
let rec Sort list: 'T[] =
4+
let rec Sort list: 'T [] =
55
let mutable updated = false
66
let mutable list = list |> Array.copy
7-
for index in 0..list.Length - 1 do
7+
for index in 0 .. list.Length - 1 do
88
if index < list.Length - 1 then
99
let current = list.[index]
1010
let next = list.[index + 1]
1111
if next < current then
1212
list.[index] <- next
1313
list.[index + 1] <- current
1414
updated <- true
15-
if updated then
16-
list <- Sort list
15+
if updated then list <- Sort list
1716
list

Algorithms/Sort/Comb_Sort.fs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
namespace Algorithms.Sort
22

33
module CombSort =
4-
let Sort list: 'T[] =
4+
let Sort list: 'T [] =
55
let mutable list = list |> Array.copy
66
let mutable gap = double list.Length
77
let mutable swaps = true
88
while gap > 1.0 || swaps do
99
gap <- gap / 1.247330950103979
10-
if gap < 1.0 then
11-
gap <- 1.0
10+
if gap < 1.0 then gap <- 1.0
1211
let mutable i = 0
1312
swaps <- false
1413
while i + int gap < list.Length do

Algorithms/Sort/Cycle_Sort.fs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
namespace Algorithms.Sort
22

33
module CycleSort =
4-
let Sort list: 'T[] =
4+
let Sort list: 'T [] =
55
let mutable list = list |> Array.copy
66
let mutable writes = 0
7-
for index in 0..list.Length - 1 do
7+
for index in 0 .. list.Length - 1 do
88
let mutable value = list.[index]
99
let mutable pos = index
10-
for i in index + 1..list.Length - 1 do
11-
if list.[i] < value then
12-
pos <- pos + 1
10+
for i in index + 1 .. list.Length - 1 do
11+
if list.[i] < value then pos <- pos + 1
1312
if pos <> index then
1413
while value = list.[pos] do
1514
pos <- pos + 1
@@ -19,9 +18,8 @@ module CycleSort =
1918
writes <- writes + 1
2019
while pos <> index do
2120
pos <- index
22-
for i in index + 1..list.Length - 1 do
23-
if list.[i] < value then
24-
pos <- pos + 1
21+
for i in index + 1 .. list.Length - 1 do
22+
if list.[i] < value then pos <- pos + 1
2523
while value = list.[pos] do
2624
pos <- pos + 1
2725
tmp <- list.[pos]

Algorithms/Sort/Gnome_Sort.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Algorithms.Sort
22

33
module GnomeSort =
4-
let Sort list: 'T[] =
4+
let Sort list: 'T [] =
55
let mutable list = list |> Array.copy
66
let mutable first = 1
77
let mutable second = 2

Algorithms/Sort/Heap_Sort.fs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
namespace Algorithms.Sort
22

33
module HeapSort =
4-
let inline swap (a: 'T[]) i j =
4+
let inline swap (a: 'T []) i j =
55
let temp = a.[i]
66
a.[i] <- a.[j]
77
a.[j] <- temp
8-
9-
let inline sift cmp (a: 'T[]) start count =
8+
9+
let inline sift cmp (a: 'T []) start count =
1010
let rec loop root child =
1111
if root * 2 + 1 < count then
12-
let p = child < count - 1 && cmp a.[child] a.[child + 1] < 0
12+
let p =
13+
child < count
14+
- 1
15+
&& cmp a.[child] a.[child + 1] < 0
16+
1317
let child = if p then child + 1 else child
1418
if cmp a.[root] a.[child] < 0 then
1519
swap a root child
1620
loop child (child * 2 + 1)
21+
1722
loop start (start * 2 + 1)
18-
19-
let inline heapsort cmp (a: 'T[]) =
23+
24+
let inline heapsort cmp (a: 'T []) =
2025
let n = a.Length
21-
for start = n/2 - 1 downto 0 do
26+
for start = n / 2 - 1 downto 0 do
2227
sift cmp a start n
2328
for term = n - 1 downto 1 do
2429
swap a term 0

Algorithms/Sort/Insertion_Sort.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace Algorithms.Sort
22

33
module InsertionSort =
4-
let Sort list: 'T[] =
4+
let Sort list: 'T [] =
55
let mutable list = list |> Array.copy
6-
for index in 1..list.Length - 1 do
6+
for index in 1 .. list.Length - 1 do
77
let item = list.[index]
88
let mutable j = index
99
while j > 0 && list.[j - 1] > item do

Algorithms/Sort/Merge_Sort.fs

+15-16
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@ module MergeSort =
44
let split list =
55
let rec aux l acc1 acc2 =
66
match l with
7-
| [] -> (acc1, acc2)
8-
| [x] -> (x::acc1, acc2)
9-
| x::y::tail ->
10-
aux tail (x::acc1) (y::acc2)
11-
in aux list [] []
12-
7+
| [] -> (acc1, acc2)
8+
| [ x ] -> (x :: acc1, acc2)
9+
| x :: y :: tail -> aux tail (x :: acc1) (y :: acc2)
10+
11+
aux list [] []
12+
1313
let rec merge l1 l2 =
1414
match (l1, l2) with
15-
| (x, []) -> x
16-
| ([], y) -> y
17-
| (x::tx, y::ty) ->
18-
if x <= y then x::merge tx l2
19-
else y::merge l1 ty
15+
| (x, []) -> x
16+
| ([], y) -> y
17+
| (x :: tx, y :: ty) -> if x <= y then x :: merge tx l2 else y :: merge l1 ty
2018

21-
let rec sort list =
19+
let rec sort list =
2220
match list with
23-
| [] -> []
24-
| [x] -> [x]
25-
| _ -> let (l1, l2) = split list
26-
in merge (sort l1) (sort l2)
21+
| [] -> []
22+
| [ x ] -> [ x ]
23+
| _ ->
24+
let (l1, l2) = split list
25+
merge (sort l1) (sort l2)

Algorithms/Sort/Pancake_Sort.fs

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
namespace Algorithms.Sort
22

33
module PancakeSort =
4-
let show data = data |> Array.iter (printf "%d ") ; printfn ""
5-
let split (data: int[]) pos = data.[0..pos], data.[(pos+1)..]
6-
4+
let show data =
5+
data |> Array.iter (printf "%d ")
6+
printfn ""
7+
8+
let split (data: int []) pos = data.[0..pos], data.[(pos + 1)..]
9+
710
let flip items pos =
811
let lower, upper = split items pos
912
Array.append (Array.rev lower) upper
10-
13+
1114
let sort items =
1215
let rec loop data limit =
13-
if limit <= 0 then data
16+
if limit <= 0 then
17+
data
1418
else
1519
let lower, upper = split data limit
16-
let indexOfMax = lower |> Array.findIndex ((=) (Array.max lower))
17-
let partialSort = Array.append (flip lower indexOfMax |> Array.rev) upper
18-
loop partialSort (limit-1)
19-
20-
loop items ((Array.length items)-1)
20+
21+
let indexOfMax =
22+
lower |> Array.findIndex ((=) (Array.max lower))
23+
24+
let partialSort =
25+
Array.append (flip lower indexOfMax |> Array.rev) upper
26+
27+
loop partialSort (limit - 1)
28+
29+
loop items ((Array.length items) - 1)

Algorithms/Sort/Quick_Sort.fs

+10-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ namespace Algorithms.Sort
22

33
module QuickSort =
44
let Sort lst =
5-
let rec aux l cont =
6-
match l with
7-
| [] -> cont []
8-
| pivot::rest ->
9-
let left, right = rest |> List.partition(fun i -> i < pivot)
10-
aux left (fun acc_left ->
11-
aux right (fun acc_right -> cont(acc_left @ pivot::acc_right)))
12-
aux lst (fun x -> x)
5+
let rec aux l cont =
6+
match l with
7+
| [] -> cont []
8+
| pivot :: rest ->
9+
let left, right =
10+
rest |> List.partition (fun i -> i < pivot)
11+
12+
aux left (fun acc_left -> aux right (fun acc_right -> cont (acc_left @ pivot :: acc_right)))
13+
14+
aux lst (id)
+5-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
namespace Algorithms.Strings
2-
3-
/// <summary>
1+
/// <summary>
42
/// Remove duplicates from sentence
53
/// <summary>
4+
namespace Algorithms.Strings
5+
66
module RemoveDuplicates =
77
let removeDuplicates (string: string) =
88
let mutable newString = ""
99
for s in string.Split() do
10-
if not (newString.Contains s) then
11-
newString <- newString + " " + s
12-
newString
10+
if not (newString.Contains s) then newString <- newString + " " + s
11+
newString

Algorithms/Strings/ReverseLetters.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ module ReverseLetters =
66
/// </summary>
77
/// <param name="input">String to reverse.</param>
88
/// <returns>Reversed string</returns>
9-
let reverseLetters(input: string) =
9+
let reverseLetters (input: string) =
1010
let mutable str = ""
1111
for phrase in input.Split() do
1212
let mutable word = ""
1313
for letter in phrase do
14-
word <- word.Insert(0, (string)letter)
14+
word <- word.Insert(0, (string) letter)
1515
str <- str + word + " "
1616
str

Algorithms/Strings/ReverseWords.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module ReverseWords =
66
/// </summary>
77
/// <param name="input">String to reverse.</param>
88
/// <returns>Reversed string</returns>
9-
let reverseWords(input: string) =
9+
let reverseWords (input: string) =
1010
let mutable str = ""
1111
for i in input.Split() do
1212
str <- str.Insert(0, i + " ")
13-
str
13+
str

Algorithms/Strings/Split.fs

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ module Split =
77
/// <param name="str">String to split into lists</param>
88
/// <param name="item">The separator on what you would like to separate your strings</param>
99
/// <returns>A string list</returns>
10-
let split(str: string, separator: string) =
10+
let split (str: string, separator: string) =
1111
let mutable newStringList = []
1212
let mutable value = ""
1313
if str.Contains separator then
1414
for c in str do
15-
match (string)c with
16-
| c when c.Contains separator -> newStringList <- newStringList |> List.append <| [value]; value <- ""
17-
| _ -> value <- value + (string)c;
18-
if value <> "" then newStringList <- newStringList |> List.append <| [value]; value <- ""
19-
else newStringList <- [value]
15+
match (string) c with
16+
| c when c.Contains separator ->
17+
newStringList <- newStringList |> List.append <| [ value ]
18+
value <- ""
19+
| _ -> value <- value + (string) c
20+
if value <> "" then
21+
newStringList <- newStringList |> List.append <| [ value ]
22+
value <- ""
23+
else
24+
newStringList <- [ value ]
2025
newStringList

Algorithms/Strings/WordOccurrence.fs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
namespace Algorithms.Strings
2+
23
open System.Collections.Generic
34

45
module WordOccurence =
5-
let wordOccurence (sentence : string) =
6+
let wordOccurence (sentence: string) =
67
// Creating a dictionary containing count of each word
78
let occurence = new Dictionary<string, int>()
89
for word in sentence.Split(" ") do
910
match word with
1011
| w when occurence.ContainsKey(w) -> occurence.Item(w) <- occurence.GetValueOrDefault(w) + 1
1112
| _ -> occurence.Add(word, 1)
12-
occurence
13+
occurence

0 commit comments

Comments
 (0)