New answers tagged

0 votes

Find the 10001st prime number

I did it this way for JavaScript Koans ...
user avatar
0 votes

Calculate square of (sum of even-indexed) - (sum of odd-indexed) subarrays from an array

This problem can be treated as a candidate for Kadane's algorithm after odd indexed elements are negated. Original array A = [1,2,-1,4,-1,-5] negate odd indexed elements B = [1,-2,-1,-4,-1,5] Once ...
user avatar
3 votes

Applying a cost function to a string based on a pattern string

Without saying this lightly, this code is a proper nightmare. Here is a list of things that do not make your code faster: Combining variable assignments on one line Writing a list comprehension used ...
user avatar
  • 49.5k
2 votes
Accepted

Coordinate Descent Non-negative Least Squares optimization

If I understand your code correctly, you subtract the columns of the matrix a from vector b, each column multiplied by a different scalar diff. You also add diff to an element of vector x. Basically: <...
user avatar
  • 2,277
1 vote

Coordinate Descent Non-negative Least Squares optimization

Avoid branches Branches in code, like those caused by if-statements, can sometimes be expensive on modern processors. Consider the case of ...
user avatar
  • 33.2k
2 votes

Optimization using a differential evolution technique, ideally with a million iterations

(No help to a lot of speed: Describe "the Whys": What's special about 1000000? Is something supposed to converge? Is there some tolerable error limit for the result of ...
user avatar
  • 4,548
2 votes

MATLAB / vs Python np.linalg.solve, heat diffusion

Can you please tell me why Python is an order of magnitude slower for only even linear solve? Answering this generically is perhaps a little off-topic for review, but: first, it's important that we ...
user avatar
  • 49.5k
2 votes

Removing medications older than 180 days ago from a list

Bugs The code has at least two bugs. The first is when two adjacent list items are more than 6 months old, the second one will not be deleted. This occurs because when an item is deleted, the rest of ...
user avatar
  • 9,130
3 votes

Removing medications older than 180 days ago from a list

Use a list comprehension! When constructing lists in python by looping through others lists, its a lot more visually de-cluttered (not necessarily more performant) to just use python's list ...
user avatar
-1 votes

Removing medications older than 180 days ago from a list

Depends on how much you care about performance. If you really care, don't use Python. If you kind of care, use Pandas in Python. I cannot speak to how you load your data in Pandas because you haven't ...
user avatar
  • 49.5k
3 votes

Laravel 9 - Metadata creation using middleware?

Redis If you really need to use specific data and you don't have other ways to do it you can use Redis. Misc improvements (Not sure if this works with Laravel 9) You also should not save direct URL ...
user avatar
  • 31
1 vote
Accepted

Approximation of the multiplicative matrix inverse (linalg.inv ())

Guess number one: you've started with a sparse matrix (good), but then you're failing to call into the sparse matrix linalg methods (almost certainly ungood). Also avoid a dense ...
user avatar
  • 49.5k
1 vote

Printing contiguous sub-arrays

Your code has one bug that causes it to crash. The line std::cout << subarray[array.size()-i-1] << " "; should instead be ...
user avatar
  • 1,063
3 votes
Accepted

Weighted Union-Find class

Documentation Docstrings are for users of the class. Docstrings should explain what is being done and returned. Currently your docstrings explain how it is being done, which should be comments. ...
user avatar
1 vote

Reducing the time for rolling hash

Two more things about something like sum=sum+(int)pattern[j]-96;: - 96 is a magic number: The reader is left to figure out what it means here. ...
user avatar
  • 4,548
1 vote

Fantasy game inventory — Ch. 5 Automate the Boring Stuff

I'm doing this in June 2022 but I don't think the problem asks for plurality for the values. In the example given, all the values are worded singularly. However if you want to go one step further to ...
user avatar
2 votes
Accepted

Is this a good way to check for an anagram if sorting is not allowed?

I feel very stupid for not have picked that up while writing the code, and I would of done the same thing if I knew that sorting was an option. Your way of handling the problem, from trying to do it ...
user avatar
  • 4,633
2 votes

Is this a good way to check for an anagram if sorting is not allowed?

This function sometimes throws an exception due to the improper repeated s1.Remove(x, 1) which eventually tries to remove a character that doesn't exist, that's ...
user avatar
  • 6,504
0 votes

Sum and bubble-sort student scores

Including some specification in cal_total_score() & student_bb_sort() is the way to go, credit going to the creators of the &...
user avatar
  • 4,548
0 votes

Sum and bubble-sort student scores

Here is what I think: Do not use bubble sort(BS), except it is your teacher's requirement. Because the time complexity for BS is n*n (n are the elements) if you want to sort ...
user avatar
1 vote

CSS gradient text that fades in and out

I see that your code can be shortened and the terrible double tags removed. For the smoothness of the gradient, I just looped it - I hope I understood the idea correctly. ...
user avatar
3 votes

Sum and bubble-sort student scores

But I wonder if there is a more efficient way. Instead of bubble-sort, use qsort(). Yet I expect OP's assignment may oblige bubble-sort. ...
user avatar
3 votes

Sum and bubble-sort student scores

Advice 1 typedef struct student { int id; int prob_1; int prob_2; int prob_3; int total_score; char grade; }Student; Since ...
user avatar
  • 25.7k
2 votes

Vectorset, a faster std::set

Enable compiler warnings and fix them My compiler warns about the wrong order of the initializer list for the constructor of vectorset::Iterator. I would simply ...
user avatar
  • 33.2k
3 votes
Accepted

Adding string to a text file without duplicates

You could make some improvements: Open the file just once in read+update mode. Instead of current_id_list, use a set, which can test whether an item is one of the ...
user avatar
2 votes
Accepted

Generate new column based on two columns of dataframe

I don't see any value in Pandas here. Use Numpy broadcasting directly between an a 10x1 array and a b 10x6 array, producing a ...
user avatar
  • 49.5k
2 votes

Filter Range VBA function for Excel

Function Over Form I have improved on my working solution, and I can now achieve the simultaneous filtration described here: For additional context, when I add simultaneous filtration, the following ...
user avatar
  • 179
1 vote
Accepted

Remove columns in HTML table where the sum is 0

Aside from the ambiguity in the question, you are definitely taking a circuitous route to identify the columns. CSS's nth-child would vastly simplify querying the ...
user avatar
9 votes

Measure the total runtime of a function in a project

The already accepted answer by Dair is better than what I am about to write here, however I think there is didactic value in trying to improve the code as written, so consider this a supplementary ...
user avatar
  • 356
14 votes
Accepted

Measure the total runtime of a function in a project

Adressing the question: Is there already a solution like this in the standard libs? Unless I am misinterpreting what you want exactly, there are profiling tools available in the standard library. ...
user avatar
  • 5,754
4 votes

Playing a game of ConHex in Python

I also think that overall your code looks pretty good. There's no magic numbers floating around, methods are generally short - although there are exceptions and that ties neatly into the documenting ...
user avatar
  • 653
3 votes
Accepted

Playing a game of ConHex in Python

Overall it's pretty good. Conventionally Conhex_game would be ConhexGame (UpperCamelCase for classes). ...
user avatar
  • 49.5k
1 vote

Ways to optimize my FFT impementation's performance

As it turned out, the issue was the compiler. I was using msvc which does bound check for vector subscript. Since the code has a lot of computation on vector using subscript, the bound check consumes ...
user avatar
2 votes
Accepted

Heston model implementation in C++

You obtain random numbers directly from random_device: std::random_device rd; .... z1 = N1(rd); It is extremely ...
user avatar
  • 53.4k
4 votes
Accepted

Extract the last row in a dataframe with a timestamp before some given time

Don't use np.random; it's deprecated in favour of the new generator methods in the family of default_rng. ...
user avatar
  • 49.5k
2 votes
Accepted

Gathering data from a file and analyzing it

You have a lot of imported symbols that are unused. If you use any self-respecting Python IDE, it will tell you about these and help you delete them. In Employee, ...
user avatar
  • 49.5k
1 vote

Find maximum value of array chunks minima (Javascript)

I find it easy to spot the one problem with the code presented that can't be fixed without outside information: It contains no "testable" specification, gives no clue as to what it is to ...
user avatar
  • 4,548

Top 50 recent answers are included