From 47a53fbf8e7fb58e7a93b0cdc6544e9861ffca9f Mon Sep 17 00:00:00 2001 From: MihirPatel_CIS4930 <60762325+TestingGround00@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:50:27 -0500 Subject: [PATCH 1/6] Adding a few elixir scripts > CalculatePi.ex -- Approximate a value of pi > FactorialIterative.ex -- Returns a factorial for a given n, in an iterative manner. > FactorialRecursive.ex -- Returns a factorial for a given n, in an recursive manner. > FibonacciIterative.ex -- Returns a Fibonacci value for a given n, in an iterative manner. > FibonacciRecursive.ex -- Returns a Fibonacci value for a given n, in a recursive manner. > MaxRecursive.ex -- Find largest value in a list. --- src/elixir/CalculatePi.ex | 31 +++++++++++++++++++++++++++++++ src/elixir/FactorialIterative.ex | 26 ++++++++++++++++++++++++++ src/elixir/FactorialRecursive.ex | 23 +++++++++++++++++++++++ src/elixir/FibonacciIterative.ex | 24 ++++++++++++++++++++++++ src/elixir/FibonacciRecursive.ex | 14 ++++++++++++++ src/elixir/MaxRecursive.ex | 19 +++++++++++++++++++ 6 files changed, 137 insertions(+) create mode 100644 src/elixir/CalculatePi.ex create mode 100644 src/elixir/FactorialIterative.ex create mode 100644 src/elixir/FactorialRecursive.ex create mode 100644 src/elixir/FibonacciIterative.ex create mode 100644 src/elixir/FibonacciRecursive.ex create mode 100644 src/elixir/MaxRecursive.ex diff --git a/src/elixir/CalculatePi.ex b/src/elixir/CalculatePi.ex new file mode 100644 index 00000000..b4b6c9f6 --- /dev/null +++ b/src/elixir/CalculatePi.ex @@ -0,0 +1,31 @@ +# Module to calculate an approximation of Pi +defmodule PiApproximation do + + # Function to calculate pi given number of terms + def calculate_pi(terms) do + + # Initialize pi accumulator + pi = 0.0 + + # Reduce over 1..terms range + # Accumulator is a tuple with pi, denominator, and operation + Enum.reduce(1..terms, {pi, 1.0, 1.0}, fn _, {acc_pi, acc_denom, acc_op} -> + + # Calculate new pi value + new_pi = acc_pi + acc_op * (4.0 / acc_denom) + + # Return tuple with updated values + {new_pi, acc_denom + 2.0, -acc_op} + end) |> elem(0) + end + +end + +# Number of terms to approximate +terms = 100_000 + +# Call approximation function +result = PiApproximation.calculate_pi(terms) + +# Print result +IO.puts(result) \ No newline at end of file diff --git a/src/elixir/FactorialIterative.ex b/src/elixir/FactorialIterative.ex new file mode 100644 index 00000000..c509bde0 --- /dev/null +++ b/src/elixir/FactorialIterative.ex @@ -0,0 +1,26 @@ +defmodule Factorial do + # Define a module called Factorial. + + def factorial(n) when n >= 0 do + # Calculate the factorial using Enum.reduce. + Enum.reduce(1..n, 1, &(&1 * &2)) + end + + defp format_factorial(i, result) do + # Format the output string. + "#{i}! = #{result}" + end + + def main do + # Entry point of the program. + nums = [0, 1, 2, 3, 4, 5] + + Enum.each(nums, fn i -> + # Iterate through the list of numbers. + result = factorial(i) + IO.puts(format_factorial(i, result)) + end) + end +end + +Factorial.main() # Execute the program. diff --git a/src/elixir/FactorialRecursive.ex b/src/elixir/FactorialRecursive.ex new file mode 100644 index 00000000..2ac02996 --- /dev/null +++ b/src/elixir/FactorialRecursive.ex @@ -0,0 +1,23 @@ +# Define Math module for math utilities +defmodule Math do + + # Recursive factorial function + def factorial(0), do: 1 # Base case + + def factorial(n) when n > 0 do + # Recursive case + n * factorial(n-1) + end + +end + +# Array of numbers to get factorials for +nums = [0, 1, 2, 3, 4, 5] + +# Loop through the nums array +for i <- nums do + + # Calculate and print factorial + IO.puts "#{i}! = #{Math.factorial(i)}" + +end \ No newline at end of file diff --git a/src/elixir/FibonacciIterative.ex b/src/elixir/FibonacciIterative.ex new file mode 100644 index 00000000..295ae7b5 --- /dev/null +++ b/src/elixir/FibonacciIterative.ex @@ -0,0 +1,24 @@ +# Define a Fibonacci module +defmodule Fib do + + # Function to calculate nth Fibonacci number + def fibonacci(n) when n >= 0 do + + # Initialize starting values + last = 0 + curr = 1 + + # Use Enum.reduce as a loop from 0 to n-1 + Enum.reduce(0..(n-1), {last, curr}, fn _, {last, curr} -> + + # Update last and curr, returning as a tuple + {curr, curr + last} + end) + + # Return the last value after reducing + |> elem(0) + end +end + +# Print 10th Fibonacci number +IO.puts(Fib.fibonacci(10)) \ No newline at end of file diff --git a/src/elixir/FibonacciRecursive.ex b/src/elixir/FibonacciRecursive.ex new file mode 100644 index 00000000..9ca4b823 --- /dev/null +++ b/src/elixir/FibonacciRecursive.ex @@ -0,0 +1,14 @@ +# Define a Fibonacci module +defmodule Fibonacci do + # Base case: If n is 0 or 1, return n. + def fibonacci(n) when n <= 1, do: n + + # Recursive case: Calculate the Fibonacci number by summing the results + # of the previous two Fibonacci numbers. + def fibonacci(n) do + fibonacci(n - 1) + fibonacci(n - 2) + end +end + +# Print the Fibonacci number for n = 10. +IO.puts("Fibonacci(n): #{Fibonacci.fibonacci(10)}") \ No newline at end of file diff --git a/src/elixir/MaxRecursive.ex b/src/elixir/MaxRecursive.ex new file mode 100644 index 00000000..75a942cd --- /dev/null +++ b/src/elixir/MaxRecursive.ex @@ -0,0 +1,19 @@ +defmodule MaxRecursive do + # Base case: When n is 1, return the first element of the list (head). + def max_recursive(nums, 1), do: hd(nums) + + # Recursive case: Calculate the maximum recursively. + def max_recursive(nums, n) do + aux = max_recursive(nums, n - 1) + if aux > Enum.at(nums, n - 1) do + aux + else + Enum.at(nums, n - 1) + end + end +end + +# Example usage +nums = [1, 2, 3, 4, 32, 6, 7, 8, 9, 10] +IO.inspect "nums = #{inspect(nums)}" +IO.inspect "Max = #{MaxRecursive.max_recursive(nums, length(nums))}" From 6dc02980a19ad271427e7bb8a07196b78ea05811 Mon Sep 17 00:00:00 2001 From: MihirPatel_CIS4930 <60762325+TestingGround00@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:54:25 -0500 Subject: [PATCH 2/6] adding elixir logo uploaded elixir logo in an svg format. Image credit: data: https://images.app.goo.gl/hXetArnAXmAZsDW18 --- logos/elixir.svg | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 logos/elixir.svg diff --git a/logos/elixir.svg b/logos/elixir.svg new file mode 100644 index 00000000..44d8f7d7 --- /dev/null +++ b/logos/elixir.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + file_type_elixir + + + + + + + + \ No newline at end of file From 659961cf0760017ea642294d56128b41b15e9d45 Mon Sep 17 00:00:00 2001 From: MihirPatel_CIS4930 <60762325+TestingGround00@users.noreply.github.com> Date: Wed, 14 Feb 2024 16:46:34 -0500 Subject: [PATCH 3/6] Updated README.md Added a column for Elixir in each table. Mapped the scripts. Replaced references to multiple online compilers with one singular compiler. Added instructions on how to access compiler sites for each language. Finally, linked each topic/item to its specific site. --- README.md | 473 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 413 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index c3b9afae..7cb3851a 100644 --- a/README.md +++ b/README.md @@ -2,67 +2,72 @@ This repository provides several classic algorithms and data structures in **Computer Science**, as well as some extra problems that are frequently encountered in programming challenges. -In order to achieve greater coverage and encourage more people to contribute to the project, the algorithms are available in the following languages: **C**, **C++**, **Java**, **Python**, **Go**, **Ruby**, **Javascript**, **Swift**, **Rust**, **Scala** and **Kotlin**. +In order to achieve greater coverage and encourage more people to contribute to the project, the algorithms are available in the following languages: **C**, **C++**, **Java**, **Python**, **Go**, **Ruby**, **Javascript**, **Swift**, **Rust**, **Scala**, **Kotlin** and **Elixir**. You can quickly access an [online compiler](https://onecompiler.com/) by clicking on the symbols corresponding to the desired programming languages in the top rows of each table. - + + @@ -121,6 +126,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -179,6 +189,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -237,6 +252,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -295,6 +315,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -353,6 +378,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -411,6 +441,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -469,6 +504,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -527,6 +567,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -585,6 +630,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -643,6 +693,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -701,6 +756,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -759,6 +819,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -817,6 +882,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -875,6 +945,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -933,6 +1008,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -991,6 +1071,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1049,6 +1134,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1107,6 +1197,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1165,9 +1260,14 @@ In order to achieve greater coverage and encourage more people to contribute to + - + + @@ -1281,9 +1386,14 @@ In order to achieve greater coverage and encourage more people to contribute to + - + + @@ -1397,6 +1512,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1455,6 +1575,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1513,6 +1638,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1571,6 +1701,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1629,66 +1764,76 @@ In order to achieve greater coverage and encourage more people to contribute to +
AlgorithmsAlgorithms - + - + - + - + - + - + - + - + - + - + - + + + + +
Dijkstra's Algorithm + + + +
Floyd–Warshall Algorithm + + + +
Binary Search + + + +
Graph Search + + + +
Linear Search (Iterative) + + + +
Linear Search (Recursive) + + + +
Linear Search (Sentinel) + + + +
Interpolation Search + + + +
Travelling Salesman + + + +
Hamiltonian Cycle + + + +
Connected Components + + + +
Exponentiation (Iterative) + + + +
Exponentiation (Recursive) + + + +
Factorial (Iterative) + + + +
Factorial (Recursive) + + + +
Fibonacci (Iterative) + + + +
Fibonacci (Recursive) + + + +
Fibonacci (Memoization) + + + +
Max (Recursive) + + + +
Min and Max (Iterative)Min and Max (Iterative) @@ -1223,6 +1323,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Min and Max (Recursive) + + + +
Min and Max (D&C)Min and Max (D&C) @@ -1339,6 +1449,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Knight's Tour + + + +
Tower of Hanoi + + + +
Genetic Algorithm + + + +
Huffman's Algorithm + + + +
LZ77 Algorithm + + + +
- + + @@ -1747,6 +1892,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1805,6 +1955,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1863,6 +2018,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1921,6 +2081,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1979,6 +2144,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2037,6 +2207,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2095,6 +2270,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2153,6 +2333,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2211,6 +2396,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2269,9 +2459,14 @@ In order to achieve greater coverage and encourage more people to contribute to + - + + @@ -2385,6 +2585,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2443,9 +2648,14 @@ In order to achieve greater coverage and encourage more people to contribute to + - + + @@ -2559,6 +2774,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2617,66 +2837,76 @@ In order to achieve greater coverage and encourage more people to contribute to +
Data StructuresData Structures - + - + - + - + - + - + - + - + - + - + - + + + + +
Binary Tree + + + +
Binary Search Tree + + + +
Double-ended Queue + + + +
Queue + + + +
Dynamic Queue + + + +
Graph + + + +
Circular Linked List + + + +
Singly Linked List + + + +
Doubly Linked List + + + +
Unordered Linked List + + + +
Sorted Linked ListSorted Linked List @@ -2327,6 +2522,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Skip List + + + +
Stack + + + +
Dynamic StackDynamic Stack @@ -2501,6 +2711,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Ring Buffer + + + +
Hash Table + + + +
- + - + @@ -2735,6 +2965,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2793,6 +3028,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2851,6 +3091,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2909,6 +3154,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2967,6 +3217,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3025,6 +3280,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3083,6 +3343,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3141,6 +3406,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3199,6 +3469,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3257,6 +3532,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3315,6 +3595,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3373,6 +3658,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3431,6 +3721,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3489,6 +3784,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3547,69 +3847,79 @@ In order to achieve greater coverage and encourage more people to contribute to +
Sorting AlgorithmsSorting Algorithms - + - + - + - + - + - + - + - + - + - + - + + + + + +
Bogosort + + + +
Bubble Sort + + + +
Bucket Sort + + + +
Cocktail Sort + + + +
Comb Sort + + + +
Counting Sort + + + +
Gnome Sort + + + +
Heapsort + + + +
Insertion Sort + + + +
Merge Sort + + + +
Quicksort + + + +
Radix Sort + + + +
Selection Sort + + + +
Shell Sort + + + +
Timsort + + + +
- + - + - + + - + + @@ -3781,9 +4101,14 @@ In order to achieve greater coverage and encourage more people to contribute to + - + + @@ -3897,6 +4227,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3955,9 +4290,14 @@ In order to achieve greater coverage and encourage more people to contribute to + - + + - + +
ExtraExtra - + - + - + - + - + - + - + - + - + - + - + + + + + +
Queue using StacksQueue using Stacks @@ -3665,9 +3975,14 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Two-Sum ProblemTwo-Sum Problem @@ -3723,6 +4038,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Palindrome + + + +
IsogramIsogram @@ -3839,6 +4164,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Leibniz Formula for Pi + + + +
Maze-Solving Algorithm + + + +
Rotten OrangesRotten Oranges @@ -4013,9 +4353,14 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Find Distinct SubsetsFind Distinct Subsets @@ -4071,6 +4416,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
@@ -4080,3 +4430,6 @@ In order to achieve greater coverage and encourage more people to contribute to Feel free to contribute to the project, all contributions are welcome. :grin: If you have questions about how to contribute, take a look at the [CONTRIBUTING](CONTRIBUTING.md) file. + +

