Algorithms Extra Practice (Optional)
Here are some practice problems that will help you prepare for our upcoming evaluation(s). They will not be evaluated, but we will go through solutions together.
Problem #1
Create an algorithm that will go through a list of words, and print out every word that contains the letter x.
You can test it out with this list:
Problem #2
Create an algorithm that will go through a list, and print out all values that are double the index they are at in the list. You can test it out with this list:
The numbers it should print are 4 (since it is at index 2, half of 4) and 8 (since it is at index 4, half of 8).
Problem #3
Create an algorithm that will print a grid of asterisks (the * symbol) containing 10 rows and 8 columns,
with the following exceptions:
- Any position where the row and the column are equal (for example, row 3, column 3), the
$symbol should be printed instead. - At row 2, column 6, print the
%symbol
Solutions
# Problem 1
words = ['cat', 'mouse', 'fox', 'coyote', 'lynx']
for word in words:
if word.count("x") > 0:
print(word)
# Problem 2
nums = [7, 3, 4, 9, 8]
for i in range(len(nums)):
if i * 2 == nums[i]:
print(nums[i])
# Problem 3
for row in range(10):
for col in range(8):
if row == col:
print("$", end="")
elif row == 2 and col == 6:
print("%", end="")
else:
print("*", end="")
print()