Convert a number to its equivalent in a specific range

I find it difficult to do something, I want to convert a number to its "equivalent" in a given range.
While the concept is really simple, it is rather difficult to explain, so I'll do my best.

Let's say you have an infinite row of tiles, but despite an infinite number of tiles, this is the same color pattern that repeats itself, for example: A row of colored tiles, with the same color pattern repeating again and again.  Each tile has a number.

As you can see, I gave a number for each tile, but we can take a range from 0 to 4 and say 0 - purple
1 - blue 2 - red
3 - green
4 - yellow

But since this color pattern repeats endlessly, in fact, 5 will have the same color as 0, 6 will have the same color as 1, 8 will have the same color as 3.
Also, we are not should forget negative numbers, -1 will have the same color as 4 for example.

Actually I want to convert any given number to the range of the range I have selected, here range [0; 4] so that I know which color corresponds to that particular tile.
I also want this method to work for other ranges, for example, it should also work if my range was [1; 5] or even [-7; -3].

I already found a method that works for ranges [0;n]

(with a n

positive integer), but with a

another positive integer:

convertedNumber = a % (n+1)

      

Here's what he gives with the playground: We are doing the conversion, which behaves as expected

But this only works for the conditions I described, and after hours of struggling with it, I still can't seem to find any good solution to get it to work for any number, positive or negative, and for any range.

Feel free to ask for more details in the comments.

Thank.

+3


source to share


3 answers


Here you go. The function takes the number you want to display and the low and high values โ€‹โ€‹of the range and returns a value in the range:

func mapNum(n: Int, low lo: Int, high hi: Int) -> Int {
    let spread = hi - lo + 1
    return ((n - lo) % spread + spread) % spread + lo
}

      



Examples:

mapNum(-9, low: 0, high: 4)    // returns "1"
mapNum(-9, low: -3, high: 1)   // returns "1"
mapNum(1, low: -11, high: -7)  // returns "-9"
mapNum(3, low: -8, high: -4)   // returns "-7"
mapNum(-5, low: 4, high: 8)    // returns "5"
mapNum(-5, low: -6, high: -2)  // returns "-5"
mapNum(-9, low: -7, high: -3)  // returns "-4"

      

+3


source


func modFromRange(range: Range<Int>, ind: Int) -> Int {
  let endIndex = abs(range.endIndex.predecessor() - range.startIndex).successor()
  let i = (ind - range.startIndex) % endIndex
  return i < 0 ? (range.startIndex + i + endIndex) : (range.startIndex + i)
}

modFromRange(1...5, ind: 1)      //  1
modFromRange(-7...(-3), ind: -8) // -3

      

What works:

modFromRange(-7...(-3), ind: -7) // -7
modFromRange(-7...(-3), ind: -2) // -7
modFromRange(-7...(-3), ind: -8) // -3
modFromRange(-7...(-3), ind:  1) // -4

modFromRange(1...5, ind:  1) // 1
modFromRange(1...5, ind:  5) // 5
modFromRange(1...5, ind:  6) // 1
modFromRange(1...5, ind:  0) // 5
modFromRange(1...5, ind: -1) // 4
modFromRange(1...5, ind: -9) // 1

For -7...-3:

[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1,  0,  1,  2,  3,  4,  5,  6,  7]
[ -5, -4, -3, -7, -6, -5, -4, -3, -7, -6, -5, -4, -3, -7, -6, -5, -4, -3]

For 1...5:

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[ 5,  1,  2,  3,  4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4,  5]

      

Or, in Swift 2, you can do it as a method:



extension Range where T : IntegerLiteralConvertible, T : IntegerArithmeticType {
  func mod(ind: T) -> T {
    let _endIndex = {$0 >= 0 ? $0 : $0 * -1}(endIndex - startIndex)
    let i = (ind - startIndex) % _endIndex
    return i < 0 ? (startIndex + i + _endIndex) : (startIndex + i)
  }
}

      

Will work like:

(1...5).mod(1) // 1
(1...5).mod(6) // 1

      

+3


source


extension Int {
    var convertedNumber: Int {
        if self < 0 {
            return  self * -4  % 5 + 1
        }
        return ( self % 5 ) + 1
    }
}
let n = -9
let result = n.convertedNumber  // 2


println(result) // 2

(-9).convertedNumber  // 2
(-8).convertedNumber  // 3
(-7).convertedNumber  // 4
(-6).convertedNumber  // 5
(-5).convertedNumber  // 1
(-4).convertedNumber  // 2
(-3).convertedNumber  // 3
(-2).convertedNumber  // 4
(-1).convertedNumber  // 5
(0).convertedNumber     // 1
(1).convertedNumber     // 2
(2).convertedNumber     // 3
(3).convertedNumber     // 4
(4).convertedNumber     // 5
(5).convertedNumber     // 1
(6).convertedNumber     // 2
(7).convertedNumber     // 3
(8).convertedNumber     // 4
(9).convertedNumber     // 5
(10).convertedNumber    // 1

      

+1


source







All Articles