diff --git a/a.c b/a.c index 53f2c14..79d48ab 100644 --- a/a.c +++ b/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 lines; +}; int main() { - vector 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(); } \ No newline at end of file diff --git a/a.py b/a.py index 74cacde..d37a58f 100644 --- a/a.py +++ b/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}") \ No newline at end of file + grid.Load() + grid.Check() + grid.Print() \ No newline at end of file