+[🔼 Back to top](# :abacus: Algorithms and Data Structures) From 2491e7e468fc7ea5cb0dee5b3e05da6db5de333a Mon Sep 17 00:00:00 2001 From: MihirPatel_CIS4930 <60762325+TestingGround00@users.noreply.github.com> Date: Wed, 14 Feb 2024 16:51:55 -0500 Subject: [PATCH 4/6] Update README.md Fixed back to top mapping --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7cb3851a..0748bdd8 100644 --- a/README.md +++ b/README.md @@ -4432,4 +4432,4 @@ Feel free to contribute to the project, all contributions are welcome. :grin: If you have questions about how to contribute, take a look at the [CONTRIBUTING](CONTRIBUTING.md) file.

-[🔼 Back to top](# :abacus: Algorithms and Data Structures) +[🔼 Back to top](#Portfolio) From 89b93d66c2430c5582593e5cb31b5ad285efca4d Mon Sep 17 00:00:00 2001 From: MihirPatel_CIS4930 <60762325+TestingGround00@users.noreply.github.com> Date: Wed, 14 Feb 2024 17:04:34 -0500 Subject: [PATCH 5/6] Revert "Update README.md" (#6) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0748bdd8..7cb3851a 100644 --- a/README.md +++ b/README.md @@ -4432,4 +4432,4 @@ Feel free to contribute to the project, all contributions are welcome. :grin: If you have questions about how to contribute, take a look at the [CONTRIBUTING](CONTRIBUTING.md) file.

-[🔼 Back to top](#Portfolio) +[🔼 Back to top](# :abacus: Algorithms and Data Structures) From 77e4dae4feed0d7afc804a38a3d4e5b5f6e66359 Mon Sep 17 00:00:00 2001 From: MihirPatel_CIS4930 <60762325+TestingGround00@users.noreply.github.com> Date: Wed, 14 Feb 2024 17:07:51 -0500 Subject: [PATCH 6/6] Update README.md (#5) (#7) Centered the title. Fixed the Back to top reference clearly. --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7cb3851a..cdc8f4ff 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# :abacus: Algorithms and Data Structures +

+

🧮Algorithms and Data Structures

+

This repository provides several classic algorithms and data structures in **Computer Science**, as well as some extra problems that are frequently encountered in programming challenges. @@ -4432,4 +4434,5 @@ Feel free to contribute to the project, all contributions are welcome. :grin: If you have questions about how to contribute, take a look at the [CONTRIBUTING](CONTRIBUTING.md) file.

-[🔼 Back to top](# :abacus: Algorithms and Data Structures) +[🔼 Back to top](https://github.com/TestingGround00/algorithms-and-data-structures/#algorithms-and-data-structures) +