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 ...
Eric Lippert's user avatar
  • 14.7k
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 ...
Gareth Rees's user avatar
  • 49.6k
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 ...
200_success's user avatar
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 ...
Retsam's user avatar
  • 426
22 votes

To 'this' or not to 'this'?

You don't need fancy logic. Good old closure is good enough here. ...
Sheepy's user avatar
  • 354
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 ...
Peilonrayz's user avatar
  • 42.7k
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 ...
AJNeufeld's user avatar
  • 33.3k
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() ...
200_success's user avatar
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 ...
Ludisposed's user avatar
  • 11.5k
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: ...
Simon Forsberg's user avatar
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 ...
Billal Begueradj's user avatar
18 votes
Accepted

Sum Square Difference, which way is more Pythonic?

I would do something like: ...
Nikos Oikou's user avatar
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 ...
Konrad Rudolph's user avatar
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: ...
Graipher's user avatar
  • 40.8k
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 <...
David Foerster's user avatar
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 ...
Graipher's user avatar
  • 40.8k
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. ...
RootTwo's user avatar
  • 9,935
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. ...
trent's user avatar
  • 1,064
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 ...
Barry's user avatar
  • 18.4k
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 = ...
twhb's user avatar
  • 266
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 ...
ThunderFrame's user avatar
  • 1,969
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 ...
Tushar's user avatar
  • 2,972
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 ...
Nkosi's user avatar
  • 3,236
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. ...
301_Moved_Permanently's user avatar
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 ...
AJNeufeld's user avatar
  • 33.3k
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 ...
l0b0's user avatar
  • 8,887
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 ...
Jeroen Vannevel's user avatar
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. ...
Mathieu Guindon's user avatar
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 ...
L. F.'s user avatar
  • 9,456

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