TEALSK12 / 2nd-semester-introduction-to-computer-science

Microsoft TEALS Program - 2nd semester follow-up to the TEALS Intro CS course
https://tealsk12.github.io/2nd-semester-introduction-to-computer-science/
51 stars 72 forks source link

Unit 4 Lesson 3 replace contrived examples #409

Open Dan-TEALS opened 2 years ago

Dan-TEALS commented 2 years ago

Discussed in https://github.com/TEALSK12/2nd-semester-introduction-to-computer-science/discussions/367

Originally posted by **jdonwells** March 11, 2022 As you may have seen here https://github.com/TEALSK12/2nd-semester-introduction-to-computer-science/discussions/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: ```python 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`. ```python 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) ``` 2. Complete the `show_matrix()` function. 3. 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 | ------------------------- ``` 4. 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: ```python 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) ```