New answers tagged

0

This should not be a complete review, but the base R function round has an optional second argument digits. It works like this > round(1.234567, digits=1) [1] 1.2 > round(1.234567, digits=2) [1] 1.23 Therefore there is no need for your function f.


4

First of all, nice code. I like the documentation and the type hints. One thing you could do is to follow a style convention for your docstrings; see some examples here. For me, I think the most glaring thing is that you have all of your tests in a main function. I would separate these tests by type and move them over to individual files, and I would ...


7

This is just a quick answer. I don't know exactly what other constraints you have, but it feels very clumsy to have to do something like this: path = Path.from_str("foo[bar]") assert path.get(Test) == 2 path.set(Test, 3) assert path.get(Test) == 3 I would instead consider for Path to take the object the path will be taken off and be a wrapper for ...


1

BUG: Your main loop for l in substrings(a): ... will return the first palindrome it finds inside a (stepping the indices back from the end), not just the longest one. If there are multiple longest palindromes it won't return them all, only the first it finds. Your code is overly tailored to this one example. Use other examples to tickle it, e.g. "zaba ...


2

A slight change to the answer: def normalize_text4(text): output: List[str] = [] punctuation_filter = str.maketrans('', '', string.punctuation) for token in nltk.word_tokenize(text, preserve_line=True): token = token.translate(punctuation_filter) if token: output.append(token.lower()) return output Usage with ...


2

You can save a little bit of time by not re-running str.maketrans for each token, since it's always going to produce the same result: import nltk from statistics import mean import string import time from typing import List def normalize_text3(text: str) -> List[str]: output: List[str] = [] punctuation_filter = str.maketrans('', '', string....


3

OP: Please leave any feedback you see fit, with special attention to the loops and data types. char type Although char is signed or unsigned, string functions of the standard library work with the data as if it is was unsigned char. ... each character shall be interpreted as if it had the type unsigned char ... C17dr §7.24.1 3 Also: char output = 0; ......


1

O(n*n) With a changing str and strlen(str) in the for(i = 1; i < strlen(str); ++i) loop, code repeatedly calculates the string length. Once is enough. Even easier, test for the null character. is...(int ch) called with char is...(int) and to...er(int) functions expect an unsigned char value or EOF. When called with a char less than zero (and not EOF)...


2

In the context of function parameter passing, char* and char[] are equivalent (because char[] decays to char*). Anyway, I would rather use title_case(char* s) because it more clearly expresses the truth: what you really get within the function is a pointer to the first character of the array. Accepting char* as string parameter implicitly suggests that the ...


0

the only advice, that I can give you, is to separate the logic in multiple methods. This will make your method shorter and allow the code to be reused. Personally, I see two other methods. String readString(Reader r, Charset charset) throws IOException { ByteArrayOutputStream ostream = copyInputToStream(r); final ByteBuffer byteBuffer =...


3

A small version compare improvement would perform up to n +1 rather than 2n compares. Instead of if(pL->major > pR->major) return LEFT; if(pR->major > pL->major) return RIGHT; if(pL->minor > pR->minor) return LEFT; if(pR->minor > pL->minor) return RIGHT; ... Compare for equality first: if(pL->major != pR->major)...


11

Don't strtok + atoi. Use strtol, which (a) doesn't need a mutable input, (b) has much better error handling and reporting, and (c) eliminates the need for independent validation. An example of use would be char * end; pVer->major = strtol(str, &end, 0); if (*end != '.') { // major is not a number. return suitable_failure; } str = end + 1; ...


7

Instead of spelling out every single element of the array, char ver_string[] = {'5','.','2','5','.','5','.','0','\0'}; just use a string literal. It's the same thing: char ver_string[] = "5.25.5.0"; Should I be printing to stderr only in the driver (main) program and leave all of that out of the main "library" code? Yes, exactly. You can return error ...


5

The validation of the string seems to leave some gaps. For instance, the version numbers seem to be valid whatever size they have. They could be zero digits large, or contain so many digits that they would not fit into an integer. I'd try not to print to standard error if this is supposed to be used as a library. In that case you might want to use separate ...


1

Spaces My first read of this code: "The \"test\" command, as well as the \"[\" command, are not required to know", "the \"==\" operator. Only a few implementations like bash and some", "versions of ksh support it.", was wrong; I didn't notice that it's actually a varargs-function accepting one line per argument. That's a little odd. This has ...


3

In addition to @MaartenBodewes brilliant answer some words to your unit tests: what I like the fact that you have unit tests at all. the naming is well thought of. what I don't like Do not reduce the content of a test method to a single line. Unit tests are documentation. You should write your unit test in a way, that they describe the testes ...


6

The challenge First of all, a method called String add(String number) is just wrong in every way that you look at it. It may be a "classic" Kata, but for me that's classic stupidity, especially if you consider that the method needs to add the numbers within the number given. The challenge really leads you into returning strings for anything. In program ...


10

Are you searching for letters ('A' and 'B') or strings ("A" and "B")? You title says "pair of Strings in a List", but your code's variables are letter and letters. You have hard-coded the search for the strings "A" and "B". Are these the only two you will look for? Might you want to search for "C" and "D"? Is the condition "all" or "none", in which case,...


4

I have some suggestions for you. Instead of using list, I suggest that you use varargs; this will remove the need to put the item in list and pass them to the method. Instead of using String, you can use the Character to reduce, by a bit, the memory footprint. In the for loop, I highly suggest that you use the if-else-if pattern or a switch instead of the ...


12

iA and iB can only take the values 0 and 1, so that if ((1 == iA || 1 == iB) && (1 != iA || 1 != iB)) { ... } can be simplified to if (iA != iB) { ... } Boolean variables would be more appropriate for this purpose, and the variable names can be improved, e.g. boolean containsA = false; boolean containsB = false;


Top 50 recent answers are included