15.10 censor


#1

I've looked at the other threads on this exercise but none of them seem to be trying what I'm trying so I'm making my own tread. Is what I'm trying even possible?

from itertools import islice
def censor(text, word):
te_xt = text.split()
wo_rd = word.split()
k=0
for i in te_xt:
if wo_rd == islice(te_xt, k, len(word)-1):
for j in range(len(word)):
te_xt[k+j] = '*'
k +=1
return "".join(te_xt)

Edited slightly, but it still doesn't work as expected.


#2

@dataslayer86890: Oh well, this is interesting.
The thing with islice is that it works this way:

isclie(): elements from the sequence[start:stop:step]
#islice('ABCDEFG', 2, None) --> C D E F G

So what happens? You pass your strings to text and word, split them (creating two separate lists) and you try to check if the element stored in the wo_rd is equal to that islice statement.
But the problem is that islice() is an iterator not a generator.
You're trying to compare a value with this "itertools.islice object at 0x1e99898"
I don't believe you'll be able to get values from it unless you do something truly "high level in Python"

That's why your output was always the same, the condition was always false.
I, personally, can't see a way to make it work with islice() but the way you did spliting the strings and turning into lists, is correct.

Hopefully someone who has far more knowledge than me in Python will show up and also check if there's a solution for it.


#3

Ah, thank you for the information. knowing that islice can't work I've replaced my if statement with

´if wo_rd == te_xt[k:len(wo_rd)]:´ which seems to work for that part and I'm just quickfixing to pass the questions and considering I have no idea what half the errors are or how it's currently doing what it's doing (It's eating spaces for no reason I can figure out) I'm just going to steal somebody else's code and continue for now. I do want to someday figure this out properly, so help is still appreciated.