Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to figure out a way to change the value of element in array to certain value in swift.

I want to replace the value with a different value as follows:

- 0 -> 0
- 1 -> 4
- 2 -> 3
- 3 -> 2
- 4 -> 1
- 5 -> 0
- 6 -> 1
- 7 -> 3
- 8 -> 2
- 9 -> 1

I have a group of 6 numbers, for example 123456, and I want to individually replace each digit with the corresponding digit as shown above.

I figured out what to do for the most part but it fails if there are 2 of the same digits. Can anyone tell me what I'm missing? For example, if my array is [1, 2, 3, 4, 5, 6] the new array will be [4, 1, 3, 0, 2, 4]. It fails in the example where the original array is [5, 5, 5, 5, 5, 5] because the new array is [0].


If anyone has an idea how to do this I would appreciate it.

Thank you!

What I have tried:

var elements = [1, 2, 3, 4, 5, 6]
for (i, digit) in elements.enumerated() {
    if digit == 1 {
        elements[i] = 4
    } else if digit == 2 {
        elements[i] = 3
    } else if digit == 3 {
        elements[i] = 2
    } else if digit == 4 {
        elements[i] = 1
    } else if digit == 5 {
        elements[i] = 0
    } else if digit == 6 {
        elements[i] = 4
    } else if digit == 7 {
        elements[i] = 3
    } else if digit == 8 {
        elements[i] = 2
    } else if digit == 9 {
        elements[i] = 1
    } else if digit == 0 {
        elements[i] = 0
    }

}
Posted
Updated 14-Nov-17 8:55am
Comments
Richard MacCutchan 14-Nov-17 11:55am    
The code looks OK, what doe you mean by the last part of your comment?
Member 13520929 14-Nov-17 12:33pm    
Actually, I just figured out the more specific problem. It isn't about having 2 or more of the same digit. There is a problem when '5' is the first digit.
Richard MacCutchan 14-Nov-17 12:42pm    
What problem? I don't know swift but I cannot see any reason why your code should not work.
Richard MacCutchan 14-Nov-17 12:57pm    
Just tried this code in C and cannot get it to fail, even if the first three digits are all 5. This must be something peculiar to swift.
Member 13520929 14-Nov-17 14:54pm    
Hi Richard, thank you for the help. I just figured out the problem. It was due to code I had after this that converted this array to an integer. When 5 was the first number in the array, this code changed it to a 0 so when it was then converted into an integer, the 0 was dropped. I fixed it by converting to a string instead. I appreciate your help!

1 solution

I just figured out the problem. It was due to code I had after this that converted this array to an integer. When 5 was the first number in the array, this code changed it to a 0 so when it was then converted into an integer, the 0 was dropped. I fixed it by converting to a string instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900