H
H
HexUserHex2021-10-02 20:51:32
Python
HexUserHex, 2021-10-02 20:51:32

I can not figure out how easier it is to increase the number by 'mask'?

I apologize if it's not clear in the title of the topic, I expressed the essence of my question :)
There is a number: 12345678 000000000 which needs to be increased as follows:

от:12345678100000000
12345678200000000
12345678300000000
... 
до:12345678999999999


But at the same time, avoid repetitions, since this number acts as a primary key in the database:
12345678100000000 <-- the very first iteration
12345678100000000 <-- the tenth iteration
12345678100000000 <-- the hundredth iteration

Example:
#!/usr/bin/env python3

def addNum(i):
 #проверяю что длина нашего 'числа' меньше 9
  if len(str(i)) < 9:
    result = ''

    #Заполняю нулями оставшееся 'место'
    for tmp in range(9-len(str(i))):
      result += str(0)

    result =  str(i) + str(result)
    
    return int(result)

def main():

    for i in range(1,  999999999):
        print(str(12345678) + str(addNum(i)))
 
              
if __name__ == "__main__":
    main()

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Kuts, 2021-10-02
@HexUserHex

In the loop, check - if the number is divisible by 10 without a remainder, then we skip it and move on to the next number

for i in range(1,  999999999):
    if i % 10 == 0:
        continue
    print("12345678{:<09d}".format(i))

# 12345678100000000
# 12345678200000000
# 12345678300000000
# 12345678400000000
# 12345678500000000
# 12345678600000000
# 12345678700000000
# 12345678800000000
# 12345678900000000
# 12345678110000000
# 12345678120000000
...

A
Alexander Karabanov, 2021-10-02
@karabanov

Formatted string literals

print(f"{12345678:<017d}") # дополнить нулями недостающие позиции, всего позиций 17

Demonstration:
~$ python3
Python 3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(f"{12345678:<017d}")
12345678000000000
>>>

PS
About printf

A
AWEme, 2021-10-02
@AWEme

import math
get_rank = lambda x: math.floor(math.log(x, 10))

def gen():
    start = 12345678
    d_rank = 9

    n = start * 10 ** d_rank

    for i in range(1, 10 ** d_rank):
        if i % 10 != 0: # вот эта строчка пропускает повторы на 10, 100 и т.д. итерациях
            yield n + i * 10 ** (d_rank - get_rank(i) - 1)

    # first yield 12345678100000000
    # last yield  12345678999999999

Weird way of generating numbers. Why didn't the usual range fit?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question