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

Filter by
Sorted by
Tagged with
9
votes
1answer
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 ...
1
vote
1answer
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: ...
35
votes
3answers
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 ...
37
votes
10answers
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 ...
15
votes
4answers
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 ...
14
votes
3answers
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 ...
17
votes
2answers
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'...
2
votes
1answer
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 ...
26
votes
9answers
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: ...
13
votes
8answers
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: ...
7
votes
1answer
834 views

Integer Log2 implemented using binary search

I saw a simple method to calculate Log2(N), where N is the power of 2: ...
1
vote
1answer
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 ...
8
votes
5answers
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. ...
7
votes
3answers
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 ...
5
votes
1answer
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: ...
5
votes
2answers
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 ...
2
votes
4answers
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. ...
10
votes
4answers
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, ...
7
votes
3answers
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: ...
6
votes
2answers
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 ...
6
votes
3answers
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 ...
5
votes
5answers
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 ...
5
votes
2answers
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 ...
4
votes
1answer
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%, ...
4
votes
1answer
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 ...
3
votes
1answer
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 (...
3
votes
2answers
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 ...
3
votes
3answers
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 ...
2
votes
1answer
611 views

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

This is a version I made: ...
1
vote
1answer
108 views

Replacing words with their abbreviations - Follow up

This is the follow up for a question you can find here: replacing words with their abbreviations The goal here is to compare two petential way of answering said question that was: This particular ...
0
votes
1answer
2k views

Filling a matrix with values

I would like to hear your recommendations on when to use JavaScript's const over let, especially when applied to mutable objects ...