This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Solves issue with swift not finding the largest two elements correctly * Solves #5560 Write a Scala program to convert temperature from celsius to kelvin * Removes #5560 changes * F# program to implement linear search (#5295) --------- Co-authored-by: David Fernandez <david@Davids-MacBook-Pro.local> Co-authored-by: Harsh Raj <harshraj8843@gmail.com>
- Loading branch information
1 parent
d9293d3
commit 25f4991
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...find-the-largest-two-elements-in-an-array/find_the_largest_two_elements_in_an_array.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)") | ||
|
14 changes: 14 additions & 0 deletions
14
program/program/implement-linear-search/ImplementLinearSearch.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
|