Home > Back-end >  Find Scalable solution to print data from Array in Swift
Find Scalable solution to print data from Array in Swift

Time:06-12

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.

CodePudding user response:

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

CodePudding user response:

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)
  • Related