Censor 10/15 Practice makes us better, why it doesn't work?


#1

Hello everyone, i tried the exercise avoiding any shortcuts

def censor(text,word):
  ttext=text.lower()
  wword=word.lower()
  lst=[]
  ltext=len(text)
  lword=len(word)
  i=0
  
  while i<=ltext:
    count=0
    for k in range(lword):
      
      if ttext[i+k]==wword[k]:
        lst.append(i+k)
        count+=1
        
      if ttext[i+k]!=wword[k] and count<=lword:
        for ii in len(count):
          lst.pop(i+k-ii)
          
    if count==lword:
      i=i+count
    else:
      i+=1
      
  for kk in len(lst):
    text[lst[kk]]='*'
  return text  

For some reason i get an error that int characters don’t have len(). First of all, can anyone see if i ma wrong in any of my logic in the algorithm and secondly why do i get this error, thank you in advance.


#2

complicated algorithm, i would recommend to write a more efficient one. As for you error, its easy to found out:

def censor(text,word):
  ttext=text.lower()
  wword=word.lower()
  lst=[]
  ltext=len(text)
  lword=len(word)
  i=0
  
  while i<=ltext:
    count=0
    for k in range(lword):
      
      if ttext[i+k]==wword[k]:
        lst.append(i+k)
        count+=1
        
      if ttext[i+k]!=wword[k] and count<=lword:
        print count
        for ii in len(count):
          lst.pop(i+k-ii)
          
    if count==lword:
      i=i+count
    else:
      i+=1
      
  for kk in len(lst):
    text[lst[kk]]='*'
  return text  
  
print censor("hey hey hey", "hey")

count has a value of zero, and integers are not iterates

i think count should be used index for some list or string


#3

Hi @aliatros, you have len(count), count is an int. The easiest way to do this exercise is to split text into a list of the words in the text, using .split(’ '), then you can check each word in the list, if it is equal to the ‘word’ to censor then set that list item to ‘***’ or '’ * len(word) which will give you an asterisk for each character. After you have checked all the items in the list you join the list back to a string again using ’ '.join(name_of_list). Then return the resulting string.


#4

I just noticed you don’t want to do it that way. But even if you do it the way you want to, which can definitely be done, the simple ways are always the best. Also @stetim94 already explained the count bit.


#5

Thank you for your reply, i do understand my mistake now, i probably wanted to write range instead of length. My “biggest mistake” is that i don’t take advantage of the perks of python and instead of trying to split the string in words, i try to split it in letters, hence the mess. I liked my way more tbh :smiley: but i will try to be more efficient next time. Thank you


#6

No problem, just remember also that making mistakes is always good, we learn from them. You’re right, range(count) instead of len(count) would make sense. Your way is not wrong, if you prefer that way, do it the way you are comfortable with. It’s not always better to write concisely, sometimes doing things in an order you’re comfortable with is best and can often be easier to read. Did you get it working.


#7

We are not expected to write case insensitive code. The text and word should be left as they are given, with no case change.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.