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).
925
questions
6
votes
6
answers
887
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
1
answer
43
views
rust sort numbered directories (non-lexicographical)
I am very new to rust and have been reading up and playing around to get a better understanding. I was trying to replicate a small task that I have done via bash scripting before, as a way to ...
1
vote
1
answer
50
views
calculating the percentage of distribution of ids per Loc for each country
Which of the two queries is theoretically better and why?
I would like to understand why query 2 takes longer although there is no relevant information in the Execution Plan.
I'm querying a Presto ...
3
votes
3
answers
87
views
Parse program arguments of the form key=value
I have two proposals for parsing arguments:
Method A
...
0
votes
0
answers
7
views
class docstring or __doc__ docstring for namedtuple? [migrated]
Based on different answers to this question: Adding docstrings to namedtuples?
attribute docstring
...
-1
votes
1
answer
150
views
Getting three numbers input interactively and process them
I'm new to Python and programming in general.
Let's imagine I have a simple task:
Given 3 numbers from user input, I need to count negative ones and print the result.
Considering what I know about ...
0
votes
2
answers
141
views
Functional Style if/else in java
I would like to ask the opinion for experts out there. The following code snippet was written by a colleague of mine who is very fond of functional style in Java.
...
2
votes
1
answer
150
views
C/C++ hash looking in flat database compared to PHP
I have a hash table which I would like to query as fast as possible.
The hash table is stored this way :
When I query a hash, I extract first 4 characters then use it to find the file. For instance a ...
3
votes
2
answers
879
views
Convert Rankine temperatures to Celsius
I have this Python Package called ToTemp under development for precise Temperature Conversions and currently thinking in changing the method's implementation to be ...
1
vote
2
answers
87
views
Circular list vs. doubly-linked list: which is faster?
In this post, I will compare performance of a circular list and a conventional list with head/tail -references:
...
5
votes
2
answers
610
views
Recursively calculating powers by squaring
Consider the following function that implements optimised O(log n) exponentiation by squaring:
...
2
votes
1
answer
65
views
Array Implementation In JavaScript
Review
I have written my own implementation of Array in JavaScript with the basic functionalities. Can anyone please review this code and point out the mistakes/how to make the implementation better?
...
0
votes
1
answer
66
views
Comparison of two ways of looping in Python [closed]
I wrote a small script to demonstrate two ways of looping in Python: with and without using the index. I want to know which way is better. Please see the example:
...
1
vote
1
answer
66
views
Build a message string depending on the type of signature of an invoice
The following method was meant to build a message depending on the type of signature of an invoice. It contains some if-else statements and code duplication.
The original method :
...
1
vote
1
answer
49
views
Comparing a weak fluent API with a strong fluent API in Java [closed]
I have this simple POJO:
...
2
votes
4
answers
986
views
Calculate the median value of combining two sorted lists
I have solved a leetcode problem (Median of Two Sorted Arrays), and I came up with two solutions.
Solution 1
...
2
votes
2
answers
70
views
Project sequence to multiple sequences using LINQ (or extension methods)
I'm creating a method that expands a sequence.
This is a sample of how it works:
Input: {1, 2, 3}
Output: {1}, {1, 2}, {1,2,3}
So far, I've been able to solve the ...
2
votes
1
answer
98
views
LeetCode#494 target sum (dart is slower than python)
I'm trying to solve a LeetCode problem target-sum, in two languages: Python & Dart. The difference of time taken is shocking for me"
Python took ~70ms while Dart took ~900ms !! Why does Dart ...
2
votes
1
answer
66
views
Selection algorithms (order statistic) in Java: linear time vs. sorting selector vs. Quickselect
This post is about computing order statistics: given an array \$A\$ (not necessarily sorted), find the \$k\$th smallest array component. The entire repository is here.
...
0
votes
0
answers
43
views
Euler's totient function in Rust, with a performance difference between generic and classic functions
I recently started to program in Rust, having like 2 good years of experience in C++, I thought it would be possible to directly switch to Rust without following a lot of tutorials, especially knowing ...
2
votes
1
answer
130
views
Card game simulations, with a multithreaded version slower than the single-threaded one
I'm new to rust and I'm trying to build a card game simulation to find all of the best actions with all possible hands. This requires a lot of simulations and my first 3 million are already taking a ...
0
votes
1
answer
65
views
Find longest "binary gap" [closed]
I'm doing a few challenges and I found this one on Codility:
A binary gap within a positive integer N is any maximal sequence of
consecutive zeros that is surrounded by ones at both ends in the
...
1
vote
1
answer
63
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 ...
0
votes
1
answer
85
views
Show numbers per step count
I have a problem where I need to display the below output
1
23
456
78910
My first solution is
...
3
votes
1
answer
95
views
"Select followed by First" versus "First followed by Select" when summing the data for a student
I am wondering which one I should choose between the following two options when I need to calculate the sum of Data of the student with ...
5
votes
1
answer
240
views
Calculate the number of people occupied at each time interval
The following scripts calculate a list with the number of people occupied at each time interval.
The intervals are defined by the timestamps of two consecutive events that come from the sorted list <...
2
votes
3
answers
596
views
Returning the other value of boolean enum
I have a class with a "mode" state. There are currently two modes and we are not likely to add more in the future.
The class has a CurrentMode property, ...
1
vote
1
answer
228
views
Functional Fibonacci in OCaml
I'm very new to OCaml, and as an exercise I decided to implement the nth Fibonacci algorithm (return a specific Fibonacci number, given an index) in different ways, using what I've learned so far.
I ...
-2
votes
1
answer
122
views
Insert row into database using static or non-static method [closed]
I would like to know if there are any benefits of one of the following 2 methods which insert an object into a database
The first function calls the Model method statically, creating a new instance of ...
2
votes
1
answer
63
views
Batching method implementation - Scala
I need to implement a function/method that operates on a sequence of values, and acts kind of like takeWhile, but with 2 differences, first, it doesn't act on a single element, it is performed on a ...
1
vote
1
answer
80
views
Categorise amount into "small", "medium" and "large"
I have a super simple function that returns a text label for a range of the input
...
3
votes
3
answers
256
views
A refactored payment service, handling several payment schemes
I was asked to refactor a class, to make it adhere to SOLID principles, with testability and readability. I thought I had done a decent job, only my feedback was:
a) Unnecessary introduction of ...
3
votes
0
answers
405
views
Return a 2D matrix from a C++ function in Python: ctypes versus pybind11
I found two ways in which I can use a custom C++ function (returning a 2D matrix) in Python. One approach that uses the Python module ctypes and another that uses the Python module pybind11.
Approach ...
3
votes
0
answers
2k
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 ...
1
vote
1
answer
132
views
Constructing routes from imported URL constants
There is a file created with the project paths in a dictionary.
Example:
...
1
vote
1
answer
54
views
Comparing two general LCA algorithms in Java
Now I have two algorithms solving a problem: given a general (multi-way) tree, and any array of node names, find the the node that is the deepest common ancestor.
...
0
votes
2
answers
207
views
Draw a triangle of stars
I decided to try to write half a pyramid of stars using programming because I wanted to see if I could do it, and I did it. However after comparing it to other people's solutions I started doubting ...
0
votes
1
answer
65
views
Time complexity of several methods of reversing a domain name string
Note: I'm aware of this existing post which is similar. However, it does not address all my questions sufficiently.
I have the following three functions, all of which do the same thing: reverse a ...
3
votes
1
answer
525
views
Python function to output a string that is justified to the right
I just solved this exercise from Think Python, 2nd edition:
Python provides a built-in function called len that returns the length of a string, so the value of len('allen') is 5. Write a function ...
3
votes
2
answers
160
views
Retry read/write until all bytes are transferred or error occurs
Currently I have a wrapper function for the read/write system calls, the purpose of which is to eliminate partial transfer ...
3
votes
1
answer
966
views
Format an integer with space as thousand separator
I need to format a number with space as thousand separator in my Java code for Android.
How can these functions be improved?
What are the pros and cons to the first recursive method?
What are the ...
1
vote
1
answer
89
views
Show progress bar for different states
I have this working code which shows the progress bar based on different states ("not-started", "progressing" and "done"). Each state will have its own class.
I notice it ...
1
vote
1
answer
162
views
Comparing a prefix tree against a HashSet of strings in Java
Here, I have made an attempt to find out whether a prefix tree is any more efficient than a simple java.util.HashSet<String>. See what I have:
...
2
votes
0
answers
48
views
Python: How to best create a dictionary from another dictionary in a library
I have the following code which builds up a dictionary of endpoints that I iterate through later on. Is this the cleanest, most scalable and performant way?
I was thinking of adding a list as a key to ...
4
votes
3
answers
101
views
Destructuring with map and assigning to a constant
I don't know exactly what to name this, but I was refactoring a code snippet and looking at this constant users I thought I'd do this destructuring:
Before:
...
1
vote
1
answer
88
views
Java: Using one class or algebraic data types to model different state?
I'm creating a tool to analyze a card game which is played by three players. A Card is associated with a number of points and looks like this:
...
0
votes
1
answer
71
views
2 ConvertToBase3 methods with great disparity of execution time
I have two methods to do the same thing: convert a number to ternary.
...
0
votes
2
answers
279
views
c++ Most clean way to implement duplicate Binary Search Tree for complex type
I have tried to make my own implementation for a BST task I was given for a complex type (Transaction), that should be able to store duplicate values as well.
However, I am not sure if I have gone ...
2
votes
2
answers
197
views
How can I optimize this pairwise Euclidean distance search algorithm?
x and y are two-dimensional arrays with dimensions (AxN) and (BxN), i.e. they have the same number of columns.
I need to get a matrix of Euclidean distances between each pair of rows from x and y.
I ...
1
vote
0
answers
51
views
Higher-order function to do throttling, written two ways
I have used below method for throttling which is a Higher Order Function (HOF), but find some difficulties to choose which one is better way to write it. Below are both methods.
return of the ...