From e3295e00a66d972de79f35b47ddf5a908553160d Mon Sep 17 00:00:00 2001 From: David Fernandez Date: Mon, 25 Mar 2024 10:42:16 -0400 Subject: [PATCH 1/6] Solves issue with swift not finding the largest two elements correctly --- ...the_largest_two_elements_in_an_array.swift | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 program/program/find-the-largest-two-elements-in-an-array/find_the_largest_two_elements_in_an_array.swift diff --git a/program/program/find-the-largest-two-elements-in-an-array/find_the_largest_two_elements_in_an_array.swift b/program/program/find-the-largest-two-elements-in-an-array/find_the_largest_two_elements_in_an_array.swift new file mode 100644 index 000000000..3cbffbf7f --- /dev/null +++ b/program/program/find-the-largest-two-elements-in-an-array/find_the_largest_two_elements_in_an_array.swift @@ -0,0 +1,29 @@ +func findLargestTwo(_ arr: [Int]) -> (Int, Int) { + var firstLargest = Int.min + var secondLargest = Int.min + + for num in arr { + if num > firstLargest { + secondLargest = firstLargest + firstLargest = num + } else if num > secondLargest && num != firstLargest { + secondLargest = num + } + } + + return (firstLargest, secondLargest) +} + +// Test cases +let arr1 = [12, 13, 1, 10, 34, 1] +let arr2 = [10, 5, 10] +let arr3 = [10, 10, 10] + +let result1 = findLargestTwo(arr1) +let result2 = findLargestTwo(arr2) +let result3 = findLargestTwo(arr3) + +print("The largest two elements are \(result1.0) and \(result1.1)") +print("The largest two elements are \(result2.0) and \(result2.1)") +print("The largest two elements are \(result3.0) and \(result3.1)") + From 6eea987b6f2b0448b5f870476f9d7e6657ef0730 Mon Sep 17 00:00:00 2001 From: David Fernandez Date: Mon, 25 Mar 2024 13:39:05 -0400 Subject: [PATCH 2/6] Solves #5560 Write a Scala program to convert temperature from celsius to kelvin --- ...onvert_temperature_from_celsius_to_kelvin.scala | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 program/program/convert-temperature-from-celsius-to-kelvin/convert_temperature_from_celsius_to_kelvin.scala diff --git a/program/program/convert-temperature-from-celsius-to-kelvin/convert_temperature_from_celsius_to_kelvin.scala b/program/program/convert-temperature-from-celsius-to-kelvin/convert_temperature_from_celsius_to_kelvin.scala new file mode 100644 index 000000000..8a67eaea8 --- /dev/null +++ b/program/program/convert-temperature-from-celsius-to-kelvin/convert_temperature_from_celsius_to_kelvin.scala @@ -0,0 +1,14 @@ +object CelsiusToKelvinConverter { + def celsiusToKelvin(celsius: Double): Double = { + val kelvin = celsius + 273.15 + kelvin + } + + def main(args: Array[String]): Unit = { + val celsius = -40.0 + val kelvin = celsiusToKelvin(celsius) + println(s"Input (C): $celsius") + println(s"Output (K): $kelvin") + } +} + From 98b3719bd46f76c3c672a007b48ff3d3c3d3f018 Mon Sep 17 00:00:00 2001 From: David Fernandez Date: Mon, 25 Mar 2024 13:58:58 -0400 Subject: [PATCH 3/6] Removes #5560 changes --- ...onvert_temperature_from_celsius_to_kelvin.scala | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 program/program/convert-temperature-from-celsius-to-kelvin/convert_temperature_from_celsius_to_kelvin.scala diff --git a/program/program/convert-temperature-from-celsius-to-kelvin/convert_temperature_from_celsius_to_kelvin.scala b/program/program/convert-temperature-from-celsius-to-kelvin/convert_temperature_from_celsius_to_kelvin.scala deleted file mode 100644 index 8a67eaea8..000000000 --- a/program/program/convert-temperature-from-celsius-to-kelvin/convert_temperature_from_celsius_to_kelvin.scala +++ /dev/null @@ -1,14 +0,0 @@ -object CelsiusToKelvinConverter { - def celsiusToKelvin(celsius: Double): Double = { - val kelvin = celsius + 273.15 - kelvin - } - - def main(args: Array[String]): Unit = { - val celsius = -40.0 - val kelvin = celsiusToKelvin(celsius) - println(s"Input (C): $celsius") - println(s"Output (K): $kelvin") - } -} - From 538e664f0ddd3977a63b9bf15ca1fb8046aec22c Mon Sep 17 00:00:00 2001 From: David Fernandez Date: Mon, 25 Mar 2024 14:08:20 -0400 Subject: [PATCH 4/6] F# program to implement linear search (#5295) --- .../ImplementLinearSearch.fs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 program/program/implement-linear-search/ImplementLinearSearch.fs diff --git a/program/program/implement-linear-search/ImplementLinearSearch.fs b/program/program/implement-linear-search/ImplementLinearSearch.fs new file mode 100644 index 000000000..b8ce0aed6 --- /dev/null +++ b/program/program/implement-linear-search/ImplementLinearSearch.fs @@ -0,0 +1,14 @@ +let rec linearSearch (list : int list) (value : int) (index : int) : int option = + match list with + | [] -> None + | x::xs -> + if x = value then Some index + else linearSearch xs value (index + 1) + +let searchValue = 4 +let list = [1; 2; 3; 4; 5] + +match linearSearch list searchValue 0 with +| Some index -> printfn "Output: %d" index +| None -> printfn "Value not found" + From 9713640efa38948972eefc2343fdedf136e4e237 Mon Sep 17 00:00:00 2001 From: David Fernandez Date: Fri, 29 Mar 2024 15:00:29 -0400 Subject: [PATCH 5/6] Solves 5173: Write a Haskell program to find the largest three elements in an array --- ..._the_largest_three_elements_in_an_array.hs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 program/program/find-the-largest-three-elements-in-an-array/find_the_largest_three_elements_in_an_array.hs diff --git a/program/program/find-the-largest-three-elements-in-an-array/find_the_largest_three_elements_in_an_array.hs b/program/program/find-the-largest-three-elements-in-an-array/find_the_largest_three_elements_in_an_array.hs new file mode 100644 index 000000000..039ea3604 --- /dev/null +++ b/program/program/find-the-largest-three-elements-in-an-array/find_the_largest_three_elements_in_an_array.hs @@ -0,0 +1,28 @@ +import Data.List (delete) + +findLargestThree :: Ord a => [a] -> [a] +findLargestThree arr = take 3 (largestThree arr) + +largestThree :: Ord a => [a] -> [a] +largestThree [] = [] +largestThree [x] = [x] +largestThree [x, y] = [max x y] +largestThree (x:y:z:xs) = let largest = maximumOfThree x y z + in largest : largestThree (delete largest (x:y:z:xs)) + +maximumOfThree :: Ord a => a -> a -> a -> a +maximumOfThree x y z = max x (max y z) + +main :: IO () +main = do + let arr1 = [10, 4, 3, 50, 23, 90] + let arr2 = [10, 4, 3, 50, 23, 90, 1, 100, 49] + putStrLn "Input array 1: " + print arr1 + putStrLn "Largest three elements: " + print (findLargestThree arr1) + putStrLn "Input array 2: " + print arr2 + putStrLn "Largest three elements: " + print (findLargestThree arr2) + From 84225ec524e970dd4883a6dc3be3beb3d260ba32 Mon Sep 17 00:00:00 2001 From: David Date: Mon, 8 Apr 2024 10:26:16 -0400 Subject: [PATCH 6/6] Create implement_ternary_search.hs Defines a function 'ternarySearch' that implements the ternary search algorithm. --- .../implement_ternary_search.hs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 program/program/implement-ternary-search/implement_ternary_search.hs diff --git a/program/program/implement-ternary-search/implement_ternary_search.hs b/program/program/implement-ternary-search/implement_ternary_search.hs new file mode 100644 index 000000000..824b39cd1 --- /dev/null +++ b/program/program/implement-ternary-search/implement_ternary_search.hs @@ -0,0 +1,25 @@ +ternarySearch :: Ord a => [a] -> a -> Int -> Int -> Maybe Int +ternarySearch arr value l r + | l > r = Nothing + | otherwise = + let partitionSize = (r - l) `div` 3 + mid1 = l + partitionSize + mid2 = r - partitionSize + in if (arr !! mid1) == value then Just mid1 + else if (arr !! mid2) == value then Just mid2 + else if value < (arr !! mid1) then ternarySearch arr value l (mid1 - 1) + else if value > (arr !! mid2) then ternarySearch arr value (mid2 + 1) r + else ternarySearch arr value (mid1 + 1) (mid2 - 1) + +-- Helper function to handle 0-based indexing +ternarySearchWrapper :: Ord a => [a] -> a -> Maybe Int +ternarySearchWrapper arr value = ternarySearch arr value 0 (length arr - 1) + +main :: IO () +main = do + let list = [1,2,3,4,5] + value = 4 + result = ternarySearchWrapper list value + case result of + Just index -> putStrLn $ "Element found at index: " ++ show index + Nothing -> putStrLn "Element not found in the array"