Share Your Experience: Take the 2024 Developer Survey

# Tag Info

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 ...
• 14.9k
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 ...
• 49.6k

### 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 ...
• 43.4k
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 ...
• 33.7k

### 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 ...
• 11.6k

### 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 ...
• 3,412
Accepted

### Sum Square Difference, which way is more Pythonic?

I would do something like: ...
• 361

### 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 ...
• 6,628
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: ...
• 41k

### 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 ...
• 41k

### 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. ...
• 10k
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. ...
• 1,064
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 ...
• 3,002
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 ...
• 3,246

### 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. ...
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 ...
• 33.7k
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 ...
• 8,927
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 ...
• 11.6k
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. ...
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 ...
• 9,560

### 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&...
• 16.3k
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: ...
• 43.4k

### 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 ...
• 13.9k

### 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 ...
• 10k

### 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. ...
• 239
Accepted

### Ask the user a number between 2 and 20

Prof's transcribed code has weaknesses ...

### 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 ...

### 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 ...
• 6,191