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
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 ...
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 ...
30
votes
6answers
3k views

To 'this' or not to 'this'?

I was given a homework and I have 2 solutions: one that uses this and other one that doesn't. I tested both solutions on jsPerf but sometimes it says the version ...
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: ...
26
votes
3answers
2k views

Java method - levels of abstraction

In his Clean Code book, Robert C. Martin states that functions should do only one thing and one should not mix different levels of abstraction. So I started wondering whether the code below meets ...
25
votes
4answers
80k views

Is the LINQ version faster than the foreach one?

Since I don't know how LINQ works under the hood, I can't decide what version is best to use in term of rapidity of execution. I've done some testing with my testing data (Point Cloud) but I can't see ...
22
votes
5answers
1k views

When a constructor calls a helper function, should the helper mutate the object or return part of the object?

I have a private method that is called from the constructor to instantiate a bunch of objects. I can do it two ways. ...
17
votes
11answers
1k views

Reversing a string

I had this as an interview question, and the interviewer pointed this out. Here's what I wrote: ...
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'...
16
votes
4answers
8k views

20 Ways to do Random Sampling

Here is a (small project) code for practicing Python, it comprises 20 variety of functions that each works to return \$n\$ random samples from a set of data. The code mostly uses the ...
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 ...
15
votes
3answers
2k views

Java and C++ stack implementations with dramatically different performance

I'm developing a small programming language for use in any project I have where I feel a small scripting language could be used well. I've written two emulators for the language, one in C++ and one in ...
15
votes
4answers
4k views

Generating formatted multiplication tables in C++

I am self studying C++ from Robert Lafore's OOP in C++. I am solving the first exercise questions of chapter-3 loop and decisions. I've written three different codes using different approaches. I want ...
14
votes
5answers
22k views

Simple countdown programs to print “3, 2, 1, Action!” using while loop

My program is a simple while loop making a countdown starting from 3 and shouts "Action!" upon reaching zero and then breaks. I would like to know which of the following ways is better in means of ...
14
votes
3answers
3k views

Unit tests for testing error codes

Both unit tests pass and they both test the same thing. The production code uses the right error codes. System Under Test (SUT) ...
14
votes
2answers
25k views

Flatten an array - loop or reduce?

Problem: Concat all sub-arrays of a given array. Example input: [[0, 1], [2, 3], [4, 5]] Example output: [0, 1, 2, 3, 4, 5] ...
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 ...
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: ...
13
votes
1answer
1k views

Functions to hide and reclaim first visible publication on a page using Selenium

I have the following functions in JavaScript: ...
13
votes
2answers
18k views

Forking with PHP (4 different approaches)

I thought about three (now four) different ways how to execute forks with PHP. What is the faster, maybe better solution and why? PHP script included ...
12
votes
6answers
2k views

“Do you owe the government?” classes

Should my class pass parameters internally or reference class level scoped variables? I'm not sure on the best approach or style for procedure calls that have parameters. Should I go with the class ...
12
votes
2answers
677 views

Printing removed items using lambdas and streams

I'm struggling to make my lambdas readable. I've seen various approaches. Below are three different examples that all do the same thing. Forgive the example: I suspect there are better actual ...
12
votes
3answers
291 views

Becoming the greatest tower builder

I am trying to learn how to write proper code in python, and stumbled upon the following problem. Bob is given a set of building blocks b, and a number ...
12
votes
2answers
15k views

Async/await and Task vs Task.Factory.StartNew and Result

I am working on creating some libraries for a project at work and I wanted to make sure I have this pattern correct. Assuming that the GetWidgets method is what I ...
11
votes
4answers
525 views

Function to format the local time

This is a statement that I use a lot in my code base: (LocalDateTime.now()).format(DateTimeFormatter.ofPattern("ssmmHHddMMMuu")) Should I create a static method ...
11
votes
4answers
2k views

Generating the sequence of the digits that appear in the sequence of natural numbers

This is a revisit to a question already asked here about a year and a half ago. Project Euler's Problem 40 involves generating the sequence of digits that appear in the natural numbers, all ...
11
votes
2answers
3k views

Parsing an email string

My program needs to parse an e-mail string. There are two possibilities to enter an e-mail-address. Either with the alias or without one, plain the e-mail-address. 1st possibity: ...
11
votes
1answer
113 views

