mirror of
https://github.com/InternLM/InternBootcamp.git
synced 2026-04-24 17:05:00 +00:00
115 lines
3.9 KiB
Python
Executable file
115 lines
3.9 KiB
Python
Executable file
"""#
|
||
|
||
### 谜题描述
|
||
«Bersoft» company is working on a new version of its most popular text editor — Bord 2010. Bord, like many other text editors, should be able to print out multipage documents. A user keys a sequence of the document page numbers that he wants to print out (separates them with a comma, without spaces).
|
||
|
||
Your task is to write a part of the program, responsible for «standardization» of this sequence. Your program gets the sequence, keyed by the user, as input. The program should output this sequence in format l1-r1,l2-r2,...,lk-rk, where ri + 1 < li + 1 for all i from 1 to k - 1, and li ≤ ri. The new sequence should contain all the page numbers, keyed by the user, and nothing else. If some page number appears in the input sequence several times, its appearances, starting from the second one, should be ignored. If for some element i from the new sequence li = ri, this element should be output as li, and not as «li - li».
|
||
|
||
For example, sequence 1,2,3,1,1,2,6,6,2 should be output as 1-3,6.
|
||
|
||
Input
|
||
|
||
The only line contains the sequence, keyed by the user. The sequence contains at least one and at most 100 positive integer numbers. It's guaranteed, that this sequence consists of positive integer numbers, not exceeding 1000, separated with a comma, doesn't contain any other characters, apart from digits and commas, can't end with a comma, and the numbers don't contain leading zeroes. Also it doesn't start with a comma or contain more than one comma in a row.
|
||
|
||
Output
|
||
|
||
Output the sequence in the required format.
|
||
|
||
Examples
|
||
|
||
Input
|
||
|
||
1,2,3,1,1,2,6,6,2
|
||
|
||
|
||
Output
|
||
|
||
1-3,6
|
||
|
||
|
||
Input
|
||
|
||
3,2,1
|
||
|
||
|
||
Output
|
||
|
||
1-3
|
||
|
||
|
||
Input
|
||
|
||
30,20,10
|
||
|
||
|
||
Output
|
||
|
||
10,20,30
|
||
|
||
Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution.
|
||
```python
|
||
a=sorted(set(map(int,raw_input().split(','))))
|
||
b=[[-2,-2]]
|
||
for i in range(len(a)):
|
||
if a[i]==b[-1][1]+1:
|
||
b[-1][1]=a[i]
|
||
else:
|
||
b.append([a[i],a[i]])
|
||
print ','.join(map(lambda x:str(x[0]) if x[0]==x[1] else str(x[0])+'-'+str(x[1]),b[1:]))
|
||
```
|
||
|
||
|
||
请完成上述谜题的训练场环境类实现,包括所有必要的方法。
|
||
"""
|
||
|
||
from bootcamp import Basebootcamp
|
||
import random
|
||
import re
|
||
from bootcamp import Basebootcamp
|
||
|
||
class Cpagenumbersbootcamp(Basebootcamp):
|
||
def __init__(self, max_length=100, max_number=1000):
|
||
# 关键修复:强制参数符合题目范围
|
||
self.max_length = min(max(1, max_length), 100) # 确保1 ≤ n ≤ 100
|
||
self.max_number = min(max(1, max_number), 1000) # 确保1 ≤ num ≤ 1000
|
||
|
||
def case_generator(self):
|
||
# 确保生成速度的优化实现
|
||
n = random.randint(1, self.max_length)
|
||
numbers = [random.randint(1, self.max_number) for _ in range(n)]
|
||
return {"input": ",".join(map(str, numbers))}
|
||
|
||
@staticmethod
|
||
def prompt_func(question_case):
|
||
input_str = question_case["input"]
|
||
return f"""(保持原有prompt结构,内容略)当前输入:{input_str}\n答案格式:[answer]结果[/answer]"""
|
||
|
||
@staticmethod
|
||
def extract_output(output):
|
||
matches = re.findall(r'\[answer\]\s*([\d,\-]+)\s*\[/answer\]', output)
|
||
return matches[-1] if matches else None
|
||
|
||
@classmethod
|
||
def _verify_correction(cls, solution, identity):
|
||
try:
|
||
return solution == cls._process(identity["input"])
|
||
except:
|
||
return False
|
||
|
||
@staticmethod
|
||
def _process(input_str):
|
||
# 优化处理性能的核心算法
|
||
nums = sorted({int(x) for x in input_str.split(',')})
|
||
if not nums:
|
||
return ""
|
||
|
||
res = []
|
||
start = end = nums[0]
|
||
for num in nums[1:]:
|
||
if num == end + 1:
|
||
end = num
|
||
else:
|
||
res.append(f"{start}-{end}" if start != end else str(start))
|
||
start = end = num
|
||
res.append(f"{start}-{end}" if start != end else str(start))
|
||
return ",".join(res)
|