0

I want to write a python function that takes 2 parameters:

  1. List of words and
  2. Ending letters

I want my function to work in such a way that it modifies the original list of words and removes the words which end with the "ending letters" specified.

For example:

list_words = ["hello", "jello","whatsup","right", "cello", "estello"]
ending = "ello"

my_func(list_words, ending)

This should give the following output:

list_words = ["whatsup","right"]

It should pop off all the strings that end with the ending letters given in the second argument of the function.

I can code this function using the .endswith method but I am not allowed to use it. How else can I do this using a loop?

yowhatsup123
  • 287
  • 1
  • 11
  • 3
    `string.endswith('ello')` is equivalent to `string[-4:] == 'ello'`. – Matthias Fripp Oct 02 '21 at 19:06
  • Not being allowed to use the standard library sounds like an exercise in using other mechanisms. Here's a starting point: can you access parts of a string? – Yann Vernier Oct 02 '21 at 19:07
  • You can use `word[-len(ending):] == ending` insted of `word.endswith(ending)` – Anwarvic Oct 02 '21 at 19:07
  • Programming is all about decomposition: breaking large problems into small pieces. Can you write your own `endswith` function that checks if a string has a particular suffix and returns `True` or `False`? Once you write that then you can tackle the whole list removal deal. – John Kugelman Oct 02 '21 at 19:07
  • @Anwarvic Doesn't always work. – no comment Oct 02 '21 at 19:33

5 Answers5

3

Try:

def my_func(list_words, ending):
    return [word for word in list_words if word[len(word)-len(ending):] != ending]
Gabio
  • 9,126
  • 3
  • 12
  • 32
0

You can easily check for the last4 characters of a string using string[-4:].

So you can use the below code

list_words = ["hello", "jello","whatsup","right", "cello", "estello"]
ending = "ello"

def my_func(wordsArray, endingStr):
    endLen = len(endingStr)
    output = []
    for x in wordsArray:
        if not x[-endLen:] == endingStr:
            output.append(x)
    return output

list_words = my_func(list_words, ending)

You can shorten the function with some list comprehension like this:

def short_func(wordsArray, endingStr):
    endLen = len(endingStr)
    output = [x for x in wordsArray if x[-endLen:] != endingStr]
    return output

list_words = short_func(list_words, ending)
Gangula
  • 5,193
  • 4
  • 30
  • 59
0

It is always better to not modify the existing list you can get a list which doesn't have the words with the ending specified like below. If you want to have it as a function you can have it in a following manner. You can assign the formatted list to list_words again.

def format_list(words, ending):
    new_list = []
    n = len(ending)
    for word in words:
        if len(word) >= n and  n > 0:
            if not word[-n:] == ending:
                new_list.append(word)
        else:
            new_list.append(word)
    return new_list 

list_words = format_list(list_words, ending)
print(list_words)
0
def filter_words(list_words, ending):
    return [*filter(lambda x: x[-len(ending):] != ending , list_words)]
zyrafal
  • 11
  • 3
0

Not allowed to use endswith? Not a problem :-P

def my_func(list_words, ending):
    list_words[:] = [word for word in list_words
                     if not word[::-1].startswith(ending[::-1])]
    return list_words

Loopholes ftw.

(Adapted to your insistence on modifying the given list. You should probably really decide whether to modify or return, though, not do both, which is rather unusual in Python.)

no comment
  • 6,381
  • 4
  • 12
  • 30