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 factorialize(number: Int) -> Int { | |
if (number == 0) { return 1 } | |
return (number * factorialize(number: number - 1)) | |
} | |
factorialize(number: 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 findLongestWord(word: String) -> String { | |
let wordArray = word.components(separatedBy: " ") | |
var maxLength = 0 | |
for index in 0..<(wordArray.count) { | |
if wordArray[index].count > maxLength { | |
maxLength = wordArray[index].count | |
} | |
} | |
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
import XCTest | |
@testable import InterviewQuestion1 | |
class InterviewQuestion1Tests: XCTestCase { | |
func addition(left: String, right: String) -> String { | |
guard left != "" || right != "" else { | |
return "Enter a correct number" | |
} | |
let leftDigitsArray = left.reversed().map { Int(String($0))! } |
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
import XCTest | |
@testable import InterviewQuestion2 | |
class InterviewQuestion2Tests: XCTestCase { | |
func sumOfDigits(for number: Int) -> Int { | |
let arrayOfDigits = Array(sequence(state: number, next: { return $0 > 0 ? ($0 % 10, $0 = $0 / 10).0 : nil })) | |
return arrayOfDigits.reduce(0, {$0 + $1}) | |
} | |
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
extension Character { | |
var isVowel: Bool { return ["a","e","i","o","u","A","E","I","O","U"].contains(self) } | |
} | |
func reverseVowel(s: String) -> String { | |
if s.count == 0 { return "" } | |
var characters = [Character]() | |
var reversedString = "" | |
s.forEach { vowel in | |
if vowel.isVowel { |
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
let dictMap: [Character: String] = [ | |
"a":".-", | |
"b":"-...", | |
"c":"-.-.", | |
"d": "-..", | |
"e":".", | |
"f":"..-.", | |
"g":"--.", | |
"h":"....", | |
"i":"..", |
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
// My Swift-y solution 28ms | |
func numJewelsInStones(_ J: String, _ S: String) -> Int { | |
var resultDict = [Character: Int]() | |
for element in Array(J) { | |
resultDict[element] = 0 | |
} | |
for element in Array(S) { |