0

I need to write the body of a Python method that does the following:

1)takes a list, where list[0] is a string and list[1] is either a list which looks the same or None

2)print every string of the list

I have to use the while loop and not use list comprehension or flatten.

def pick_cherries_iter(field):
    """"e.g.
    >>> cherry_field = ['cherry1', ['cherry2', ['cherry3', ['cherry4', ['Yay!!!', None]]]]]
    >>> pick_cherries_iter(cherry_field)
    cherry1
    cherry2
    cherry3
    cherry4
    Yay!!!"""

    _______________________
    _______________________
    _______________________
    _______________________
    while _________________:
        _______________________
        _______________________
        _______________________

I know that for the example above I can print cheery1 if I print cherry_field[0] or cherry1 for cherry_field[1][0] or cherry2 for cherry_filed[1][1][0] etc, however I am not sure how to go through these elements using the while loop.

2 Answers2

0

I would do this recursively because you have no way of knowing if an element is a list or not.

#!/usr/bin/python -E

cherry_field = ['cherry1', ['cherry2', ['cherry3', ['cherry4', ['Yay!!!', None]]]]]
def print_list(field):
    i = 0
    list_length = len(field)
    while i < list_length:
     if field[i] is not None and type(field[i]) is not list:
        print(field[i])
     else:
        if field[i] is not None:
           print_list(field[i])
     i += 1
     if i < list_length and type(field[i]) is list:
        print_list(field[i])
        i += 1


def pick_cherries(field):
    if type(field) is list:
       print_list(field)

pick_cherries(cherry_field)
Lulz
  • 1
  • 1
  • Does your answer include list comprehension? – M.Papapetros Nov 19 '18 at 14:25
  • I just tested on a variety of nested lists, made a couple minor changes, and it seems to work on everything so far. That's the advantage of recursion. – Lulz Nov 19 '18 at 14:36
  • For example, it can also do this list: cherry_field = ['cherry1', ['cherry2', ['cherry3', ['cherry4', ['Yay!!!', None]]]],'cherry5'] – Lulz Nov 19 '18 at 14:41
  • Your answer using recursion is just fine, my question is whether you are using list coprehension or not. – M.Papapetros Nov 19 '18 at 15:32
  • Umm, no. Referring back to the OP's question, I do not see that requirement. – Lulz Nov 19 '18 at 19:51
0

I think this should work for you. Please check it.

Using While Loop:

def pick_cherry(field):
    """"e.g.
    >>> cherry_field = ['cherry1', ['cherry2', ['cherry3', ['cherry4', ['Yay!!!',None]]]]]
    >>> pick_cherry(cherry_field)
    cherry1
    cherry2
    cherry3
    cherry4
    Yay!!!"""

    while field[1] != None:
        temp = field[0]
        print temp
        field = field[1]
    print field[0]

Using Flatten(and Recursion):

flatten_field = []

def pick_cherry(field):
    if field[1] != None:
        flatten_field.append(field[0])
        pick_cherry(field[1])
    else:
        flatten_field.append(field[0])

def flatten_func(field):
    """"e.g.
    >>> cherry_field = ['cherry1', ['cherry2', ['cherry3', ['cherry4', ['Yay!!!',None]]]]]
    >>> flatten_func(cherry_field)
    cherry1
    cherry2
    cherry3
    cherry4
    Yay!!!"""

    pick_cherry(field)

    for item in flatten_field:
        print item
Dinesh Suthar
  • 148
  • 2
  • 12