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

发布于 2025-02-06 18:42:14 字数 615 浏览 2 评论 0原文

我有一个从1到100开始的数组然后,“ AB”都可以除外,如果将来我想添加数字8应该打印为“ C”,并由4& 8应该打印“ AC”,按5& 8应该打印“ bc”,如果这三个则为“ ABC”

所需的输出:

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

我写了这本书,

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)
    }
}

请提供一个可扩展的解决方案,以保持添加If-Else不是一个不错的选择。

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"

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

渔村楼浪 2025-02-13 18:42:14

您非常接近,但您不需要其他条件。如果字符串与另一个条件匹配,只需将字符添加到字符串中:

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)
}

使用字典存储字符:

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)
}

请注意,字典是无序的集合。如果您需要对字符进行排序,则需要在迭代其键值对之前通过其值对其值进行排序:

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)
}

You were pretty close but you don't need the else conditions. 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)
}
抽个烟儿 2025-02-13 18:42:14

在这里,它不是使用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)

::

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

如果您只需要[任何]类型,然后

var resultArray = [Any]()

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

Here it is, instead of using if-else, you can just add up whenever you need

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

var resultArray = [Any]()

resultArray = stringArray.map({ number in
    if let num = Int(number) { return num }
    else { return number }
})
        
print(resultArray)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文