18

Search Space Your search space is too large. Since 0 < a < b < c and a + b + c = 1000, you can put hard limits on both a and c. The maximum a can be is 332, since 333 + 334 + 335 > 1000. Similarly, the minimum value c can be is 335, since 332 + 333 + 334 < 1000. As well, the maximum value c can have is 997 (1 + 2 + 997 = 1000). Based on ...


13

This is a similar idea to the other answers here, but the implementation is a bit different. First of all, we can assume that the array's length is at least 3, since it needs to have at least two of the same values and one different value. Let's start by handling the case where the stray value is not in the first element. We could simply write: a.find(v =&...


11

Note that your code actually gives the wrong answer, it returns a > b. Since a and b are the two smaller variables, you should actually start looping over them in order, so it looks more like: for a in range(1, 1000): # Don't know why you assumed a > 1 here for b in range(a + 1, 1000): c = 1000 - a - b Also, it doesn't matter for this ...


10

early pruning This is very nice: // delete any that are over our budget Doing it before sorting can slightly speed the sorting operation. I say slightly because "items over budget" is determined by the input, and it will be some fraction f of an input item category, so the savings is O(f * n * log n). early discard This is a bigger deal. ...


8

The good thing first: You divide and conquer the problem by creating some reasonable (and well named) methods. You could have gone all in by making methods for combining and final selection as well: ... var combinedTotals = Combine(affordableKeyboards, affordableDrives); return SelectMaxBuy(combinedTotals, budget); } But as shown below, dividing the ...


7

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....


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 nonnegative. Optimization You could optimize s before walking the array. Since array indices are nonnegative integers, you ...


7

Bugs You return 'No Police!' instead of returning 0, but you don't check the return value. You might consider printing the string and returning 0 instead. Likewise for the no-thieves case. Style You are nominally compliant with pep-8, but your style is very much a beginner style. Why do you have variables named t_list and p_list? Do you really think that ...


7

The natural way to tackle this problem (as in many similar games) is starting from the final position. Let B[k] be the maximum score that Player 1 can get if the game starts with only the last k stones left. B[1], B[2], B[3] are initial values that can be computed directly (just take all remaining stones), and then you can fill in B[4], B[5], B[6], ... in ...


7

A note on efficiency. Currently the code exhibit an exponential time complexity. It can be reduced significantly. Notice that the same position is inspected more than once. For example, the opening sequences 3, 1, 2, 2 and 1, 3 all lead to the same position. Further down the game the situation aggravates. Keep track of the positions already inspected, and ...


7

O(n) You can transform the (row,column) coordinate of a bishop to a (row+column, row-column) coordinate. The row+column coordinate tells you which upper-left to lower right diagonal the bishop is on. The row-column coordinate tells you which upper-right to lower-left diagonal the bishop is on. row + col row - col 7 8 9 10 11 ...


7

General Guidelines You have coded everything in a single class Program. Take advantage of the fact C# is an object oriented language. Create at least one custom class that defines this problem. Your current implementation is very strict and specific to 2 types of items. What if Monica needs to buy from n item types? It is up to you to decide the scope of ...


7

3 good answers already, but there's more! I don't like that you modify the array you are given. This sort of thing would need to be documented, and generally creates confusion for all. You don't need arrays as inputs, so you could take IEnumerables instead without any added cost, which makes the code easier to reuse and communicates to the consumer that you ...


7

Instead of returning a custom string, just return the values and leave the printing to the caller. This way it is at least feasible for this function to be used elsewhere. def get_triplet(): ... return a, b, c if __name__ == '__main__': a, b, c = get_triplet() print(f"a = {a}, b = {b}, c = {c}, product = {a*b*c}") Here I also used an f-...


7

If you want a solution that doesn't involve any coding, you can use the fact that Pythagorean triples \$a < b < c\$ are of the form \$a = 2pqr\$ \$b = p(q^2 - r^2)\$ \$c = p(q^2 + r^2)\$ or the same equations with \$a\$ and \$b\$ switched. Here \$p, q, r\$ are positive integers, which are uniquely determined by the condition \$p = \gcd(a, b, c)\$....


