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).
52
questions
11
votes
1
answer
501
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 ...
7
votes
6
answers
1k
views
Compute "negative" version of vector of vectors
Given a std::vector<std::vector<double>> (e.g., representing some points in a space with D dimensions), I want to ...
2
votes
2
answers
92
views
Common Unit Testing Code – Follow Up
This question is a follow up question to the Common Unit Testing Code portion of my lexical analyzer questions.
My primary concern is the code in the header file and the C source file that implements ...
1
vote
1
answer
2k
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:
...
39
votes
10
answers
9k
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 ...
36
votes
3
answers
11k
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 ...
19
votes
2
answers
540
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', ...
16
votes
3
answers
4k
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 ...
16
votes
4
answers
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 ...
4
votes
3
answers
548
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
1
answer
680
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 ...
1
vote
0
answers
1k
views
Median of three partitioning taking more time than standard quicksort in java
I implemented a standard quicksort and I have a project where I need to improve it. I am trying to make quicksort faster by implementing median of 3 partitioning. I copied codes from trusted ...
1
vote
1
answer
64
views
Shortest path in dags: Part 2/3 - the preprocessing and shortest path algorithms
Part 1/3
Part 3/3
I have this library for performing shortest path queries on dags (directed acyclic graphs). This post presents the shortest path algorithms and the topological sorters. Two ...
30
votes
9
answers
58k
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:
...
17
votes
3
answers
2k
views
Shoe shine shop model in Rust
I'm learning Rust and a few questions did arise during translation of my C++ code to Rust. There are comments in Rust code I'd like to be answered. Is there an idiomatic way to solve this task? The ...
14
votes
8
answers
29k
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:
...
12
votes
4
answers
6k
views
Extraction of value from a dict-tree, given a path of arbitrary length
Problem: We read a complex json-file which grants us a tree in terms of a python "dict of dicts". We're then requested to retrieve a specific path from the tree, represented by a list of elements. The ...
11
votes
4
answers
9k
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, ...
10
votes
1
answer
365
views
Resizable containers with VueJS
A month ago I reviewed the code in Resizable split DIVs Vue.js, suggesting that the OP use computed properties and bound styles to simplify the code (at least reducing the need to use ...
8
votes
1
answer
3k
views
Integer Log2 implemented using binary search
I saw a simple method to calculate Log2(N), where N is the power of 2:
...
8
votes
5
answers
31k
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.
...
8
votes
3
answers
1k
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:
...
8
votes
2
answers
524
views
Command line parser only with switches (if-less)
I've been playing around with C# 7 and created this if-less and switch-only command-line parser. What do you think of it? Can it be made more C# 7 or otherwise ...
7
votes
6
answers
3k
views
Checking if an integer is a palindrome using either a string or a dict
I have solved the palindrome question on LeetCode today, but I wasn't happy with the original solution - I used strings, it worked slowly and used a lot of memory.
So I rewrote the answer using a ...
7
votes
3
answers
257
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 ...
7
votes
3
answers
266
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 ...
6
votes
2
answers
568
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
...
5
votes
5
answers
453
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
1
answer
157
views
Shoe shine shop model in Rust and C++ - Follow-up
I've just refactored my Rust and C++ code which simulates the shoeshine shop model from this question. What else can be improved?
C++:
...
5
votes
5
answers
2k
views
Least cost swapping in C++
I've made a solution for a problem which involves changing order of objects having some mass, so it costs a mass of an object A and a mass of an object B to make a swap. The program needs to read a ...
5
votes
1
answer
2k
views
Laravel Scope get request data/param
I want to pass the param from the request on the scope method in the model and serve it as json to be rendered by select2. Which is better?
1. Pass request from the controller like this
controller:
<...
5
votes
3
answers
221
views
PHP classes with functions used for building a web application
The following class is set of functions I use for web development(examples below) and I was wondering how to structure it.
Examples:
...
5
votes
2
answers
1k
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 ...
5
votes
2
answers
523
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 ...
5
votes
2
answers
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 ...
5
votes
1
answer
15k
views
Substring vs Split performance test
Some people are telling me that using string.Split is very inefficient since it creates a lot of arrays in the heap. So, what are the alternatives if its real?
The challenge? Taking a large string ...
5
votes
1
answer
355
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:
...
4
votes
1
answer
259
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
1
answer
122
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
1
answer
84
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
3
answers
174
views
Comparing 8 different Disjoint-Set data structure variants in Java
The Wikipedia page on Disjoint-Set data structures presents \$4\$ distinct algorithms for finding the root node of the tree, and \$2\$ distinct algorithms for performing the union operation. In this ...
3
votes
1
answer
5k
views
Function equivalent to strcmp()
I'm starting out and need feedback in order to discover the things I'm doing wrong.
This is supposed to behave like strcmp():
...
2
votes
4
answers
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.
...
2
votes
0
answers
5k
views
What is better - using SerialPort with or without await/async methods?
I'm having an implementation using SerialPort in C# using Visual Studio 2019. I'm using it in a Windows Application, using .NET Framework 4.7.2.
When I open and ...
2
votes
1
answer
689
views
Which is a better implementation of a stack data structure (based on an array)?
This is a version I made:
...
2
votes
1
answer
523
views
=PRINTF("{1} - {2}", ... ) in Excel without VBA - LAMBDA functions with variable number of arguments
The TEXTJOIN function has a really nice interface:
=TEXTJOIN(delimiter, ignore_empty, text1, [text2], …, [text252])
... where ...
2
votes
0
answers
39
views
Fast algorithms in Javascript for shortest path queries in directed unweighted graphs - follow-up
(See the previous iteration.)
Now I have reworked my pathfinder program, and I ended up with this:
...
1
vote
1
answer
204
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 ...
1
vote
2
answers
4k
views
Return row count from mysqli prepared statement
I want to know if it is okay/safe to use a prepared statement and mysqli_num_rows like this:
...
1
vote
1
answer
875
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 ...