Data Structures and Algorithms Python language chapter exercises

Consolidation

. 1  # R & lt-3.2 
2  # N0 = 16 
. 3  
. 4  # R & lt-3.3 
. 5  # N0 = 20 is 
. 6  
. 7  # R & lt-3.4 
. 8  # constant function, a linear function? 
. 9  
10  # R & lt-3.5 of 
. 11  # logN the c-th power = clogn The slope is a fixed constant, so that a straight line 
12 is  
13 is  # R & lt-3.6 
14  # 2 * (. 1 + 0 + 2 + ... + n-) = 2 * (n-* (n-+. 1)) / 2 = n-* (n- + 1'd) 
15  
16  # R & lt-3.7 
. 17  # big O notation listed complexity of the algorithm in the worst case 
18 is  
. 19  # R & lt 3.23- 
20 is  O (n-)
 21 is 
22 is  # R & lt-3.24 
23 is  O (n-)
 24  
25  # R & lt-3.25 
26 is  O (N²)
 27  
28  # R & lt-3.26 
29  O (n-)
 30  
31 is  # R & lt-3.27 
32  O (n³)
 33 is  
34 is  # R & lt-3.29 
35  O (nlogn)
 36  
37 [  # R & lt 3.30- 
38 is  O (nlogn)
 39  
40  # R & lt 3.31- 
41 is  the best running time: O (nlogn)
 42 is  worst case running time: O (N²)
 43 is  
44 is  # R & lt 3.32- 
45  O ( n-)
 46 is  
47 # R & lt 3.33- 
48  interference factor when the value is small, a large disturbance factors
 49  
50  # R & lt 3.34- 
51 is 1/1 + 1/2 + 1/3 + ... +. 1 / J + ... + 1 / n (n harmonic number)
Consolidate part of the code reference

Innovation

  . 1  # C-3.35 
  2  # using the sort number plus three adjacent traverse are the same: O (nlogn) + O (n-) ---> O (nlogn) 
  . 3  
  . 4  # C-3.36 
  . 5  Import Random
   . 6  
  . 7  
  . 8  class GetBigNum:
   . 9      DEF  the __init__ (Self, Data):
 10          self.data = Data
 . 11          self.n = len (Data)
 12 is  
13 is      DEF get_some_bigger (Self, NUM):
 14          bigger_data = []
 15          the while len (self.data)> Self. n-- NUM:
 16             max_val = self.get_max()
 17             bigger_data.append(max_val)
 18             self.data.remove(max_val)
 19         return bigger_data
 20 
 21     def get_max(self):
 22         max_val = self.data[0]
 23         for i in range(1, len(self.data)):
 24             if self.data[i] > max_val:
 25                 max_val = self.data[i]
 26         return max_val
 27 
 28 
 29 # 算法运行时间:O(n)
 30 # test_data = [1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 14, 15, 15, 16, 17, 18, 19]
 31 # big_num = GetBigNum(test_data)
 32 # print(big_num.get_some_bigger(10))
 33 
 34 # 或者使用排序加data[-10:]  O(nlogn)
 35 
 36 # C-3.45
 37 # def find_unique_lost(s):
 38 #     total = 1
 39 #     for i in range(len(s) + 1):
 40 #         total *= (i + 1)
 41 #     for j in s:
 42 #         total /= (j + 1)
 43 #     return int (Total -. 1) 
44 is  
45  
46 is  # test_s = [0,. 1, 2,. 3,. 4,. 6] 
47  # Print (find_unique_lost (test_s)) 
48  
49  
50  # C-3.46 
51 is  # 2 sheep is a special case 
52  
53 is  
54 is  # C-3.50 
55  # 1. 
56 is  DEF sum_num (X, n-, Data):
 57 is      sum_result = 0
 58  
59      for I in Range (n-+. 1 ):
 60          sum_xi. 1 =
 61 is          for J in range(i):
 62             sum_xi *= x
 63         sum_result += data[i] * sum_xi
 64     return sum_result
 65 
 66 
 67 # print(sum_num(2, 3, [1, 2, 3, 4]))
 68 
 69 
 70 # 2.
 71 def sum_num2(x, n, data):
 72     sum_result = 0
 73     sum_xi = 1
 74     for i in range(n+1):
 75         sum_xi *= x
 76         sum_result += data[i] * sum_xi
 77     return sum_result / x
 78 
 79 
 80 # print(sum_num2(2, 3, [1, 2, 3, 4]))
 81 
 82 
 83 # C-3.54
 84 from collections import defaultdict
 85 
 86 def get_max_amount(data):
 87     total_amount = 0
 88     n = len(data)
 89     max_amount = 0
 90     max_amount_dic = defaultdict()
 91     max_amount_dic['max_amount'] = dict()
 92     while total_amount < n:
 93         for i in range(n):
 94             val_amount = 1
 95             for val in data[i+1:]:
 96                 if data[i] == val:
 97                     val_amount += 1
 98             if val_amount >= max_amount:
 99                 max_amount = val_amount
100                 max_amount_val = set()
101                 max_amount_val.add(data[i])
102                 if max_amount in max_amount_dic['max_amount']:
103                     max_amount_val.update(max_amount_dic['max_amount'])
104                 max_amount_dic['max_amount'] = {max_amount: max_amount_val}
105                 total_amount += val_amount
106     return max_amount_dic
107 
108 
109 # test_data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 60]
110 # print(get_max_amount(test_data))
111 #
112 # S = []
113 # for i in range(1000):
114 #     S.append(random.randint(0, 4000))
115 # print(get_max_amount(S))
116 
117 
118 # 更优解
119 def find_most_frequent(n):
120     s = []
121     for _ in range(n):
122         s.append(random.randint(0, 4*n - 1))
123 
124     restore_list = [0] * (4*n)
125     most_int = 0
126     for num in s:
127         restore_list[num] += 1
128         if restore_list[num] >= restore_list[most_int]:
129             most_int = num
130     if most_int == 1:
131         return False
132     else:
133         return most_int, restore_list[most_int]
134 
135 
136 print(find_most_frequent(1000))
Innovation part of the code reference

 

Guess you like

Origin www.cnblogs.com/xiaomingyang/p/12554471.html
Recommended