HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Merges given list of integer entries acccordingly

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
acccordinglymergeslistgivenintegerentries

Problem

I am developing a 2048 program in Python. As a part of that project, I am working on merging method that merges the given integer entries.

I am interested in some suggestion to improve my code.

def creating_resulting_list(length_line):
    """
    Function that create a resulting_list with equal number of \
    zeros as length of line
    """
    from itertools import repeat
    return list(repeat(0, length_line))

#BEGIN Test
#print creating_resulting_list(2) == [0, 0]
#print creating_resulting_list(0) == []
#END Test

def moving_non_zeros(line, resulting_list):
    """
    Function that moves non zero entries in line \
    to left most in resulting_list
    """
    if not all(line):
        index_resulting_list = 0
        for dummy_value in line:
            if dummy_value:
                resulting_list[index_resulting_list] = dummy_value
                index_resulting_list += 1
    else:
        resulting_list = line[:]
    return resulting_list 

#BEGIN Test
#result_list = moving_non_zeros([4, 0, 4, 4], creating_resulting_list(4)) 

#result_list = moving_non_zeros([16, 16, 4, 4], creating_resulting_list(4)) 
#END Test

def locating_same_values(result_list):
    """
    Function that locates and merges the same values
    """
    for dummy_index, dummy_value in enumerate(result_list):
        if dummy_index+1  2:
                return moving_non_zeros(result_list, creating_resulting_list(len(result_list)))
    return result_list

#BEGIN Test

print locating_same_values([16, 16, 4, 4])
print locating_same_values([2, 2, 2, 2, 2])
#END Test

def merge(line):
    """
    Function that merges a single row or column in 2048.
    """
    result_list = creating_resulting_list(len(line))
    result_list = moving_non_zeros(line, result_list)
    return locating_same_values(result_list)

Solution

I'm just going to review creating_resulting_list.

def creating_resulting_list(length_line):
    """
    Function that create a resulting_list with equal number of \
    zeros as length of line
    """
    from itertools import repeat
    return list(repeat(0, length_line))

#BEGIN Test
#print creating_resulting_list(2) == [0, 0]
#print creating_resulting_list(0) == []
#END Test


-
There's no need to use backslashes at the ends of lines inside a triple-quoted string.

-
The name creating_resulting_list doesn't help the reader understand what it does. What is a "resulting list" anyway? What this function does is to create a list of zeros. So a better name would be zeros_list or just zeros (see for example numpy.zeros, which is similar).

-
The argument is called length_line. But this is too specific: nothing in this function cares about lines. By avoiding dependencies like this, you can make code more self-contained and easier to understand in isolation. So I would call this argument just length.

-
Since the import statement is inside this function, it gets run every time the function is called. If you moved this statement to the top level of your module, then it would be run only once.

-
The test cases should be made automatically runnable, using the unittest or doctest modules. I'll show below how to do it using the latter.

-
The sequence documentation says:


s n or n s    equivalent to adding s to itself n times

so you don't need itertools.repeat, you can just multiply.

Revised code with doctests:

def zeros(length):
    """Create and return a list of zeros with the given length.

    >>> zeros(2)
    [0, 0]
    >>> zeros(0)
    []

    """
    return [0] * length


The doctests can be run using python -m doctest program.py.

Code Snippets

def creating_resulting_list(length_line):
    """
    Function that create a resulting_list with equal number of \
    zeros as length of line
    """
    from itertools import repeat
    return list(repeat(0, length_line))

#BEGIN Test
#print creating_resulting_list(2) == [0, 0]
#print creating_resulting_list(0) == []
#END Test
def zeros(length):
    """Create and return a list of zeros with the given length.

    >>> zeros(2)
    [0, 0]
    >>> zeros(0)
    []

    """
    return [0] * length

Context

StackExchange Code Review Q#116332, answer score: 8

Revisions (0)

No revisions yet.