# Tag Info

8

Your code performs one recursion per character and recursion depth is limited. For long inputs, your code will raise RecursionError: maximum recursion depth exceeded. Replacing recursion with an explicit loop solves the issue. Also, if you start the calculation at k = 1 with memo[-1] and memo[0] properly initialized and then work upwards to k = len(s), the ...

7

Before speaking about the actual algorithm, let me hint you at the official Style Guide for Python Code (often just called PEP 8), a set of guidelines to write idiomatic-looking Python code. A core takeaway of the read should be, that in Python lower_case_with_underscores is the preferred way to name variables and functions. Fortunately you don't have to ...

6

If the grader is complaining about "time limit exceeded," it must be because some loop is executing too many times. All of your functions are clearly O(1) — they have no loops. So which loop is executing too many times? It must be the only loop in the entire program: while(!isFull(n)){ cin>>a; push(a); } Is it possible that ...

5

WARNINGS First of all, let's listen to the warning we get when we run this and get rid off it: UserWarning: Using slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning warnings.warn('Using slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning') You can fix that by installing the ...

4

I hope you don't mind a pure C solution. For me it is easier to optimize code without C++ abstractions. But it should be straightforward to convert it to idiomatic C++ code. #include <assert.h> #include <math.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #...

4

Ultimate optimization avoid calling pd.DataFrame.append function within a loop as it'll create a copy of accumulated dataframe on each loop iteration. Apply pandas.concat to concatenate pandas objects at once. no need to gzip.open as pandas.read_csv already allows on-the-fly decompression of on-disk data. compression : {‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’...

4

You could make your life way easier if you used the deque data structure. from collections import deque d = deque(maxlen=2) d.append(1) d.append(2) d.append(3) sum(d) # gives 5, which is the sum of the last two inserted elements. With this, you call the sum function only when you really need to, which will improve your performance if you don't need the ...

4

How can I decrease code length? You could split your big for loop into 2 smaller ones. The first one would print all diagonals up to the main one, and the other one the remaining ones. For instance, let's say there's a matrix like: 1 2 3 4 5 6 7 8 9 Then, your first loop would print 1 4 2 7 5 3 and the second one 8 6 9. The code would look conciser and ...

3

I think there's three big things you might be able to do to improve the performance of this. Write your query such that it doesn't need to execute the subquery for each line and/or have a HAVING clause by using an inner join which will automatically exclude those with no match. Use ST_Distance_Sphere(POINT(:longitude, :latitude), POINT(cities.longitude, ...

3

In Go, measure performance. Run benchmarks using the Go testing package. For example, $go test transpose_test.go -bench=. -benchmem BenchmarkTranspose-4 2471407 473 ns/op 320 B/op 13 allocs/op BenchmarkTransposeOpt-4 9023720 136 ns/op 224 B/op 2 allocs/op$ As you can see, minimizing allocations is important. Efficient memory ...

2

C++ port of Björn's code using a single large array instead of one hashmap per thread. Björn's C code is very nice, but this question was tagged C++ so i thought it was worth showing that too. The changes from my code above are very small: Using std::thread instead of std::async, because we don't need to return anything Use a std::unique_ptr of std::atomic&...

1

Readability Your code is visually unappealing. You have multiple PEP 8 violations, and your variable names really don't speak volumes. You have 2 spaces of indentation which is pretty much un-heard of in Python. If we move your code into a function and perform a little clean up we can get something like: import numpy as np def get_indexes(tokens, word): ...

1

It seems this could be simplified with a small lookup table and then concatenating strings. Using a table as simple as: const lookup = { blue: 'b', green: 'f', purple: 'g', yellow: 'p' }; we can then lookup the color values and concatenate them together. Combined with a simple check to make sure the color is valid and the 2 colors aren't the same, ...

1

I find the code presented here notably more readable than the previous iteration - the main apparent difference is meaningful naming of variables (most not in snake_case, yet). It looks like you want the results in one file per file of GpsData: such should be specified explicitly, as should be whether output records need to stay in the order of jobs in the ...

1

Firstly, main() must return an int: int main(void) We can make the array initialization easier to read with judicious use of whitespace: static const int N = 5; const int pixel_array[5][5] = { {1, 3, 6, 10, 15}, {2, 5, 9, 14, 19}, {4, 8, 13, 18, 22}, {7, 12, 17, 21, 24}, {11, 16, 20, 23, 25} }; Instead of the if/...

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