New answers tagged

0 votes

Closest Pair of Points - Optimizing Code for big sets of points

What do you want to optimise it for - what are your bottlenecks right now? You could profile the code to identify improvement areas, but if you truly are concerned with speed or memory footprint here ...
user avatar
  • 906
0 votes

Calculating square roots using binary search

You're not using binarysearch here, but a form of Newton's Method. This will work, and is the appropriate algorithm for floating point numbers, but unlike binarysearch, it takes no advantage of the ...
user avatar
  • 111
0 votes

Calculating square roots using binary search

Not familiar with rust but there are a few improvements. when writing the code usually we go for the happy scenario, in the case of enterprise applications users may enter parameters which are not in ...
user avatar
1 vote

Printing the first n numbers of the Fibonacci sequence

n--; }while(n >= 1); You don't need to consume the input argument. You already have an up-to-date value. ...
user avatar
  • 20.3k
1 vote

Printing the first n numbers of the Fibonacci sequence

JavaScript number primitives and Number objects use IEEE 754 Floating-Point Arithmetic. The Number.MAX_SAFE_INTEGER constant represents the maximum safe integer in JavaScript (2^53 - 1). Use ...
user avatar
  • 3,273
1 vote

Printing the first n numbers of the Fibonacci sequence

For readability, I prefer the (pythonesque?!) destructuring assignment  (and array over, say, BigUint64Array[n]). I do not find value added by the name suffix ...
user avatar
  • 4,293
1 vote
Accepted

Solution to arithmetic arranger from freecodecamp

Use PEP484 type hints. The too-many-problems and too-many-digits constraints are not good ones, but whatever: we'll keep them because the problem asks so. You call ...
user avatar
  • 47.9k
1 vote
Accepted

Number of paths in a randomly labeled complete graph

Prefer struct over std::tuple where possible std::tuple has several drawbacks over declaring ...
user avatar
  • 32.5k
1 vote

Calculate the value of PI in JS

There's no need to recalculate the factorials and powers from scratch each time around the loop. Remember the last values and just multiply to get the next value. Approximate idea (untested, and by a ...
user avatar
  • 57.7k
1 vote

Case-insensitive sort in C

Bug: String handling is unsigned For all functions in this subclause, each character shall be interpreted as if it had the type unsigned char (and therefore every possible object representation is ...
user avatar
0 votes

Find all pairs of natural numbers whose sum is 12

The spec is wrong, at least in its example data. There's one too many [6,6] and one too few [5,7]. The spec asks you to think ...
user avatar
  • 47.9k
2 votes

Find all pairs of natural numbers whose sum is 12

Reading the input. The format of the input file is not clarified in great detail, but it looks like the same format one would get from printing a Python list of integers. Which means that its format ...
user avatar
  • 9,500
2 votes

Deepest pit of an array

function deepest_pit(B) { The JavaScript style guide here indicates that camelCase is recommended for functions. JavaScript is ...
user avatar
6 votes

Deepest pit of an array

Overall, reading this code, my reaction is "huh?". It looks like a bunch of gibberish at first (and second) glance. There are no english words. I would not want to maintain this code after ...
user avatar
3 votes

Topological Sort C++

We use std::unordered_map but haven't included the corresponding header. Perhaps that works on your implementation, but it's not portable to depend on that. ...
user avatar
  • 57.7k
1 vote

Find the 'n' most frequent words in a text, aka word frequency

Comments are inline in /// ...
user avatar
  • 121
3 votes
Accepted

С++: Find max element from section in matrix

Create a class Matrix Instead of passing around vectors of vectors, consider creating a class Matrix. That makes the code much ...
user avatar
  • 32.5k
0 votes

Long arithmetic addition in JS

Review comments are inline in ///. ...
user avatar
  • 121
1 vote

Estimating the total production count from a few random serial numbers

The review comments are inline after ///. A summary of the review follows the code ...
user avatar
  • 121
0 votes

Duplicate Binary Search Improvements

First, I want to say thank you to everyone who offered their critiques and advice. Not only did I learn a lot about binary search, I learned a lot about how to write better Python code. ...
user avatar
2 votes

Duplicate Binary Search Improvements

Style issue There is a style guide for Python called PEP 8 which is definitly worth reading. Among other things: Avoid trailing whitespace anywhere Comparisons to singletons like None should always ...
user avatar
  • 28.7k
1 vote

Reading 12-bit words from 32-bit stream

It took me a while to figure out what is really going on here, because the explanation is either incomplete, incorrect, or both. In fact, the 12-bit values are not being read in the order shown, are ...
user avatar
  • 8,116
2 votes
Accepted

isPalindrom-function in TypeScript

I have used the double-equals operator, because the values can't be something else then strings. Is it still necessary to use the triple-equals operator in TypeScript? This is backwards. Necessary? ...
user avatar
  • 20.3k
2 votes

Reading 12-bit words from 32-bit stream

Overview: I can't see anything wrong with this code on a read through it (I did not compile it). All my comments are minor and just nit picky. So the below is just a few things I would do differently (...
user avatar
  • 91.5k
1 vote
Accepted

Subset Product Algorithm

Reasons to put code in functions (and classes): Code on module level is evaluated right away: I can't try it out using copy&paste if there is an input() ...
user avatar
  • 4,293
1 vote

Algorithm for twoSum

You can see that your solution is indeed O(N2), because: The outer loop goes through N elements. For each iteration of the outer loop, the inner loop goes through, on average, N/2 elements. ...
user avatar
0 votes
Accepted

Algorithm for twoSum

Hopefully it's obvious that the "worst case" runtime will happen when there is no matching pair in $nums.* ** In that situation, every (unordered) pair of ...
user avatar

Top 50 recent answers are included