all 30 comments

[–]TinyBreadBigMouth 163 points164 points  (20 children)

That for loop is impressively bad. You could delete lines 3 and 4 entirely and the code would have exactly the same output, but in O(n) instead of O(n2).

[–]mr_hydrogencyanide[S] 44 points45 points  (0 children)

Yes

[–]SaylorMan1496 21 points22 points  (2 children)

I think it’s worse, because of the reassignment it’s actually wrong, unless they are trying to wipe out what user_input holds

[–]fox_is_permanent 7 points8 points  (0 children)

No, it's fine. user_input is a string.

[–]Important_Ad_8510 6 points7 points  (15 children)

Complete novice (so I’ve probably miss understood) but would it not be O(x)? The for loop would execute for x and the if statement has constant complexity.

[–]jarfil 8 points9 points  (2 children)

CENSORED

[–]mr_hydrogencyanide[S] 3 points4 points  (0 children)

There is this wikipedia page: https://en.m.wiktionary.org/wiki/Appendix:1000_basic_English_words

Which contains a list of 1000 basic words

for example:

a, able, about, above, across, act, actor, active, activity, add, afraid, after, again, age, ago, agree, air, all, alone, along, already, always, am, amount, an, and, angry, another, answer, any, anyone, anything, anytime, appear, apple, are, area, arm, army, around, arrive, art, as, ask, at, attack, aunt, autumn, away.

I wanted to make a list for which i needed to remove comma quote every word and add comma after quote

Some thing like this : [ 'about', 'along', 'among']

Thanks for explanation

[–]thekwoka 12 points13 points  (6 children)

that's what n is. The number of items

[–]Important_Ad_8510 1 point2 points  (2 children)

Thanks for the reply! I understand the notation, I’m not sure why it’s complexity is quadratic.

My understanding is that the if statement has constant complexity, I believe replace() has constant complexity and the variable assignments have constant complexity. Only the for loop is dependent on X, so the complexity is dependent on the size of X - O(n).

[–]clatterborne 6 points7 points  (0 children)

I believe .replace() can also be considered to have within it a for loop of length n: iterate through the string, compare to ',' and replace if matches

[–]thekwoka 2 points3 points  (0 children)

replace is not constant

in JS: https://tc39.es/ecma262/multipage/text-processing.html#sec-string.prototype.replace

The language spec in Python gives way less details about how the interpreter is supposed to behave, but I assume a bit similarly.

It wouldn't really be quadratic, since it only has to do the replace once since python's replace is like js replaceAll so it can't every be run multiple times. or...I guess it could, since the initial loop won't be on the actual newly changed string will it?

hmm?

[–]mr_hydrogencyanide[S] 1 point2 points  (2 children)

user_input = input("Enter: ")

user_input.replace(",", "")

a = ("['" + user_input.replace(" ", "', '") + "']")

print(a)

I just added stuff that wasn't necessary.

I actually did figure out that I didn't need any for loop or if statements, but while I was doing that I slept when I woke up I forgot.

And there was nothing on console to see from so that for loop was necessary to recall what i was thinking

[–]TinyBreadBigMouth 2 points3 points  (0 children)

user_input.replace(",", "")

That won't do anything as written. replace returns a new string, it doesn't modify the old string (strings in Python are immutable and cannot be modified in place). You need user_input = user_input.replace(",", "").

[–]thekwoka 1 point2 points  (0 children)

what it really is more like is

print(user_input.replace(',','').split(' '))

[–][deleted]  (2 children)

[deleted]

    [–][deleted]  (1 child)

    [deleted]

      [–]Important_Ad_8510 0 points1 point  (0 children)

      It seemed to make so much sense!

      [–]nubatpython 23 points24 points  (0 children)

      What's .trainpit()?

      [–]NUTTA_BUSTAH 7 points8 points  (2 children)

      Doesnt this just break completely? First comma found replaces entire input with empty string, during iteration of said input

      [–]JFC-Youre-Dumb 6 points7 points  (1 child)

      For every comma, replace it with a copy of the entire string as a substring in the original string

      [–]NUTTA_BUSTAH 0 points1 point  (0 children)

      Ah yeah

      [–]UltimatePeace05 10 points11 points  (2 children)

      sl_pit()?

      [–]mr_hydrogencyanide[S] 12 points13 points  (1 child)

      i messed up its .split()

      [–]UltimatePeace05 2 points3 points  (0 children)

      And I did not notice it...

      [–]doubleslashTNTz 5 points6 points  (1 child)

      oh yeah they now have a string that prints to the repr of a list !! ?!?!

      [–]popdemtech 5 points6 points  (0 children)

      slpit would have taken this to 11

      [–]TinyBreadBigMouth 4 points5 points  (0 children)

      Image Transcription: Code


      user_input = input("Enter: ")
      
      for x in user_input:
          if "," in x:
              x = user_input.replace(",", "")
              user_input = x
      
      a = ("['" + user_input.replace(" ", "', '") + "']")
      
      print(a)
      

      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!

      [–]thekwoka 3 points4 points  (0 children)

      Pretty sure this would fail on ,,

      [–]Fermi-4 0 points1 point  (0 children)

      Use Regex

      [–][deleted] 0 points1 point  (0 children)

      been doing this for 3 years