It should be the other way round. Splitting by anything other than zero is wrong solution because you could choose a char not existing in the input string. And the given code example actually deals with that.
Honestly, after a while I though this could be just an issue with phrasing. What I was going for was the word "anything". I was tought that there's a big difference between "all" and "any". Like in methods "allMatch" and "anyMatch". And my initial idea was that you just pick any arbitrary char and split.
Why is it called "longestZero"? That name makes no sense at all. Call it "longestSequenceOfZeros" because that's what it is.
int number = 0. No, "number" is a useless name. This should be something like "zerosInCurrentSequence".
int max = 0. Again, useless name. "longestSequence"
Why a for-loop? The foreach char makes much more sense.
Why the ternary at the end? Surely the repeat function can handle a zero input.
If there are no zeros in the string, your function for some reason returns an arbitrary space. Why?
Some see this as nitpicky, but I'm so tired of digging through functions with indescriptive names and variables like "num, count, max, i, current" trying to understand what each variable is and what it's supposed to do because the names sure as hell don't help.
In c/c++ single quotes and double quotes have actual different meanings- single quotes are character literals and double quotes are strings- important for comparison operations. The code wouldn’t work in the same way if only double quotes were used afaik.
Do note that this is java code, meaning that single quotes are char literals and double quotes are string literals. They cannot be used interchangeably.
For langs that have both as literals, i think it's a good idea to set a project-scoped standard for which string literal style is used, to prevent code from different developers mixing these styles. i'm usually in favour of double quotes just because single quotes are only for character literals in most langs i write in.
I'd at least make an exception for strings that contain the chosen type of quote, to save some escaping. Especially to save multiple levels of escaping like
cmd = "echo \"this string is \\\"readable\\\"!\"";
Good code is pretty good. I still prefer breaking out if/else in brackets because it's a visible break for my eyes. I like it though. Small and compact.
I don't understand why you need to count zeros before making the string of zeros. Why not initialise an empty string and instead of counting the zeros just add the zeros to the empty string? Instead of number++ you do ans += "0". Then you check the length of the string in the return statement and return either the string or the failed case.
I'm not a Java developer, so I might be missing something here.
Also, thank you very much for posting both versions of the code! That is the way of posting that greatly benefits everyone on this sub imo.
Because strings are fairly heavyweight, and appending to strings is far heavier than a pre-pass using ints. Additionally, I'm pretty sure that the repeat() uses StringBuilder under the hood.
if it was working on a significantly long string it might be a problem, but it might be a stretch to say it has insanely more operations.
we're looking at (more than likely) less than a millisecond difference in speed, which would just be considered optimizations for the sake of optimizations, depending on the use-case
I'd rather have it be more readable and easier to understand than "faster".
Honestly, the shorter solution took me a lot longer to read & comprehend than op's simplified solution (i.e. have to parse the regular expression, why the matches are being replaced with a space and then split by the space, etc). Regardless I feel like this is the type of function where the name and/or a small comment above the function header can be enough for describing what it does.
getting into opinions at this point. imo, it was pretty easy to grok what it was doing when reading it. the regex is pretty standard so no huge effort there either, imo
Both of these are just OK. Really the design of the method is questionable. Why return a string here? Why not return an integer? For that matter is it necessary to take a string as the argument? It could also be appropriate to return a span if you really don't want an int. This really doesn't look done to me in either version.
Additionally, in the top version the major lines of code difference comes from the formatting of the if clauses. Personally I find the second attempt at formatting those conditionals to be more readable and maintainable. Just because your code takes up less screen space, doesn't mean it's better in any sense.
I'd have a lot of questions seeing either of these during a code review.
42
u/zed_three Dec 28 '21
3am version is fine? Not great, 10am version definitely better, but I wouldn't say it's particularly egregious
34
u/GustavoCinque Dec 28 '21
Split the string by anything other than zero, remove null objects, order by size desc, get first. Done.
55
u/Greenimba Dec 28 '21 edited Dec 28 '21
IMO, the 10AM version is worse in a lot of ways.
Some see this as nitpicky, but I'm so tired of digging through functions with indescriptive names and variables like "num, count, max, i, current" trying to understand what each variable is and what it's supposed to do because the names sure as hell don't help.
1
u/M4A28 Dec 29 '21
OK, this code purpose is to find the longest consecutive sequence of zeros in string
5
u/mpaxton1 Dec 28 '21
Good code is pretty good. I still prefer breaking out if/else in brackets because it's a visible break for my eyes. I like it though. Small and compact.
8
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Dec 28 '21
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
3
u/KnockKnockP wicked Dec 29 '21
i like 3am code better
5
u/alex_wot Dec 28 '21
I don't understand why you need to count zeros before making the string of zeros. Why not initialise an empty string and instead of counting the zeros just add the zeros to the empty string? Instead of
number++you doans += "0". Then you check the length of the string in the return statement and return either the string or the failed case.I'm not a Java developer, so I might be missing something here.
Also, thank you very much for posting both versions of the code! That is the way of posting that greatly benefits everyone on this sub imo.
12
u/FrankSuzki Dec 28 '21 edited Dec 28 '21
Better in one line:
Replace all non-zero chars by a space, split by spaces, return the longest string (that can only be zeroes), if there is no strings return a space.
2
u/suppow Dec 28 '21 edited Dec 29 '21
i hate that '==' ligature, it
mesmakes it look like '=', i think one like '≡' would be easier to distinguish.1
u/hey-have-a-nice-day Dec 29 '21
I agree. 10 am = bad code for me since my brain only works at night
1
u/DizzySkin Dec 29 '21
Both of these are just OK. Really the design of the method is questionable. Why return a string here? Why not return an integer? For that matter is it necessary to take a string as the argument? It could also be appropriate to return a span if you really don't want an int. This really doesn't look done to me in either version.
Additionally, in the top version the major lines of code difference comes from the formatting of the if clauses. Personally I find the second attempt at formatting those conditionals to be more readable and maintainable. Just because your code takes up less screen space, doesn't mean it's better in any sense.
I'd have a lot of questions seeing either of these during a code review.
1
u/[deleted] Dec 29 '21
What language is this?
1
u/Motor-Maleficent Dec 29 '21
Where's the good code without the coffee?
-1
u/toastal Dec 29 '21 edited Dec 29 '21
Man, for loops are unreadable. I'm so far down the higher-order functions rabbit hole I can't even read or follow all this mutable state.
-9
u/pretty_meta Dec 28 '21
Eliminating 'if' branches from your code will be even faster. Maybe you can do something like this in Java?
-7
u/Wugliwu Dec 28 '21
Hm sexy public static methods are so comfy to use, I'll use this one anywhere!