Skip to main content
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.9k
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
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
  • 43.4k
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.8k
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.6k
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
  • 41k
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
  • 41k
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
  • 10k
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

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
  • 3,002
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,246
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.8k
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,917
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,570
13 votes

Get the first item in a sequence that matches a condition

I have to be honest, I actually prefer option 3, and it's what I've used previously. It gives a clear separation of steps: "Produce the data, then filter it&...
Carcigenicate's user avatar
13 votes
Accepted

Leap Year Calculator Using If Elif and Else Only

Your code is nice. Contrasting to the instructor's solution, you have cleanly inverted the arrow anti-pattern to be as 'flat' as possible. I like your code more. Some possibly uninteresting feedback: ...
Peilonrayz's user avatar
  • 43.4k
12 votes
Accepted

On Knuth's "Algorithm L" to generate permutations in lexicographic order

Optimising the functional approach Iterative vs functional approach: Is it possible to use the (existing) functional methods without losing performance? I don't think it's possible to use the ...
Hamish's user avatar
  • 433
12 votes

Get the encrypted payload from an unencrypted wrapper PDF document

Positive Changes getReader() is only called once, while called multiple times before. There are more early exits as before, which results in less nested and lesser ...
dfhwze's user avatar
  • 13.9k
12 votes

Shuffling multiple iterators in order

functools.singledispatch functools library includes the singledispatch() decorator. It lets you provide a generic function, but provide special cases based on the ...
RootTwo's user avatar
  • 10k
12 votes

Extraction of value from a dict-tree, given a path of arbitrary length

Use a library. In particular, glom does an admirable job of this, and is actively developed. ...
Cireo's user avatar
  • 239
12 votes
Accepted

Ask the user a number between 2 and 20

Prof's transcribed code has weaknesses ...
chux - Reinstate Monica's user avatar
12 votes

Print a word, one character per line, backwards

The shorter code, the better (as long as it does not goes into extreme which makes the code obfuscated). So the second solution is better - it's using fewer lines and variables to achieve the same ...
yedpodtrzitko's user avatar
12 votes

Convert Rankine temperatures to Celsius

Let the caller handle iterables I think allowing an iterable is a bad idea. The caller can already use map(Rankine.to_celsius, temps) or the like with your first ...
Richard Neumann's user avatar
11 votes

Inserting and fetching values slower on an unordered_map in C++ than on a dictionary in Python

Here are some things to improve your code. First, we'll address the performance issue, followed by a number of other things that could be improved. Use const references where practical The code ...
Edward's user avatar
  • 66.5k

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