sbs5@cobra:~/cs200/www/lectures$ date Mon Oct 21 02:29:35 PM EDT 2024 sbs5@cobra:~/cs200/www/lectures$ pwd /home/accts/sbs5/cs200/www/lectures sbs5@cobra:~/cs200/www/lectures$ p Python 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> x = 'x = {!r};print(x.format(x))';print(x.format(x)) >>> >>> x = 'x = {!r};print(x.format(x))';print(x.format(x)) >>> x = 'x = {!r};print(x.format(x))';print(x.format(x)) >>> >>> x = 'x = {!r};print(x.format(x))';print(x.format(x)) >>> from stack import * >>> l = [1,2,3] >>> l.pop() 3 >>> l [1, 2] >>> l[1], l[0] = l[0], l[1] >>> l [2, 1] >>> help(stack) Help on class stack in module stack: class stack(builtins.object) | stack(items=[]) | | ## a common use of classes is to implement data structures | ## this is an example of a stack, | ## which is a LIFO - last in first out - structure | ## it is a collection. | ## items are added to the stack with push and removed with pop | ## ----------------------------------------------------------- | ## we will see that the python virtual machine for interpreting | ## byte code is based on a stack architecture. | ## ----------------------------------------------------------- | | Methods defined here: | | __eq__(self, other) | Return self==value. | | __init__(self, items=[]) | Initialize self. See help(type(self)) for accurate signature. | | __iter__(self) | Return iterator for the stack. | | __repr__(self) | Return repr(self). | | copy(self) | # copy constructor - clone the current instance | | isEmpty(self) | | peek(self) | | pop(self) | | push(self, item) | | rotate(self) | ## swap the top two items in the stack | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __hash__ = None >>> s stack([1, 2, 3, 4]) >>> s.size 4 >>> s.isempty() Traceback (most recent call last): File "", line 1, in AttributeError: 'stack' object has no attribute 'isempty'. Did you mean: 'isEmpty'? >>> s.isEmpty() False >>> s.peek() 4 >>> s stack([1, 2, 3, 4]) >>> s.pop() 4 >>> s stack([1, 2, 3]) >>> s.isEmpty() False >>> s.rotate() >>> s stack([1, 3, 2]) >>> s.rotate() >>> s stack([1, 2, 3]) >>> type(s) >>> s2 = s.copy() >>> s == s2 True >>> id(s) 127264401688320 >>> id(s2) 127264405399840 >>> s is s2 False >>> s is not s2 True >>> test(s) 3 2 1 [3, 2, 1] >>> s stack([1, 2, 3]) >>> s.rotate() >>> s stack([1, 3, 2]) >>> test(s) 2 3 1 [2, 3, 1] >>> revstr('abcdef') 'fedcba' >>> 'abcdef'[::-1] 'fedcba' >>> sbs5@cobra:~/cs200/www/lectures$ cd /c/cs200/hws sbs5@cobra:/c/cs200/hws$ ls hw0a.pyc hw1.py hw3a.pyc hw4.py hw0.py hw2a.pyc hw3.py __pycache__ hw1a.pyc hw2.py hw4a.pyc sbs5@cobra:/c/cs200/hws$ p Python 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import hw4a >>> hw4a.balanced >>> hw4a.balanced('()') True >>> hw4a.balanced('())') False >>> hw4a.balanced('()()') True >>> hw4a.balanced('())(') False >>> hw4a.balanced('()(xxxx)') True >>> hw4a.balanced('ajajaj') True >>> hw4a.balanced('ajajaj)') False >>> q Traceback (most recent call last): File "", line 1, in NameError: name 'q' is not defined >>> hw4a.q Traceback (most recent call last): File "", line 1, in AttributeError: module 'hw4a' has no attribute 'q'. Did you mean: 'q2'? >>> hw4a.q2 queue2(stack([4, 5, 6]), stack([3, 2, 1])) >>> q = queue() Traceback (most recent call last): File "", line 1, in NameError: name 'queue' is not defined >>> q = hw4a.queue() >>> q queue([]) >>> dir(q) ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'copy', 'data', 'dequeue', 'enqueue', 'isempty', 'peek', 'size'] >>> q.size 0 >>> q.enqueue(1) >>> q queue([1]) >>> q.enqueue(2) >>> q.enqueue(3) >>> q.enqueue(4) >>> q queue([1, 2, 3, 4]) >>> str(q) 'queue([1, 2, 3, 4])' >>> repr(q) 'queue([1, 2, 3, 4])' >>> q.peek() 1 >>> q.dequeue() 1 >>> q.dequeue() 2 >>> q.dequeue() 3 >>> q queue([4]) >>> q.peek() 4 >>> q.dequeue() 4 >>> q queue([]) >>> q.peek() 'queue is empty' >>> q.dequeue() 'queue is empty' >>> q.enqueue(2) >>> q.enqueue(1 ... ) >>> q.enqueue(3) >>> q.enqueue(4) >>> q queue([2, 1, 3, 4]) >>> test Traceback (most recent call last): File "", line 1, in NameError: name 'test' is not defined >>> hw4a.text Traceback (most recent call last): File "", line 1, in AttributeError: module 'hw4a' has no attribute 'text'. Did you mean: 'test'? >>> hw4a.test >>> hw4a.test(q) Traceback (most recent call last): File "", line 1, in TypeError: test() missing 1 required positional argument: 'expected' >>> q2 = q.copy() >>> q2 queue([2, 1, 3, 4]) >>> q queue([2, 1, 3, 4]) >>> q == q2 True >>> id(q) 135887890378720 >>> id(q2) 135887890378816 >>> q is q2 False >>> q is not q2 True >>> for i in q: ... print (i) ... 2 1 3 4 >>> [x for x in q] [2, 1, 3, 4] >>> q queue([2, 1, 3, 4]) >>> q2 queue([2, 1, 3, 4]) >>> q2 = hw4a.queue2() >>> q2 queue2(stack([]), stack([])) >>> q2.enqueue(1) >>> q2.enqueue(2) >>> q2.enqueue(3) >>> q2 queue2(stack([1, 2, 3]), stack([])) >>> q2.peek() 1 >>> q queue([2, 1, 3, 4]) >>> q2 queue2(stack([]), stack([3, 2, 1])) >>> q2.pop()q File "", line 1 q2.pop()q ^ SyntaxError: invalid syntax >>> q2.dequeue() 1 >>> q2 queue2(stack([]), stack([3, 2])) >>> q2.dequeue() 2 >>> q2 queue2(stack([]), stack([3])) >>> q2.enqueue(4) >>> q4 Traceback (most recent call last): File "", line 1, in NameError: name 'q4' is not defined. Did you mean: 'q'? >>> q2 queue2(stack([4]), stack([3])) >>> q2.dequeue() 3 >>> q2 queue2(stack([4]), stack([])) >>> q2.peek() 4 >>> q2 queue2(stack([]), stack([4])) >>> q2.isempty() False >>> q2.dequeue() 4 >>> q2 queue2(stack([]), stack([])) >>> q2.isempty() True >>> command dirs File "", line 1 command dirs ^^^^ SyntaxError: invalid syntax >>> q2.enqueue(4) >>> q2.enqueue(3) >>> q2.enqueue(2) >>> q2.enqueue(1) >>> q2 queue2(stack([4, 3, 2, 1]), stack([])) >>> for i in q2: ... print (i) ... 4 3 2 1 >>> [x for x in q2] [4, 3, 2, 1] >>> q2 queue2(stack([4, 3, 2, 1]), stack([])) >>> q2 queue2(stack([4, 3, 2, 1]), stack([])) >>> q3 = q2.copy() >>> q3 queue2(stack([4, 3, 2, 1]), stack([])) >>> q2 == q2 True >>> q3.peek() 4 >>> q3 queue2(stack([]), stack([1, 2, 3, 4])) >>> q2 queue2(stack([4, 3, 2, 1]), stack([])) >>> q2 == q3 True >>> q2 queue2(stack([4, 3, 2, 1]), stack([])) >>> q3 queue2(stack([]), stack([1, 2, 3, 4])) >>> q2 queue2(stack([4, 3, 2, 1]), stack([])) >>> hw4a.reverse(q2) Traceback (most recent call last): File "", line 1, in AttributeError: module 'hw4a' has no attribute 'reverse'. Did you mean: 'reverseq'? >>> hw4a.reverseq(q2) queue2(stack([1, 2, 3, 4]), stack([])) >>> hw4a.reverseq(q2) queue2(stack([4, 3, 2, 1]), stack([])) >>> q3 queue2(stack([]), stack([1, 2, 3, 4])) >>> hw4a.reverseq(q3) queue2(stack([1, 2, 3, 4]), stack([])) >>> q queue([2, 1, 3, 4]) >>> hw4a.reverseq(q) queue([4, 3, 1, 2]) >>> d = {} >>> d['a'] = 'alpha' >>> d {'a': 'alpha'} >>> d['a'] 'alpha' >>> sbs5@cobra:/c/cs200/hws$ exit Process shell finished