7

Review Your solution naively walks the array of ascending integers from starting position s = 0. In some situations, this means you are walking tons of negative numbers, knowing they can never match an array index, which is always positive. Optimization You could optimize s before walking the array. Since array indices are positive integers, you should ...


6

I took your previous, invalid solution, and amended it so it does work correctly. So in a way, I am still reviewing your code. function fixedPoint(data) { const lastIndex = data.length - 1; if (data[0] > 0 || data[lastIndex] < 0) return -1; var left = 0; var right = lastIndex - 1; while(left <= right) { let middle = Math....


4

The use of static variables to read the input file is kind of ugly and makes it harder to reason about what the code is doing. I'd ditch the archives, rdFont and fileLine variables, moving them into the main method. The openFont and readFileLine methods don't feel necessary either - openFile could just return the File that was picked and let the caller do ...


3

Here is a simplified version of KIKO Software code, which solves the problem in O(lg(n)) operations. function fixedPoint(data) { const lastIndex = data.length - 1; if (lastIndex < 0 || data[0] > 0 || data[lastIndex] < lastIndex) return -1; var left = 0; var right = lastIndex; while(left + 1 < right)...


3

Combining lookup and insertion functionality in one function looks like an unnecessary violation of SRP. I strongly recommend to separate them. Keeping the error inside the structure is indeed questionable. Notice that once you split lookup and insert, return errcode; becomes natural: every function besides insert would just return an error code, and insert ...


3

General It's a good idea to give it a day or two before you accept an answer. Accepting too early discourages other people from answering. Use @Override when overriding methods in a parent type. This makes the code easier to read and helps the compiler to warn you when something is amiss. Your variable names are mostly meaningless, which makes your code ...


3

I'm just going to do a detailed review of the more general class. With respect to Connect Four, have you read the paper by the person who solved it? They took a strategy-based approach, but there may be some tips for the evaluation function. /** * Maps each visited state to its parent state. */ private final Map<S, S> parents = new ...


3

You're coding javascript the "old" way. With es6 you have a lot of new features available. Take a look at them. Don't use self invoking anonymous functions /// bad (function(){ // code here })(); Use block-scope functions /// good { // code here } Give meaningful names to your variables and methods. Functions should not have more than 2 parameters. ...


2

I have some nitpicks to talk about before getting in a bigger thing : distance should be named distanceTo, equal should be areEqual. OOP wise, the function pointsFormsQuadrangle shouldn't be in the class Point, because it breaks single responsibility principle, you could extract it to someplace else. Nitpicks are over. In pointsFormsSquare, you call ...


2

The way to use a trie here is to sort the letters of each dictionary word: [ AD, ADD, DAD ] becomes A > D [ ad ] > D [ add, dad ] Then walk the trie and stop descending when isWordInTray returns false. Worst-case complexity is unchanged (arguably worse, since sorting time is not linear); actual runtime is greatly improved.


2

x and y are terrible variable names. length1 and length2 would be better. matrix = [[-1]*(x) for val in range (0,y)] You are not using val in this list comprehension. Convention is to use _ for throw-away, variables needed for syntax reasons, but not actually used. 0 is the default minimum in range() statements. If you loop from 0 to some limit, you ...


1

changing mutable object Since you are changing the original array, this code run twice can provide strange results. In general I try to avoid changing the arguments passed into a function I write, unless it's explicitly stated, or expected (like list.sort) accumulate What you are looking for is the largest difference between tha cumulative sum where the ...


1

I would merge the 2 loops (there is one in the max part), since not all parts are relevant for the maximum: def best_subsum(array): running_sum = 0 for i in range(1,len(array)): if array[i-1] > 0: array[i] = array[i] + array[i-1] if array[i] > running_sum: running_sum = array[i] return running_sum array = [...


1

I have previously looked at this problem - many years ago. The first step is to create an indexed list of all the words in the dictionary, with the characters sorted in alphabetical order. This is a one-off cost O(n) [Where n in this case is the size of the dictionary, not the number of letters in the Scrabble tray]. Now, for each search of n letters, you ...


1

Your second solution is good and fast, but you asked about more elegant solution, so I am suggesting mine. Firstly, I solved this task by Python, then convert all logic into Javascript. It is a little slower, than your (20 ms) and use more memory, but I think it more straightforward and understandable. The code only answers do not liked on this site, so I ...


1

Your tests look ok. I have three concerns: If I read correctly, your "single digit modifications" test cycle is going to have over 1000000000000000000000 cycles. That's... not practical. pick a compromise. The positive tests are checking calculate and validate. I see no reason not to check both in your negative tests too. You're only checking ...


1

I don't really see anything that jumps out as an obvious simplification. I looked online for other code for this task and only found one in python which uses the same basic algorithm. The only suggestion I have is for readability, the name match in the functional solution might be slightly confusing for anyone reading the code. Perhaps a more appropriate ...


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