changemyusername / algorithms-sedgewick-python Goto Github PK
View Code? Open in Web Editor NEWAlgorithms(4th edition) by Robert Sedgewick and Kevin Wayne exercises in python
Algorithms(4th edition) by Robert Sedgewick and Kevin Wayne exercises in python
In the test example of 1.3.12 exercise, the first(or head) element of the stack should be 4, not 1. The copy of the stack is done in reverse, breaking it's structure:
I suppose it should be done through another temporary stack to ensure structure integrity:
# 1.3.12 practice
@classmethod
def copy(cls, old_stack: Stack) -> None:
"""
Copy existing stack and return new stack.
Args:
cls: Stack class
stack (Stack): existing stack
Returns:
Stack: new stack
>>> s = Stack()
>>> s.push(1)
>>> s.push(2)
>>> s.push(3)
>>> s_copy = Stack.copy(s)
>>> s_copy._first.val
3
>>> s_copy.size()
3
>>> for item in s_copy:
... print(item)
...
3
2
1
"""
s_tmp = cls()
s_copy = cls()
if isinstance(old_stack, Stack) and not old_stack.is_empty():
for item in old_stack:
s_tmp.push(item)
for item in s_tmp:
s_copy.push(item)
return s_copy
O(N) time is expected.
But O(N) space, I don't think we can avoid that.
And I guess the constructor here should be changed too, because it has the same behaviour:
Maybe even make the option to make a copy in 'init' if there is a 'Stack' in 'args'.
Hallo .
Refering to chapter_5/module_5_2.py section TernarySearchTries I tried the keys()-method.
I inserted: ['she', 'sells', 'sea', 'shells', 'by', 'the', 'sea', 'shore'].
the result from keys() -> ['ea', 'ells', 'he', 'hells', 'hore']
Can you pls help me?
BR Johann
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.