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
func sum(_ nums: [Int], _ target: Int) -> [Int] { | |
var sumIndex: [Int] = [] | |
for x in 0..<target { | |
for y in 0...x{ | |
if (nums.firstIndex(of: x) != nil) { | |
if (nums.firstIndex(of: y) != nil) { | |
if x + y == target { | |
sumIndex.append(nums.firstIndex(of: y)!) | |
sumIndex.append(nums.firstIndex(of: x)!) | |
} |
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
func fizzBuzz(_ n: Int) -> [String] { | |
var results: [String] = [] | |
for num in 1...n { | |
if num % 3 == 0, num % 5 == 0 { | |
results.append("fizzBuzz") | |
} else if num % 3 == 0, num % 5 != 0 { | |
results.append("fizz") | |
} else if num % 5 == 0, num % 3 != 0 { | |
results.append("Buzz") |
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
Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1. | |
Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order. | |
Example 1: | |
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6] |
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
func sumAndProduct(sum: Int, product: Int) -> [Int] { | |
let number1 = Double(sum) * 0.5 | |
let x = Double(sum) - number1 | |
let y = number1 | |
return [Int(x), Int(y)] | |
} |
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
func numberOfVowels(inString: String) { | |
inString.count | |
let firstA = firstA.firstIndex(of: "a") ?? firstA.endIndex | |
} | |
numberOfVowels(in: "lake") |