Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit 4 Lesson 3 replace contrived examples #409

Open
Dan-TEALS opened this issue Jun 27, 2022 Discussed in #367 · 0 comments
Open

Unit 4 Lesson 3 replace contrived examples #409

Dan-TEALS opened this issue Jun 27, 2022 Discussed in #367 · 0 comments
Assignees

Comments

@Dan-TEALS
Copy link
Contributor

Discussed in #367

Originally posted by jdonwells March 11, 2022
As you may have seen here #365 I don't like contrived examples because it forces us into a position of showing code that isn't the best. I am often reminded of the old, old expression "you can write FORTRAN code in any language." Meaning that you can take an idiosyncrasy from one language to the next as baggage. Each language has a unique style, especially Python. It is tempting to write good Java examples as part of a Python class.

Starting with the Do Now we get caught up with stars. Using loops to create blocks of stars is a problem to me. We have already shown them how to create this code:

def draw_square(n):
    print((' *' * n + '\n') * n)


draw_square(6)

Which doesn't use any loops at all. Hence, it is a contrived example.

But on slide 7 of the pptx file there is an alternative that isn't contrived. There is a problem with the way it is written, but that can be fixed.

I am also going to replace the Lab which asks for the above code with no loops to be written. We should advocate eliminating most nested loops except when it is a natural thing to do. So only a one day lab is needed.

Here is my slide show. https://docs.google.com/presentation/d/1mVnFa5v27qs6DK3X-IzGoEDMf8Q1FBezGKGd-wyGqL4/edit?usp=sharing
Here is my Lab:

Lab 4.03 - Loops

Instructions

In this lab we will be drawing matrices to the console. A matrix will be represented by a list of lists. A list will contain rows. Each row is then represented by a list of individual items one per column.

These matrices could be used for some kind of game. Maybe they are used for some kind of calculations. Either way we must be able to show them in some useful and easy to understand format. That is your assignment.

You will write a single function, show_matrix(matrix) that will draw all 3 example matrices. You may create helper functions to be called by show_matrix(matrix) as needed. The solution may or may not be complex enough to warrant additional functions. You decide.

  1. Copy and paste this starter code into your IDE. The functions make_random_row() and make_random_matrix() both use a new type of for loop to create a list. Write down in your notebook how you think they work. Notice that we are importing a single function from the random library with from import.
from random import randint


def make_random_row(columns):
    return [randint(0, 9) for i in range(columns)]


def make_random_matrix(rows, columns):
    return [make_random_row(columns) for i in range(rows)]


board1 = make_random_matrix(3, 3)  # make 3 examples
board2 = make_random_matrix(2, 5)
board3 = make_random_matrix(6, 6)


def show_matrix(matrix):
    line = "-" * (len(matrix[0]) * 4 + 1)
    print(line)
    # finish this function.


show_matrix(board1)
print()
show_matrix(board2)
print()
show_matrix(board3)
  1. Complete the show_matrix() function.
  2. Produce output similar to the following in the console.
-------------
| 7 | 0 | 3 |
-------------
| 0 | 2 | 0 |
-------------
| 6 | 7 | 5 |
-------------

---------------------
| 3 | 1 | 0 | 4 | 1 |
---------------------
| 5 | 7 | 9 | 9 | 2 |
---------------------

-------------------------
| 6 | 2 | 8 | 2 | 3 | 6 |
-------------------------
| 4 | 7 | 9 | 0 | 7 | 2 |
-------------------------
| 6 | 9 | 8 | 3 | 8 | 2 |
-------------------------
| 9 | 9 | 3 | 7 | 3 | 7 |
-------------------------
| 7 | 0 | 0 | 1 | 3 | 4 |
-------------------------
| 5 | 6 | 3 | 7 | 8 | 6 |
-------------------------
  1. Notice that each item in the matrix is a single digit integer. Look carefully at how there is a single space on either side of the digit in the drawing. Do your best to replicate this output exactly.

An example solution to the above lab:

from random import randint


def make_random_row(columns):
    return [randint(0, 9) for i in range(columns)]


def make_random_matrix(rows, columns):
    return [make_random_row(columns) for i in range(rows)]


board1 = make_random_matrix(3, 3)
board2 = make_random_matrix(2, 5)
board3 = make_random_matrix(6, 6)


def show_matrix(matrix):
    line = "-" * (len(matrix[0]) * 4 + 1)
    print(line)
    for row in matrix:
        for column in row:
            print("| " + str(column) + " ", end="")
        print("|")
        print(line)


show_matrix(board1)
print()
show_matrix(board2)
print()
show_matrix(board3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant