3

Some suggestions: Use variable names that tells the reader what they are. Dont do using namespace std; in the global namespace. Always check that <stream> >> variable actually worked or else your program will run with uninitialized variables and cause undefined behaviour if they are read. Use an unsigned type when dealing with subscripting. If ...


3

Couple of small things: Use emplace_back rather than push_back when you just have the parameters for the constructors: longList.push_back(Task{ taskInput }); // This is better written as: longList.emplace_back(taskInput); The difference between the two: push_back(Task{ taskInput });. This creates a "Task" object as an input parameter. It then calls ...


3

Overall You don't use encapsulation. Which makes your list vulnerable to incorrect initialization and accidental incorrect modification from outside the list. You use several C based style choice rather than C++ style which make your codde harder to read. Code Review Only a list of int? class Node { public: int value; // int only ...


3

You have the possibility of an infinite loop. Consider: logBase10Estimate(2147483647). Should be about 9. We need a value of \$10^i\$ which is greater than 1000000000. So, what values does x take on? 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 1410065408 1215752192 -727379968 1316134912 ...


2

The printing should not be there :) You don't need another variable x. You can modify the local copy n, because you won't need its original value for the entire algorithm and because it is passed by value, you won't change the value for the caller. Also check for valid input. static int logBase10Estimate(int n) { if (n <= 0) { throw new ...


2

Just like you I am also learning algorithms mostly in python, that I am also using to learn haskell. I am fighting myself not to change variable names to long but readable ones! Here it use not using any folding or anything, just recursion. bubbleSort :: (Ord a) => [a] -> [a] bubbleSort [x] = [x] bubbleSort (x:y:xs) | x < y = x : bubbleSort (y:...


1

Since the code is not compiling, it will be hard to do a proper code review. In my opinion, you should rename the variable cmd in both of the methods, since it can be confusing. BinaryTree#put method In my opinion, it's a bad choice to use the range operators (<, >, <=, >=) with the compareTo method; since you always get one of those values (-1, 0 &...


1

Declare for-loop-variables inside for instead of reusing them. So instead of: int v[501][501], i, j, m, n, o, p; cin >> m >> n >> o >> p; for (i = o; i >= 1; i--) v[i][p] = o - i; for(i = o;i <= m; i++) v[i][p] = i - o; for(i = 1; i <= m; i++) for(j = 1; j <= n; j++){ use (except you should also change ...


1

BUG: Your main loop for l in substrings(a): ... will return the first palindrome it finds inside a (stepping the indices back from the end), not just the longest one. If there are multiple longest palindromes it won't return them all, only the first it finds. Your code is overly tailored to this one example. Use other examples to tickle it, e.g. "zaba ...


1

1. You never check if head was assigned a valid pointer before dereferencing it here: int find(struct Node *head, int n) { int count = 1; //if count equal too n return node->data if(count == n) return head->value; // <--- here //recursively decrease n and increase // head to next pointer return find(head->...


1

It's pretty readable, and all comments are pretty minor ones. You can probably squeeze out a little performance increase, but not much. One thing you could do is to skip setting variables like n = len(prefix) (you also don't use n in the following line). You could also consider collections.deque instead of list where relevant, which can sometimes give you ...


1

I start from mathematical consideration using one of the examples you provided: Input: 10 output: 3 2,3,4,5,6,8,9,10 7 11 All the elements multiple of 2 are in the set containing 2, the other sets will always contain just one prime number like {7} and {11} : if it were not so the number would be not prime and would be contained in another previous set. ...


1

Algorithm in O(N^2) The current solution will be slow for large N, because it has a complexity of O(N^2) (It checks every element in the array for every possible position of the k adjusted elements => O(N * (N-K)) => O(N^2)). There is an O(N) solution. Consider that as the K-element segment "slides" along the array, there are only two elements whose value ...


1

You can speed some things up by using more list and dict comprehensions, and by reducing your function calls. Some examples follow. d = dict() vs d = {}: 131 ns vs 30 ns. len_of_half_a = int(len(a)/2) vs len_of_half_a = len(a)//2: 201 ns vs 99 ns. I used Python 3.8.1 for both tests. Granted that this isn't much, but several of these tiny improvements ...


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