Write a program to complete the following command: new id - create a specified sequence of Numbers for id (id < 200000) Add id num - to join the sequence of Numbers for id integer num merge id1 id2 - merger sequence number of id1 and id2, and make id2 empty unique id - remove repetitive element in the sequence id out id - from small to large, output the elements of sequence of Numbers for ids separated by Spaces
时间: 2024-03-22 12:39:16 浏览: 37
Sure! Here is a Python program that can perform the operations you described:
```python
from collections import OrderedDict
class NumberSequence:
def __init__(self):
self.sequences = {}
def new(self, id):
if id >= 200000:
return "Error: id must be less than 200000"
self.sequences[id] = []
return "Success: created sequence for id {}".format(id)
def add(self, id, num):
if id not in self.sequences:
return "Error: sequence for id {} does not exist".format(id)
self.sequences[id].append(num)
return "Success: added {} to sequence for id {}".format(num, id)
def merge(self, id1, id2):
if id1 not in self.sequences:
return "Error: sequence for id {} does not exist".format(id1)
if id2 not in self.sequences:
return "Error: sequence for id {} does not exist".format(id2)
merged_sequence = self.sequences[id1] + self.sequences[id2]
self.sequences[id1] = merged_sequence
self.sequences[id2] = []
return "Success: merged sequence for id {} and id {}, and emptied id {}".format(id1, id2, id2)
def unique(self, id):
if id not in self.sequences:
return "Error: sequence for id {} does not exist".format(id)
unique_sequence = list(OrderedDict.fromkeys(self.sequences[id]))
self.sequences[id] = unique_sequence
return "Success: removed duplicate elements from sequence for id {}".format(id)
def out(self, id):
if id not in self.sequences:
return "Error: sequence for id {} does not exist".format(id)
sorted_sequence = sorted(self.sequences[id])
output_str = " ".join(str(num) for num in sorted_sequence)
return output_str
```
To use this program, you can create a `NumberSequence` object and call its methods to perform the desired operations. For example:
```python
ns = NumberSequence()
print(ns.new(1)) # Success: created sequence for id 1
print(ns.add(1, 5)) # Success: added 5 to sequence for id 1
print(ns.add(1, 3)) # Success: added 3 to sequence for id 1
print(ns.add(2, 7)) # Error: sequence for id 2 does not exist
print(ns.merge(1, 2)) # Error: sequence for id 2 does not exist
print(ns.new(2)) # Success: created sequence for id 2
print(ns.add(2, 7)) # Success: added 7 to sequence for id 2
print(ns.add(2, 3)) # Success: added 3 to sequence for id 2
print(ns.merge(1, 2)) # Success: merged sequence for id 1 and id 2, and emptied id 2
print(ns.out(1)) # 3 5 7
print(ns.unique(1)) # Success: removed duplicate elements from sequence for id 1
print(ns.out(1)) # 3 5 7
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)