# Questions tagged [comparative-review]

This tag is used for questions which present a problem and multiple solutions, where the asker wishes to know which solution is best (and why).

31 questions
Filter by
Sorted by
Tagged with
303 views

### Sequence-based enumeration of permutations in lexicographic order

This is follow-up on On Knuth's "Algorithm L" to generate permutations in lexicographic order, where I presented the following method to enumerate all permutations of array elements in ...
667 views

### Opening a file and printing its contents line by line

Everything that I've read online says that the best way to open a file in python is: ...
8k views

### Ensuring user input is an integer in a range

I have isolated a little bit of code that was causing a small debate between myself and another user. I have taken some of the things that he said and meshed it with the code that was being reviewed ...
7k views

### Which FizzBuzz is better, and why?

I was in an interview and the guy asked me to do the typical FizzBuzz question of printing numbers 1-100, but for each factor of 3 print Fizz, each factor of 5 print Buzz, and each factor of both ...
3k views

### Sieve of Sundaram for Project Euler 7: Python implementation slower than C++ and R

A friend of mine recently started learning R, and she complained to me that R is very slow. She was working on Project Euler Problem 7, which asks for the value of the 10001st prime number. For ...
2k views

### On Knuth's “Algorithm L” to generate permutations in lexicographic order

I needed a method to generate all permutations of given elements, so I decided to implement "Algorithm L (Lexicographic permutation generation)" from Donald E. Knuth, "GENERATING ALL PERMUTATIONS" in ...
441 views

### Count the occurence of nucleobases in DNA string

Inspired by this meta question I decided to take a look at Rosalind. Their first challenge seemed easy enough: An example of a length 21 DNA string (whose alphabet contains the symbols 'A', 'C', 'G'...
360 views

### 3D bin packing in Java

I made some changes to the first code here I wrote and I want to know which one is more correct logically (before the changes or after the changes). It's really hard to manually test these kind of ...
55k views

### Check a string to see if it is a palindrome

I have written two code pieces to check if a given string is a palindrome: ...
21k views

### Two FizzBuzz solutions

Below are two solutions to the FizzBuzz problem in Python. Which one of these is more "Pythonic" and why is it more "Pythonic" than the other? Solution One: ...
834 views

### Integer Log2 implemented using binary search

I saw a simple method to calculate Log2(N), where N is the power of 2: ...
55 views

### Setting Property Values Programmatically in a JSON Object

Through the lens of the SOLID principles, which one of the following makes the most sense? I want to ensure the approach I select keeps everything simple and maintainable as we add properties, modify ...
25k views

### Checking if an array is sorted

I've come up with two ways to check to see if array is sorted and return true if it is. ...
224 views

### Project Euler #5 (LCM 1 - 20) Follow up

I'm actually spending time on it, unlike the first time and going through, understanding and implementing the several suggestions I've landed on two different approaches. Approach 1: Employing Java 8 ...
337 views

### Two keyboard handlers for a video game character

The code below is equivalent. I can see pros and cons for both versions. Which is better: the short, clever way, or the long, ctrl+c way? Short version: ...
1k views

### HackerEarth Girlfriend's Demand challenge, solved two ways

I solved the Girlfriend's Demand challenge on HackerEarth. In summary: Input: First line of the input contains a string S. Next line contains an integer Q, the number of queries. Each of the next ...
5k views

### Calculating a base price with surcharge conditions

The following code has a lot of conditionals. I am trying to write it in a functional programming way. ...
6k views

### Trimming fgets(): strlen() or strtok()

After a successful fgets(buffer, ...), it is often desirable to trim a potential End-of-Line '\n'. Of the following 2 methods, ...
205 views

### Leetcode 38: The “count-and-say” sequence

38. Count and Say The count-and-say sequence is the sequence of integers with the first five terms as following: ...
452 views

### Finding the number of capital letters in a word grouped by letter

Please see the code options below: Client App - this is the same for both options LetterCounter lc = new LetterCounter("HeLLo"); Option 1 ...
255 views

### Enumerating new articles whose publication start date has passed

(cn.PublishEnd == null || cn.PublishEnd < DateTime.Now) vs (cn.PublishEnd ?? DateTime.MinValue < DateTime.Now) Which ...
370 views

### Decimal Quantities needed to meet cost

I asked this question recently: Finding the number of capital letters in a word grouped by letter. The answerer answered my specific question so I have marked it as answered. However, I would like ...
931 views

### Providing const- and non-const overloads of a strstr() implementation

In a question about reimplementing strstr(), I posted an answer. I pointed out a need for const- and non-const versions of the function, and provided ...
141 views

### Comparing three data structures for dealing with probability distributions in Java

Introduction Suppose you are given three elements $a, b, c$ with respective weights $1, 1, 3$. Now, a probability distribution data structures will return upon request $a$ with probability 20%, ...
100 views

### Implementing an array backed list

I post the below code as review/answer to a question in codereview. (Custom Dynamic Array) however, seems the answer itself having issues looking at the comments that I received. I would appreciate ...
67 views

### Handling arguments in a Python-like range() function in JavaScript

I'm making a Python-like range() function (see Python docs) using JavaScript. This function can take from 1 to 3 arguments. There are 3 variables (...
68 views

### Optimizing Luhn check digit algorithm

The internet as a whole and Code Review in special already provide a decent amount of implementations of the Luhn check digit algorithm. They often follow a relatively "naive" strategy, in that they ...
245 views

### Parse 2D matrix, 2 versions

I'm writing a little C program that computes matrices (for learning purpose). The matrix is fed through arguments in the following form : "{{43,543.324,34},{43,432,87}}" The user doesn't give the ...
611 views

### Which is a better implementation of a stack data structure (based on an array)?

This is a version I made: ...