Hot answers tagged

32 votes
Accepted

Looking for a way to create a particular matrix in python in a less amount of time

The NumPy Reference should be the first place you look when you have a problem like this. The operations you need are nearly always in there somewhere. And functions that you find while browsing the ...
  • 49.3k
25 votes
Accepted

A grid and a menu walked into a program

public class tenxten { Java classes should start with a capital letter, and according to Java conventions should be named with something called "PascalCase". A ...
25 votes

The grid looked at the menu and said "Something with class, please"

private static final int RANDOM_NUMBER = 10; Everyone knows the true random number is 4. Less jokingly, what's the ...
  • 22.2k
23 votes

Python 3 function to rotate an image 90°

You can use NumPy module that's good with arrays and matrices. It has a built-in for exactly that purpose - ...
  • 886
22 votes
Accepted

Python 3 function to rotate an image 90°

I believe the time complexity is \$O(n^2)\$, but I'd like to know for sure There's a general method for figuring out the time complexity for a piece of code, which is to annotate each line with the ...
  • 49.3k
20 votes
Accepted

A library to do maths with matrices written from scratch

Compared to Python's NumPy, my matrix multiplication is quite a bit slower. Can it be made faster without using sophisticated matrix multiplication algorithms like Strassen's? One issue is that you ...
  • 7,673
18 votes
Accepted

Matrix class in C#

I like that your braces and indentation is beautifully consistent. But, this is C#, and your Java is showing ;-) - Kudos for the mostly-consistent PascalCase type ...
16 votes

A grid and a menu walked into a program

Don't store data in static fields The biggest issue in this program is that all the data is stored in static fields. It would make sense to make everything non-...
  • 110k
15 votes
Accepted

Enter my Matrix

I will mostly focus on performance in this review because you have specifically asked for it and the other reviews did not cover it yet. Performance The first thing to do when optimizing for ...
  • 4,240
15 votes
Accepted

AVX SIMD in matrix multiplication

The biggest performance problem (for small matrices), and a common mistake, is doing this: ...
  • 6,759
15 votes
Accepted

Triangle area computation and linear transformations

Here are a few of the high- and low-level ideas: too many comments. Remember, comments tend to age and outdate, they need to be maintained as the code changes. And, if they are over-used, they hurt ...
  • 17.1k
13 votes

Enter the Matrix

This is by no means a full review, but I did notice something. The way you raise errors could use a little work if you're striving for maintainable code. ...
  • 30.7k
13 votes
Accepted

The grid looked at the menu and said "Something with class, please"

Field Visibility Restrict field visibility as much as possible. Use private final wherever you can (the compiler will let you know if you can't). Using ...
13 votes
Accepted

Balanced Random Assignment in Python

Just create a list of 18 ones and 18 twos, shuffle it, then reshape to 6x6: ...
  • 401
12 votes
Accepted

Python algorithm that converts array-like data into MathJax

This looks like a handy tool to have around, maybe we can make it even better. Style As per the official Python Style Guide (often known by his nickname PEP8), keyword-arguments of functions should ...
  • 7,184
11 votes
Accepted

Enter the Matrix

Public Sub Class_Initialize() End Sub Avoid empty members; this initializer serves no purpose, remove it. Although I could infer ...
11 votes

Custom matrix class

The first rule of matrix classes in C++ is that you don't write them by yourself; there are already dozens around, often highly optimized with [smart] expression templates and other cryptic stuff: ...
  • 19.9k
11 votes

2048 merge function

Use automatic tests In your doc-string you state: ...
  • 27.3k
11 votes
Accepted

2048 merge function

A couple of typical patterns to iterate over pairs of items in Python are: for prev, next in zip(nums[:-1], nums[1:]): ... and ...
  • 6,532
11 votes
Accepted

Matrix multiplication in Java

In addition to what Hosch has told you: Make use of try with resources You utilize several classes which implement the Closable interface, but aren't explicitly ...
  • 9,779
11 votes

Matrix class in C#

A bug: Transpose does data[i, j] = data[j, i];, but it returns a new matrix, it's not supposed to change the matrix in-place, ...
  • 6,759
10 votes

Capture the notion of invertible functions

Naming: There are a lot of names I'd change. Some of them matter more than others (local names in a let are not that big a deal even if they're terrible, but a name for a widely used function should ...
10 votes

SIMD matrix multiplication

Restructure into a function to make it DRYer Right now the structure of your code is pretty unpleasant, not very DRY, etc. The first thing I'd recommend is restructuring this to work in a function. ...
  • 7,673
10 votes

JavaScript solution for diagonal difference

You don't need the inner for loops because you know exactly which array element you want for each sum: ...
  • 1,885
10 votes

A library to do maths with matrices written from scratch

for loops in C++ In C++ you can declare the loop variable in the statement instead, like this: ...
  • 110k
10 votes

Enter my Matrix

boolean validIndices looks suspicious: it never returns false, so why bother to return anything at all? Notice that it also ...
  • 54.1k
10 votes
Accepted

4×4 matrix multiplication

Avoid using namespace std This can cause name collisions because it adds every name in the std namespace to the global ...
  • 1,423
10 votes
Accepted

Rotate an N × N matrix 90 degrees clockwise

Are you up to date? There is really not much to the problem and there are a variety of solutions. It is also hard to know what the interviews are looking for. For some it is knowledge of latest ...
  • 20.6k

Only top scored, non community-wiki answers of a minimum length are eligible