Hot answers tagged

17

First of all, there is a major problem with your class: it does not respect the rule of three/five/zero: it has a non trivial destructor that destroys memory allocated in constructor it neither defines nor deletes the copy constructor and assignment operator Let us look at the following code: DynamicArray dynarr[16]; // initialize values in dynarr if ...


6

Don't talk like a pirate I would prefer the name array (another reason not to using namespace std;) to arr. And I'd prefer a more descriptive name like data or numbers to either. Don't waste work int push(int value) { length++; if (length == (size + 1)) grow(); data[length - 1] = value; return length; } ...


6

Consider using size_t instead of int. The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to problems, particularly as 64-bit architectures become more prevalent. It is generally bad practice to repeat code that does the same thing. Consider changing copyToNewSize to something ...


5

Missing #include <memory> Since you use std::allocator, you have to #include <memory>, otherwise your code might fail to compile. Avoid unnecessary use of this-> Your code is full of this->, but in C++ you almost never have to use that, unless you have local variables in a member function that shadow member variables. Add empty lines to ...


3

Your date format is Y-m-d which means that you can safely sort these values as simple strings. rsort() is all that you require. A native function will always be more concise and efficient than a custom (non-native) function. If your date string were not zero-padded or the date units were not arranged with descending unit values, then additional work would ...


3

Advice 1 In Java Collection Framework (the data structure framework residing in java.util.*), the item type is denoted by E, and not T. I suggest you do this: public class FloatingQueue<E> { ... E here and there. } Advice 2 FloatingQueue() { .... } You are clearly missing the public keyword here; without it, only the code in the same package ...


3

That takes quadratic time. I'd say the intended/standard way is to compute prefix products and suffix products and for any number, multiply its prefix product with its suffix product. That's linear time. And unlike the divide-the-whole-product-by-the-number approach it doesn't have an issue with zeros or with the whole-array product being too large.


3

Don't raise BaseException Typo: posible -> possible Don't type-hint a list; instead use List[str] in the case of your posible_config_files Consider making a reusable solution that separates "all existing config files" from "the first existing config file" Suggested from pathlib import Path from typing import Iterable def config_files(...


2

Broad review It seems this code is part of a class method, yet it is difficult to know anything about the method or class other than the fact that it has a data member. Consider following PSR-1 and PSR-12. Indentation is not always consistent - e.g. some lines are indented with two spaces and others appear to have four spaces yet at the same indentation ...


2

Don't use three separate named variables for the three stack tops. You should be able to have a single indexable piece of code when doing the pushing/popping rather than a switch statement. I think the point of having three stacks in one array is to allow the sum of all three to use the full capacity, in any permutation. The way you did it is identical to ...


2

The properly working code had a for loop. To minimize operations visually, we could use Array.from() method (see Array.from() documentation for more). As the question hint states, it is possible to achieve the same result by using Array.from(). The missing part you did was that the first element of the output array should be 0. The mistake was to add 3 or ...


2

Length is a property of the array static int KthElementInTwoArrays(int[] arr1,int arr1_length, int[] arr2, int arr2_length,int k){ It is unnecessary to pass the array lengths separately. In Java, the length is a property of the array. So arr1.length is the length of that array. You would only need a separate variable if you wanted to work with only ...


2

Here are a couple details from a quick glance at the code: If you are implemented a queue, consider actually implementing the Java Queue interface: FloatingQueue implements Queue Check you usage of booleans: if (isEmpty() == false) { ... } Is written in a more readable way as: if (!isEmpty()) { ... } Also, your isEmpty method is simply returning ...


1

Did you consider what would happen if there are more than one element in the array with the same maximum value? max returns one of them, but your find will return the index to all of them. One solution is to use the new syntax for max: [M,I] = max(A,[],___,'linear') (I don’t know when it was introduced, but I just learned about it now). This syntax is equal ...


1

This can be simplified by using destructuring assignment into an array, followed by simple assignment into the object obj. While it may not be much more efficient it can eliminate the need to use the forEach method. const val = "someValue,display"; const [columnVal, displayValue] = val.split(","); const obj = {columnVal, display: displayValue === "...


1

When having no clue on how to resolve such a problem it often helps to go through a couple of examples or use cases. Re-using those later as test cases helps to ensure everything works as intended. Try to keep the cases as small as possible but just big enough. When choosing them consider the corner cases of how things operate. E.g. let's take a capacity of ...


1

The solution you proposed consisted of two nested loops: First loop iterates n time. Inside that, you have a filter and a second loop for all the elements except the currentNum, which is n - 1 time. Therefore time complexity is \$\mathcal{O}(n ( (n - 1)+n )) = \mathcal{O}(n^2)\$ If you want to iterate over all the elements in an array except for one, a ...


1

Thanks Eliaz for your question, I'll give you a few pointers to help you. These are pretty standard boiler-plate code patterns in any Python project. For example: For configuration files, you should utilise ConfigParser. Using argparse for user input or default choices like with unattended script running Using logging to help figure out what happens during ...


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