Hot answers tagged

11 votes
Accepted

Print all "balanced" sequences of 'A' and 'B'

Refactor to use functions Try to avoid boolean variables. Pseudocode like: ...
  • 2,983
6 votes

Auctionsite API Wrapper in Python

In API.__init__, you're assigning a value to self.base_url. However, that value is never changed and thus is the same for every ...
6 votes

Print all "balanced" sequences of 'A' and 'B'

Avoid unnecessary computations The requirement that all prefixes of length \$\geq 4\$ must be balanced excludes a lot of combinations. For any prefix length, 80% of the combinations of that length are ...
  • 109k
5 votes

Number guessing game with difficulties and highscore

Adding to @DeathIncarnate's great answer: Don't use global variables It's fine to access global constants, but manipulating global variables such as dif is bad ...
5 votes
Accepted

Number guessing game with difficulties and highscore

PEP-8 First off, some minor things your variable names aren't compliant with PEP-8, which recommends using: UPPER_SNAKE for top-level variables ...
5 votes

Reversing a Python string using [::-1]

Using string slicing as you have done is pretty much the best way, as discussed in this answer. You should consistently put a space before and after the = ...
4 votes

Auctionsite API Wrapper in Python

Payload This code is pretty hard to read (and possibly prone to encoding errors): ...
  • 5,607
3 votes

mypy does not recognize the correct return type

Currently MyPy does not know that the function is_valid_credentials is a type guard that narrows the type of the passed credentials Introduced in Python 3.10 ...
3 votes
Accepted

Simplify nested list comprehension

Mypy doesn't know that calling _parse_name multiple times will always return the same value if the same value is passed to it. You could try using walrus operator ...
3 votes
Accepted

Automate the Boring Stuff Chapter 9 - Regex Search

Use stronger input validation The code checks: if Path(user_path).exists() != True: And later it does: ...
  • 109k
2 votes
Accepted

Iterating over JSON more effectively

Much more complicated than it needs to be. You extend a list, then traverse the list to construct a counter, then traverse again to get a sorted sequence, then traverse again to get a list of keys ...
  • 52.9k
2 votes

Auctionsite API Wrapper in Python

Use the start position parameter of .find(...) instead of string slicing This code uses a string slice: ...
  • 109k
2 votes
Accepted

Automate the Boring Stuff Chapter 9 - Mad Libs Exercise

Use descriptive names It's good to use descriptive names for variables (and everything) to help readers understand the elements of the code and how they might interact. Names like ...
  • 109k
2 votes

Reversing a Python string using [::-1]

Is there another way of reversing a string other than using [::-1] method? Yes there is. There are plenty of tutorials on the Internet, consider this one for example: Python Reverse String - 5 Ways ...
  • 5,607
2 votes

Derivative calculator simplifier using python

Firstly I recommend using some form of standard Python formatter, so your code is standardized and will allow others to more easily assist you. In my answer I use the ...
2 votes
Accepted

Estimate an average duration of every game state in a football match given implied goals for home and away teams

It's clear that you've been working at generic Python quality, and from that perspective this code is half-decent. Where it falls over is Numpy use. In one sense, well-written Numpy code doesn't look ...
  • 52.9k
1 vote
Accepted

Create a forecast matrix from time series samples

Yes, Numpy already has a built-in sliding window method (though it's perhaps a little obscure). This should indeed be memory-friendly, occupying about (W + N) and not W(W + N) due to it being a view. ...
  • 52.9k
1 vote
Accepted

Increase efficiency of stick cutting algorithm

Two reviewers advised how to avoid establishing the length of current longest piece from scratch, as that promises worst case run time at least quadratic in the number m of cuts. In greybeard's ...
1 vote

mypy does not recognize the correct return type

Consider your code: ...
  • 131
1 vote

input character name and age then print name and age list the characters that are older

PEP-8 The Style Guide for Python Code has several recommendations you should follow. White Space You should have a space around operators like =, ...
  • 32.1k
1 vote

input character name and age then print name and age list the characters that are older

Don't treat the other characters specially - just put them in a sequence (a tuple, in this case), and compare them in a loop. Consider adding a validation loop for the age. Your code needs to be moved ...
  • 52.9k
1 vote

How to using custom colormap with pixel calculation, gray scale value by opencv and make it workable on camera

Looking at just the code, I find it difficult to see its purpose. In particular, I don't find colormap: Document your code. In the code. Define a function for the mapping presented. Find or define a ...
1 vote

Pairwise Max Product

Making it faster The maximum product of two numbers in the input will be either: the product of the two highest numbers the product of the two lowest numbers note that this is possible when the ...
  • 109k

Only top scored, non community-wiki answers of a minimum length are eligible