New answers tagged

0 votes

Print a hollow Rhombus

Imagine you know someone beginning Java three years after writing this. You show them the code presented: Can they easily find out from looking at the code what it is to do? Whether it is correct? Do ...
greybeard's user avatar
  • 6,104
2 votes

Optimizing __getitem__ for a bioinformatics script in Python

new to the area and I've been learning while developing Great, welcome! Software is almost as interesting as biology. And it's much less messy. measurements much longer for larger files “When you ...
J_H's user avatar
  • 16.8k
3 votes

Spatial radius search in Boid simulation

Nice problem! I gotta tell ya, QuadTrees is definitely the appropriate datastructure. And it's just not that hard. Let coord be the bit interleaving of ...
J_H's user avatar
  • 16.8k
3 votes

Spatial radius search in Boid simulation

The slowness comes from drawing Your query() function is actually quite efficient. The problem comes from drawing the boids. If you remove the calls to ...
G. Sliepen's user avatar
  • 50.4k
2 votes

Pentomino solver in Python

Kudos on tackling such an interesting problem! design of Public API ...
J_H's user avatar
  • 16.8k
0 votes

Sum of all possible concatenations of array values

You can reduce the line count of Donald's answer to improve readability by: Removing the for loop with Array.reduce(), Using ...
Santiago Bendavid's user avatar
1 vote

Trailing Digits

finally I found a link with a lot of examples. I found trailingdigits.py with a much more elegant approach github.com/RussellDash332/kattis/blob/main/src/… and another webpage that shows more detail ...
qleoz12's user avatar
  • 141
1 vote
Accepted

Given an array, remove zero or more elements to maximize the reduction where you add odd values and subtract even values

You could simply use this pattern: [greatest value, smallest value, greatest value, smallest value, ...] local maximum, local minimum, ... That means, go until ...
Nina Scholz's user avatar
2 votes

Trailing Digits

Picture yourself coming back to "V1 code" a decade from now: You can read it, you can run it. Do you know what does, and to what end? Name things for what they are good for. ...
greybeard's user avatar
  • 6,104
2 votes

Finding a distinct triplet where the sum of the values is divisible by a given number

Its possible to solve the problem in O(N + D*log(D)) time, using the Fast Fourier Transform (FFT). In a nutshell- reduce each value mod ...
Dillon Davis's user avatar
1 vote

Finding a distinct triplet where the sum of the values is divisible by a given number

I'd prefer divisible_triplets(integers, divisor). And it deserves a docstring. You state I want to count the number of … - I don't want to be caught telling people ...
greybeard's user avatar
  • 6,104
2 votes

Finding a distinct triplet where the sum of the values is divisible by a given number

One of the weaknesses of the code is that we're actually finding every triple. We're not required to do so - the only thing that matters is the count of results. That means that we can group the ...
Toby Speight's user avatar
2 votes

Finding a distinct triplet where the sum of the values is divisible by a given number

We can preprocess the array by setting each a_i to a_i modulo d. We're asked for a sum of ...
J_H's user avatar
  • 16.8k
1 vote

Finding a distinct triplet where the sum of the values is divisible by a given number

One improvement might be derived from changing every array entry to its modulo 5 value: ...
Joop Eggen's user avatar
  • 3,376
2 votes

Python (API requests)

Here goes the new code following @J_H advices: Project structure ...
Aldimar J's user avatar
2 votes

Project Euler 60: Prime Pair Sets

Eliminate 2 and 5 from your sieve! Any 2+ digit prime cannot end in a 2 or a 5, as these will be multiples of ...
AJNeufeld's user avatar
  • 33.3k
1 vote

another Merge Sort

Code risks invoking undefined behavior: None of the calls to malloc() have their return values checked. ...
Harith's user avatar
  • 2,302
1 vote

another Merge Sort

Is there any other good practices that I've missed to follow in my try? Sort stability Agree with @Martin York code has stable sort concerns. A stable sort keeps the array elements in the same order ...
chux - Reinstate Monica's user avatar
3 votes
Accepted

