12
Comments
decrypted_msg is misleading. It's not decrypted but rather "not encrypted". But really just call it "plaintext", as the problem statement does.
Similarly, "enciphered" and "encrypted" are not just inconsistent (make up your mind), but go against the problem statement's term "ciphertext".
The end-...
10
One more option is to not recurse at all, but to instead compute the sequence until we get the desired term. This not only avoids the original algorithm's exponential complexity; it also avoids the at least linear (in terms of counting values) memory complexity of memoization. (Recursion/memoization can also run into implementation-specific issues such as ...
8
If you cannot use functools.cache or functools.lru_cache and if
you don't understand or want to use a decorator, as shown in another
answer, here a low-tech approach to memoization. Please note
that the functools approach is easier, more robust, etc. But
if you are not worried about all of that and just want simple code
that is radically faster than your ...
6
You should use an if "__name__" == __main__: guard.
From Python 3.2 you can use functools.lru_cache and from Python 3.9 you can use functools.cache to memoize the function calls, to only call T \$O(n)\$ times. Which is a fancy name for caching the input and output of the function. The way this works is by wrapping a function. Each time the ...
5
Decorators
The problem to solve
The top of your code should be moved into another function as you have duplicated the code elsewhere.
To DRY your code we can build a decorator.
To explain decorators I'll start from two made up function and work up.
In our world negative numbers are not allowed, if we get a negative number we return.
We have two functions one ...
5
Your code looks un-Pythonic because you are using methods like length rather dunder methods like __len__.
Node
Before that we can change Node to use dataclasses. dataclasses will automatically give your class some boiler-plate, like __init__ and __repr__. To use dataclasses.dataclass you need to type your class. To type attributes we can use attr: type.
from ...
3
Bug: on the first line of run_script, SCRIPT.items() should be script.items(). As written, it executes the global SCRIPT and not the argument to the function.
It doesn't seem like Agent should inherit from Driver
If you research Selenium best practices, you will find a few that make sense for your use case (most are geared toward testing). Two of them are ...
2
Start with some of the low-hanging fruit. (1) Inside your primary for-loop, you
repeatedly used index() to hunt for a value that can be easily computed in
advance. (2) You are assembling the output data via string concatenation, but
Python strings are immutable, which implies the creation of an entirely new
string on each iteration of the loop. That can ...
2
My take, in short:
in programming there is nothing worse than silently swallowing exceptions :) And the fact that you are catching any exception, and not just those related to those specific operations, means than all kinds of errors will go unnoticed.
At the very least, the exceptions should be logged somewhere
be more specific and catch relevant ...
1
Benchmarking a worst-case (key with 1,000 random letters and plaintext with 100,000 random letters).
As @FMc pointed out, isolating the Vigenère algorithm in a function is helpful. Here I do that for benchmarking.
Benchmark results:
74.76 ms 73.98 ms 73.28 ms original
8.97 ms 8.34 ms 8.01 ms Kelly_Bundy_1
20.81 ms 20.70 ms 21.24 ms Kelly_Bundy_2
...
1
There's comments above report which should really be a docstring,
since they explain the function's workings.
The default parameters for report are a bit suspect:
The expression payload if payload else None is redundant, can just
be payload then.
str(reason) for the default value None for reason is odd,
maybe that also needs special casing and could be left ...
1
SERVER_1_PASS = os.getenv('PASS_1') or ""
SERVER_2_PASS = os.getenv('PASS_2') or ""
I don't think it's a good idea to pass secrets in the environment - that's too easily read by other processes. Instead, prefer to hold them in a file that's accessible only by the user. Since we're using SSH, we even already have such a file ($HOME/.ssh/...
1
It's good to see code that comes with tests - well done!
I would add some more tests in places. Specifically, we ought to test is_palindrome() with a single-letter input, as that's something that could easily get broken, and find_palindrome() needs testing with some strings where the longest palindrome is at beginning, middle or end.
I'm concerned at
...
1
Your question made me want to give it a shot, too. The solution ended up pretty much like the pseudo-code suggested by @Marc , and Python is of course pretty close in readability anyway. The below code passes on the site and runs (there is some deviation between runs) faster than c. 95% and at with less memory usage than c. 75% of solutions. The code ...
1
My suggestion about your current solution:
def max_area(height) -> int:
areas = []
coords = {x: (x, y) for x, y in enumerate(height)}
for x in coords:
higher = [k for k in coords if coords[k][1] >= coords[x][1]]
area = max(abs(coords[j][0] - coords[x][0]) for j in higher) * coords[x][1]
areas.append(area)
return ...
1
Code Review - elearningapp/models.py - Course
FYKI - Im not a Python expert!
Anit's recommendation are marked like this!
Well the first thing I would do is make separate files for the classes.
class Course(models.Model):
name = models.CharField(max_length=100)
number_of_questions_per_test = models.IntegerField(default=10)
Lets remove comments as ...
Only top voted, non community-wiki answers of a minimum length are eligible
Related Tags
python × 13283python-3.x × 4429
performance × 2001
beginner × 1736
python-2.x × 1239
algorithm × 1074
programming-challenge × 919
numpy × 639
object-oriented × 604
pandas × 475
strings × 441
game × 419
web-scraping × 367
time-limit-exceeded × 337
django × 306
tkinter × 289
parsing × 287
csv × 286
hash-map × 270
recursion × 219
regex × 213
random × 212
file-system × 201
file × 200
sorting × 195