Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list_reduction
#1
"""
Write a function called list_reduction, that takes a single argument - a list
of strings, L. The function must:
  1. Transform the list, *in place* (without returning a new list), so that:
    1.1. Starting from the beginning of the list, remove strings which are not
    alphanumeric (i.e. strings that have characters other than digits and
    alphabet lettrs)
    1.2. The transformed list must contain at least two elements, even if they
    are not alphanumeric
  2. The function must use a while loop
  3. The function documentation should read:
    Remove non-alphanumeric from a list, while keeping at least 2 strings

Example:
L = ['<', 'a', 'href', '<',]
list_reduction(L) -> None
# New value of L -> ['a', 'href']


L = ['Hello', '$', '?' ,'!']
list_reduction(L) -> None
# New value of L -> ['Hello', '!']

L = ['More', 'Than', 'Words']
list_reduction(L) -> None
# New value of L -> ['More', 'Than', 'Words']
def list_reduction(*args):
    """ Remove non-alphanumeric from a list, while keeping at least 2 stings"""
    L = args[0]
    M = len(L)
    z = []
    for i in range(M):
        x = str(L[i])
        y = x.isalnum()
        if y == True:
            z.append(x)
    n = len(z)
    while (n < 2):
        z.append(x)
    print z

k = [ '>', 'ab', '!' ]
list_reduction(k)
when i'm executing this... I'm getting a memory error: what am i doing wrong
$ python list_reduction.py
Error:
Traceback (most recent call last):   File "list_reduction.py", line 47, in <module>     list_reduction(k)   File "list_reduction.py", line 43, in list_reduction     z.append(x) MemoryError

this is what i was missing in the while loop i guess...
n+=1
Reply
#2
The value of n is not altered on the while loop so z is appended infinitely until all memory is used up.
Reply
#3
I'm going to say this on every thread where you use *args and shouldn't be, so that others who come across this code are less likely to accidentally adopt a bad practice:

You shouldn't be writing code like
def list_reduction(*args):
   """ Remove non-alphanumeric from a list, while keeping at least 2 stings"""
   L = args[0]
   M = len(L)
with *args and instead should use named parameters, as you've been told several times, and this would be the better way to do it
def list_reduction(L):
   """ Remove non-alphanumeric from a list, while keeping at least 2 stings"""
   M = len(L)
Then you have an issue with single-letter variable names, but that's a secondary issue. Stop using *args this way. If you're confused as to why, or have any questions, please ask us, but what you're doing right now is bad.
Reply
#4
Why are you using M at all, anyway? Just iterate over the list:

def something(a_list):
   for item in a_list:
       # do things
But that's besides the point. Your code actually doesn't do what the question asks. You're told the function should return None, which you do, but also that you mutate the list you're passed, which you don't do. The very first instruction is that you change the list in place, instead of creating a new list... and then almost the very first thing your function does is create a new list.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020