#๐Ÿ”’ Problem with associating index and value (today's leetcode challenge)

26 messages ยท Page 1 of 1 (latest)

hollow wind
#

I've now tried a few times to check a value and then do something with its index in a function.

Challenge:
A fancy string is a string where no three consecutive characters are equal.

Given a string s, delete the minimum possible number of characters from s to make it fancy.

s = "leeetcode"

def fun():
    for i, c in enumerate(s):
        if c = c+1 = c+2:
            remove()

I can't seem to properly digest how to correctly reference the index. I still have trouble with data structures. I need more exercises like this to get a feeling for it, have some repetition.

Is there some good way to remember how to do this thing, a good analogy maybe?

woven phoenixBOT
#

@hollow wind

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

uncut pewter
#

Index is i in your case. You can still do s[i] and stuff.

Tbh it's one of the cases where I'd ditch "proper" for-each loops using enumerate and just loop over range(len(s)).

Because unless you know smart way of index-less iterating over several elements, you just easily get confused as to what i and c are, as shown here

hollow wind
#

i know the code is incorrect but doing i = i+1 = i+2 will give me the indexes

uncut pewter
#

Also don't actually remove stuff from things you iterate over - instead, make a new one with elements (here: letters) to keep

hollow wind
#

so its jsut 1, 2, 3 in that case

#

so the case will never happen

uncut pewter
hollow wind
#

i know, its the reason why i did c instead

uncut pewter
#

So s[i]==s[i+1]==s[i+2]

ancient cedar
uncut pewter
#

^Yeah, that's why I said to use range. It's one of those cases where enumerate just makes it more confusing and causes index errors...

hollow wind
#

i have a hard time seeing a variable or a table or array (like that) and understand what it'll actually represent

ancient cedar
#

(in first place, string[index + n] will error for the last n characters as you're trying to index beyond the string length)

hollow wind
#

in case of s[1] it'll give me the character

#

so e

#

i should start logging more

uncut pewter
hollow wind
#

i did but its still all so unusual to work with

#

(not even a week into learning this as my first programming language)

ancient cedar
#

I recommend creating a new list and appending characters you want to keep, instead of trying to remove characters from an existing list/string

hollow wind
#

so might just be the little time I've spent and it'll become more obvious and more natural to me

vale dust
#

This is a very common pattern you'll see quite often

woven phoenixBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.