Programming questions are off-topic here. Do not ask questions about how to write code in Python. However, conceptual questions about computer science are more appropriate. See our help center for the scope of this site.
Questions tagged [python]
226 questions
13
votes
3 answers
Which other programming languages apart from Python and predecessor are out there using indentation to define code blocks?
Python quite famously uses indentation to syntactically define blocks of code. (See Compound statements in the Python Language Reference). After years of using Python I'm still intrigued by and very fond of this syntax feature.
But I wonder: Apart…
halloleo
- 257
- 2
- 5
11
votes
5 answers
Does an algorithm's space complexity include input?
Consider the Kadane's algorithm for finding maximum subarray within an array:
def max_subarray(numbers):
"""Find the largest sum of any contiguous subarray."""
best_sum = 0
current_sum = 0
for x in numbers:
current_sum =…
Eugene Yarmash
- 275
- 3
- 8
9
votes
12 answers
Time complexity of min() and max() on a list of constant size?
If you use min() or max() on a constant sized list, even in a loop, is the time complexity O(1)?
anshur
- 145
- 1
- 1
- 3
9
votes
7 answers
Checking equality of integers: O(1) in C but O(log n) in Python 3?
Consider these equivalent functions in C and Python 3. Most devs would immediately claim both are $O(1)$.
def is_equal(a: int, b: int) -> bool:
return a == b
int is_equal(int a, int b) {
return a == b;
}
But consider what is happening under…
jtschoonhoven
- 297
- 1
- 3
- 9
8
votes
3 answers
What is the expected time complexity of checking equality of two arbitrary strings?
The simple (naive?) answer would be O(n) where n is the length of the shorter string. Because in the worst case you must compare every pair of characters.
So far so good. I think we can all agree that checking equality of two equal length strings…
jtschoonhoven
- 297
- 1
- 3
- 9
7
votes
3 answers
Space complexity for storing integers in Python
So I was watching this mock interview by an Airbnb engineer on interviewing.io (https://youtu.be/cdCeU8DJvPM?t=1224) and around ~20:11 seconds he raises an interesting point.
The question that the interviewee was trying to solve was -
Array 1 has…
Sidharth Samant
- 295
- 1
- 3
- 6
4
votes
1 answer
Why does parallelising slow down this simple problem against looping through all the data?
I've been using multiprocessing and parallelisation for the first time this week on a very large data set using 32 CPUs. I decided to explore it for a smaller task just to see if I could learn anything, just on the 4 CPUs of my Mac.
I created a task…
quanty
- 205
- 1
- 5
4
votes
1 answer
What are the fundamentals of calculating space complexity in loops?
Imagine you loop n times, and every iteration you create a string of space n with scope only within that iteration (thus it is no longer accessible in the next iteration). I would look and say that I use O(n^2) space because, for n iterations, I use…
Steven Zeng
- 43
- 3
4
votes
1 answer
Warnsdorff's rule: more errors with odd sized boards
I wrote an algorithm based on the Warnsdorff's rule to solve the knight's tour problem, where you need to create a sequence of moves of a knight on a chessboard such that the knight visits every square exactly once.
I noticed something strange,…
Tortar
- 167
- 7
4
votes
0 answers
Subtype Check with Type DAG
Trying to understand how compiler/static-type-checker checks for subtyping, I run into 2 problems.
1. Reachability in DAG
Since both Python/C++ support multiple inheriatnce, the types can be represented as a DAG.
A C
| / \
B D
|\ |
E F /
| …
mq7
- 141
- 1
3
votes
1 answer
How to use Latin hypercube sampling with fixed points?
I use Latin hypercube sampling to select what point to evaluate my function. As evaluations take a lot of time, I want to limit the time by adding already evaluated points.
I thought about taking the min distance between the points, and if I have an…
HennyKo
- 131
- 3
3
votes
1 answer
Understanding Applicative Evaluation Order with the Z-Combinator
I am trying to understand how the Z-combinator (Y-combinator for applicative order languages) definition came about. As Python is applicative I am using Python for this.
So I know Python's evaluation order is applicative. But I seem to be…
lo tolmencre
- 295
- 1
- 3
- 8
3
votes
2 answers
Find 1s in almost all 0 array using comparisons only
So, we are given a 100 long array, with 97 0s and 3 1s of which we do not know the locations. We must find them using only a compare function, which I managed to write (in Python):
def compare(i, j):
comparisons.append((i, j))
if data[i] ==…
user555076
- 61
- 6
3
votes
1 answer
How does numpy.linalg.inv calculate the inverse of a matrix?
What is the algorithm behind this routine and is there documentation available for it?
oogabooga
- 31
- 1
3
votes
2 answers
Number of Inversions found in Selection sort vs Exchange sort
Exchange sort is similar to selection sort, just swaps "overly eagerly" instead of finding the minimum and doing only one swap. And the swaps affect how often the if ... is true. For example for 8000 random numbers, in exchange sort it's true ~270…
Kelly Bundy
- 530
- 2
- 16