Björn Lindqvist
  • Member for 9 years, 2 months
  • Last seen more than a month ago
Generic dynamic array implementation in C
Accepted answer
10 votes

See this Stackoverflow answer for a discussion on the optimal growth factor for dynamic arrays. The gist of it is that it depends, but growth factors around the golden ratio are easier for the memory ...

View answer
Multi Threaded High Performance txt file parsing
Accepted answer
7 votes

I hope you don't mind a pure C solution. For me it is easier to optimize code without C++ abstractions. But it should be straightforward to convert it to idiomatic C++ code. #include <assert.h> ...

View answer
Merge Sort in Java with Test cases
Accepted answer
4 votes

Naming Prefer to use conventional names. That is, the same names used by the Arrays class in the Java standard library: data => a comparator => c start => fromIndex stop => toIndex I've ...

View answer
Print a word with numbers substituted for characters
4 votes

Think about the problem as a repeated Cartesian product. It allows us to use the very useful product function from the itertools module: >>> list(product('ab', 'AB', 'xy')) [('a', 'A', 'x'), ...

View answer
Getting a path from list of arcs in C++
3 votes

The key is to create a mapping for the edges: vector<int> get_pretty_path(const vector<pair<int,int>>& edges, int s, int t) { map<int,int> edgeMap = {begin(edges), end(...

View answer
Java file analyzer byte-by-byte
Accepted answer
3 votes

Replace try (FileInputStream fis = new FileInputStream(file)) { with try (InputStream fis = new BufferedInputStream(new FileInputStream(file))) { See also Why is using BufferedInputStream to read ...

View answer
Minesweeper - Python 3 (beginner)
Accepted answer
3 votes

I think implementing a game is a good way to learn a new programming language. If you are up for it, you could try coding a graphical version of Minesweeper using a game library such as Pygame or ...

View answer
Matrix class with overloaded `operator[]`
3 votes

Pragma once Instead of #ifndef DP2_MATRIX_H #define DP2_MATRIX_H consider using #pragma once Once is non-standard, but supported by all compilers supporting C++14 and reduces compilation times. ...

View answer
Dynamic Array Implementation in C++
3 votes

The code is pretty good as it is. Here is how I would improve it: Constant handling In C++, you should prefer const over #define. So #define INITIAL_CAPACITY 5 becomes const int INITIAL_CAPACITY =...

View answer
OpenGL Hello Triangles
2 votes

If you don't want obsolete and deprecated stuff, don't use GLUT, see Is GLUT obsolete?. Use GLFW instead. I've personally used it and it is in my opinion easy to learn and much better than GLUT. ...

View answer
Convert rows with multiple values to row with single value
2 votes

It is probably easier to work with the underlying Numpy array directly than through Pandas. Ensure that all factor columns comes before all data columns, then this code will work: import pandas as pd ...

View answer
Gradient Descent Algorithm using Pandas + GIF Visualization
Accepted answer
2 votes

Nothing happens! When I ran the script my first thought was that it had hung because nothing happened. Apparently the calculations are just slow on my computer. To reassure the user that the program ...

View answer
Pong-Like Game. First game I ever made. advice needed
2 votes

A trick you can use which is very common in graphics programming is "clamping." Instead of if(x < 0) x = 0; if(x > PaintSurface.getPaintSurfaceDimension().width - width) x = ...

View answer
Safer & simpler allocation functions and macros
2 votes

If you want people to use your library, then you want to think about how to make it easy for others to adopt it. I would consider revamping the implementation into a Single-file library. Simply put, ...

View answer
Java RSA / AES-GCM encryption utility
Accepted answer
2 votes

Pokémon error handling "Catch them all" Never (ever!) write code like this: try { ... } catch (Exception e) { ... } It allows the program to continue executing even if fatal errors are ...

View answer
C++ cleaner optimised code, Calculator
2 votes

1) Be consistent when placing braces. The following is not consisent: if(a == 0 or b==0){ printf("Mnozite s nulom, a kad se mnozi s nulom rezultat je uvijek 0: \n"); umnozak = 0; } else { ...

View answer
Simple Graph in Python
2 votes

You should embrace the builtin types Python provides for you. I will show you how you can replace every method in your class with functions based on those. First __init__ becomes: def create_graph(...

View answer
Keras stacked LSTM model for multiclass classification
1 votes

If I understand your problem, your samples are 150-dimensional vectors which is to be classified into one of four categories. If so, an LSTM architecture is a very poor fit because there are no ...

View answer
First program in Python for data analysis
Accepted answer
1 votes

Here are some of the issues I've found with your code. But there are many more left and it is very hard to review your code without seeing some sample data files. Consider rewriting your code, taking ...

View answer
A simple hangman game
Accepted answer
1 votes

Avoid mixing import styles from string import ascii_lowercase import random Either way of importing is fine. But try and stick with one style rather than mixing different ones. So: from string ...

View answer
improving speed of this numpy-based diffraction calculator
Accepted answer
1 votes

I managed to make your script a bit faster. See the comments in the source code: import numpy as np import matplotlib.pyplot as plt import time # Makes it so numpy always outputs the same random ...

View answer
Optimize option pricing code
Accepted answer
1 votes

I changed the main part of the script to: from time import time start = time() val = LSMPut(1, 0.06, 0.15, 100, 90, 20, 40_000, 5) print(f'Done in {time() - start:.3} seconds') print(val) The point ...

View answer
Python Fenwick Tree
1 votes

Segment trees and Fenwick trees are usually implemented as implicit data structures. That is, as an array with the tree structure implicitly given by the array indices. Your code instead stores the ...

View answer
Graph degree as solution for undirected graph paint
Accepted answer
1 votes

It is not. Consider a triangle graph which has tree nodes and three edges. All vertices has degree two but three colors are required to color it. Your function would fail for that input. In fact, ...

View answer
N-Body Gravitational Simulation of Point-Masses in Python
Accepted answer
1 votes

Below is a list of my suggestions. You can find my changes to your code here: https://gist.github.com/bjourne/6270cff58c0238bb863e189ea63d6254 Kill your darlings There's a saying in movie production ...

View answer
Binary Search Tree insert while keeping track of parent for node to be added
1 votes

Problems with your code: struct node * bstInsert(struct node *n, int x) { if (n != NULL) { what if n is null, e.g an empty tree? How does your tree get started if you can't insert anything in it ...

View answer
C code structure
0 votes

The line is equivalent to the following: int left; if (i - 1 == -1) { left = NO_OF_PHILOSOPHERS - 1; } else { left = i - 1; } Which simplifies to: int left = i - 1; if (left == -1) { left ...

View answer