基于C#程序的Swift电话词生成器实现 [英] Telephone Word Generator Implementation in Swift based on C# Program

查看:74
本文介绍了基于C#程序的Swift电话词生成器实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试实现电话单词生成器.我在网络上四处寻找可以使用的东西,这就是我发现的东西.

I'm currently trying to implement the Telephone word generator. I looked all around the web for something that I could use and this is what I found.

我在C#中找到了此实现:

I found this implementation in 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中实现它,但是遇到了一些问题.

Now, I tried to implement it in Swift, but I'm getting a few issues..

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'与字符"不同
  • curPhoneKey = pinDict[number[curDigIndex]] // '(Int, String)' not convertible to 'Array<Character>' Not sure how to solve this...
  • output[curDigIndex] = pinDict[i] // '@lvalue $T8' is not identical to 'Character'

关于如何在Swift中实现此实现的任何想法?

Any ideas on how to have this implementation working in 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"]?谢谢!

The comparison array is ["ABC, "DEF", "DEF, "PQRS"] . Now, how do you permute every letter in each combination on it's own instead of permuting the element, so it looks like this? ["A", "D", "D", "P"], ["B", "D", "D", "P"] ? THANKS!

推荐答案

您将需要以下字符串扩展名:

You will need this string extension:

extension String {
    subscript (index:Int) -> String { return String(Array(self)[index]) }
}

首先,我们将Int数组转换为String数组

First lets convert the Int Array to a String Array

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"] 

然后,我们将需要为每个数字嵌套一个循环,并使用它们的索引组合选择字母,使用字符串插值将它们连接起来,并将其附加到输出数组中.

Then we will need to nest one loop for each number and pick the letters using the index combination of them, join them using string interpolation and append it to the output array.

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]

这篇关于基于C#程序的Swift电话词生成器实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