Answer the question
In order to leave comments, you need to log in
Philosophical question about discount coupons?
Task: it is
necessary to write a generator of promotional coupons of approximately the following form XXXX-XXXX-XXXX-XXXX (plus\minus\approximately). It is necessary to generate them over dofiga (100k +)..
Questions:
1. which python libraries to pay attention to ???..
2. and how to choose the right library ??..
3. where can I look at an example of such a project ?? ...
Answer the question
In order to leave comments, you need to log in
What kind of developers went to solve the problem in 2 lines, they need some libraries: https://jsfiddle.net/rLr7kc4z/1/
Just randomly generate coupons. Of course, ideally, you need a check for similarity, but sorting through 100k is not very convenient. To optimize this task, when generating one coupon, you need to enter some kind of indicator, for example, the sum of all ord() from each sign. Then it will not be necessary to check every coupon, but only those that have the same amount. This will speed up the check. Well, for the generation itself, you just need a uniform random. If you know how to write code, there will be no problems with either generation or verification. If you don't know how, study or order. Moreover, you can order the implementation even in low-level languages)
where can i find an example of such a project?
import random, string
random.seed()
'-'.join((''.join([random.choice(string.uppercase + string.digits) \
for x in range(4)]) for y in range(4)))
what python libraries to pay attention to ???.OMG! It is written without any hands in several lines!
I would generate the first 6 digits sequentially (10 ^ 6 combinations), and the remaining 10 would be pure random. Then nothing is guaranteed to match.
If you need verifiability without a database, then the last 10 digits - some hash from the first six with a salt.
total = 10
print("RANDOM:")
import random
for n in range(total):
rest = random.randint(0,10**10-1)
code = "{n:06}{rest:010}".format(n=n, rest=rest)
formatted = code[0:4] + "-" + code[4:8] + "-" + code[8:12] + "-" + code[12:]
print(formatted)
print("TESTABLE:")
import hashlib
SALT = "some_salt"
def generate(n):
_bytes = (str(n) + SALT).encode("ascii")
_hash = hashlib.md5(_bytes).hexdigest()
rest = int(_hash, base=16) % (10**10)
code = "{n:06}{rest:010}".format(n=n, rest=rest)
formatted = code[0:4] + "-" + code[4:8] + "-" + code[8:12] + "-" + code[12:]
return formatted
def test(code):
raw_code = code.replace("-","")
n = int(raw_code[0:6])
return code == generate(n)
for n in range(total):
code = generate(n)
print(code, test(code))
ATP to everyone who responded for the recommendations ..
here .. already scribbled a couple of lines ..))
import random
import string
length = 16 # кол-во символов в купоне
quantity = 100000 # кол-во купонов
chars = string.ascii_uppercase + string.ascii_lowercase + string.digits # набор символов для купонов
def mask(c, length): # маски для купонов
if length == 16:
c = c[0:4] + '-' + c[4:8] + '-' + c[8:12] + '-' + c[12:16] # маска на 16 символов
elif length == 12:
pass
elif length == 9:
pass
return c
def coupon(length): # ф-я генерации одного купона заданной длинны.
l = []
c = str
for i in range(0, length):
l.append(random.choice(chars))
c = ''.join(l)
c = mask(c, length)
return c
def generator(quantity): # а теперь генерим их сколько надо
a_file = open('data.txt', 'w')
for i in range(0, quantity):
a = coupon(length)
line = (a + '\n')
a_file.write(line)
# print(a)
a_file.close()
generator(quantity)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question