-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion_17.py
executable file
·70 lines (60 loc) · 1.92 KB
/
question_17.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
__author__ = 'Chaithra'
notes = '''
This problem deals with circular linked lists. The circular list is sorted, but the head does not point to the lowest
element, it can point to a random element.
'''
from listutils import *
# insert into a sorted linked list so that the resulting list is still sorted. head does *NOT* point to the minimum node.
# do not modify head unless required.
def insert(head,node):
prev=None
temp=prev=head
if temp.value>node.value:
if temp==head:
node.next=temp
while temp.next!=head:
temp=temp.next
temp.next=node
return node
temp=temp.next
while temp!=head:
if temp.value>node.value:
node.next=temp
prev.next=node
return head
else:
prev=temp
temp = temp.next
prev.next=node
node.next=head
return head
def insert_sorted(head, value):
node=to_circular_list([value])
if node==None:
return head
if head==None:
return node
min=head.value
min_node=head
temp=head.next
while temp!=head:
if min>temp.value:
min=temp.value
min_node=temp
else:
temp=temp.next
insert(min_node,node)
return head
def check_insertion(input, value, output):
head = to_circular_list(input)
head = insert_sorted(head, value)
assert output == from_circular_list(head)
def test_insert_sorted():
check_insertion([11, 13, 5, 7, 9], 15, [11,13, 15, 5, 7, 9])
check_insertion([11, 13, 5, 7, 9], 10, [11, 13, 5, 7, 9, 10])
check_insertion([11, 13, 5, 7, 9], 3, [11, 13, 3, 5, 7, 9])
check_insertion([11, 13, 5, 7, 9], 6, [11, 13, 5, 6, 7, 9])
check_insertion([5], 10, [5,10])
check_insertion([5], 1, [5,1])
check_insertion([], 10, [10])
check_insertion([5, 8], 7, [5,7,8])