# Tag Info

11

Styling Try to make the code follow the PEP 8 Style Guidelines, this will make your code follow the Python conventions, and also more readable. So do if l == 1 instead of if l==1, my_atoi instead of myAtoi, etc Code structure It seems that page requires the code to be inside a class. Leaving the exercise aside for a moment: You don't need that function ...

10

So far, your code has some general problems that are much more urgent than performance: You are using namespace std;. This is an extremely bad habit and will ruin your life ("this is a contest and I write everything in main" is not an acceptable excuse). See Why is using namespace std; considered bad practice?. The main function is very long and a reader ...

9

This code does not implement a rolling hash. For every iteration of the main loop, the hash is reset and then entirely re-calculated from nothing with an inner loop. A rolling hash would remove a character from the hash and then add a new character, doing only a constant amount of work per sub-string. There are some edge-cases for you to work out, but main ...

5

The code looks straight-forward and really optimized for size and simplicity. There's a small detail that I would change, though: replace cmove with cmovz, to make the code more expressive. It's not that "being equal" would be of any interest here, it's the zeroness of %ecx that is interesting. I like the omitted second jmp in memmove. It's obvious after ...

5

Review Don't use abbreviated variable names s, curAns and posAns; use self describing names: value, index and targetIndex instead. The nullable int could be replaced with -1. This reads cleaner and allows you to bypass the final ?? operator in curAns ?? -1. You can optimize the count curAns.Value + target.Length with adding -1 because we don't care about ...

5

I challenge the need for a list of booleans. There are alternatives available in the .NET Framework that deal with a sequence of booleans. If the flags are static and fixed: [Flags] enum IGreetYou : uint { None = 0, Hi = 1 << 0, Hello = 1 << 1, Yow = 1 << 2 } with a simple conversion to string as: var value = ...

5

Just a couple of remarks: You should add a newline after the last line: $./nh First char match index: 18 Char: t Exist count: 1 Exist count: 2 Exist count: 3 Exist count: 4 Position: 18$ I don't know what compiler you use but with when compiled with gcc and -Wall -Wextra -pedantic you get: gcc -O2 nh.c -lm -o nh -Wall -Wextra -pedantic ...

4

You can also use strip() to remove whitespaces in strings. strip() will copy your string and remove both leading and trailing whitespaces. foo_string = ' So much space for activities! ' >>> foo_string.strip() 'So much space for activities!' You can also use this to only remove leading whitespaces or trailing, using lstrip() and rstrip(), ...

4

I think the shorter, the better. Since you are in Python 3, if you really want to make explicit that the function is returning a string, you can use type hints: def trimString(string) -> str: You can also specify it in the parameter: def trimString(string: str) -> str: (keep in mind that Python will ignore type hints, but some IDEs like PyCharm use ...

3

regarding: char *str = strdup(format_string()); The memory returned from format_string() is already allocated from the heap. The posted code has two memory leaks. the allocated memory in the function: format_string() is never passed to free() the allocated memory in the function: strdup() is never passed to free() suggest: char *str = format_string());...

3

3

There's a bug in your code if memchr finds %sil in the last byte of %rdi; if %rcx tests to be zero and yet the byte has been found, it will incorrectly return zero. To fix that, do something like this: .globl memchr memchr: mov %rdx, %rcx movzbl %sil, %eax repne scasb sete %cl lea -1(%rdi), %rax test %cl, %cl ...

3

It seems that you've managed to fix the algorithm, so it does what it's supposed to do. But the concept is the same and performance isn't improved. Still you could use some more descriptive names, and i instead of j1 (why 1?). You could use foreach (string target in targets) { ... } instead of for (int j1;...) because you don't use the index to anything ...

3

