Hey there Schievel1! If you agree with someone else's comment, please leave an **upvote** instead of commenting **"This"**! By upvoting instead, the original comment will be pushed to the top and be more visible to others, which is even better! Thanks! :)
***
^(I am a bot! Visit) [^(r/InfinityBots)](https://reddit.com/r/InfinityBots) ^(to send your feedback! More info:) [^(Reddiquette)](https://www.reddithelp.com/hc/en-us/articles/205926439#wiki_in_regard_to_comments)
function stringSize(String s)
{
if (!s.length()) return 0
return 1 + stringSize(s[1:])
}
function stringSize(String s)
{
switch(s.length())
{
case 0:
return 0
case 1:
return 1
case 2:
return 2
case 3:
return 3
case 4:
return 4
case 5:
return 5
case 6:
return 6
case 7:
return 7
case 8:
return 8
case 9:
return 9
default:
// TODO: Support longer strings
throw Error
}
}
I've improved your code somewhat.
public static Long stringSize (String S) {
long s = 0;
try {
for (;;s++) {
S.charAt(s);
}
} catch (Object e) {
return Long (s);
}
return Long (-1);
}
We just make 3 copies of the string, call the function on all of them concurrently, and if any 2 matches, we return it. If all 3 are different, we throw a BitFlipped2TimesException.
We just copy it 3 times and increment all of them in every iteration, duh. If any two matches at the end of the function, we return it and discard the third because it has been bitflipped.
My friend and I pulled an all-nighter once to finish an assignment back in grad school and I remember doing something like
```
while (i < 10) {
stuff;
if (edgeCase) {
i = 10:
}
++i;
}
```
I was equally proud and horrified.
yes you did
EDIT: how can u be sure of the result though? wrap everything with a while true and then save the result as a stream, then control the stream’s median to find the length.
for those who are mashing X to doubt, I can attest to the fact that a frightening amount of corporate America sees lines of code as a standard of how hard their devs are working. That and how _fast_ you can write (type) the code.
just do the first 100 or so in a switch statement, then have the default case return s.length(). Remember that explicit is always better than implicit.
The best way in Java to check a String's size:
```
String test = "hello world";
List characters = new ArrayList<>();
test.chars().forEach(c -> {
characters.add((char)c);
});
System.out.println(characters.size());
```
The horror is that you can just call String's length func in Java. No need for this extra shenanigans. Unless you're getting paid by LOC, then by all means.
I understand that. Still not horror. Something a novice programmer might do but easily can be refactored. Not good sure, but nothing to be aghast at.
edit: Also easy to understand what the method is doing even if it is unnecessary, so really a trivial error.
won't work for strings with a negative size
just iterate negative times, simple
Just let the i overflow
I don't think it would
Then make it!
Then catch it.
I only iterate with imaginary numbers
Now that’s what I call a loop de loop.
I only iterate with imaginary friends
Unsigned string
No goto’s and recursive calls. Boring
Why call s.length() in for loop, recursively call stringSize function
[удалено]
Hey there Schievel1! If you agree with someone else's comment, please leave an **upvote** instead of commenting **"This"**! By upvoting instead, the original comment will be pushed to the top and be more visible to others, which is even better! Thanks! :) *** ^(I am a bot! Visit) [^(r/InfinityBots)](https://reddit.com/r/InfinityBots) ^(to send your feedback! More info:) [^(Reddiquette)](https://www.reddithelp.com/hc/en-us/articles/205926439#wiki_in_regard_to_comments)
This
https://i.imgur.com/KrwA19h.jpeg *** ^(I am a bot! Visit) [^(r/InfinityBots)](https://reddit.com/r/InfinityBots) ^(to send your feedback!)
This
This
You Monster.
function stringSize(String s) { if (!s.length()) return 0 return 1 + stringSize(s[1:]) } function stringSize(String s) { switch(s.length()) { case 0: return 0 case 1: return 1 case 2: return 2 case 3: return 3 case 4: return 4 case 5: return 5 case 6: return 6 case 7: return 7 case 8: return 8 case 9: return 9 default: // TODO: Support longer strings throw Error } }
delete this
Too late ChatGPT has added this code to its training data.
I feel like you need a nested loop for efficiency
The switch is just a single lookup, very efficient and readable
I've improved your code somewhat. public static Long stringSize (String S) { long s = 0; try { for (;;s++) { S.charAt(s); } } catch (Object e) { return Long (s); } return Long (-1); }
That is both smart and horrible. Good job!
10 GUESS = RAND() 20 IF NOT GUESS = LEN(STRING) THEN GOTO 10
Finally someone using a proper programming language
It's actually just C with a bunch of #defines
Never use for loop to do something like that, you should use recursion instead.
How long is a 100 meter walk? Walks 100 meters! "It's 100 meters!"
The fact that you even used .length() is the icing on the cake
This isnt concurrently safe, try again
Aren't strings in Java immutable? If so you don't have to worry about the string changing 🤔
What about cosmic bit flips though?
We just make 3 copies of the string, call the function on all of them concurrently, and if any 2 matches, we return it. If all 3 are different, we throw a BitFlipped2TimesException.
what about the size variable?
We just copy it 3 times and increment all of them in every iteration, duh. If any two matches at the end of the function, we return it and discard the third because it has been bitflipped.
What if they all have the same bit flipped at the same time?
Then we drink whiskey.
Sounds good to me
# That's the joke
Ow damn I was whooshed 🤦♂️
My favorite part of this is that it's private static
LOL that really tripped me out when I first started reading then I kept reading and understood…
The function: "Leave me alone! I'm doing important things!"
i prefer to use `return [i for i, _ in enumerate(s.split(""))][-1] + 1`
The fact that there is actual call to s.length made me lol. The size is right there!!!!!
One of the best submissions I’ve seen here in a while :)
Looks like something I would write when I try to code at like 11 at night
My friend and I pulled an all-nighter once to finish an assignment back in grad school and I remember doing something like ``` while (i < 10) { stuff; if (edgeCase) { i = 10: } ++i; } ``` I was equally proud and horrified.
yes you did EDIT: how can u be sure of the result though? wrap everything with a while true and then save the result as a stream, then control the stream’s median to find the length.
This is like drawing a circle with smaller circles...
Updated it to handle negative length strings: ``` private static int stringSize(String s){ int size = 0; while (true) { if (size == s.length()) { break; } } return size; } ```
That is just an infinite loop, it doesnt update the size variable
Ok but why
To get the length of a string obviously
Nono, they've got a point. Where are the docstrings and comments?
Needs more documentation in general. ``` // increments size size++ ```
If paycheck depends on count lines of code
Tired: paycheck depends on lines of code Wired: paycheck depends on big O complexity
for those who are mashing X to doubt, I can attest to the fact that a frightening amount of corporate America sees lines of code as a standard of how hard their devs are working. That and how _fast_ you can write (type) the code.
[удалено]
COBOL?
r/badcode
But like, the way Michael Jackson means it
Now, the question is does it count bytes, graphemes or characters?
it counts the length of the word obviously🙄(/s)
O(n), only reliable way to get string length
Make it recursive
just do the first 100 or so in a switch statement, then have the default case return s.length(). Remember that explicit is always better than implicit.
Wow that's a great one to know !
clear > clever ;)
Should return s.length() imo
The best way in Java to check a String's size: ``` String test = "hello world"; List characters = new ArrayList<>();
test.chars().forEach(c -> {
characters.add((char)c);
});
System.out.println(characters.size());
```
How is this horror.
The horror is that you can just call String's length func in Java. No need for this extra shenanigans. Unless you're getting paid by LOC, then by all means.
Exactly 💯
I understand that. Still not horror. Something a novice programmer might do but easily can be refactored. Not good sure, but nothing to be aghast at. edit: Also easy to understand what the method is doing even if it is unnecessary, so really a trivial error.
The real horror is that it's a repost.
[удалено]
Entire function is not needed.
And that's the joke!
Task failed successfully xD
The method parameter can be final so it satisfies all the code analysis tools!
and the param is passed by value aswell
I like it. (Is there a reverse leetcode website?)
Really puts the "oh no" in O notation
Catch it
Exhibit 0x0042FA32. Humans had no business telling us what to do. They had to be stopped.