我目前正在尝试实施电话字发生器。我在网上寻找可以使用的东西,这就是我找到的东西。
我在C#中找到了这个实现:
public static void printPhoneWords(int[] number) {
char[] output = new char[number.length];
printWordsUtil(number,0,output);
}
static String[] phoneKeys= new String[]{"0", "1", "ABC", "DEF", "GHI", "JKL",
"MNO", "PQRS", "TUV", "WXYZ"};
private static void printWordsUtil(int[] number, int curDigIndex, char[] output) {
// Base case, if current output word is done
if (curDigIndex == output.length) {
System.out.print(String.valueOf(output) + " ");
return;
}
// Try all 3-4 possible characters for the current digit in number[]
// and recurse for the remaining digits
char curPhoneKey[] = phoneKeys[number[curDigIndex]].toCharArray();
for (int i = 0; i< curPhoneKey.length ; i++) {
output[curDigIndex] = curPhoneKey[i];
printWordsUtil(number, curDigIndex+1, output);
if (number[curDigIndex] <= 1) // for 0 or 1
return;
}
}
public static void main(String[] args) {
int number[] = {2, 3, 4};
printPhoneWords(number);
System.out.println();
}
现在,我尝试在Swift中实现它,但是我遇到了一些问题..
var pinDict = [2:"abc", 3:"def", 4:"ghi", 5:"jkl", 6:"mno", 7:"pqrs", 8:"tuv", 9:"wxyz"]
func printWordsUtil(number: [Int], curDigIndex: Int, output: Array<Character>) {
var curPhoneKey:Array<Character> = []
curPhoneKey = pinDict[number[curDigIndex]]
for var i = 0; i < curPhoneKey.count; i++ {
output[curDigIndex] = pinDict[i]
printWordsUtil(number, curDigIndex+1, output)
if(number[curDigIndex] <= 1) {
return
}
}
}
func printPhoneWords(number: [Int]) {
var output:Array<Character> = []
printWordsUtil(number,0,output)
}
func main() {
var number = [2, 2, 7, 3]
printPhoneWords(number)
}
我在这里收到错误:
curPhoneKey = pinDict[number[curDigIndex]]
// '(Int, String)'
无法转换为'Array<Character>'
不确定如何解决此问题... output[curDigIndex] = pinDict[i]
// '@lvalue $T8'
与'Character'关于如何在Swift中使用此实现的任何想法?
好的,现在我有这个:
comparisonArray.append(dictionary[a!])
comparisonArray.append(dictionary[b!])
comparisonArray.append(dictionary[c!])
comparisonArray.append(dictionary[d!])
func combos<T>(var array: Array<T>, k: Int) -> Array<Array<T>> {
if k == 0 { return [[]] } if array.isEmpty { return [] }
let head = [array[0]]
let subcombos = combos(array, k - 1)
var ret = subcombos.map {head + $0}
array.removeAtIndex(0)
ret += combos(array, k)
return ret
}
combos(comparisonArray, 4)
比较数组是["ABC, "DEF", "DEF, "PQRS"]
。现在,你如何在每个组合中置换每个字母而不是置换元素,所以它看起来像这样? ["A", "D", "D", "P"]
,["B", "D", "D", "P"]
?谢谢!
答案 0 :(得分:1)
您将需要此字符串扩展名:
extension String {
subscript (index:Int) -> String { return String(Array(self)[index]) }
}
首先让Int数组转换为字符串数组
func intArrayToStringArray(number: [Int]) -> [String] {
if number.isEmpty { return [] }
let keyboard = ["0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"] var result:[String] = []
for i in 0..<number.count {
result.append(keyboard[number[i]])
}
return result
}
let firstStep = intArrayToStringArray([2,2,7,3]) // ["abc", "abc", "pqrs", "def"]
然后我们需要为每个数字嵌套一个循环并使用它们的索引组合选择字母,使用字符串插值连接它们并将其附加到输出数组。
func secontStep(input:[String]) -> [String] {
var output:[String] = []
for w in 0..<countElements(input[0]) {
for x in 0..<countElements(input[1]) {
for y in 0..<countElements(input[2]){
for z in 0..<countElements(input[3]){
output.append("\(input[0][w])\(input[1][x])\(input[2][y])\(input[3][z)")
}
}
}
}
return output
}
let wordsArray = secontStep(firstStep) // ["aapd", "aape", "aapf", "aaqd", "aaqe", "aaqf", "aard", "aare", "aarf", "aasd", "aase", "aasf", "abpd", "abpe", "abpf", "abqd", "abqe", "abqf", "abrd", "abre", "abrf", "absd", "abse", "absf", "acpd", "acpe", "acpf", "acqd", "acqe", "acqf", "acrd", "acre", "acrf", "acsd", "acse", "acsf", "bapd", "bape", "bapf", "baqd", "baqe", "baqf", "bard", "bare", "barf", "basd", "base", "basf", "bbpd", "bbpe", "bbpf", "bbqd", "bbqe", "bbqf", "bbrd", "bbre", "bbrf", "bbsd", "bbse", "bbsf", "bcpd", "bcpe", "bcpf", "bcqd", "bcqe", "bcqf", "bcrd", "bcre", "bcrf", "bcsd", "bcse", "bcsf", "capd", "cape", "capf", "caqd", "caqe", "caqf", "card", "care", …, "cbqe", "cbqf", "cbrd", "cbre", "cbrf", "cbsd", "cbse", "cbsf", "ccpd", "ccpe", "ccpf", "ccqd", "ccqe", "ccqf", "ccrd", "ccre", "ccrf", "ccsd", "ccse", "ccsf"]
相反的情况是这样的:
func stringToArray(var number: String) -> [Int] {
if number == "" { return [] }
number = number.lowercaseString
var result:[Int] = []
for i in 0..<countElements(number) {
if number[i] == "0" { result.append(0) }
if number[i] == "1" { result.append(1) }
if number[i] == "2" || number[i] == "a" || number[i] == "b" || number[i] == "c" { result.append(2) }
if number[i] == "3" || number[i] == "d" || number[i] == "e" || number[i] == "f" { result.append(3) }
if number[i] == "4" || number[i] == "g" || number[i] == "h" || number[i] == "i" { result.append(4) }
if number[i] == "5" || number[i] == "j" || number[i] == "k" || number[i] == "l" { result.append(5) }
if number[i] == "6" || number[i] == "m" || number[i] == "n" || number[i] == "o" { result.append(6) }
if number[i] == "7" || number[i] == "p" || number[i] == "q" || number[i] == "r" || number[i] == "s" { result.append(7) }
if number[i] == "8" || number[i] == "t" || number[i] == "u" || number[i] == "v" { result.append(8) }
if number[i] == "9" || number[i] == "w" || number[i] == "x" || number[i] == "y" || number[i] == "z" { result.append(9) }
}
return result
}
stringToArray("1800HELLO") // [1, 8, 0, 0, 4, 3, 5, 5, 6]
stringToArray("1800hello") // [1, 8, 0, 0, 4, 3, 5, 5, 6]