Specification SliceBySize I have verified your method against that of the OP. The OP splits slices in chunks of desired length, with any remainder in a smaller chunk. [TestMethod()] public void SplitTestNotEqual() { string input = "12345"; string[] expected = new string[] { "123", "45" }; string[] actual = input.Split(3); CollectionAssert....

2

You should also take into consideration the JS methods when calculating time and space complexity. It's not as simple as $O(n^2)$ time because you used two nested for loops. Slice, Split, reverse, join are all also $O(n)$ operations under the hood. Split also creates a brand new array so that is also $O(n)$ space.

2

Adding on to the previous answer by @Arkadiusz Drabczyk: A simple, trivial implementation of contains could be done like this: ssize_t contains(const char * needle, const char *haystack) { char *needle_in_haystack; if(!needle || !haystack) return -1; needle_in_haystack = strstr(haystack, needle); return needle_in_haystack ? ...

2

main() it is considered bad practice for the main() method. at the very least, it is customary to catch all exceptions and print the stack trace to allow debugging. All IO resources (files, network sockets, DB connetions, etc) should be closed before the program exits. Java 7 has introduced the try-with-resources Statement where the compiler ensures proper ...

2

If you don't want to reinvent the wheel, you can implement a very short and simple solution using java.util: public class Main { public static void main(final String[] args) throws IOException { final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); final BufferedWriter bufferedWriter = new ...

1

In memmove you have the following: cmp %rdi, %rsi jge 0f (cmp rsi, rdi in Intel syntax I believe.) For rsi = 8000_0000_0000_0000h and rdi = 7FFF_FFFF_FFFF_FFFFh (we want to jump to make a forward move here) the signed-comparison conditional branch "jump if greater or equal" evaluates rsi as being "less than" rdi (rsi being a negative number ...

1

Your custom function is 36 lines of very hard to digest code. You are performing a battery of if-elseif-else conditionals on the same variable -- for this reason, it is most appropriate to employ a switch case (even though I have a strong bias against them) as a matter of best practice. If the goal is to obfuscate the output AND the code, I reckon you've ...

1

You don't need the first loop and all the length calculations. Btw., the function doesn't succeed, if the first char is found, but only the second occourrence of the first char fits with needle. The task can be reduced to a few lines: int contains(char *buf, char *needle) { char *src, *srch, *srcp; for(src=buf; *src; src++) { for(srch = ...

1

Your code doesn't work. It returns 0 for haystack "abbc" and needle "bc", even though the haystack contains the needle.

1

Avoid explicitly constructing QString and QStringLiteral objects In many cases, it's totally unnecessary to explicitly write QString("...") or QStringLiteral("..."). Constant string literals will be implicitly converted to QString in many cases. Also, while QStringLiteral might avoid a copy in some cases, it looks like premature optimization to me. So for ...

1

I don't know how stable the precise HTML structure of those pages is, could that change in the future? Using a HTML parsing library might be a more robust approach. Some remarks concerning the Swift implementation: Don't force-unwrap optionals. If one of the searched strings is not found, your program will terminate with a runtime error. Use optional ...

1

"abc".IndexOfAny2("c", "abc") fails with an ArgumentOutOfRangeException, because IndexOf requires startIndex + count to not exceed the length of the string. For a tiny improvement, make curAns a normal integer and initialize it with s.Length. After the necessary changes, you'll end up with fewer checks inside the foreach loop. Most time is spent in string....

1

I'll attempt to make two points: Differentiate where Strings are held in a pool; and, Evaluate the code-duration where the sting value is held (i.e. start-to-finish number lines of code). Since Java 11 (if I recall correctly), all strings not using Unicode are byte arrays. So the discussion should be about whether the reference to a string is shared (...

1

At the high level bit, there's not much to complain about and a lot to like. Not so much because the code works (a necessary but non-trivial requirement for this site). But because the logic is sound (sets/hashmaps) and can be described and understood easily without a lot of details in the code. But I might be biased because I've written similar code to so ...

1

To me, the code does not express the solution in a way that is easily connected to the description of the problem. "Message" becomes data. It's not really clear to me how "number of ways" is represented in the code except as the ultimate return value. Business Logic Deeper down, the code does not reflect the way I reason through the examples. I understand ...

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