Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automate the Boring Stuff With Python, Chapter 4 Exercise

Tags:

python

list

I'm newbie and doing Al Sweigar's book at the moment. In chapter 4's exercise, he asks the following,

Say you have a list of lists where each value in the inner lists is a one-character string, like this:

 grid = [['.', '.', '.', '.', '.', '.'],
         ['.', 'O', 'O', '.', '.', '.'],
         ['O', 'O', 'O', 'O', '.', '.'],
         ['O', 'O', 'O', 'O', 'O', '.'],
         ['.', 'O', 'O', 'O', 'O', 'O'],
         ['O', 'O', 'O', 'O', 'O', '.'],
         ['O', 'O', 'O', 'O', '.', '.'],
         ['.', 'O', 'O', '.', '.', '.'],
         ['.', '.', '.', '.', '.', '.']] 

You can think of grid[x][y] as being the character at the x- and y-coordinates of a “picture” drawn with text characters. The (0, 0) origin will be in the upper-left corner, the x-coordinates increase going right, and w the y-coordinates increase going down. Copy the previous grid value, and write code that uses it to print the image.

..OO.OO.. 
.OOOOOOO. 
.OOOOOOO. 
..OOOOO.. 
...OOO... 
....O....

So I have written the code and it does what he asks for but I think its very poorly written and I wanted to ask you how can I improve it. My code,

grid = [['.', '.', '.', '.', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['.', 'O', 'O', 'O', 'O', 'O'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]

newString = ''

for i in range(len(grid)):
    newString += str(grid[i][0])

newString1 = '\n'
for i in range(len(grid)):
    newString1 += str(grid[i][1])

newString2 = '\n'
for i in range(len(grid)):
    newString2 += str(grid[i][2])

newString3 = '\n'
for i in range(len(grid)):
    newString3 += str(grid[i][3])

newString4 = '\n'
for i in range(len(grid)):
    newString4 += str(grid[i][4])

newString5 = '\n'
for i in range(len(grid)):
    newString5 += str(grid[i][5])

print(newString+newString1+newString2+newString3+newString4+newString5)

Output of program:

..OO.OO..
.OOOOOOO.
.OOOOOOO.
..OOOOO..
...OOO...
....O....
like image 443
Donj10 Avatar asked May 24 '15 14:05

Donj10


People also ask

What does automate the boring stuff with Python teach?

Practical Programming for Total Beginners In Automate the Boring Stuff with Python, you'll learn how to use Python to write programs that do in minutes what would take you hours to do by hand - no prior programming experience required.

Is automate the boring stuff with Python good for beginners?

It's a must buy for beginners. This book can take you from having no programming experience whatsoever to being able to read and write Python. The fruits of your hard work: the able to scrape the web, automate things, and make your life easier!

What version of Python is automate the boring stuff?

AUTOMATE THE BORING STUFF WITH PYTHON, 2ND EDITION. For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc.

Why does this expression cause an error How can you fix it I have eaten 99 burritos?

The expression causes an error because 99 is an integer, and only strings can be concatenated to other strings with the + operator. The correct way is I have eaten ' + str(99) + ' burritos.


1 Answers

    grid = [['.', '.', '.', '.', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['.', 'O', 'O', 'O', 'O', 'O'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]

    for i in range(len(grid[0])):
        print()
        for j in range(len(grid)):
            print(grid[j][i], end='')
like image 162
m.b Avatar answered Sep 20 '22 18:09

m.b