CMU 15-112: Fundamentals of Programming and Computer Science
Extra Practice for Unit 5 (Due never)
- These problems will help you prepare for midterm1. They are
optional and you are encouraged to collaborate when working on them.
-
You may also wish to see
extra-practice5-ct-and-roc.html.
-
This unit we are not providing a starter file. You should now be
able to make your own (though of course we are happy to help
you get started!).
- Do not use recursion this week.
- Do not hardcode the test cases in your solutions.
- You may assumed 2d lists are rectangular unless explicitly stated otherwise.
- isRectangular(L)
Write the function isRectangular(L) that takes a possibly-2d (or possibly not)
list L and returns True if the list is in fact 2d, and if it is also
rectangular, so each row has the same number of elements. Return False
otherwise.
- hasNoPrimes(L)
Write the function hasNoPrimes(L) that takes a 2d list L of integers,
and returns True if L does not contain any primes, and False otherwise.
- hasDuplicates(L)
Write the function hasDuplicates(L) that takes a 2d list L of arbitrary values,
and returns True if L contains any duplicate values (that is, if any two
values in L are equal to each other), and False otherwise.
- fixMostlyMagicSquare(L)
In this week's writing session, we wrote isMostlyMagicSquare(L). Here,
say we have a mostly magic square L, but then we modify L by changing
exactly one value in L so that it no longer is a mostly magic square.
For this exercise, we assume we have just such a list L, and your task
is to find and fix that change. So, given the list L, return a new list
M such that M is the same as L, only with exactly one value changed,
and M is a mostly magic square.
- makeMagicSquare(n)
Write the function makeMagicSquare(n) that takes a positive odd integer
n and returns an nxn magic square by following
De La Loubere's Method as described
here.
If n is not a positive odd integer, return None.
- isLatinSquare(board)
Write the function isLatinSquare(a) that takes a 2d list and returns
True if it is a
Latin square
and False otherwise.
- matrixMultiply(m1, m2)
Write the function matrixMultiply(m1, m2) that takes two 2d lists (that
we will consider to be matrices) and returns a
new 2d list that is the result of
multiplying the two matrices. Return None if the two matrices
cannot be multiplied for any reason.
- isKnightsTour(board)
Background:
A "knight's tour"
in chess is a sequence of legal knight moves such that the knight
visits every square exactly once. We can represent a (supposed)
knight's tour as an NxN list of the integers from 1 to N2 listing
the positions in order that the knight occupied on the tour. If it is
a legal knight's tour, then all the numbers from 1 to N2 will be
included and each move from k to (k+1) will be a legal knight's move.
With this in mind, write the function isKnightsTour(board) that takes such a
2d list of integers and returns True if it represents a legal knight's tour
and False otherwise.
- nQueensChecker(board)
Background: The "N Queens" problem asks if we can place N queens
on an NxN chessboard such that no two queens are attacking each other.
For most values of N, there are many ways to solve this problem. Here,
you will write the function nQueensChecker(board) that takes a 2d list of
booleans where True indicates a queen is present and False indicates a blank
cell, and returns True if this NxN board contains N queens all of which do
not attack any others, and False otherwise.
- Games, games, games!
Have fun writing your own
console-based 2d board games (human-human mainly, but maybe a simple human-computer game) such as:
- Checkers
- Chess
-
Isola
-
Fox and Hounds
- Backgammon
- Stratego
- Or many, many others...