Counting times a file was committed to Git

A long time ago I implemented some code to walk the git commit graph. I recently updated the accompanying analytics part of the code base to follow file moves/renames in order to get a more accurate ...
11
votes
2answers
9k views

Packing and unpacking two 32-bit integers into an unsigned 64-bit integer

Does this look good for combining/retrieving two 32 bit integers (a type and an index) into/from an unsigned 64 bit integer (a unique ID)? The point is to hide in the API the composition of the ...
11
votes
1answer
329 views

Testing a list of proxies for validity concurrently

I have a class that tests a list of proxies for validity concurrently, which I have created in 2 different ways, one utilizing Parallel.ForEach and the other using a TPL Dataflow ActionBlock. I am ...
10
votes
8answers
12k views

Calculate square of a number without multiplication

I wrote these two methods to calculate the square of a number: ...
10
votes
5answers
3k views

“Of three points supplied, is one a midpoint of the other two?”

Write a method named hasMidpoint that accepts three integers as parameters and returns true if one of the integers is the midpoint between the other two integers; ...
10
votes
6answers
3k views

Getting filtered autolist

Is there a better way to write the following code fragment? I can't have more than 10 conditional statement in my method as it gives cyclomatic complexity. ...
10
votes
4answers
2k views

Title case a sentence function

I am new to programming. I have written a solution in two different ways, but would like to know what is considered a better solution, and why. Additionally, in terms of performance, why would one ...
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, ...
10
votes
2answers
953 views

Processing text files for data extraction and analysis

I started learning Haskell to see if I can use it at my job. A lot of my work is processing text files for data extraction and analysis. For my first test, I added a counter at the end of each line ...
10
votes
2answers
391 views

Find index of double number delimiter

I have a String with a double number. Unfortunately, the number is created on backends with different locals, so it could be ...
10
votes
2answers
6k views

Parsing CSV file with BufferedReader vs Scanner

I have to parse a file line-by-line and in single line I have split by ",". The first string is the name and the second is the count. Finally, I have to display the Key and Count, for example: ...
10
votes
1answer
666 views

Subclassing pathlib.Path

This arises from the SO Question subclassing pathlib.Path. The pathlib sources show that Path, when invoked, selects one of ...
9
votes
6answers
1k views

Length of a C string, capped to some maximum

If I have a oneliner in C such as size_t maxSizeOf(const char *s) { return strlen(s) > M_MAX_SIZE ? M_MAX_SIZE : strlen(s); } would it be better to have ...
9
votes
6answers
5k views

Sum two integers, unless they are the same, in which case return double their sum

I was doing a simple problem on codingbat and testing out my python. The problem was: Given two int values, return their sum. Unless the two values are the same, then return double their sum. <...
9
votes
5answers
285 views

Applying a string-identified effect on an image

Task: Given an image object and an effect identifier (string), apply the effect identified by the string on the image. The library of effects can grow as the program grows. Code 1: ...
9
votes
2answers
2k views

Fastest C++ code to get common prefix length

In a C++ program, I have the following function to find the length of the longest common prefix between to char arrays a and b. ...
9
votes
1answer
1k views

Implementing a string class in C++

I recently attended an interview for a C++ position in a very reputed bank. The interviewer started off by asking me to implement a string class but was only interested in constructor, copy-...
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 ...
9
votes
1answer
481 views

Horridly Inefficient Project Euler #14 in Python - Collatz Sequences

I created this solution a while ago, and have been going back over my problems to try to make them all more efficient. I have been stuck on this one, and have tried a few different ideas. So far, my ...
9
votes
4answers
1k views

Filter function implementations using copy_if algorithm vs. for loop

I have the following method of some class, which also defines the method isAllowed: ...
8
votes
5answers
1k views

Assembling a user agent descriptor, implemented using StringBuilder vs String

I think performance won't be a big issue here as the concatenation only happens once (2-3 at worse because of multiple threads). Out of the two methods, which one would you prefer, in terms of ...
8
votes
4answers
2k views

Reversing a string - two approaches

Test Case: Input- "I'm hungry!" Output- "!yrgnuh m'I" Approach 1: In this approach, I used a empty string and bind it with the input string reversely. ...
8
votes
3answers
1k views

Creating a status object based on the command type and action

I need to create an object based on some properties of another given object. In my example setRules, add properties to object ...