LINUX.ORG.RU

История изменений

Исправление rtxtxtrx, (текущая версия) :

Рабочий вроде:

~   
❯ ipython
Python 3.12.4 (main, Jun  7 2024, 06:33:07) [GCC 14.1.1 20240522]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.26.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: def find_expression_with_expected_value(expected_value):
   ...:     digits = ['9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
   ...:     operations = ['', '+', '-']
   ...:     permutation = [0] * (len(digits) - 1)
   ...: 
   ...:     def next_permutation(permutation, cardinality):
   ...:         i = len(permutation) - 1
   ...:         while i >= 0 and permutation[i] == cardinality - 1:
   ...:             permutation[i] = 0
   ...:             i -= 1
   ...:         if i < 0:
   ...:             return False
   ...:         permutation[i] += 1
   ...:         return True
   ...: 
   ...:     results = []
   ...: 
   ...:     while True:
   ...:         expression_chars = []
   ...:         for i in range(len(digits) - 1):
   ...:             expression_chars.append(digits[i])
   ...:             expression_chars.append(operations[permutation[i]])
   ...:         expression_chars.append(digits[-1])
   ...:         expression_string = ''.join(expression_chars)
   ...:         expression_value = eval(expression_string)
   ...:         if expression_value == expected_value:
   ...:             results.append(expression_string)
   ...:         if not next_permutation(permutation, len(operations)):
   ...:             break
   ...: 
   ...:     return results
   ...: 
   ...: # Пример использования:
   ...: expected_value = 200
   ...: expressions = find_expression_with_expected_value(expected_value)
   ...: for expr in expressions:
   ...:     print(expr)
   ...: 
98+76-5+43-2-10
98-7+65+43+2-1+0
98-7+65+43+2-1-0
9-8+7-6-5-4-3+210
9-8-7-6-5+4+3+210

Но это не я, а чертгпт переписал на петухон

Исправление rtxtxtrx, :

Рабочий вроде:

~   
❯ ipython
Python 3.12.4 (main, Jun  7 2024, 06:33:07) [GCC 14.1.1 20240522]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.26.0 -- An enhanced Interactive Python. Type '?' for help.

   ...:     digits = ['9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
   ...:     operations = ['', '+', '-']
   ...:     permutation = [0] * (len(digits) - 1)
   ...: 
   ...:     def next_permutation(permutation, cardinality):
   ...:         i = len(permutation) - 1
   ...:         while i >= 0 and permutation[i] == cardinality - 1:
   ...:             permutation[i] = 0
   ...:             i -= 1
   ...:         if i < 0:
   ...:             return False
   ...:         permutation[i] += 1
   ...:         return True
   ...: 
   ...:     results = []
   ...: 
   ...:     while True:
   ...:         expression_chars = []
   ...:         for i in range(len(digits) - 1):
   ...:             expression_chars.append(digits[i])
   ...:             expression_chars.append(operations[permutation[i]])
   ...:         expression_chars.append(digits[-1])
   ...:         expression_string = ''.join(expression_chars)
   ...:         expression_value = eval(expression_string)
   ...:         if expression_value == expected_value:
   ...:             results.append(expression_string)
   ...:         if not next_permutation(permutation, len(operations)):
   ...:             break
   ...: 
   ...:     return results
   ...: 
   ...: # Пример использования:
   ...: expected_value = 200
   ...: expressions = find_expression_with_expected_value(expected_value)
   ...: for expr in expressions:
   ...:     print(expr)
   ...: 
98+76-5+43-2-10
98-7+65+43+2-1+0
98-7+65+43+2-1-0
9-8+7-6-5-4-3+210
9-8-7-6-5+4+3+210

Но это не я, а чертгпт переписал на петухон

Исходная версия rtxtxtrx, :

Рабочий вроде:

~   
❯ ipython
Python 3.12.4 (main, Jun  7 2024, 06:33:07) [GCC 14.1.1 20240522]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.26.0 -- An enhanced Interactive Python. Type '?' for help.

   ...:     digits = ['9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
   ...:     operations = ['', '+', '-']
   ...:     permutation = [0] * (len(digits) - 1)
   ...: 
   ...:     def next_permutation(permutation, cardinality):
   ...:         i = len(permutation) - 1
   ...:         while i >= 0 and permutation[i] == cardinality - 1:
   ...:             permutation[i] = 0
   ...:             i -= 1
   ...:         if i < 0:
   ...:             return False
   ...:         permutation[i] += 1
   ...:         return True
   ...: 
   ...:     results = []
   ...: 
   ...:     while True:
   ...:         expression_chars = []
   ...:         for i in range(len(digits) - 1):
   ...:             expression_chars.append(digits[i])
   ...:             expression_chars.append(operations[permutation[i]])
   ...:         expression_chars.append(digits[-1])
   ...:         expression_string = ''.join(expression_chars)
   ...:         expression_value = eval(expression_string)
   ...:         if expression_value == expected_value:
   ...:             results.append(expression_string)
   ...:         if not next_permutation(permutation, len(operations)):
   ...:             break
   ...: 
   ...:     return results
   ...: 
   ...: # Пример использования:
   ...: expected_value = 200
   ...: expressions = find_expression_with_expected_value(expected_value)
   ...: for expr in expressions:
   ...:     print(expr)
   ...: 
98+76-5+43-2-10
98-7+65+43+2-1+0
98-7+65+43+2-1-0
9-8+7-6-5-4-3+210
9-8-7-6-5+4+3+210

Но это не я, а чертгпт переписал на петухон