New answers tagged

-1

The below solution is simpler and it should pass all the test cases. You don't need the sorting techniques to solve this. Your code is quite complicated as you did a sort in each loop which is not needed at all. Here what we can do instead: 1- Use Set to remove duplicate scores 2- Use LinkedHashSet to keep the set in order state 3- Save the Set again to a ...


0

def cleanWord(word): # To remove punctuations The comment is a better name without_punctuation. Also you should be following snake case as per PEP-8. Much of your logic in the longest word function can be handled by max with a key function. The overall solution can be written like so. def without_punctuation(text): ...


5

Naming things Avoid single-character variable names, unless it's something that is very commonly used, like i for a loop index. Consider r and c, even if you guess it has something to do with coordinates, are those positions or sizes? I would personally use width and height here as unambiguous names for the size of the grid. Note that in the constructor's ...


1

It's good to see code that comes with tests - well done! I would add some more tests in places. Specifically, we ought to test is_palindrome() with a single-letter input, as that's something that could easily get broken, and find_palindrome() needs testing with some strings where the longest palindrome is at beginning, middle or end. I'm concerned at ...


0

Name of Algorithm What you had implemented is not a correct sort algorithm (as it cannot handle duplicate items). So there won't be any sort algorithm work like this. The most similar one I can see would be Counting sort. Counting sort also track how many occurrence of each integer in the array so it would work with duplicate values. Time complexity Time ...


2

Review Style Use const for constant variables. arr, parts, minMax all should be constants. Spaces between operators eg value+1 is value + 1, and after commas eg [71,825,522,901 should be [71, 825, 522, 901 JavaScript uses camelCase, avoid using snake_case. to_sort should be toSort Naming is inconsistent. You call the array, toSort, and arr. Try and use ...


1

Your question made me want to give it a shot, too. The solution ended up pretty much like the pseudo-code suggested by @Marc , and Python is of course pretty close in readability anyway. The below code passes on the site and runs (there is some deviation between runs) faster than c. 95% and at with less memory usage than c. 75% of solutions. The code ...


0

Actual time complexity As you correctly state, the expected time complexity of Dijkstra's on a square matrix is O(n^2 log n). However, your algorithm has a complexity of at least O(n^4) due to the way you locate the minimum vertex. for _ in range(n): for currj in range(n): # <--- executed n^2 times here mini,minj = self.minimumVertex(...


1

My suggestion about your current solution: def max_area(height) -> int: areas = [] coords = {x: (x, y) for x, y in enumerate(height)} for x in coords: higher = [k for k in coords if coords[k][1] >= coords[x][1]] area = max(abs(coords[j][0] - coords[x][0]) for j in higher) * coords[x][1] areas.append(area) return ...


1

pswap() and reverse() aren't part of the public interface, so ought to be declared with static linkage. If we passed pairs of pointers, rather than indices, we wouldn't need to also pass the start of array. Even with indices, we don't need to pass l - we can supply the subarray beginning at l. The C++ function std::next_permutation() has a more useful ...


2

size and i in reverse look superficial. Consider void reverse(int A[], int l, int r) { while (l < r) { pswap(&A[l++], &A[r--]); } } The outer loop looks very strange. Its body is executed exactly once, when the longest decreasing interval is detected. Consider lifting the body out of the loop. As a side point, I don't see the ...


1

Use const instead of let to prevent accidental overwrite of variables. Use consistent variable names. Seems weird to combine snake_case and camelCase. For empty input, min and max is undefined. You can check this case and return empty array directly. The getMinMax function is hidden inside the sort function and only called once, you can unwrap it and just ...


1

Naming The question starts off talking about N and K. Your code begins with reading in z and d. The question text is ready-made documentation for your code; use the same names for the variables! n, k = map(int, input().split()) Guilty, I said N and K but used n and k. PEP 8, the Style Guide for Python Code recommends using snake_case for variables. So we ...


3

Formatting First off, the formatting including indentation is really terrible. For other people to be able to read and understand your code you need to abide to commonly accepted code conventions. For example: if(n==1) return q; int result =0; On a casual read through it is impossible to tell to which line(s) the if applies to. At the very least you need ...


3

One way to simplify your code a lot is to generate a new board in your mergeLeft() function, instead of trying to modify it in place. It opens up room to use other techniques that would greatly simplify the logic in your function, like, popping results from the source array as you work with them, or filtering out zeros. An example of how you might ...


3

Review Some review points on your code Always delimit code blocks with {}. Eg if (target !== 0) startIndex++; is safer as if (target !== 0) { startIndex++; } Avoid using continue (Its a goto in disguise). Rather the line if (array[endIndex] === 0) continue; is better as if (array[endIndex]) { /*... loop code ...*/ }. See rewrite. Try to reduce ...


3

While aepot answer is a good answer and got my upvote it does only work for type int. Which I know the question for an example uses int in their code but if wanting it to work for all data types will need to change the code a bit. public static class EnumerableExtensions { public static IEnumerable<TSource> FilterRepeatingElements<TSource>(...


2

Use the Doxygen language to document your functions It looks like you use some ad-hoc way to document your functions. It's very good to add that documentation, but there are tools out there that make it even more useful, like Doxygen. Once you reformat your comments to follow the Doxygen syntax, you can then use the Doxygen tools to generate documentation in ...


2

A short review; You want to put this in a function That function should either create the numeronyms or do the outputting, not both You are using && as an if statement, great for code golfing, not so great for code review Using .map() doesnt really make sense if you are not actually mapping, .forEach() makes more sense If you insist on split and map ...


2

Know the language Part of being a programmer is being familiar with the language/s you are using. These days languages are changing yearly thus keeping up to date is very important. MDN JavaScript provides a good JavaScript reference. Every now and then its good to peruse the site to keep your knowledge up to date. Strings String.slice will slice to the end ...


3

Overview: You need to concentrate on writing self-documenting code. This basically means using self-documenting variable names and writing functions that do specific obvious self-documented tasks. int main(int argc, char* argv[]) { // Handle command line arguments. // Set appropriate variables. playGuessNumberGame(); // Pass parameters you ...


4

Before we talk about improving the efficiency of your function, lets talk about safety: You never check the validity of your function parameters. You have no way of knowing how big buff is. For the first issue, if str or delim is null, passing them directly in to strstr will cause a segfault and crash your program. The second issue is worse: if buff is ...


3

int splitv(char buff[], char str[], char delim[], int index) { // returns 0 if an element is written. Else non 0. A few points about this interface: Shouldn't delim be a pointer to const char? I think the function ought not to be modifying it. Similarly, if we can avoid modifying the input string (which is one major advantage to not using strtok()), ...


Top 50 recent answers are included