-1

So I saw this post, on limiting numbers, but I noticed it would cut a number off if it wasn't within the range.

Is there a way to do this like so:

variable = 25

If your range is 1-20, can variable become 5?

Or if variable = 41, have it become 1?

I have tried this:

 def loop(number, maxn, minn):
     if number > maxn:
         number = maxn - number
     return number

With a range of 0-20, and a number of 40, I got -20?!?

Community
  • 1
  • 1
MasterHolbytla
  • 177
  • 1
  • 2
  • 11
  • Can you elaborate more on the problem. What have you tried? Are you trying to set a valid for a number and if it's off the range, then set it to 1? – dmlittle Sep 25 '16 at 17:23
  • 4
    The term is _modulo operation_. I'm sure you can find a solution yourself, now that you know how this operation is called. – Łukasz Rogalski Sep 25 '16 at 17:23
  • @dmlittle If a number is greater than the range, I would like it to loop back from start, thus, if the input is 5 greater than the max of the range, it should be 5 greater than the min of the range. – MasterHolbytla Sep 25 '16 at 17:25
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/questions/how-to-ask). – TigerhawkT3 Sep 25 '16 at 17:25
  • 1
    Provide a algorithm (not code, but the way you think) why `41` gives us `1` – Jeon Sep 25 '16 at 17:30

2 Answers2

0

One option would be to set variable = (|variable| % (MAX - MIN + 1)) + MIN. This will give you a number that is between MIN and MAX, inclusively.

Note that this only works consistently if variable, MAX and MIN are non-negative integers, and MAX >= MIN >= 0.

The reasoning behind this is as follows: |variable| % (MAX - MIN + 1) is necessarily non-negative, given that MAX - MIN + 1 and |variable| are non-negative, and so the modulos must be non-negative. Thus the lowest it can be is zero. We add MIN after, so the number at its lowest in total can be MIN.

At most, |variable| % (MAX - MIN + 1) is MAX - MIN, due again to the nature of the modulos operator. Adding MIN afterwards gives us MAX. Thus the highest output will be MAX.

No variable will give a number that is lower than MIN or higher than MAX, and there is always an equal number of variable such that n = (|variable| % (MAX - MIN + 1)) + MIN, for all n between MIN and MAX inclusively.

Cisplatin
  • 2,860
  • 3
  • 36
  • 56
0

Just figured it out.

def loop(n, minn, maxn):

    while n > maxn:
        n = n - (maxn - minn)
    while n < minn:
        n = n + (maxn - minn)

    return n

I think that should work for most cases, including negative numbers and ranges.

MasterHolbytla
  • 177
  • 1
  • 2
  • 11