Last active
October 19, 2017 02:16
-
-
Save dautermann/a7cf7869c7842e1ca812fd4b17ddfb19 to your computer and use it in GitHub Desktop.
Facebook Interview Question: Telephone Passphrase Equivalents
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
// A telephone keypad has letters associated with each number (e.g. 2 = abc, 3 = def). | |
// Given a passphrase of "fb1" (e.g. one that you might use to log into a bank account), | |
// come up with an algorithm that would assemble an array that contains all the | |
// different possible letter combinations that, when typed into a telephone dial pad, | |
// would be equivalent to the original passphrase. That is, "fb1" equals "321" numerically; | |
// matching equivalent combinations include: "da1", "db1", "dc1", "ea1", "eb1", "ec1", "fa1" and "fc1". | |
// This can be dropped into an iOS Playground in Xcode | |
import UIKit | |
let inputPassphrase = "fb1" | |
func add(these digits : [Character], toThis currentOutputArray : [String]) -> [String] | |
{ | |
var newOutputArray = [String]() | |
for eachDigit in digits | |
{ | |
if currentOutputArray.count > 0 | |
{ | |
for eachEntry in currentOutputArray | |
{ | |
newOutputArray.append(eachEntry + String(eachDigit)) | |
} | |
} else { | |
newOutputArray.append(String(eachDigit)) | |
} | |
} | |
return newOutputArray | |
} | |
// as long as you know the input is UTF8! | |
let inputPassphraseArray = Array(inputPassphrase) | |
var numericEquivalentArray = [Int]() | |
var outputArray = [String]() | |
// convert the passphrase into the numeric equivalent | |
for eachCharacter in inputPassphraseArray | |
{ | |
switch eachCharacter | |
{ | |
case Character("a"), Character("b"), Character("c") : | |
numericEquivalentArray.append(2) | |
case Character("d"), Character("e"), Character("f") : | |
numericEquivalentArray.append(3) | |
case Character("g"), Character("h"), Character("i") : | |
numericEquivalentArray.append(4) | |
case Character("j"), Character("k"), Character("l") : | |
numericEquivalentArray.append(5) | |
case Character("m"), Character("n"), Character("o") : | |
numericEquivalentArray.append(6) | |
case Character("p"), Character("q"), Character("r"), Character("s") : | |
numericEquivalentArray.append(7) | |
case Character("t"), Character("u"), Character("o") : | |
numericEquivalentArray.append(8) | |
case Character("w"), Character("x"), Character("y"), Character("z") : | |
numericEquivalentArray.append(9) | |
case Character("0") : | |
numericEquivalentArray.append(0) | |
case Character("1") : | |
numericEquivalentArray.append(1) | |
default : | |
print("no equivalent here!") | |
} | |
} | |
// now for each digit in the numeric equivalent array, append each letter to the current output array | |
for eachDigit in numericEquivalentArray | |
{ | |
switch eachDigit | |
{ | |
case 0 : | |
outputArray = add(these: [Character("0")], toThis: outputArray) | |
case 1 : | |
outputArray = add(these: [Character("1")], toThis: outputArray) | |
case 2 : | |
outputArray = add(these: [Character("a"), Character("b"), Character("c")], toThis: outputArray) | |
case 3 : | |
outputArray = add(these: [Character("d"), Character("e"), Character("f")], toThis: outputArray) | |
case 4 : | |
outputArray = add(these: [Character("g"), Character("h"), Character("i")], toThis: outputArray) | |
case 5 : | |
outputArray = add(these: [Character("j"), Character("k"), Character("l")], toThis: outputArray) | |
case 6 : | |
outputArray = add(these: [Character("m"), Character("n"), Character("o")], toThis: outputArray) | |
case 7 : | |
outputArray = add(these: [Character("p"), Character("q"), Character("r"), Character("s")], toThis: outputArray) | |
case 8 : | |
outputArray = add(these: [Character("t"), Character("u"), Character("v")], toThis: outputArray) | |
case 9 : | |
outputArray = add(these: [Character("w"), Character("x"), Character("y"), Character("z")], toThis: outputArray) | |
default : | |
print("unexpected digit") | |
} | |
} | |
print("outputArray is \(outputArray)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment