Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.49 KB | None | 0 0
  1. """
  2. -------------------------------------------------------
  3. sorted_list_array.py
  4. Array version of the SortedList ADT.
  5. -------------------------------------------------------
  6. Author: David Brown
  7. ID: 999999999
  8. Email: dbrown@wlu.ca
  9. __updated__ = "2017-02-10"
  10. -------------------------------------------------------
  11. """
  12. from copy import deepcopy
  13.  
  14.  
  15. class SortedList:
  16.  
  17. def __init__(self):
  18. """
  19. -------------------------------------------------------
  20. Initializes an empty SortedList.
  21. Use: sl = SortedList()
  22. -------------------------------------------------------
  23. Postconditions:
  24. Initializes an empty sorted list.
  25. -------------------------------------------------------
  26. """
  27. self._values = []
  28. return
  29.  
  30. def is_empty(self):
  31. """
  32. -------------------------------------------------------
  33. Determines if the sorted list is empty.
  34. Use: b = sl.is_empty()
  35. -------------------------------------------------------
  36. Postconditions:
  37. Returns True if the sorted list is empty, False otherwise.
  38. -------------------------------------------------------
  39. """
  40. return len(self._values) == 0
  41.  
  42. def __len__(self):
  43. """
  44. -------------------------------------------------------
  45. Returns the size of the sorted list.
  46. Use: n = len(sl)
  47. -------------------------------------------------------
  48. Postconditions:
  49. Returns the number of values in the sorted list.
  50. -------------------------------------------------------
  51. """
  52. return len(self._values)
  53.  
  54. def insert(self, value):
  55. """
  56. -------------------------------------------------------
  57. Inserts value at the proper place in the sorted list.
  58. Must be a stable insertion, i.e. consecutive insertions
  59. of the same value must keep their order preserved.
  60. Use: sl.insert(value)
  61. -------------------------------------------------------
  62. Preconditions:
  63. value - a data element (?)
  64. Postconditions:
  65. value inserted at its sorted position within the sorted list.
  66. -------------------------------------------------------
  67. """
  68. # Index of beginning of subarray to search.
  69. low = 0
  70. # Index of end of subarray to search.
  71. high = len(self._values) - 1
  72.  
  73. while low <= high:
  74. # Find the middle of the current subarray.
  75. # (avoids overflow on large values vs (low + high)//2
  76. mid = (high - low) // 2 + low
  77.  
  78. if self._values[mid] > value:
  79. # search the left subarray.
  80. high = mid - 1
  81. else:
  82. # Default: search the right subarray.
  83. low = mid + 1
  84. self._values.insert(low, value)
  85. return
  86.  
  87. def _binary_search(self, key):
  88. """
  89. -------------------------------------------------------
  90. Searches for the first occurrence of key in the sorted list.
  91. Performs a stable search.
  92. Private helper method - used only by other ADT methods.
  93. Use: i = self._binary_search(key)
  94. -------------------------------------------------------
  95. Preconditions:
  96. key - a data element (?)
  97. Postconditions:
  98. returns
  99. i - the index of the first occurrence of key in
  100. the list, -1 if key is not found. (int)
  101. -------------------------------------------------------
  102. """
  103. # Index of beginning of subarray to search.
  104. low = 0
  105. # Index of end of subarray to search.
  106. high = len(self._values) - 1
  107.  
  108. while low < high:
  109. # Find the middle of the current subarray.
  110. # (avoids overflow on large values vs (low + high)//2
  111. mid = (high - low) // 2 + low
  112.  
  113. if self._values[mid] < key:
  114. # Search the right subarray.
  115. low = mid + 1
  116. else:
  117. # Default: search the left subarray.
  118. high = mid
  119.  
  120. # Deferred test for equality.
  121. if low == high and self._values[low] == key:
  122. i = low
  123. else:
  124. i = -1
  125. return i
  126.  
  127. def remove(self, key):
  128. """
  129. -------------------------------------------------------
  130. Finds, removes, and returns the value in the sorted list that matches key.
  131. Use: value = sl.remove( key )
  132. -------------------------------------------------------
  133. Preconditions:
  134. key - a partial data element (?)
  135. Postconditions:
  136. returns
  137. value - the full value matching key, otherwise None (?)
  138. -------------------------------------------------------
  139. """
  140.  
  141. i = self._binary_search(key)
  142.  
  143. if i == -1:
  144. value = None
  145. else:
  146. value = self._values.pop(i)
  147.  
  148. return value
  149.  
  150.  
  151.  
  152. def find(self, key):
  153. """
  154. -------------------------------------------------------
  155. Finds and returns a copy of value in list that matches key.
  156. Use: value = l.find( key )
  157. -------------------------------------------------------
  158. Preconditions:
  159. key - a partial data element (?)
  160. Postconditions:
  161. returns
  162. value - a copy of the full value matching key, otherwise None (?)
  163. -------------------------------------------------------
  164. """
  165. assert len(self._values) > 0, "Cannot find in an empty list"
  166.  
  167. i = self._binary_search(key)
  168.  
  169. if i == -1:
  170. value = None
  171. else:
  172. value = self._values[i]
  173.  
  174. return deepcopy(value)
  175.  
  176.  
  177. def peek(self):
  178. """
  179. -------------------------------------------------------
  180. Returns a copy of the first value in list.
  181. Use: value = l.peek()
  182. -------------------------------------------------------
  183. Postconditions:
  184. returns
  185. value - a copy of the first value in the list (?)
  186. -------------------------------------------------------
  187. """
  188. assert len(self._values) > 0, "Cannot peek at an empty list"
  189.  
  190. value = self._values[0]
  191.  
  192. return deepcopy(value)
  193.  
  194. def index(self, key):
  195. """
  196. -------------------------------------------------------
  197. Finds the location of the first occurrence of key in the sorted list.
  198. Use: n = sl.index( key )
  199. -------------------------------------------------------
  200. Preconditions:
  201. key - a data element (?)
  202. Postconditions:
  203. returns
  204. i - the location of the full value matching key, otherwise -1 (int)
  205. -------------------------------------------------------
  206. """
  207.  
  208.  
  209. i = self._binary_search(key)
  210.  
  211. return i
  212.  
  213. def _valid_index(self, i):
  214. """
  215. -------------------------------------------------------
  216. Private helper method to validate an index value.
  217. Python index values can be positive or negative and range from
  218. -len(list) to len(list) - 1
  219. Use: assert self._valid_index(i)
  220. -------------------------------------------------------
  221. Preconditions:
  222. i - an index value (int)
  223. Postconditions:
  224. returns
  225. True if i is a valid index, False otherwise.
  226. -------------------------------------------------------
  227. """
  228. n = len(self._values)
  229. return -n <= i < n
  230.  
  231. def __getitem__(self, i):
  232. """
  233. ---------------------------------------------------------
  234. Returns a copy of the nth element of the sorted list.
  235. Use: value = sl[i]
  236. -------------------------------------------------------
  237. Preconditions:
  238. i - index of the element to access (int)
  239. Postconditions:
  240. returns
  241. value - the i-th element of the sorted list (?)
  242. -------------------------------------------------------
  243. """
  244. assert self._valid_index(i), "Invalid index value"
  245.  
  246. value = self._values[i]
  247.  
  248. return deepcopy(value)
  249.  
  250.  
  251.  
  252. def __contains__(self, key):
  253. """
  254. ---------------------------------------------------------
  255. Determines if the sorted list contains key.
  256. Use: b = key in sl
  257. -------------------------------------------------------
  258. Preconditions:
  259. key - a partial data element (?)
  260. Postconditions:
  261. returns
  262. True if the sorted list contains key, False otherwise. (boolean)
  263. -------------------------------------------------------
  264. """
  265.  
  266. i = self._binary_search(key)
  267.  
  268. return i > -1
  269.  
  270.  
  271.  
  272. def max(self):
  273. """
  274. -------------------------------------------------------
  275. Finds the maximum value in the sorted list.
  276. Use: value = sl.max()
  277. -------------------------------------------------------
  278. Postconditions:
  279. returns
  280. value - a copy of the maximum value in the sorted list (?)
  281. -------------------------------------------------------
  282. """
  283. assert len(self._values) > 0, "Cannot find maximum of an empty list"
  284.  
  285. value = self._values[-1]
  286.  
  287. return deepcopy(value)
  288.  
  289. def min(self):
  290. """
  291. -------------------------------------------------------
  292. Finds the minimum value in the sorted list.
  293. Use: value = sl.min()
  294. -------------------------------------------------------
  295. Postconditions:
  296. returns
  297. value - a copy of the minimum value in the sorted list (?)
  298. -------------------------------------------------------
  299. """
  300. assert len(self._values) > 0, "Cannot find minimum of an empty list"
  301.  
  302. value = self._values[0]
  303.  
  304. return deepcopy(value)
  305.  
  306.  
  307. def count(self, key):
  308. """
  309. -------------------------------------------------------
  310. Determines the number of times key appears in list.
  311. Use: n = sl.count(key)
  312. -------------------------------------------------------
  313. Preconditions:
  314. key - a data element (?)
  315. Postconditions:
  316. returns
  317. number - number of appearances of key in list (int)
  318. -------------------------------------------------------
  319. """
  320. i = self._binary_search(key)
  321. n = len(self._values)
  322.  
  323. if i == -1:
  324. number = 0
  325. else:
  326.  
  327. number = 1
  328. i += 1
  329.  
  330. while i < n and self._values[i] == key:
  331. i += 1
  332. number += 1
  333. return number
  334.  
  335.  
  336.  
  337. def clean(self):
  338. """
  339. ---------------------------------------------------------
  340. Removes duplicates from the sorted list.
  341. Use: sl.clean()
  342. -------------------------------------------------------
  343. Postconditions:
  344. The list contains one and only one of each value formerly present
  345. in the list. The first occurrence of each value is preserved.
  346. -------------------------------------------------------
  347. """
  348.  
  349. temp = []
  350. i=0
  351. while i < len(self._values):
  352. if self._values[i] not in temp:
  353. temp.append(self._values[i])
  354. i+=1
  355.  
  356. #clear the self._values
  357.  
  358. while len(self._values) > 0:
  359. disc = self._values.pop()
  360.  
  361. while len(temp) > 0:
  362. val = temp.pop(0)
  363. self._values.append(val)
  364.  
  365.  
  366. return
  367.  
  368. def intersection(self, rs):
  369. """
  370. -------------------------------------------------------
  371. Returns a list that contains only values that appear in both
  372. the current List and rs.
  373. Use: new_list = sl.intersection(rs)
  374. -------------------------------------------------------
  375. Preconditions:
  376. rs - another sorted list (SortedList)
  377. Postconditions:
  378. returns
  379. new_list - A new list that contains only the values found in both the current
  380. sorted list and second. Values do not repeat (SortedList)
  381. -------------------------------------------------------
  382. """
  383. new_list = SortedList()
  384.  
  385. i = 0
  386. while i < len(self._values):
  387. val = self._values[i]
  388. x = 0
  389. while x < len(rs._values):
  390. if val == rs._values[x] and val not in new_list:
  391. new_list._values.append(val)
  392. x+=1
  393.  
  394. x=0
  395. i+=1
  396.  
  397. return new_list
  398.  
  399. def union(self, rs):
  400. """
  401. -------------------------------------------------------
  402. Returns a list that contains all values in both
  403. the current sorted list and rs.
  404. Use: new_list = sl.union(rs)
  405. -------------------------------------------------------
  406. Preconditions:
  407. second - another sorted list (SortedList)
  408. Postconditions:
  409. returns
  410. new_list - A new list that contains all values found in both the current
  411. sorted list and second. Values do not repeat (SortedList)
  412. -------------------------------------------------------
  413. """
  414. new_list = SortedList()
  415.  
  416. temp = []
  417.  
  418. while len(self._values) > 0:
  419. val1 = self._values.pop()
  420. if val1 not in temp:
  421. temp.append(val1)
  422.  
  423. while len(rs._values) > 0:
  424. val2 = rs._values.pop()
  425. if val2 not in temp:
  426. temp.append(val2)
  427.  
  428.  
  429. i=0
  430. while i < len(temp):
  431. j =0
  432. while j < len(temp)-1:
  433. if temp[j] > temp[j+1]:
  434. temp[j+1], temp[j] = temp[j], temp[j+1]
  435. j+=1
  436. j=0
  437. i+=1
  438.  
  439. while len(temp) > 0:
  440. val3 = temp.pop(0)
  441. new_list._values.append(val3)
  442.  
  443.  
  444.  
  445.  
  446. return new_list
  447.  
  448. def pop(self, *args):
  449. """
  450. -------------------------------------------------------
  451. Finds, removes, and returns the value in list whose index matches i.
  452. Use: value = l.pop()
  453. Use: value = l.pop(i)
  454. -------------------------------------------------------
  455. Preconditions:
  456. args - an array of arguments (tuple of int)
  457. args[0], if it exists, is the index i
  458. Postconditions:
  459. returns
  460. value - if args exists, the value at position args[0], otherwise the last
  461. value in the list, value is removed from the list (?)
  462. -------------------------------------------------------
  463. """
  464. assert len(self._values) > 0, "Cannot pop from an empty list"
  465. assert len(args) <= 1, "No more than 1 argument allowed"
  466.  
  467. if len(args) == 1:
  468. # pop the element at position i
  469. i = args[0]
  470. value = self._values.pop(i)
  471. else:
  472. # pop the last element
  473. value = self._values.pop()
  474. return value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement