简体   繁体   English

查找可扩展的解决方案以在 Swift 中从 Array 打印数据

[英]Find Scalable solution to print data from Array in Swift

I have an array starting from 1 to 100 and I have to print element if the number is divisible by 4 it should print the letter "A" and if the number is divisible by 5 it should print the letter "B" and if it is divisible by both then "AB" I want to make a scalable solution if in future I want to add number divisible by 8 should print "C" and divisible by 4 & 8 should print "AC", by 5&8 should print "BC" and if all three then "ABC"我有一个从 1 到 100 的数组,如果数字可以被 4 整除,我必须打印元素,它应该打印字母“A”,如果数字可以被 5 整除,它应该打印字母“B”,如果它是可被两者整除然后“AB”我想制定一个可扩展的解决方案,如果将来我想添加可被 8 整除的数字应该打印“C”并且被 4 和 8 整除应该打印“AC”,被 5 和 8 应该打印“BC”和如果所有三个则“ABC”

desired output:所需的输出:

1
2
3
A
B
6
7
C
9
B
11
AB
13
14
...

I wrote this我写了这个

for number in 1...100 {
    if number.isMultiple(of: 4) && !number.isMultiple(of: 5){
        print("A"
    } else if !number.isMultiple(of: 4) && number.isMultiple(of: 5){
        print("B")
    } else if number.isMultiple(of: 4) && number.isMultiple(of: 5){
        print("AB")
    } else {
        print(number)
    }
}

Please provide a scalable solution to keep adding If-else is not a good option.请提供一个可扩展的解决方案来继续添加 If-else 不是一个好的选择。

You were pretty close but you don't need the else conditions.你非常接近,但你不需要 else 条件。 Just add the character to the string if it matches another condition:如果匹配另一个条件,只需将字符添加到字符串中:

for number in 1...100 {
    var string = ""
    if number.isMultiple(of: 4) { string.append("A") }
    if number.isMultiple(of: 5) { string.append("B") }
    if number.isMultiple(of: 8) { string.append("C") }
    print(string.isEmpty ? number : string)
}

Using a dictionary to store the characters:使用字典存储字符:

let dict = [
    4: "A",
    5: "B",
    8: "C"
]
for number in 1...100 {
    var string = ""
    for (key, character) in dict where number.isMultiple(of: key) {
        string.append(character)
    }
    print(string.isEmpty ? number : string)
}

Note that dictionary is an unordered collection.请注意,字典是无序集合。 If you need the characters to be sorted you would need to sort the dictionary by its values before iterating its key value pairs:如果您需要对字符进行排序,则需要在迭代其键值对之前按其值对字典进行排序:

let sortedDict = dict.sorted(by: { $0.value < $1.value }) 
for number in 1...100 {
    var string = ""
    for (key, character) in sortedDict where number.isMultiple(of: key) {
        string.append(character)
    }
    print(string.isEmpty ? number : string)
}

Here it is, instead of using if-else, you can just add up whenever you need在这里,您可以在需要时添加,而不是使用 if-else

var stringArray = [String]()
for number in 0...100 {
    stringArray.append(String(number))
}

// stringArray = ["0","1", "2", "3",....,"99", "100"]
// Adding a zero before to compare with the index
        
stringArray = stringArray.enumerated().map({ index, item in
     var value = item
            if index % 4 == 0 {
                value = Int(item) == nil ?  item + "A":  "A"
            }
            return value
        })
        
stringArray = stringArray.enumerated().map({ index, item in
            var value = item
            if index % 5 == 0 {
                value = Int(item) == nil ?  item + "B":  "B"
            }
            return value
        })
        
stringArray = stringArray.enumerated().map({ index, item in
            var value = item
            if index % 8 == 0 {
                value = Int(item) == nil ?  item + "C":  "C"
            }
            return value
        })

stringArray.removeFirst()
print(stringArray)

Result::结果::

"1", "2", "3", "A", "B", "6", "7", "AC", "9", "B", "11", "A", "13", "14", "B", "AC", "17", "18", "19", "AB", "21", "22", "23", "AC", "B", "26", "27", "A", "29", "B", "31", "AC", "33", "34", "B", "A", "37", "38", "39", "ABC", "41", "42", "43", "A", "B", "46", "47", "AC", "49", "B", "51", "A", "53", "54", "B", "AC", "57", "58", "59", "AB", "61", "62", "63", "AC", "B", "66", "67", "A", "69", "B", "71", "AC", "73", "74", "B", "A", "77", "78", "79", "ABC", "81", "82", "83", "A", "B", "86", "87", "AC", "89", "B", "91", "A", "93", "94", "B", "AC", "97", "98", "99", "AB"

if you just want [Any] type then just如果您只想输入 [Any] 类型,那么只需

var resultArray = [Any]()

resultArray = stringArray.map({ number in
    if let num = Int(number) { return num }
    else { return number }
})
        
print(resultArray)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM