39
votes
Accepted
Find nth Fibonacci Number, using iteration and recursion
Why do most people (on the internet) recommend using recursion because it's simpler and easier to write the program? Logically I thought that we should write it in a way that is fast and simple.
This ...
36
votes
Accepted
20 Ways to do Random Sampling
It would be worth your while to take a look at the implementation of random.sample to see how it works. (It's different from all 20 implementations in the post, and ...
32
votes
Accepted
Comparing Old vs. New: Temperature Converter
Your second solution is basically a brute-force if-else tree, with lots of repetitive code, and not particularly interesting to review. I'll just focus on the first solution.
Your terminology is ...
29
votes
Accepted
To 'this' or not to 'this'?
First and foremost:
Anyway, should performance be always the top priority?
Definitely not. Don't write things that are needlessly unperformant, but only be overly concerned with performance when ...
22
votes
To 'this' or not to 'this'?
You don't need fancy logic.
Good old closure is good enough here.
...
21
votes
Sum two integers, unless they are the same, in which case return double their sum
The existing answers mostly have just said is is wrong here. Which it undoubtedly is.
The reason it's bad to use here is if you pass ...
21
votes
Accepted
Extraction of value from a dict-tree, given a path of arbitrary length
Recursive Approach Review
In this method you are performing too many look-ups.
First, you are looking up the 0th value of p two or three times. List indexing ...
20
votes
Accepted
Length of a C string, capped to some maximum
All three versions can be improved.
The first function has the problem that strlen(s) is likely to be called twice. strlen() ...
20
votes
Simple countdown programs to print "3, 2, 1, Action!" using while loop
Why does it has to be a while?
Maybe it is preference but for loops look cleaner.
Reversing can be done by using ...
19
votes
Function to format the local time
First of all, the parenthesis around LocalDateTime.now() are useless.
Now, let's see what we're really doing here:
...
19
votes
Simple countdown programs to print "3, 2, 1, Action!" using while loop
You can get rid of some instructions by writing this instead:
count_down = 3
while (count_down):
print(count_down)
count_down -= 1
print('Action!')
Note ...
18
votes
Accepted
18
votes
Checking if an integer is a palindrome using either a string or a dict
Apart from performance, there are several other problems in your implementation which the other answers did not address.
This should not be a class. It should be a function, because it being a class ...
18
votes
Accepted
Get the first item in a sequence that matches a condition
This is a use case for the walrus operator :=, which was introduced in Python 3.8. It allows you to write 1) without repeating yourself:
...
17
votes
Length of a C string, capped to some maximum
Beyond the already mentioned[1] disadvantages[2], your code takes an avoidable performance hit if strlen(s) ≫ M_MAX_SIZE, since <...
17
votes
20 Ways to do Random Sampling
You should not use eval without a very good reason. This is not a very good reason.
Even though here you are actually fully in control of what gets passed to ...
17
votes
Sum Square Difference, which way is more Pythonic?
I find myself struggling more to understand and read what is going on
This is the key insight. A chunk of code is likely to be read more times than it was written. So write your code to be read. ...
16
votes
Accepted
Shoe shine shop model in Rust
Correctness of the solution
On reflection, I'm not sure either the C++ or the Rust code solves the problem as stated. I'm not completely sure I understand the shoe shine shop model so I may be wrong. ...
15
votes
Accepted
Count the occurence of nucleobases in DNA string
Your code is fine and readable from a C/Java perspective. I don't think it's particularly Ruby-ic to use return statements. Just put ...
15
votes
Accepted
Creating a status object based on the command type and action
If you know about the format of cmd you can merge the properties you're checking and check against both in one step.
status = ...
15
votes
"Of three points supplied, is one a midpoint of the other two?"
All of the previous answers seem to work on averages of each of the 2 numbers (expressed as either division or multiplication). But I'm working on averages of all 3 numbers, on the assertion that the ...
15
votes
Accepted
Functions to hide and reclaim first visible publication on a page using Selenium
As the code is similar, they can be merged together.
I'll suggest you to break down the functions into smaller reusable chunks which will follow the Single Responsibility Principle (SRP). These ...
15
votes
Accepted
Parsing an email string
Consider taking advantage of existing features that could provide an additional layer of validation.
mainly System.Net.Mail.MailAddress
Also as mentioned in a ...
15
votes
20 Ways to do Random Sampling
Besides the good points of @Graipher's answer:
Examples n° 6, 7, 10, 11, 12, 14, 15, 16, and 20 are the only one that modify the input data and thus only them need to copy the input to not modify it.
...
15
votes
Accepted
Recursive LinkedList. Which implementation is better and why?
Both of these implementations are bad.
The first is probably worse.
every node in the linked list, from self.head to the last node's ...
15
votes
Accepted
Print a word, one character per line, backwards
Looking at the two implementations side by side, ignoring the goal of the exercise and alternative implementations, I'd say the second example is better:
It uses one fewer variables.
It does a simple ...
13
votes
Accepted
Comparing Equals() method from MSDN
I like my implementation as it's much more succinct
Which isn't necessarily a good thing: we care about readable code, not about oneliners. It means we don't use 5 lines to write what can be written ...
13
votes
Accepted
Start an EventHandler in different Thread
Don't reinvent the wheel: use EventArgs
The StateChanged event isn't compliant with established C# conventions for creating events.
...
13
votes
Accepted
Avoid using C Strings on C++ code to trim leading whitespace
Suggestions on your code:
Your code currently returns the input string unmodified if it contains nothing but spaces and tabs. I'd say it should return an empty string.
You always use ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
comparative-review × 931python × 179
java × 169
performance × 148
c# × 146
javascript × 107
c++ × 82
algorithm × 65
strings × 65
beginner × 62
programming-challenge × 57
php × 54
python-3.x × 53
object-oriented × 53
c × 44
functional-programming × 30
array × 26
ruby × 23
interview-questions × 18
error-handling × 18
unit-testing × 17
complexity × 17
haskell × 16
datetime × 16
hash-map × 16