Home > Enterprise >  function that uses a while loop to return characters in a list while also being subject to other con
function that uses a while loop to return characters in a list while also being subject to other con

Time:04-07

To preface, because I’m learning to use while loops, I want to write this function with only if statements, in statements, and while loops. I think I may also need to use break or continue. I don’t want to use for loops or forms of list comprehension.

the function itself should looks like take_last(lst)

It should normally take in a list of words and returns the last value in the list with the first and last characters of it removed.

for instance,

take_last[“Fish”, “Meat”, “Fruit”, “Cake”]

will return “ak” as this is the last word in the list with the characters, “C” and “e” removed. This part of the function is relatively simple. I think it can be done with

return (lst[-1])[slice(1,-1)]

However, it is subject to two different conditions that may make writing the function difficult.

  1. if the first name in the list has less than 3 characters, the function will stop and return “finished”

i.e take_list([“SH”, “Meat”]) will stop at “SH” and instead returns “finished”.

  1. otherwise, if this condition is not fulfilled and the first word has 3 or more characters, the function will continue iterating through the list until it finds another word with less than 3 characters. it will then stop at this point and return the previous character in the list with the first and last character in it removed.

I.e if the input is take_last([“Fish”, “Meat”, “Fruit”, “SH”, “Cake”]) the function breaks at “SH” and instead “rui” is returned. (Fruit without the ‘F

What I am Trying

Although I am not sure how to structure the while loop, I have written the function with the first condition already fulfilled.

def take_last(lst):
    if len(lst[0]) < 3:
        return "finished"
    else:
        return (lst[-1])[slice(1,-1)]

take_last([“Fish”, “Meat”, “Fruit”, “Cake”]) will return “ake”

and take_last([“Fi”, “Meat”, “Fruit”, “Cake”]) will return

“finished”

I know that somewhere in my function I will need an elif statement followed by a while loop but I’m not sure how this would look. Any help would be appreciated.

CodePudding user response:

Here's a simple way to do it.

def take_last(lst):
    i = 0
    while len(lst[i]) >= 3:
        i  = 1
    if i == 0:
        return "finished"
    else:
        return lst[i-1][1:-1]

The while loop repeats as long as the string at index i is at least 3 characters long. So when it's done, i contains the index of the first short string; if there are no short elements, it will contain the length of the list.

If the first string is short, i will be 0, so there's no element before it and we return finished.

Otherwise we return the slice of the element before i. This will be the last element when there are no short elements.

CodePudding user response:

So returning the last word with 1st and 3rd chars removed is the last possible thing we can do. There may be conditions before it which may cause the the function to stop early.

So, now we know what becomes our default return

  return (lst[-1])[slice(1,-1)]

Similarly, the first condition you are checking is correct as well. So that fixes our starting if.

    if len(lst[0]) < 3:
        return "finished"

Now the condition 2. We can tackle this in the else clause. We need to iterate through the list looking for a word less than 3 chars. If we find it, we return the element just before it after truncation .

else:
    i = 1
    while (i < len(lst)):
        if len(lst[i]) < 3:
            return (lst[i-1])[slice(1,-1)]
        i  = 1

Everything put together:

def take_last(lst):
    if len(lst[0]) < 3:
        return "finished"
    else:
        i = 1
        while (i < len(lst)):
            if len(lst[i]) < 3:
                return (lst[i-1])[slice(1,-1)]
            i  = 1
    return (lst[-1])[slice(1,-1)]

CodePudding user response:

You can easily do this with a regular for loop in a way that, to me, feels a lot more natural than doing it with a while loop:

def take_last(lst):
    prev = None
    for val in lst:
        if len(val) < 3:
            break
        prev = val
    if prev is None:
        return "finished"
    else:
        return prev[1:-1]
  • Related