Python self.next.prev=self.prev 类双链接列表: 定义初始(自我,头部): self.head=头 @静力学方法 来自_列表(lst)的def: sentinel=DoublyLinkedNode(-math.inf) 以前的=哨兵 对于lst中的项目: 节点=双链接节点(项目) node.prev=上一个 previous.next=节点 上一个=节点 返回双链接列表(sentinel) def to_列表(自身): 结果=[] 当前=self.head.next 虽然当前值不是“无”: result.append(current.value) 当前=当前。下一步 返回结果
以下是我用来验证代码的单元测试:Python self.next.prev=self.prev 类双链接列表: 定义初始(自我,头部): self.head=头 @静力学方法 来自_列表(lst)的def: sentinel=DoublyLinkedNode(-math.inf) 以前的=哨兵 对于lst中的项目: 节点=双链接节点(项目) node.prev=上一个 previous.next=节点 上一个=节点 返回双链接列表(sentinel) def to_列表(自身): 结果=[] 当前=self.head.next 虽然当前值不是“无”: result.append(current.value) 当前=当前。下一步 返回结果,python,arrays,algorithm,sorting,shuffle,Python,Arrays,Algorithm,Sorting,Shuffle,以下是我用来验证代码的单元测试: 导入单元测试 类TestSort(unittest.TestCase): def测试_排序(自): 测试用例=[ #(投入、预期结果) ([1, 2, 3, 4, 10, 5, 6], [1, 2, 3, 4, 5, 6, 10]), ([1, 2, 5, 4, 10, 6, 0], [0, 1, 2, 4, 5, 6, 10]), ([1], [1]), ([1, 3, 2], [1, 2, 3]), ([], []) ] 对于测试用例中的(测试输入,预期):
导入单元测试
类TestSort(unittest.TestCase):
def测试_排序(自):
测试用例=[
#(投入、预期结果)
([1, 2, 3, 4, 10, 5, 6], [1, 2, 3, 4, 5, 6, 10]),
([1, 2, 5, 4, 10, 6, 0], [0, 1, 2, 4, 5, 6, 10]),
([1], [1]),
([1, 3, 2], [1, 2, 3]),
([], [])
]
对于测试用例中的(测试输入,预期):
结果=排序(测试输入)
self.assertEqual(预期、结果)
您是从数组中选择k个元素还是选择k个随机整数?你说数组中有k个元素,但是你的代码创建了新的随机数ints@zamsler这是等价的。将k个新的随机整数想象成它们位于n
大小的数组中,被选中并从中删除。“nk排序”数组通常意味着没有任何元素距离其排序位置超过k
个位置。对k
个元素进行随机洗牌可能会产生交换第一个和最后一个洗牌元素的结果,并且每个元素都会远离其排序位置k
个位置。您应该要求对此进行澄清。@rcgldr这是非常清楚的,没有给出这样的限制。他们被随机地混在一起。他们甚至给出了2 3 4 5 6 1
@Gulzar的例子——我所想到的实际术语是一个被我自己接受的答案。接得好,我其实不知道那个细节。
import numpy as np
def __generate_unsorted_array(size, is_integer=False, max_int_value=100000):
return np.random.randint(max_int_value, size=size) if is_integer else np.random.rand(size)
def generate_nk_unsorted_array(n, k, is_integer=False, max_int_value=100000):
assert k <= n
unsorted_n_array = __generate_unsorted_array(n - k, is_integer, max_int_value=max_int_value)
sorted_n_array = sorted(unsorted_n_array)
random_k_array = __generate_unsorted_array(k, is_integer, max_int_value=max_int_value)
insertion_inds = np.random.choice(n - k + 1, k, replace=True) # can put two unsorted next to each other.
nk_unsorted_array = np.insert(sorted_n_array, insertion_inds, random_k_array)
return list(nk_unsorted_array)
def merge_sorted_lists(la, lb):
if la is None or la == []:
return lb
if lb is None or lb == []:
return la
a_ind = b_ind = 0
a_len = len(la)
b_len = len(lb)
merged = []
while a_ind < a_len and b_ind < b_len:
a_value = la[a_ind]
b_value = lb[b_ind]
if a_value < b_value:
merged.append(la[a_ind])
a_ind += 1
else:
merged.append(lb[b_ind])
b_ind += 1
# get the leftovers into merged
while a_ind < a_len:
merged.append(la[a_ind])
a_ind += 1
while b_ind < b_len:
merged.append(lb[b_ind])
b_ind += 1
return merged
def sort_nk_unsorted_list(nk_unsorted_list):
working_copy = nk_unsorted_list.copy() # just for ease of testing
requires_resorting = []
current_list_length = len(working_copy)
i = 0
while i < current_list_length - 1 and 1 < current_list_length:
if i == -1:
i = 0
first = working_copy[i]
second = working_copy[i + 1]
if second < first:
requires_resorting.append(first)
requires_resorting.append(second)
del working_copy[i + 1]
del working_copy[i]
i -= 2
current_list_length -= 2
i += 1
sorted_2k_elements = sorted(requires_resorting)
sorted_nk_list = merge_sorted_lists(sorted_2k_elements, working_copy)
return sorted_nk_list