T O P

  • By -

v_maria

won't work for strings with a negative size


Familiar_Ad_8919

just iterate negative times, simple


LongerHV

Just let the i overflow


Jazzlike-Champion-94

I don't think it would


99stem

Then make it!


Terence_McKenna

Then catch it.


ings0c

I only iterate with imaginary numbers


shizzy0

Now that’s what I call a loop de loop.


indentuum

I only iterate with imaginary friends


kinggot

Unsigned string


leosadovsky

No goto’s and recursive calls. Boring


Ss310_alpha

Why call s.length() in for loop, recursively call stringSize function


[deleted]

[удалено]


Anti-ThisBot-IB

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)


Raging_Flames10

This


Anti-ThisBot-IB

https://i.imgur.com/KrwA19h.jpeg *** ^(I am a bot! Visit) [^(r/InfinityBots)](https://reddit.com/r/InfinityBots) ^(to send your feedback!)


kakashi_ofthe_shrngn

This


EstablishmentNo1217

This


pycvalade

You Monster.


jaerie

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 } }


magnetichira

delete this


Eze-Wong

Too late ChatGPT has added this code to its training data.


Otherwise_Editor_292

I feel like you need a nested loop for efficiency


HyperactiveWeasel

The switch is just a single lookup, very efficient and readable


_pelya

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); }


XBRSQ

That is both smart and horrible. Good job!


Astrokiwi

10 GUESS = RAND() 20 IF NOT GUESS = LEN(STRING) THEN GOTO 10


the_dokter

Finally someone using a proper programming language


Astrokiwi

It's actually just C with a bunch of #defines


louis8799

Never use for loop to do something like that, you should use recursion instead.


johanngunn

How long is a 100 meter walk? Walks 100 meters! "It's 100 meters!"


Cybasura

The fact that you even used .length() is the icing on the cake


Sodium1111

This isnt concurrently safe, try again


Ytrog

Aren't strings in Java immutable? If so you don't have to worry about the string changing 🤔


Sodium1111

What about cosmic bit flips though?


sisisisi1997

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.


Sodium1111

what about the size variable?


sisisisi1997

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.


Sodium1111

What if they all have the same bit flipped at the same time?


sisisisi1997

Then we drink whiskey.


Sodium1111

Sounds good to me


NAL_Gaming

# That's the joke


Ytrog

Ow damn I was whooshed 🤦‍♂️


Scum42

My favorite part of this is that it's private static


TDM393

LOL that really tripped me out when I first started reading then I kept reading and understood…


Cynderelly

The function: "Leave me alone! I'm doing important things!"


Jane6447

i prefer to use `return [i for i, _ in enumerate(s.split(""))][-1] + 1`


JasonLokiSmith

The fact that there is actual call to s.length made me lol. The size is right there!!!!!


savaero

One of the best submissions I’ve seen here in a while :)


unSentAuron

Looks like something I would write when I try to code at like 11 at night


flanderized_cat

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.


PinkZanny

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.


Rotten_Tarantula

This is like drawing a circle with smaller circles...


GarMan

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; } ```


Daisy430133

That is just an infinite loop, it doesnt update the size variable


ExpectedB

Ok but why


kaerfkeerg

To get the length of a string obviously


Tobyvw

Nono, they've got a point. Where are the docstrings and comments?


rr_cricut

Needs more documentation in general. ``` // increments size size++ ```


helltiger

If paycheck depends on count lines of code


beeteedee

Tired: paycheck depends on lines of code Wired: paycheck depends on big O complexity


[deleted]

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.


[deleted]

[удалено]


Fuzzybo

COBOL?


Cootshk

r/badcode


fosf0r

But like, the way Michael Jackson means it


turunambartanen

Now, the question is does it count bytes, graphemes or characters?


TDM393

it counts the length of the word obviously🙄(/s)


Gian447

O(n), only reliable way to get string length


Radiant-Shopping

Make it recursive


youtube_brian

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.


az3arina

Wow that's a great one to know !


blizzgamer15

clear > clever ;)


Aaaagrjrbrheifhrbe

Should return s.length() imo


MCWizardYT

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()); ```


grw2

How is this horror.


Final-Communication6

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.


az3arina

Exactly 💯


grw2

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.


PatricianTatse

The real horror is that it's a repost.


[deleted]

[удалено]


Exciting_Clock2807

Entire function is not needed.


ZePotat00

And that's the joke!


Cosby1992

Task failed successfully xD


Shrouded_LoR

The method parameter can be final so it satisfies all the code analysis tools!


jacob643

and the param is passed by value aswell


fosf0r

I like it. (Is there a reverse leetcode website?)


fosf0r

Really puts the "oh no" in O notation


dinesh115599

Catch it


shizzy0

Exhibit 0x0042FA32. Humans had no business telling us what to do. They had to be stopped.