CREATING EXOTIC STRUCTURE LISTS IN PYTHON

Here I present very simple Python script that creates lists with very complicated inner structure.
You may use this script for free and make any changes you want.

 
#
#  exotic_list.py - create exotic structure list for tests.
#
#  Project: Udacity search engine final exam
#  Author: S.Zabinskis
#
#  April, 2012
#

import random


# is_list from homework6
def is_list(p):
    return isinstance(p, list)


# randomly choose position in list
# if o is not list or is empty - return None
def choose_element(o):
    if is_list(o) and len(o)>0:
        pos=random.randint(0,len(o)-1)
        if is_list(o[pos]):
            return o[pos]
    return None
        


# append number/string/list to the list
def append_element(o):
    # append list or scalar to the list with equal probability
    if random.random() < 0.5:
        o.append([])
    else:
        # choose between number [0..9] or string with equal probability
        if random.random() < 0.5:
            o.append(random.randint(0,9))
        else:
            o.append('x')
    return
    


# nend - number of operations to create list
# p_depth - probability of going "deeper"
# returns list

def create_exotic_list(nend, p_depth):
    random.seed()
    # begin with empty list
    o = []
    # create another reference
    oc=o
    while nend>0:
        succeeded=0
        if random.random() < p_depth:
            # randomly choose element
            occ=choose_element(oc)
            if is_list(occ):
                # go deeper
                oc=occ
                succeeded=1
            else:
                # if list doesn't contain any list, we randomly append list to it
                list_exists=False;
                for e in oc:
                    if is_list(e):
                        list_exists=True
                        break
                if not list_exists:
                   oc.append([])
        else:
            if is_list(oc):
                append_element(oc)
                succeeded=1
        # decrement iterations counter only in case 
        # if some operation was really performed on list
        nend-=succeeded
    return o

To create list call function create_exotic_list. The first parameter is for number of operations used to create elements.
The second parameter is for probability that controls "depth" of list. Largest probability makes list more "deep". Smallest probability creates more "traditional shape" list.
Here are some trials with "depth factor" increasing from 0.1 to 0.9.

 
>>> o=create_exotic_list(20, 0.1)
>>> o
[[[], 3, 3, ['x', []], 4, 'x', 'x'], 4, 3, 'x', 4, [], [], 'x', []]
>>> o=create_exotic_list(20, 0.2)
>>> o
[[], 4, 4, 'x', 'x', 6, 'x', [], 'x', 1, 7, 'x', 8, 3, [], [], [], 'x', [], []]
>>> o=create_exotic_list(20, 0.3)
>>> o
['x', ['x', ['x', [[[[[[], 'x', 7]]]], []]], 'x']]
>>> o=create_exotic_list(20, 0.4)
>>> o
[[[], [[[3, [], [], [[[[]]]], 'x', []]]], 3]]
>>> o=create_exotic_list(20, 0.5)
>>> o
[[7, [[[], 'x', 8, 'x', [], 'x', [], 'x', []], 5, [], 'x', 7], 'x'], 'x', 6]
>>> o=create_exotic_list(20, 0.6)
>>> o
[[[[[4, [[[[]]], 0], []], 'x', 2]], 'x'], 'x', [], [], []]
>>> o=create_exotic_list(20, 0.7)
>>> o
[[[['x', [['x', [], 'x', 4, [[[[3, [], []]]]]]]]], [], []], []]
>>> o=create_exotic_list(20, 0.8)
>>> o
[[[[[[[[[[[[]]]]]], 'x', 6, []], 'x'], 'x', 'x'], []]]]
>>> o=create_exotic_list(20, 0.9)
>>> o
[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]], [], []]]]]]
>>>