Skip to content

Commit 91d1508

Browse files
Neha PeddintiNeha Peddinti
authored andcommitted
Add practice problems/solutions to Ch.9-11
1 parent 0e569f0 commit 91d1508

File tree

14 files changed

+476
-0
lines changed

14 files changed

+476
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Address
3+
4+
Create a 2D list where each row represents a
5+
person and has 3 elements: their name, their age,
6+
and their address.
7+
8+
The entire list should have 4 such entries (4 people),
9+
so it will be a 4x3 list.
10+
11+
Display the name and address of the 2nd person in the list.
12+
13+
Then, display the entire list with the format:
14+
name (age): address
15+
"""
16+
17+
# Insert your code here.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Random Grid
3+
4+
Create a 2D list with 4 rows and a randomly
5+
determined number of columns. The column
6+
number should be a random EVEN number between
7+
2 and 16 (inclusive).
8+
9+
All the even column numbers (including 0) should
10+
be filled with asterisks (*). The odd numbered
11+
columns should be filled with underscores (_).
12+
13+
Display the grid at the end by printing out
14+
elements individually: don't use print(list).
15+
Assume that you don't know the size of the grid
16+
beforehand. In other words, if you wanted to display
17+
the 2D list without knowing the number of rows and
18+
columns in it, how would you code this?
19+
20+
For example, a 4x6 grid would display this:
21+
*_*_*_
22+
*_*_*_
23+
*_*_*_
24+
25+
This might be useful:
26+
print("a")
27+
print("a")
28+
would display the "a"s with newlines:
29+
a
30+
a
31+
32+
print("a", end=" ")
33+
print("a")
34+
changes the end of the first "a" from a newline to a space.
35+
The output is this:
36+
a a
37+
38+
"""
39+
40+
# Insert your code here.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Address
3+
4+
Create a 2D list where each row represents a
5+
person and has 3 elements: their name, their age,
6+
and their address.
7+
8+
The entire list should have 4 such entries (4 people),
9+
so it will be a 4x3 list.
10+
11+
Display the name and address of the 2nd person in the list.
12+
13+
Then, display the entire list with the format:
14+
name (age): address
15+
"""
16+
17+
contacts = [
18+
["Jeremy", 10, "45 Pancake Road"],
19+
["Nicey", 18, "111 Cupcake Street"],
20+
["Hawthorne", 15, "19 Sinister Avenue"],
21+
["Nilah", 14, "Banks of the Nile River"]
22+
]
23+
24+
# 2nd person
25+
print(contacts[1][0] + ": " + contacts[1][2])
26+
print()
27+
28+
# Display the entire list.
29+
for contact in contacts:
30+
print(
31+
contact[0] + " (%d): " % contact[1] + contact[2]
32+
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Random Grid
3+
4+
Create a 2D list with 4 rows and a randomly
5+
determined number of columns. The column
6+
number should be a random EVEN number between
7+
2 and 16 (inclusive).
8+
9+
All the even column numbers (including 0) should
10+
be filled with asterisks (*). The odd numbered
11+
columns should be filled with underscores (_).
12+
13+
Display the grid at the end by printing out
14+
elements individually: don't use print(list).
15+
Assume that you don't know the size of the grid
16+
beforehand. In other words, if you wanted to display
17+
the 2D list without knowing the number of rows and
18+
columns in it, how would you code this?
19+
20+
For example, a 4x6 grid would display this:
21+
*_*_*_
22+
*_*_*_
23+
*_*_*_
24+
25+
This might be useful:
26+
print("a")
27+
print("a")
28+
would display the "a"s with newlines:
29+
a
30+
a
31+
32+
print("a", end=" ")
33+
print("a")
34+
changes the end of the first "a" from a newline to a space.
35+
The output is this:
36+
a a
37+
38+
"""
39+
import random
40+
41+
grid = []
42+
cols = random.randint(1, 8) * 2
43+
44+
# Fill the grid 2D list.
45+
for row in range(4):
46+
grid.append([])
47+
for col in range(cols):
48+
grid[row].append(
49+
"*" if (col % 2 == 0) else "_"
50+
)
51+
52+
# Display the grid without knowing the size beforehand.
53+
for row in range(len(grid)):
54+
for col in range(len(grid[row])):
55+
print(grid[row][col], end="")
56+
print()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Case
3+
4+
Display the string "Apple" in the following formats:
5+
1) normally
6+
2) all uppercase
7+
3) all lowercase
8+
9+
Display the string "mRoWiE" in the same 3 formats.
10+
11+
Ask the user to input a sentence, and display this
12+
input in the same 3 formats.
13+
14+
Do this in AT MOST 8 lines of code.
15+
By the end of the program, 9 lines should have been
16+
displayed (3 formats for each of the 3 strings).
17+
18+
Example of the 3 formats for one string:
19+
Apple
20+
APPLE
21+
apple
22+
"""
23+
24+
# Insert your code here.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Product
3+
4+
Write a function that takes a list
5+
of numbers as input and returns
6+
the product of all the numbers in
7+
the list.
8+
9+
Use it to print the products of the
10+
following sets of numbers:
11+
-1, 5, 3, 2, 8
12+
2.5, 3, 0
13+
4, 3, 7, 10
14+
"""
15+
16+
# Insert your code here.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Rect
3+
4+
Write a function that takes in 2 integer
5+
parameters: length, and width.
6+
7+
The function should print out a rectangle of
8+
asterisks (*) with that length and width.
9+
10+
Example, if the length is 5 and the width is 3,
11+
the function should print:
12+
*****
13+
*****
14+
*****
15+
16+
Useful information:
17+
1) print("a", end="")
18+
removes the newline from the end of the print statement.
19+
2) print("a" * 5) displays "aaaaa".
20+
21+
Use the function to display rectangles with
22+
the following dimensions (with a linebreak
23+
between each one):
24+
2x6
25+
7x4
26+
3x5
27+
28+
"""
29+
30+
# Insert your code here.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Case
3+
4+
Display the string "Apple" in the following formats:
5+
1) normally
6+
2) all uppercase
7+
3) all lowercase
8+
9+
Display the string "mRoWiE" in the same 3 formats.
10+
11+
Ask the user to input a sentence, and display this
12+
input in the same 3 formats.
13+
14+
Do this in AT MOST 8 lines of code.
15+
By the end of the program, 9 lines should have been
16+
displayed (3 formats for each of the 3 strings).
17+
18+
Example of the 3 formats for one string:
19+
Apple
20+
APPLE
21+
apple
22+
"""
23+
24+
25+
# Define a function that prints the 3 formats.
26+
def display(str):
27+
print(str)
28+
print(str.upper())
29+
print(str.lower())
30+
31+
32+
display("Apple")
33+
display("mRoWiE")
34+
display(input("Enter a sentence: "))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Product
3+
4+
Write a function that takes a list
5+
of numbers as input and returns
6+
the product of all the numbers in
7+
the list.
8+
9+
Use it to print the products of the
10+
following sets of numbers:
11+
-1, 5, 3, 2, 8
12+
2.5, 3, 0
13+
4, 3, 7, 10
14+
"""
15+
16+
17+
# Define a product() function with a list parameter.
18+
def product(list):
19+
product = 1
20+
for i in list:
21+
product *= i
22+
return product
23+
24+
25+
# Use the function to display products, where
26+
# each set of numbers is given as a list.
27+
print(product([-1, 5, 3, 2, 8]))
28+
print(product([2.5, 3, 0]))
29+
print(product([4, 3, 7, 10]))
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Rect
3+
4+
Write a function that takes in 2 integer
5+
parameters: length, and width.
6+
7+
The function should print out a rectangle of
8+
asterisks (*) with that length and width.
9+
10+
Example, if the length is 5 and the width is 3,
11+
the function should print:
12+
*****
13+
*****
14+
*****
15+
16+
Useful information:
17+
1) print("a", end="")
18+
removes the newline from the end of the print statement.
19+
2) print("a" * 5) displays "aaaaa".
20+
21+
Use the function to display rectangles with
22+
the following dimensions (with a linebreak
23+
between each one):
24+
2x6
25+
7x4
26+
3x5
27+
28+
"""
29+
30+
31+
# Define a function with length and width parameters.
32+
def draw_rect(length, width):
33+
for row in range(width):
34+
print("*" * length)
35+
print()
36+
37+
38+
# Use the function to draw rectangles of various sizes.
39+
draw_rect(2, 6)
40+
draw_rect(7, 4)
41+
draw_rect(3, 5)

0 commit comments

Comments
 (0)