def c(n, k):
t = min(n - k, k)
n_fact = [n-i for i in range(t)]
for i in range(t):
for j in range(len(n_fact)):
if n_fact[j] % (i + 1) == 0:
n_fact[j] = n_fact[j] // (i + 1)
break
result = 1
for j in range(len(n_fact)):
result *= n_fact[j]
return result
print(c(500, 1))
print(c(500, 5))
print(c(500, 10))
SELECT pd.product_id,
d.name,
d.discount_percent,
d.discount_amount
FROM s_products_discount pd
INNER JOIN s_discounts d
ON d.id = pd.discount_id
AND d.visible = 1
AND d.date_from <= '2018-05-05'
AND d.date_to >= '2018-05-05'
WHERE pd.product_id IN ('41987', '41988', '41989', '41990', '41991', '41992', '41993', '41994')
AND NOT EXISTS (SELECT 1
FROM s_discounts dd
WHERE dd.id = pd.discount_id
AND dd.priority > d.priority
AND dd.visible = 1
AND dd.date_from <= '2018-05-05'
AND dd.date_to >= '2018-05-05')
SELECT pd.product_id,
d.name,
d.discount_percent,
d.discount_amount
d.priority
FROM s_products_discount pd
INNER JOIN s_discounts d
ON d.id = pd.discount_id
AND dd.visible = 1
AND dd.date_from <= '2018-05-05'
AND dd.date_to >= '2018-05-05'
AND d.priority = (SELECT MAX(dd.priority)
FROM s_discounts dd
WHERE dd.id = pd.discount_id
AND dd.visible = 1
AND dd.date_from <= '2018-05-05'
AND dd.date_to >= '2018-05-05')
WHERE pd.product_id IN ('41987', '41988', '41989', '41990', '41991', '41992', '41993', '41994')
Ну или как вариант понять, а точно ли нужно менять значение у ключа 'opcode',
может проще начитать из БД (если это все из БД), расшифровку кода в отдельный ключ 'opcode_brief',
тогда и словарь не потребуется.