Practical Programming
An Introduction to Computer Science
Using Python 3
(Pragmatic Programmers)
(Pragmatic Bookshelf)
Paul Gries (著) Jennifer Campbell (著)
Jason Montojo (著) Lynn Beighley (編集)
開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 14(Object-Oriented Programming)、14.8(Exercises) 5-a, b, c, d.を解いてみる。
14.8(Exercises) 5-a, b, c, d.
コード(BBEdit)
sample5.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import math
# a.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
# b.
class LineSegment:
def __init__(self, start, stop):
self.start = start
self.stop = stop
# c.
@property
def slope(self):
return (self.stop.y - self.start.x) / (self.stop.x - self.start.y)
# d.
@property
def length(self):
return math.sqrt((self.stop.x - self.start.x) ** 2 +
(self.stop.y - self.start.y) ** 2)
if __name__ == '__main__':
segment = LineSegment(Point(1, 1), Point(3, 2))
print(segment.slope)
print(segment.length)
入出力結果(Terminal, IPython)
$ ./sample5.py 0.5 2.23606797749979 $
0 コメント:
コメントを投稿