InternBootcamp/internbootcamp/bootcamp/eyaroslavandalgorithm/eyaroslavandalgorithm.py
2025-05-23 15:27:15 +08:00

216 lines
7.5 KiB
Python
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""#
### 谜题描述
Yaroslav likes algorithms. We'll describe one of his favorite algorithms.
1. The algorithm receives a string as the input. We denote this input string as a.
2. The algorithm consists of some number of command. Сommand number i looks either as si >> wi, or as si <> wi, where si and wi are some possibly empty strings of length at most 7, consisting of digits and characters \"?\".
3. At each iteration, the algorithm looks for a command with the minimum index i, such that si occurs in a as a substring. If this command is not found the algorithm terminates.
4. Let's denote the number of the found command as k. In string a the first occurrence of the string sk is replaced by string wk. If the found command at that had form sk >> wk, then the algorithm continues its execution and proceeds to the next iteration. Otherwise, the algorithm terminates.
5. The value of string a after algorithm termination is considered to be the output of the algorithm.
Yaroslav has a set of n positive integers, he needs to come up with his favorite algorithm that will increase each of the given numbers by one. More formally, if we consider each number as a string representing the decimal representation of the number, then being run on each of these strings separately, the algorithm should receive the output string that is a recording of the corresponding number increased by one.
Help Yaroslav.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the set. The next n lines contains one positive integer each. All the given numbers are less than 1025.
Output
Print the algorithm which can individually increase each number of the set. In the i-th line print the command number i without spaces.
Your algorithm will be launched for each of these numbers. The answer will be considered correct if:
* Each line will a correct algorithm command (see the description in the problem statement).
* The number of commands should not exceed 50.
* The algorithm will increase each of the given numbers by one.
* To get a respond, the algorithm will perform no more than 200 iterations for each number.
Examples
Input
2
10
79
Output
10&lt;&gt;11
79&lt;&gt;80
Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution.
```python
C = [
\"??0>>0??\",
\"??1>>1??\",
\"??2>>2??\",
\"??3>>3??\",
\"??4>>4??\",
\"??5>>5??\",
\"??6>>6??\",
\"??7>>7??\",
\"??8>>8??\",
\"??9>>9??\",
\"??>>?\",
\"0?<>1\",
\"1?<>2\",
\"2?<>3\",
\"3?<>4\",
\"4?<>5\",
\"5?<>6\",
\"6?<>7\",
\"7?<>8\",
\"8?<>9\",
\"9?>>?0\",
\"?<>1\",
\"0>>??0\",
\"1>>??1\",
\"2>>??2\",
\"3>>??3\",
\"4>>??4\",
\"5>>??5\",
\"6>>??6\",
\"7>>??7\",
\"8>>??8\",
\"9>>??9\",
]
print '\n'.join(C)
```
请完成上述谜题的训练场环境类实现,包括所有必要的方法。
"""
from bootcamp import Basebootcamp
import random
from bootcamp import Basebootcamp
class Eyaroslavandalgorithmbootcamp(Basebootcamp):
def __init__(self, **params):
self.params = {
'n': params.get('n', 2),
'max_iterations': 200,
'max_commands': 50
}
def case_generator(self):
n = self.params['n']
numbers = []
for _ in range(n):
num_length = random.randint(1, 25)
# 生成包含进位的测试用例
if random.random() < 0.3:
num_str = '9' * num_length
elif random.random() < 0.3:
num_str = '1' + '9' * (num_length - 1)
else:
num = random.randint(1, 10**25)
num_str = str(num).lstrip('0')
if not num_str:
num_str = '0'
numbers.append(num_str)
return {'n': n, 'numbers': numbers, 'max_iterations': self.params['max_iterations'], 'max_commands': self.params['max_commands']}
@staticmethod
def prompt_func(question_case):
n = question_case['n']
numbers = question_case['numbers']
problem = "设计一个算法使得对于以下每个数字算法将该数字增加1。"
problem += "算法由一系列命令组成,每个命令的形式为 'si >> wi''si <> wi'"
problem += "其中si和wi是长度不超过7的字符串可包含数字和'?'"
problem += "算法执行过程如下:\n"
problem += "1. 每次找到第一个匹配的命令,替换后可能继续或终止。\n"
problem += "2. 如果使用 '>>',则继续执行;如果使用 '<>',则终止。\n"
problem += "以下是要处理的数字:\n"
for num in numbers:
problem += f"{num}\n"
problem += "请给出你的命令列表,每个命令占一行,放在[answer]标签内。\n"
problem += "示例:\n"
problem += "[answer]\n"
problem += "10<>11\n"
problem += "79<>80\n"
problem += "[/answer]\n"
return problem
@staticmethod
def extract_output(output):
start = output.rfind("[answer]")
if start == -1:
return None
end = output.find("[/answer]", start)
if end == -1:
return None
content = output[start+8:end].strip()
commands = []
for line in content.split('\n'):
line = line.strip()
if not line:
continue
if '>>' in line:
parts = line.split('>>', 1)
if len(parts) != 2:
return None
si, wi = parts
if len(si) > 7 or len(wi) > 7:
return None
if not all(c in '0123456789?' for c in si + wi):
return None
elif '<>' in line:
parts = line.split('<>', 1)
if len(parts) != 2:
return None
si, wi = parts
if len(si) > 7 or len(wi) > 7:
return None
if not all(c in '0123456789?' for c in si + wi):
return None
else:
return None
commands.append(line)
return commands
@classmethod
def _verify_correction(cls, solution, identity):
if not solution:
return False
if len(solution) > identity.get('max_commands', 50):
return False
numbers = identity['numbers']
max_iterations = identity.get('max_iterations', 200)
for s in numbers:
input_str = s
expected = str(int(s) + 1)
a = input_str
iterations = 0
while iterations < max_iterations:
found = False
for cmd in solution:
if '>>' in cmd:
si, wi = cmd.split('>>', 1)
cmd_type = '>>'
elif '<>' in cmd:
si, wi = cmd.split('<>', 1)
cmd_type = '<>'
else:
continue
pos = a.find(si)
if pos != -1:
a = a[:pos] + wi + a[pos+len(si):]
found = True
if cmd_type == '<>':
break
break
if not found:
break
iterations += 1
if iterations >= max_iterations:
break
if a != expected:
return False
return True