Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.37 KB | None | 0 0
  1. """
  2. -------------------------------------------------------
  3. list_array.py
  4. [description of functions]
  5. -------------------------------------------------------
  6. Author: Muntake Lali
  7. ID: 170543610
  8. Email: lali3610@mylaurier.ca
  9. __updated__ = "2018-01-29"
  10. -------------------------------------------------------
  11. """
  12. from copy import deepcopy
  13.  
  14.  
  15. class List:
  16.  
  17. def __init__(self):
  18. """
  19. -------------------------------------------------------
  20. Initializes an empty list.
  21. Use: l = List()
  22. -------------------------------------------------------
  23. Postconditions:
  24. Initializes an empty list.
  25. -------------------------------------------------------
  26. """
  27. self._values = []
  28. return
  29.  
  30. def is_empty(self):
  31. """
  32. -------------------------------------------------------
  33. Determines if the list is empty.
  34. Use: b = l.is_empty()
  35. -------------------------------------------------------
  36. Postconditions:
  37. returns
  38. True if the list is empty, False otherwise.
  39. -------------------------------------------------------
  40. """
  41. return len(self._values) == 0
  42.  
  43. def __len__(self):
  44. """
  45. -------------------------------------------------------
  46. Returns the size of the list.
  47. Use: n = len(l)
  48. -------------------------------------------------------
  49. Postconditions:
  50. Returns the number of values in the list.
  51. -------------------------------------------------------
  52. """
  53. return len(self._values)
  54.  
  55. def insert(self, i, value):
  56. """
  57. -------------------------------------------------------
  58. Inserts a copy of value into the list at index i.
  59. Use: l.insert(i, value)
  60. -------------------------------------------------------
  61. Preconditions:
  62. i - index value (int)
  63. value - a data element (?)
  64. Postconditions:
  65. a copy of value is added to index i, all other values are pushed right
  66. If i outside of range of length of list, appended to end
  67. -------------------------------------------------------
  68. """
  69. if i+1 > len(self._values):
  70. self.append(deepcopy(value))
  71. else:
  72. new_list=self._values[:i]+ [deepcopy(value)]+ self._values[i:]
  73. self._values=deepcopy(new_list)
  74.  
  75.  
  76. return
  77.  
  78.  
  79.  
  80.  
  81. # your code here
  82.  
  83. return
  84.  
  85. def _linear_search(self, key):
  86. """
  87. -------------------------------------------------------
  88. Searches for the first occurrence of key in the list.
  89. Private helper method - used only by other ADT methods.
  90. Use: i = self._linear_search(key)
  91. -------------------------------------------------------
  92. Preconditions:
  93. key - a partial data element (?)
  94. Postconditions:
  95. returns
  96. i - the index of key in the list, -1 if key is not found (int)
  97. -------------------------------------------------------
  98. """
  99.  
  100. i=0
  101. while i<len(self._values) and self._values[i]!=key :
  102. i+=1
  103. # this will only happen id the key is not found in the list
  104. if i== len(self._values):
  105. i=-1
  106.  
  107.  
  108.  
  109.  
  110. return i
  111.  
  112.  
  113.  
  114. def _linear_search_r_aux(self,i,key):
  115.  
  116. #recursion base case
  117.  
  118. if i==len(self._values):
  119. i=-1
  120.  
  121.  
  122.  
  123. elif self._values[i]!=key:
  124.  
  125. #recursive step
  126.  
  127. i=self._linear_search_r_aux(i+1,key)
  128.  
  129.  
  130.  
  131. return i
  132.  
  133.  
  134. def _linear_search_r(self,key):
  135. """
  136. -------------------------------------------------------
  137. Searches for the first occurrence of key in the list.
  138. Private helper methods - used only by other ADT methods.
  139. Use: p, c, i = self._linear_search(key)
  140. -------------------------------------------------------
  141. Preconditions:
  142. key - a partial data element (?)
  143. Postconditions:
  144. returns
  145. previous - pointer to the node previous to the node containing key (_ListNode)
  146. current - pointer to the node containing key (_ListNode)
  147. index - index of the node containing key, -1 if key not found (int)
  148. -------------------------------------------------------
  149. """
  150.  
  151. i=0
  152.  
  153. if len(self._values)==0:
  154. i=-1
  155.  
  156. else:
  157.  
  158. i=self._linear_search_r_aux(i,key)
  159.  
  160. return i
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174. def remove(self, key):
  175. """
  176. -------------------------------------------------------
  177. Finds, removes, and returns the value in list that matches key.
  178. Use: value = l.remove(key)
  179. -------------------------------------------------------
  180. Preconditions:
  181. key - a partial data element (?)
  182. Postconditions:
  183. returns
  184. value - the full value matching key, otherwise None (?)
  185. -------------------------------------------------------
  186. """
  187. assert len(self._values) > 0, "Cannot remove from an empty list"
  188.  
  189.  
  190.  
  191. i=self._linear_search(key)
  192.  
  193.  
  194. if i==-1:
  195. value=None
  196. else:
  197. value=self._values.pop(i)
  198.  
  199. return value
  200.  
  201. def find(self, key):
  202. """
  203. -------------------------------------------------------
  204. Finds and returns a copy of value in list that matches key.
  205. Use: value = l.find(key)
  206. -------------------------------------------------------
  207. Preconditions:
  208. key - a partial data element (?)
  209. Postconditions:
  210. returns
  211. value - a copy of the full value matching key, otherwise None (?)
  212. -------------------------------------------------------
  213. """
  214. i=self._linear_search(key)
  215.  
  216. if i==-1:
  217. value=None
  218.  
  219. value=self._values[i]
  220.  
  221. return value
  222.  
  223.  
  224.  
  225.  
  226. # your code here
  227.  
  228. return value
  229.  
  230. def peek(self):
  231. """
  232. -------------------------------------------------------
  233. Returns a copy of the first value in list.
  234. Use: value = l.peek()
  235. -------------------------------------------------------
  236. Postconditions:
  237. returns
  238. value - a copy of the first value in the list (?)
  239. -------------------------------------------------------
  240. """
  241. assert len(self._values) > 0, "Cannot peek at an empty list"
  242.  
  243. # your code here
  244.  
  245. return value
  246.  
  247. def index(self, key):
  248. """
  249. -------------------------------------------------------
  250. Finds location of a value by key in list.
  251. Use: n = l.index(key)
  252. -------------------------------------------------------
  253. Preconditions:
  254. key - a partial data element (?)
  255. Postconditions:
  256. returns
  257. i - the index of the location of key in the list, -1 if
  258. key is not in the list. (int)
  259. -------------------------------------------------------
  260. """
  261.  
  262. i=self._linear_search_r(key)
  263.  
  264.  
  265. return i
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272. return i
  273.  
  274. def _valid_index(self, i):
  275. """
  276. -------------------------------------------------------
  277. Private helper method to validate an index value.
  278. Python index values can be positive or negative and range from
  279. -len(list) to len(list) - 1
  280. Use: assert self._valid_index(i)
  281. -------------------------------------------------------
  282. Preconditions:
  283. i - an index value (int)
  284. Postconditions:
  285. returns
  286. True if i is a valid index, False otherwise.
  287. -------------------------------------------------------
  288. """
  289. n = len(self._values)
  290. return -n <= i < n
  291.  
  292. def __getitem__(self, i):
  293. """
  294. ---------------------------------------------------------
  295. Returns a copy of the nth element of the list.
  296. Use: value = l[i]
  297. -------------------------------------------------------
  298. Preconditions:
  299. i - index of the element to access (int)
  300. Postconditions:
  301. returns
  302. value - the i-th element of list (?)
  303. -------------------------------------------------------
  304. """
  305. assert self._valid_index(i), "Invalid index value"
  306.  
  307. value= deepcopy(self._values[i])
  308.  
  309. return value
  310.  
  311.  
  312.  
  313. # your code here
  314.  
  315. return value
  316.  
  317. def __setitem__(self, i, value):
  318. """
  319. ---------------------------------------------------------
  320. Places a copy of value into the list at position n.
  321. Use: l[i] = value
  322. -------------------------------------------------------
  323. Preconditions:
  324. i - index of the element to access (int)
  325. value - a data value (?)
  326. Postconditions:
  327. The i-th element of list contains a copy of value. The existing
  328. value at i is overwritten.
  329. -------------------------------------------------------
  330. """
  331. assert self._valid_index(i), "Invalid index value"
  332.  
  333. self._values[i]=deepcopy(value)
  334.  
  335.  
  336.  
  337. return
  338.  
  339. def __contains__(self, key):
  340. """
  341. ---------------------------------------------------------
  342. Determines if the list contains key.
  343. Use: b = key in l
  344. -------------------------------------------------------
  345. Preconditions:
  346. key - a partial data element (?)
  347. Postconditions:
  348. returns
  349. True if the list contains key, False otherwise. (boolean)
  350. -------------------------------------------------------
  351. """
  352. result=True
  353.  
  354. l=self._linear_search(key)
  355.  
  356. if l==-1:
  357. result=False
  358.  
  359. return result
  360.  
  361.  
  362. # your code here
  363.  
  364. def max(self):
  365. """
  366. -------------------------------------------------------
  367. Finds the maximum value in list.
  368. Use: value = l.max()
  369. -------------------------------------------------------
  370. Postconditions:
  371. returns
  372. value - a copy of the maximum value in the list (?)
  373. -------------------------------------------------------
  374. """
  375. assert len(self._values) > 0, "Cannot find maximum of an empty list"
  376. value=self._values[0]
  377. for i in self:
  378.  
  379. if value>i :
  380. value=i
  381.  
  382. return value
  383.  
  384. # your code here
  385.  
  386. return value
  387.  
  388. def min(self):
  389. """
  390. -------------------------------------------------------
  391. Finds the minimum value in list.
  392. Use: value = l.min()
  393. -------------------------------------------------------
  394. Postconditions:
  395. returns
  396. value - a copy of the minimum value in the list (?)
  397. -------------------------------------------------------
  398. """
  399. assert len(self._values) > 0, "Cannot find minimum of an empty list"
  400.  
  401. value=self._values[0]
  402. for i in self:
  403.  
  404. if value<i :
  405. value=i
  406.  
  407. return value
  408.  
  409.  
  410.  
  411. def count(self, key):
  412. """
  413. -------------------------------------------------------
  414. Finds the number of times key appears in list.
  415. Use: n = l.count(key)
  416. -------------------------------------------------------
  417. Preconditions:
  418. key - a partial data element (?)
  419. Postconditions:
  420. returns
  421. number - number of times key appears in list (int)
  422. -------------------------------------------------------
  423. """
  424. assert len(self._values) > 0, "Cannot count keys in an empty list"
  425.  
  426. number=0
  427.  
  428. for i in self:
  429.  
  430. if i==key:
  431. number+=1
  432.  
  433.  
  434. return number
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443. # your code here
  444.  
  445. return number
  446.  
  447. def reverse(self):
  448. """
  449. -------------------------------------------------------
  450. Reverses the order of the elements in list.
  451. -------------------------------------------------------
  452. Postconditions:
  453. The contents of list are reversed in order with respect
  454. to their order before the operation was called.
  455. -------------------------------------------------------
  456. """
  457.  
  458. # your code here
  459.  
  460. return
  461.  
  462. def append(self, value):
  463. """
  464. ---------------------------------------------------------
  465. Appends a copy of value to the end of the list.
  466. Use: l.append(value)
  467. -------------------------------------------------------
  468. Preconditions:
  469. value - a data element (?)
  470. Postconditions:
  471. a copy of value is added to the end of the list.
  472. -------------------------------------------------------
  473. """
  474.  
  475. self._values+=[deepcopy(value)]
  476.  
  477.  
  478.  
  479.  
  480. # your code here
  481.  
  482. return
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492. def clean(self):
  493. """
  494. ---------------------------------------------------------
  495. Removes duplicates from the list.
  496. Use: l.clean()
  497. -------------------------------------------------------
  498. Postconditions:
  499. The list contains one and only one of each value formerly present
  500. in the list. The first occurrence of each value is preserved.
  501. -------------------------------------------------------
  502. """
  503. temp_list=List()
  504.  
  505. for i in range (len(self._values)):
  506.  
  507. lin=self._linear_search(self._values[i])
  508.  
  509. if self._values[lin] not in temp_list._values:
  510.  
  511. temp_list._values.append(self._values[lin])
  512.  
  513.  
  514.  
  515. for j in range (len(self._values)):
  516. self._values.pop(0)
  517.  
  518.  
  519. self._values=self._values + temp_list._values
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529. return
  530.  
  531. def pop(self, *args):
  532. """
  533. -------------------------------------------------------
  534. Finds, removes, and returns the value in list whose index matches i.
  535. Use: value = l.pop()
  536. Use: value = l.pop(i)
  537. -------------------------------------------------------
  538. Preconditions:
  539. args - an array of arguments (tuple of int)
  540. args[0], if it exists, is the index i
  541. Postconditions:
  542. returns
  543. value - if args exists, the value at position args[0], otherwise the last
  544. value in the list, value is removed from the list (?)
  545. -------------------------------------------------------
  546. """
  547. assert len(self._values) > 0, "Cannot pop from an empty list"
  548. assert len(args) <= 1, "No more than 1 argument allowed"
  549.  
  550. if len(args) == 1:
  551. # pop the element at position i
  552. i = args[0]
  553. value = self._values.pop(i)
  554. else:
  555. # pop the last element
  556. value = self._values.pop()
  557. return value
  558.  
  559. def __iter__(self):
  560. """
  561. USE FOR TESTING ONLY
  562. -------------------------------------------------------
  563. Generates a Python iterator. Iterates through the list
  564. from front to rear.
  565. Use: for v in q:
  566. -------------------------------------------------------
  567. Postconditions:
  568. returns
  569. value - the next value in the list (?)
  570. -------------------------------------------------------
  571. """
  572. for value in self._values:
  573. yield value
  574.  
  575.  
  576.  
  577.  
  578. def identical(self, rs):
  579. """
  580. ---------------------------------------------------------
  581. Determines whether two lists are identical, i.e. same values appear
  582. in the same locations in both lists. (iterative version)
  583. Use: b = l.identical(rs)
  584. -------------------------------------------------------
  585. Preconditions:
  586. rs - another list (List)
  587. Postconditions:
  588. returns
  589. is_identical - True if this list contains the same values as rs
  590. in the same order, otherwise False. (boolean)
  591. ---------
  592. """
  593.  
  594. is_identical=True
  595.  
  596. if len(self._values) != len(rs._values):
  597. is_identical=False
  598. else:
  599.  
  600. for i in range(len(self._values)):
  601.  
  602. if self._values[i]!=rs._values[i]:
  603. is_identical=False
  604.  
  605.  
  606.  
  607.  
  608. return is_identical
  609.  
  610.  
  611. def remove_many(self, key):
  612. """
  613. -------------------------------------------------------
  614. Finds and removes all values in the list that match key.
  615. Use: l.remove_many(key)
  616. -------------------------------------------------------
  617. Preconditions:
  618. key - a data element (?)
  619. Postconditions:
  620. Removes all values matching key.
  621. -------------------------------------------------------
  622. """
  623.  
  624. temp_list=[]
  625.  
  626.  
  627. for i in range(len(self._values)):
  628.  
  629. value=self._values.pop()
  630.  
  631. if value!=key:
  632.  
  633. temp_list.append(value)
  634.  
  635.  
  636.  
  637. self._values+=temp_list[::-1]
  638.  
  639.  
  640.  
  641.  
  642. def intersection(self, rs):
  643. """
  644. -------------------------------------------------------
  645. Returns a list that contains only values that appear in both
  646. the current List and rs.
  647. Use: l2 = 11.intersection(rs)
  648. -------------------------------------------------------
  649. Preconditions:
  650. rs - another List (List)
  651. Postconditions:
  652. returns
  653. new_list - A List that contains only the values found in both
  654. the current List and rs. Values do not repeat. (List)
  655. -------------------------------------------------------
  656. """
  657.  
  658. new_list=List()
  659.  
  660.  
  661. for i in range (len(self._values)):
  662.  
  663. if self._values[i] in rs._values and self._values[i] not in new_list:
  664. new_list.append(self._values[i])
  665.  
  666.  
  667.  
  668.  
  669. return new_list
  670.  
  671.  
  672. # your code here
  673.  
  674. return new_list
  675.  
  676. def union(self, rs):
  677. """
  678. -------------------------------------------------------
  679. Returns a list that contains all values in both
  680. the current List and rs.
  681. Use: nl = l.union(rs)
  682. -------------------------------------------------------
  683. Preconditions:
  684. rs - another list (List)
  685. Postconditions:
  686. returns
  687. new_list - contains all values found in both the current
  688. List and rs. Values do not repeat. (List)
  689. -------------------------------------------------------
  690. """
  691. new_list=List()
  692.  
  693. for i in range( len(self._values)):
  694.  
  695.  
  696. new_list.append(self._values[i])
  697.  
  698.  
  699. for j in range( len(rs._values)):
  700.  
  701. if rs._values[j] not in new_list:
  702. new_list.append(rs._values[j])
  703.  
  704.  
  705.  
  706. return new_list
  707.  
  708.  
  709.  
  710. def insert_front(self, value):
  711. """
  712. -------------------------------------------------------
  713. Inserts a copy of value into the front of the list.
  714. Use: l.insert_front(value)
  715. -------------------------------------------------------
  716. Preconditions:
  717. value - a data element. (?)
  718. Postconditions:
  719. value is added to the front of the list.
  720. -------------------------------------------------------
  721. """
  722.  
  723.  
  724. self.insert(0, value)
  725.  
  726.  
  727.  
  728.  
  729. def remove_front(self):
  730. """
  731. -------------------------------------------------------
  732. Removes the first node in the list.
  733. Use: value = l.remove_front()
  734. -------------------------------------------------------
  735. Postconditions:
  736. returns
  737. value - the first value in the list (?)
  738. -------------------------------------------------------
  739. """
  740. assert len(self._values) > 0, "Cannot remove from an empty list"
  741.  
  742. value= self._values.pop(0)
  743.  
  744. return value
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751. def combine(self, s2):
  752. """
  753. -------------------------------------------------------
  754. Combines contents of two lists into a third.
  755. Use: new_list = l1.combine(s2)
  756. -------------------------------------------------------
  757. Preconditions:
  758. s2- an array-based List (List)
  759. Postconditions:
  760. returns
  761. new_list - the contents of the current List and s2
  762. are interlaced into new_list - current List and s2
  763. are empty (List)
  764. -------------------------------------------------------
  765. """
  766.  
  767. new_list=List()
  768.  
  769. while len (self._values)!=0 or len( s2._values)!=0:
  770.  
  771. if len (self._values)!=0:
  772. value1=self._values.pop(0)
  773. new_list.append(value1)
  774.  
  775.  
  776. if len(s2._values)!=0:
  777. value2=s2._values.pop(0)
  778. new_list.append (value2)
  779.  
  780.  
  781. return new_list
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789. def split(self):
  790. """
  791. -------------------------------------------------------
  792. Splits list into two parts. ls contains the first half,
  793. rs the second half. Current list becomes empty.
  794. Use: ls, rs = l.split()
  795. -------------------------------------------------------
  796. Postconditions:
  797. returns
  798. ls - a new List with >= 50% of the original List (List)
  799. rs - a new List with <= 50% of the original List (List)
  800. -------------------------------------------------------
  801. """
  802. ls=List()
  803. rs=List()
  804.  
  805. counter=0
  806. size=len(self._values)/2
  807.  
  808.  
  809. while counter < size:
  810.  
  811. value1=self._values.pop(0)
  812. ls.append(value1)
  813.  
  814. counter+=1
  815.  
  816. for i in range (len(self._values)):
  817. value2=self._values.pop(0)
  818. rs.append(value2)
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826. return ls, rs
  827.  
  828. def split_alt(self):
  829. """
  830. -------------------------------------------------------
  831. Split a List into two parts. even contains the even indexed
  832. elements, odd contains the odd indexed elements.
  833. Order of even and odd is not significant. (iterative version)
  834. Use: even, odd = l.split_alt()
  835. -------------------------------------------------------
  836. Postconditions:
  837. returns
  838. even - the even indexed elements of the list (List)
  839. odd - the odd indexed elements of the list (List)
  840. The List is empty.
  841. -------------------------------------------------------
  842. """
  843. even=List()
  844. odd=List()
  845.  
  846. index =0
  847.  
  848. while len(self._values)!=0:
  849.  
  850. if len (self._values)>0 and (self._values[index]) % 2 == 0:
  851. value1=self._values.pop(0)
  852. even._values.append(value1)
  853.  
  854.  
  855. if len (self._values)>0 and (self._values[index]) % 2 != 0:
  856. value2=self._values.pop(0)
  857. odd._values.append(value2)
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864. return even, odd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement