pythonds | Problem Solving with Algorithms and Data Structures | Learning library
kandi X-RAY | pythonds Summary
kandi X-RAY | pythonds Summary
Problem Solving with Algorithms and Data Structures using Python
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of pythonds
pythonds Key Features
pythonds Examples and Code Snippets
Community Discussions
Trending Discussions on pythonds
QUESTION
I'm confused by the following example. Let a=[1]
be a python list and try to write a function which re-assigns the list such that we will have a=[2]
after running the function.
A simple function which works is
...ANSWER
Answered 2021-Feb-04 at 19:49arr
is a reference to a list object
When you write arr[0]=1
you change the element in this referenced object.
But when you write arr=[..new list..]
you just make arr
refer to a new object, and it does not affect previous object.
QUESTION
I'm learning about Big-O notation, not for a class, just learning by reading on my own. I'm reading through Pythonds and did the exercise where you're tasked with writing a "not optimal" Python function to find the minimum number in a list. The function should compare each number to every other number on the list: O(n**2).
Here's what I came up with:
...ANSWER
Answered 2020-Jun-25 at 18:05Your code is actually O(nlogn)
(average case) and not O(n^2)
.
Take a look on all(x<=y for y in alist)
, and recall that to yield False
, it is enough that one element is bigger than x
, no need to go through all values of alist
.
Assume your list is shuffled randomly (and uniformly), and let's examine how many elements needs to be traversed:
QUESTION
My textbook says that the following algorithm has an efficiency of O(n):
...ANSWER
Answered 2020-Apr-30 at 18:45You are correct that the runtime of the code that you've posted here is O(n2), not O(n), for precisely the reason that you've indicated.
Conceptually, the algorithm you're implementing goes like this:
- Maintain a collection of all the items seen so far.
- For each item in the list:
- If that item is in the collection, report a duplicate exists.
- Otherwise, add it to the collection.
- Report that there are no duplicates.
The reason the code you've posted here is slow is because the cost of checking whether a duplicate exists is O(n) when using a list to track the items seen so far. In fact, if you're using a list of the existing elements, what you're doing is essentially equivalent to just checking the previous elements of the array to see if any of them are equal!
You can speed this up by switching your implementation so that you use a set
to track prior elements rather than a list. Sets have (expected) O(1) lookups and insertions, so this will make your code run in (expected) O(1) time.
QUESTION
I'm writing a program where I'm converting a function to prefix and calculate.
...ANSWER
Answered 2020-Apr-05 at 06:42Replace if token in "0123456789"
(checks if token
is a substring of "0123456789"
) with if token.isdigit()
(checks if token
consists of decimal digits).
QUESTION
class TreeNode:
def __init__(self,key,val,left=None,right=None,
parent=None):
self.key = key
self.payload = val
self.leftChild = left
self.rightChild = right
self.parent = parent
# Instance method
def isLeftChild(self):
return self.parent and self.parent.leftChild == self
...ANSWER
Answered 2019-Nov-28 at 10:41self.parent # Make sure this is not a root node
and
self.parent.leftChild == self
# self = current node object,
# check if current node's parent's leftChild attribute also
# points to this current node object
QUESTION
I can't understand the recursion.
The main()
function aligns the turtle. The tree()
function is called with branchLen = 75
. So, it passes the "if" condition and goes up. According to my understanding, the turtle should take 5 consecutive right turns with its length decreasing as 75, 60, 45, 30, 15. After this, it won't satisfy the "if " condition anymore. The code would run only till line 5 (first recursive call). So, a single line leaning toward RHS should be displayed. There shouldn't be any left turns.
But this does not happen, a full symmetrical tree is made.
Please explain how.
See the link for more clarity on the question.
Thanks!
https://interactivepython.org/runestone/static/pythonds/Recursion/pythondsintro-VisualizingRecursion.html
ANSWER
Answered 2017-Jul-18 at 13:54Each call to tree
remembers where it is. You are correct that the first thing to happen is a chain of forward and right turns until tree (0,t)
is called. That call doesn't satisfy the if
test, so does nothing. However, that does not affect any other tree
call. So, back in tree(15,t)
, execution continues with line 6, and similarly for all the other tree
calls.
As an exercise, you might try pasting a copy of tree
each place it is called, and filling in the numbers for branchLen
. Each time tree
is called, that is effectively what happens.
Imagine branchLen
were part of the function name, rather than a parameter. You would have a family of functions tree75(t)
, tree60
, ... tree0
. tree75()
would be:
QUESTION
I know this is a very basic question and I found sample codes online but I cannot figure out why it works.
If we need to traverse a binary tree in preorder fashion, one of the way to do so (quoted here http://interactivepython.org/courselib/static/pythonds/Trees/TreeTraversals.html) is by using something like:
...ANSWER
Answered 2019-Apr-18 at 00:33Recursion is something that is hard to grasp when starting out with programming. One of the things that you have to realize is that, if you properly have set up base cases (eg. if self.left, if self.right, etc), you can use the function that you are working on as if it is already working.
Let's think about an example of counting all the nodes in a tree with the function countNodes()
:
Say we have node x that is somewhere in the tree. When x is called to count all of the nodes of its subtree, how does it do that?
Remember how I said that we have to pretend the function countNodes()
exists and that it already works? Well, let's do just that.
X needs to count itself because it is a node. Therefore the count is 1 so far. After it counted itself, it has to count all the nodes on the left and all the nodes on the right. So to count the nodes in a tree starting at any node x, we call countNodes() for the left and countNodes() for the right.
So the code would look like this:
QUESTION
My program is supposed to evaluate given infix expressions and evaluate them. It is assumed that only the operators in the method calls at the bottom of the code will be used for any given infix expressions that would be run by the program.
...ANSWER
Answered 2019-Mar-03 at 23:05The –
characters in your third call:
QUESTION
I'm following along with the videos for Interactive Python's course on data structures and algorithms. In one segement the following piece of code appears. It's to demonstrate a example of O(n**2) complexity.
It's supposed to loop through the range starting from 1000, and ending at 10000. But I have no idea why 100000 is given to the randrange function in the list comprehension on line 2.
Thanks in advance!
Note: i'm following along with this course - http://interactivepython.org/runestone/static/pythonds/AlgorithmAnalysis/BigONotation.html
...ANSWER
Answered 2019-Jan-11 at 20:00This is a time trial, testing how fast findmin()
is. That's best done with randomised data, to avoid pathological cases. The list comprehension produces the test data. The 100000
is just an upper bound for the random values in that list, high enough to ensure that even for a list with 10k integers there is a nice spread of values.
Note that it is better to use the timeit
module to execute time trials.
QUESTION
In this online textbook https://runestone.academy/runestone/static/pythonds/SortSearch/TheMergeSort.html they give the following code for mergesort:
...ANSWER
Answered 2019-Jan-01 at 23:05Slicing shouldn't affect the time complexity at all in terms of it's order of magnitude. The constant factor is another discussion.
The key part of understanding how the time complexity is unchanged is here:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install pythonds
You can use pythonds like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page