-1

I need to be able to take a given integer and calculate which "1000 range" it belongs to. For example: for the input 13456, the output should be "13000-13999". Another example: the input is 100234; the output is "100000-100999".

One solution is this answer. However, I would like to avoid hard coding the ranges to allow it to scale automatically.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Neil
  • 43
  • 10

2 Answers2

2

You can remove the last 3 digits of the number by subtracting the result of taking the number modulus 1000 to get the start of the range (alternatively, perform integer division by 1000 and then multiply by 1000 again). Add 999 to get the end of the range.

x = int(input())
rangeStart = x - x % 1000 #alternatively, (x // 1000) * 1000
rangeEnd = rangeStart + 999
print(str(rangeStart) + "-" + str(rangeEnd))
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
1
def f(e):
    print(e // 1000 * 1000, '-', e // 1000 * 1000 + 999)