Module 6: The Grid Object
This commit is contained in:
parent
0f86a88375
commit
c685a78b09
60
a.c
60
a.c
@ -6,26 +6,50 @@ using namespace std;
|
||||
|
||||
// For compiling C++ code
|
||||
// g++ a.c -o a
|
||||
struct Grid
|
||||
{
|
||||
Grid(string n) {
|
||||
name = n;
|
||||
}
|
||||
int rows() const { return lines.size(); }
|
||||
int cols() const {
|
||||
if (lines.empty()) {
|
||||
return 0;
|
||||
} else {
|
||||
return lines[0].size(); }
|
||||
}
|
||||
void Load() {
|
||||
lines.push_back("DOG....");
|
||||
lines.push_back("---....");
|
||||
lines.push_back("----...");
|
||||
lines.push_back("-------");
|
||||
lines.push_back("...----");
|
||||
lines.push_back("....---");
|
||||
lines.push_back("....CAT");
|
||||
}
|
||||
void Check() const {
|
||||
for (string s : lines)
|
||||
{
|
||||
assert(s.size() == cols());
|
||||
}
|
||||
}
|
||||
void Print() const {
|
||||
cout << "Grid: " << name
|
||||
<< " (rows=" << rows()
|
||||
<< ", cols=" << cols() << ")\n";
|
||||
for (string s : lines) {
|
||||
cout << " " << s << "\n";
|
||||
}
|
||||
}
|
||||
string name; // string are initialized empty
|
||||
vector<string> lines;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<string> grid;
|
||||
Grid grid("MY GRID");
|
||||
|
||||
grid.push_back("DOG....");
|
||||
grid.push_back("---....");
|
||||
grid.push_back("----...");
|
||||
grid.push_back("-------");
|
||||
grid.push_back("...----");
|
||||
grid.push_back("....---");
|
||||
grid.push_back("....CAT");
|
||||
|
||||
int rows = grid.size();
|
||||
int cols = grid[0].size();
|
||||
for (string s : grid)
|
||||
{
|
||||
assert(s.size() == cols);
|
||||
}
|
||||
|
||||
cout << "rows=" << rows << "\n";
|
||||
cout << "cols=" << cols << "\n";
|
||||
grid.Load();
|
||||
grid.Check();
|
||||
grid.Print();
|
||||
}
|
55
a.py
55
a.py
@ -1,18 +1,41 @@
|
||||
class Grid:
|
||||
|
||||
def __init__(self, n):
|
||||
self.name = n
|
||||
self.lines = []
|
||||
|
||||
def rows(self):
|
||||
return len(self.lines)
|
||||
|
||||
def cols(self):
|
||||
if self.lines == []:
|
||||
return 0
|
||||
else:
|
||||
return len(self.lines[0])
|
||||
|
||||
def Load(self):
|
||||
self.lines.append("DOG....")
|
||||
self.lines.append("---....")
|
||||
self.lines.append("----...")
|
||||
self.lines.append("-------")
|
||||
self.lines.append("...----")
|
||||
self.lines.append("....---")
|
||||
self.lines.append("....CAT")
|
||||
|
||||
def Check(self):
|
||||
for s in self.lines:
|
||||
assert len(s) == self.cols()
|
||||
|
||||
def Print(self):
|
||||
print(f"Grid: {self.name} "
|
||||
f"(rows={self.rows()},"
|
||||
f" cols={self.cols()})")
|
||||
for s in self.lines:
|
||||
print(f" {''.join(s)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
grid = []
|
||||
grid = Grid("MY GRID")
|
||||
|
||||
grid.append("DOG....")
|
||||
grid.append("---....")
|
||||
grid.append("----...")
|
||||
grid.append("-------")
|
||||
grid.append("...----")
|
||||
grid.append("....---")
|
||||
grid.append("....CAT")
|
||||
|
||||
rows = len(grid)
|
||||
cols = len(grid[0])
|
||||
for s in grid:
|
||||
assert(len(s) == cols)
|
||||
|
||||
print(f"rows={rows}")
|
||||
print(f"cols={cols}")
|
||||
grid.Load()
|
||||
grid.Check()
|
||||
grid.Print()
|
Loading…
x
Reference in New Issue
Block a user