another Merge Sort

Three things pop out to me: This is not a stable sort. Probably does not matter, as you currently only support integers. This only support integer sorts. If you look at the standard qsort you will ...
Martin York's user avatar
  • 94.3k
1 vote

Python: Detect if a number is divisible by any number in a list

i is not divisible by a factor of step_size iff gcd(i, step_size) == 1. Otherwise, that gcd ...
harold's user avatar
  • 9,059
2 votes

Python: Detect if a number is divisible by any number in a list

natural log I'm unhappy with this expression: prime_count = ... len(str(number)) // 3 Since the length computes log10(), and <...
J_H's user avatar
  • 16.8k
3 votes
Accepted

Python (API requests)

In a pathname like logs/err-logs.log, the -logs suffix seems redudnant. one level down Based on ...
J_H's user avatar
  • 16.8k
2 votes
Accepted

Distance on a unit grid

min_x = min(abs(x1 - x2), dist_x = (x1 - x2) ** 2 We can see the border cross algorithm and the standard algorithm are ...
Peilonrayz's user avatar
  • 42.7k
4 votes

Find the closest named color

Incorrect calculation of color distance sRGB is a logarithmic scale RGB (AKA sRGB) as used by the browser is a logarithmic scale (apart from the very darker values which are linear) Your calculation ...
Blindman67's user avatar
  • 21.6k
3 votes
Accepted

Multi producer/consumer lock-free queue

This is not lock-free Unfortunately, your queue is not lock-free. Basically, your _canUpdate is used as a lock. Consider a thread that has taken the lock, so it's ...
G. Sliepen's user avatar
  • 50.4k
1 vote

Optimisation: Fastest way to multiply every number in an array by 2

Thanks to @harold the goal of getting under 50 ns was achieved with loop unrolling and removing the unnecessary copy instruction. The code below is similar to his but is bithacked and instead of using ...
Henners2002's user avatar
1 vote

Optimisation: Fastest way to multiply every number in an array by 2

A couple of further things we can do are: Use Vector256.StoreUnsafe for the store. It's not a huge deal, but going by the assembly, the ...
harold's user avatar
  • 9,059
0 votes
Accepted

How to optimize the player movement code like in Tomb Of The Musk game?

In terms of performance what I have done is removed unnecessary method calls and helped out your branch predictor by removing if/else statements. What I have mainly done though is to enhance code ...
Henners2002's user avatar
1 vote

Optimisation: Fastest way to multiply every number in an array by 2

After a suggestion by @harold a further 15ns has been shaved off by using the Vector.ShiftLeft(v, 1) method instead of doing the multiplication and secondly to use <...
Henners2002's user avatar
1 vote

Optimisation: Fastest way to multiply every number in an array by 2

Upon a suggestion from @iSR5 I have reimplemented the SIMD instruction using the MemoryMarshall method. This has shaved off a further 60ns. I think SIMD instructions and the memory marshall method go ...
Henners2002's user avatar
3 votes

Optimisation: Fastest way to multiply every number in an array by 2

I have shaved off about 100ns with the following improvements: used a shift operator to perform the multiplication by 2, since I know that my Vector<int>.Count...
Henners2002's user avatar
3 votes
Accepted

Convert 64-bit Gray code to integer

While it's not your code, I'll point out that the routine from OEIS can be significantly shortened by using an assignment expression, available since Python 3.8: ...
benrg's user avatar
  • 466
6 votes

Convert 64-bit Gray code to integer

I assume that your first example is depicted in human-legible (i.e. big-endian) order. The right-most digit is indeed then the least-significant digit. When you call ...
Reinderien's user avatar
-3 votes

Brute force password cracker in Python

...
bisht nirmal's user avatar
0 votes

calculate the number of ways to pick two different indices

use boring business vocabulary Your task ... two different indices i < j, such that a[i] + a[j] ... for (let e = ... if ((a[i] + a[e]) ... ...
J_H's user avatar
  • 16.8k

Top 50 recent answers are included