6

You are representing each state of the universe as a list of bits. However, CPUs are experts at handling sequences of bits — that's what an integer is! Given two bits, the way to find out whether exactly one of them is 1 is to use the ^ operator (XOR). You should be able to solve this challenge using bit-shifting (<< and >> operators) and ^.


6

As dfhwze and Roland already pointed out, a hash alone is not sufficient to determine whether two things are equal, so you still need to do a string comparison afterwards if the hashes match. Otherwise you will get wrong results from time to time. Not to mention the effect of hash randomization between different application runs... The idea behind Rabin-...


6

What's going on with the indentation? long long No. We're not in the 1970s. Use <cstdint>. Here you probably want std::uint_fast64_t. IMO n should be const and the copy (x) should be the variable which is modified in the loop. for(long long i=2;i*i<=n;i++) The first step to optimising prime divisor searches is to use a wheel. If you ...


6

I know this isn't really what you're asking for, but I have a couple suggestions about how the code is setup and written. Coming from languages that use long as a type, a line like: self.storedURLS[tiny] = long Is a little jarring initially. No, long is not a type or builtin in Python, but I think it would be neater if a more descriptive name was used, ...


6

In an interview setting it is quite hard to come with an efficient solution (unless you happen to be very good in mental multiplication; however a \$99 * 91\$ example is a strong hint). The key to an efficient solution is an observation that \$999999 * 999001 = 999000000999\$ is a quite large palindromic product. It means that you don't have to test the ...


6

Integer Division In Python, 10 / 2 is equal to 5.0, not 5. Python has the integer division operator (//) which produces an integral value after division, instead of a floating point value. To prevent storing both int and float keys in the count dictionary, you should use: count[n] = collatz_count(n // 2) + 1 Cache Your count cache works nicely. ...


5

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)...


5

If I were you, I would stop having the tests mixed up with the implementation. It is somehow "smart", and for small projects like this, you can say it doesn't matter. But IMO it does matter because it prevents you from thinking properly about the design of you code. Your implementation is a nearly one-to-one implementation of the C++ from Geeks and it ...


5

While I agree with the other answer that you should not return a special string denoting failure, in Python you should refrain from returning any special return value (though if you do, None is an OK choice). Instead you usually want to raise an informative exception, which can then be handled by the caller. Here the code already raises a KeyError, which ...


5

If you really want to cut down on the number of characters that you need to guarantee a certain amount of robustness, it would be wise to pick a greater pool of characters to choose from. At the moment you are basically left with 16 valid values (0, 1, ..., 9, A, ..., F). Case does not count here since 0xA is equal to 0xa. For 6 characters that leaves you ...


5

First impressions: nicely presented code; good use of the appropriate standard library functions and classes. A minor suggestion would be to change the name, given that reduce is a well-known concept in functional programming (and is a function in <numeric>). Perhaps call it compress? I'd suggest extracting the constant 3 to give it a meaningful ...


5

I'll try to offer an alternative solution to your problem. The linked kata contains the following piece of information You are given an odd-length array of integers, in which all of them are the same, except for one single number This means, that the array is guaranteed to contain even number of same elements. This fact could be leveraged to achieve a ...


5

simplify a and fix search space Given the nested for loop, your variable a can be decremented simpler: Moreover, your search space is far too large. Other authors have addressed his in more detail, but if you want to check every possible triplet from (1-1000, then you need to change your second for loop to: def get_triplet(): for c in range(2, 1000): ...


5

Solution 1 You have two equations and three unknowns. Since three minus two is one, you should only require one search loop. a + b + c = 1000 => c = 1000 - a - b a2 + b2 = c2 => a2 + b2 = (1000 - a - b)2 => a2 + b2 = 10002 - 2000a - 2000b + 2ab + a2 + b2 => 0 = 1000000 - 2000a - 2000b + 2ab => 0 = 500000 - 1000a - 1000b + ab => 1000b - ab = 500000 - ...


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 ...


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