-1

I am trying to solve the python exercise for 'remove suffix ness from the string' and have written the following code, but I am unable to pass the test

def remove_suffix_ness(word):
    no_suffix = [word[:-4] for word in word]
    for i in range(len(no_suffix)):
        if no_suffix[i][-1] == 'i':
        no_suffix[i] = no_suffix[i][:-1] + 'y'
    else:
        pass
    return ', '.join(no_suffix)

The code is supposed to take the following as Input:

Input = ['heaviness', 'sadness', 'softness', 'crabbiness', 'lightness', 'artiness', 'edginess']

and Output should be a string without ness and if the string is ending with 'i' I have to replace 'i' with 'y'. It should look something like this:

Output = heavy, sad, soft, crabby, light, arty, edgy

I am getting the following message:

TEST FAILURE
    IndexError: string index out of range
Hemant
  • 1
  • 2
  • Your code works and does what it is supposed to do, so the problem is with the test itself. How do you test the code? – TDG Feb 19 '22 at 11:19
  • @TDG My guess is that the parameter name is correct. – Pychopath Feb 19 '22 at 11:23
  • Please show the actual exercise instructions, not paraphrased. – Pychopath Feb 19 '22 at 11:27
  • 1
    the instructions are as: Implement the remove_suffix_ness() function that takes in a word str, and returns the root word without the ness suffix. If the root word originally ended in a consonant followed by a 'y', then the 'y' was changed to 'i'. Removing 'ness' needs to restore the 'y' in those root words. e.g. happiness --> happi --> happy. – Hemant Feb 19 '22 at 19:37

1 Answers1

0

I think the problem is your test doesn't pass a list of strings to your method, but only a single word each time.

Can you try to return the solution for a single word?

def remove_suffix_ness(word):
   return word[:-5] + 'y' if word.endswith('iness') else word[:-4]
Christian Weiss
  • 119
  • 1
  • 14
  • Thanks, your solution worked. But how to get the code work in both cases, i.e. a single word or a list of words is passed? – Hemant Feb 19 '22 at 19:49
  • You could use a list operation with the method above, for example: `l = [remove_suffix_ness(word) for word in words]`, if you have a list passed to a method. In case you don't know what type you're getting you could do a type check before hand like `if isinstance(word, str): # do sth else: # do sth. with the list` – Christian Weiss Feb 19 '22 at 22:47
  • @Hemant Why are you asking about that, though? As the instructions you now shared confirm, the function is supposed to handle *one* word, not multiple. – Pychopath Feb 19 '22 at 23:28
  • @Pychopath I am new to coding and Python, I am trying to learn and experiment with writing codes. Any input from anyone kind enough to help will add to my learning curve. – Hemant Feb 20 '22 at 20:04