'Python challenge - rows and columns count

I'm working on a challenge which basically consist on assigning to a set of rows and columns on a table, a value by a function in python. In this case the character's value to be placed on a specific position on the talbe is the "#". I have been able to make it work, but as you will se on the ouput is that the top which represent the columns list is not totally aligned (top right hand side) to the row line. On the attached screen shot you'll see a more detailed description.

Below is the code I am using where I'have been able to get this far.

def bucle_for1(lista):
    x = 0
    
    for i in lista:
        if x <= 9:
            print(x,"", i)
        else:
             print(x, i)
        x += 1

def crear_mundo(d):
    mundo = []
    columna = []
    
    for x in range(d):
        for x in range(d):
            if x <= 9:
                columna.append(" ")
                if x >= 10:
                    columna.append("")
                else:
                    columna.append(" ")
            else:
                columna.append("")
        mundo.append(columna)
        columna = []
    return mundo

def crear_listadocolumnas(d):
    listadocolumnas = []
    for x in range(d):
        listadocolumnas.append(str(x))      
    return listadocolumnas

def muros(mundo):
    muros = [[0,1],[0,2],[1,1],[1,2],[2,1],[2,2],[2,2],
            [3,1],[3,2],[3,3],[3,4],[3,5],[3,6],[3,7],
            [3,8],[3,9],[3,10],[3,11],[4,1],[4,2],[4,3],
            [4,4],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],
            [4,11],[5,11],[6,11],[6,11],[6,12],[6,13],
            [6,14],[6,15]]
    for x in range(len(muros)):
        f = muros [x][0]
        c = muros [x][1]
        mundo [f][c] = "#"
    return mundo

mundo = crear_mundo(32)
print("  ", crear_listadocolumnas(32))
bucle_for1(muros(mundo))

Code output

I'm now where I wanted to be with the code (thanks to Display name). Now, I was wodering if you could help me on how to look for the width and len of the following according to the same output. What function should I use so I can calculate the width of column 1 and 2 and also to know the len of the same columns 1 and 2?



Solution 1:[1]

Is this what you were looking for? Basically, if x is greater than 9 (I.E. 2 characters long, I add

    def bucle_for1(lista):
    x = 0

    for i in lista:
        if x <= 9:
            print(x,"", i)
        else:
            print(x, i)
        x += 1



def crear_mundo(d):
    mundo = []
    columna = []
    for x in range(d):
        for x in range(d):
            if x <= 9:
                columna.append(" ")
                if x >= 10:
                    columna.append("")
                else:
                    columna.append(" ")
            else:
                columna.append("")
            if x>9:
                columna[x] = "  "
        mundo.append(columna)
        columna = []
    return mundo



def crear_listadocolumnas(d):
    listadocolumnas = []
    for x in range(d):
        listadocolumnas.append(str(x))
    return listadocolumnas


def muros(mundo):
    muros = [[0,1],[0,2],[1,1],[1,2],[2,1],[2,2],[2,2],
             [3,1],[3,2],[3,3],[3,4],[3,5],[3,6],[3,7],
             [3,8],[3,9],[3,10],[3,11],[4,1],[4,2],[4,3],
             [4,4],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],
             [4,11],[5,11],[6,11],[6,11],[6,12],[6,13],
             [6,14],[6,15]]
    for x in range(len(muros)):
        f = muros [x][0]
        c = muros [x][1]
        mundo [f][c] = "#"
        if c>9:
            mundo[f][c] += " "
    return mundo

mundo = crear_mundo(32)
print("  ", crear_listadocolumnas(32))
bucle_for1(muros(mundo))

Add single white space after column 31 You can also use this code if you want lines beyond 31 to be a single white space instead of an empty string.

def crear_mundo(d):
    mundo = []
    columna = []
    for x in range(d):
        for x in range(d):
            if x <= 9:
                columna.append(" ")
                if x >= 10:
                    columna.append("")
                else:
                    columna.append(" ")
            else:
                columna.append(" ")
            if x>9:
                columna[x] += " "
        mundo.append(columna)
        columna = []
    return mundo

def muros(mundo):
        muros = [[0,1],[0,2],[1,1],[1,2],[2,1],[2,2],[2,2],
                 [3,1],[3,2],[3,3],[3,4],[3,5],[3,6],[3,7],
                 [3,8],[3,9],[3,10],[3,11],[4,1],[4,2],[4,3],
                 [4,4],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],
                 [4,11],[5,11],[6,11],[6,11],[6,12],[6,13],
                 [6,14],[6,15]]
        for x in range(len(muros)):
            f = muros [x][0]
            c = muros [x][1]
            mundo [f][c] = "#"
            if c>9:
                mundo[f][c] += " "
        return mundo

Example of code

To Match the column length, use this:

def bucle_for1(lista):
    x = 0

    for i in lista:
        if x <= 9:
            print(x,"", i)
        else:
            print(x, i)
        x += 1



def crear_mundo(d):
    mundo = []
    columna = []
    for x in range(d):
        for x in range(d):
            if x <= 9:
                columna.append(" ")
                if x >= 10:
                    columna.append("")
            else:
                columna.append(" ")
            if x>9:
                columna[x] += " "
        mundo.append(columna)
        columna = []
    return mundo



def crear_listadocolumnas(d):
    listadocolumnas = []
    for x in range(d):
        listadocolumnas.append(str(x))
    return listadocolumnas


def muros(mundo):
    muros = [[0,1],[0,2],[1,1],[1,2],[2,1],[2,2],[2,2],
             [3,1],[3,2],[3,3],[3,4],[3,5],[3,6],[3,7],
             [3,8],[3,9],[3,10],[3,11],[4,1],[4,2],[4,3],
             [4,4],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],
             [4,11],[5,11],[6,11],[6,11],[6,12],[6,13],
             [6,14],[6,15]]
    for x in range(len(muros)):
        f = muros [x][0]
        c = muros [x][1]
        mundo [f][c] = "#"
        if c>9:
            mundo[f][c] += " "
    return mundo

mundo = crear_mundo(32)
print("  ", crear_listadocolumnas(32))
bucle_for1(muros(mundo))

Example: An Example of matched width

Get character length of the printed rows

def bucle_for1(lista):
    x = 0

    for i in lista:
        y=0
        if x <= 9: y=1
        print(len(str(str(x)+""+ str(i))) + (y))
        if x <= 9:
            print(x,"", i)
        else:
            print(x, i)
        x += 1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1