1+ """
2+ In order to win the prize for most cookies sold, my friend Alice and
3+ # I are going to merge our Girl Scout Cookies orders and enter as one unit.
4+ # Each order is represented by an "order id" (an integer).
5+ We have our lists of orders sorted numerically already, in lists.
6+ Write a function to merge our lists of orders into one sorted list.
7+ """
8+
9+ def merge_lists (my_list , alices_list ):
10+
11+ result = []
12+ index_alice_list = 0
13+ index_my_list = 0
14+
15+ while index_alice_list < len (alices_list ) and index_my_list < len (my_list ):
16+ if alices_list [index_alice_list ] < my_list [index_my_list ]:
17+ result .append (alices_list [index_alice_list ])
18+ index_alice_list += 1
19+ elif alices_list [index_alice_list ] > my_list [index_my_list ]:
20+ result .append (my_list [index_my_list ])
21+ index_my_list += 1
22+ else :
23+ result .append (my_list [index_my_list ])
24+ result .append (alices_list [index_alice_list ])
25+ index_my_list += 1
26+ index_alice_list += 1
27+
28+ if index_alice_list < len (alices_list ):
29+ result .extend (alices_list [index_alice_list :])
30+
31+ if index_my_list < len (my_list ):
32+ result .extend (my_list [index_my_list :])
33+
34+ return result
35+
36+
37+ my_list = [3 , 4 , 6 , 10 , 11 , 15 ]
38+ alices_list = [1 , 5 , 8 , 12 , 14 , 19 ]
39+
40+
41+ print merge_lists (my_list , alices_list )
42+ print "Must be [1, 3, 4, 5, 6, 8, 10, 11, 12, 14, 15, 19]"
43+
44+
45+ # Or just using Timsort
46+ def merge_sorted_lists (arr1 , arr2 ):
47+ return sorted (arr1 + arr2 )
48+
49+ print merge_sorted_lists (my_list , alices_list )
50+ print "Must be [1, 3, 4, 5, 6, 8, 10, 11, 12, 14, 15, 19]"
0 commit comments