Created
December 14, 2012 09:18
-
-
Save seungwon0/4283961 to your computer and use it in GitHub Desktop.
draws circle using ASCII characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# circle.py - draws circle using ASCII characters | |
# | |
# http://kldp.org/node/135436 | |
# | |
# Seungwon Jeong <[email protected]> | |
# | |
# Copyright (C) 2012 by Seungwon Jeong | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, but | |
# WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
# General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see | |
# <http://www.gnu.org/licenses/>. | |
import sys | |
def circle(r): | |
tolerance = r * 0.8 | |
rows = list() | |
for y in range(-r, r + 1): | |
row = list() | |
for x in range(-r, r + 1): | |
value = x * x + y * y - r * r | |
if value >= -tolerance and value <= tolerance: | |
row.append('*') | |
else: | |
row.append('.') | |
rows.append(''.join(row)) | |
return "\n".join(rows) | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print 'Usage: %s r' % (sys.argv[0]) | |
sys.exit(2) | |
r = int(sys.argv[1]) | |
print circle(r) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# test_circle.py - tests circle.py | |
# | |
# http://kldp.org/node/135436 | |
# | |
# Seungwon Jeong <[email protected]> | |
# | |
# Copyright (C) 2012 by Seungwon Jeong | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, but | |
# WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
# General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see | |
# <http://www.gnu.org/licenses/>. | |
import unittest | |
import circle | |
class CircleTest(unittest.TestCase): | |
circle1 = '''\ | |
.*. | |
*.* | |
.*.''' | |
circle2 = '''\ | |
.***. | |
*...* | |
*...* | |
*...* | |
.***.''' | |
circle3 = '''\ | |
..***.. | |
.*...*. | |
*.....* | |
*.....* | |
*.....* | |
.*...*. | |
..***..''' | |
circle4 = '''\ | |
...***... | |
.**...**. | |
.*.....*. | |
*.......* | |
*.......* | |
*.......* | |
.*.....*. | |
.**...**. | |
...***...''' | |
circle7 = '''\ | |
.....*****..... | |
...**.....**... | |
..*.........*.. | |
.*...........*. | |
.*...........*. | |
*.............* | |
*.............* | |
*.............* | |
*.............* | |
*.............* | |
.*...........*. | |
.*...........*. | |
..*.........*.. | |
...**.....**... | |
.....*****.....''' | |
def testCircle(self): | |
self.assertEqual(circle.circle(1), self.circle1, 'r = 1') | |
self.assertEqual(circle.circle(2), self.circle2, 'r = 2') | |
self.assertEqual(circle.circle(3), self.circle3, 'r = 3') | |
self.assertEqual(circle.circle(4), self.circle4, 'r = 4') | |
self.assertEqual(circle.circle(7), self.circle7, 'r = 7') | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment