• API
• FAQ
• Tools
• Archive
daily pastebin goal
23%
SHARE
TWEET

# Untitled

a guest Dec 9th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import stdio
2. import sys
3.
4.
5. class Interval:
6.     """
7.     Represents a 1-dimensional interval [lbound, rbound].
8.     """
9.
10.     def __init__(self, lbound, rbound):
11.         """
12.         Constructs a new interval given its lower and upper bounds.
13.         """
14.
15.         self._lbound = lbound
16.         self._rbound = rbound
17.
18.     def lbound(self):
19.         """
20.         Returns the lower bound of the interval.
21.         """
22.
23.         return self._lbound
24.
25.     def rbound(self):
26.         """
27.         Returns the upper bound of the interval.
28.         """
29.
30.         return self._rbound
31.
32.     def contains(self, x):
33.         """
34.         Returns True if self contains the point x and False otherwise.
35.         """
36.
37.         return (self._lbound <= x <= self._rbound)
38.
39.     def intersects(self, other):
40.         """
41.         Returns True if self intersects other and False othewise.
42.         """
43. # test if both bounds are between bounds of self
44.
45.         return self.contains(other._lbound) or self.contains(other._rbound)
46.
47.     def __str__(self):
48.         """
49.         Returns a string representation of self.
50.         """
51.
52.         return '[' + str(self._lbound) + ', ' + str(self._rbound) + ']'
53.
54.
55. # Test client [DO NOT EDIT]. Reads a float x from the command line and
56. # writes to standard output: all of the intervals from standard input
57. # (each defined by a pair of floats) that contain x; and all pairs
58. # of intervals from standard input that intersect one another.
59. def _main():
60.     x = float(sys.argv[1])
61.     intervals = []
62.     while not stdio.isEmpty():
65.         intervals += [Interval(lbound, rbound)]
66.     for i in range(len(intervals)):
67.         if intervals[i].contains(x):
68.             stdio.writef('%s contains %f\n', intervals[i], x)
69.     for i in range(len(intervals)):
70.         for j in range(i + 1, len(intervals)):
71.             if intervals[i].intersects(intervals[j]):
72.                 stdio.writef('%s intersects %s\n', intervals[i], intervals[j])
73.
74. if __name__ == '__main__':
75.     _main()
76.
77.
78.
79. import math
80. import stdio
81. import sys
82.
83.
84. class Location:
85.     """
86.     Represents a location on Earth.
87.     """
88.
89.     def __init__(self, lat, lon):
90.         """
91.         Constructs a new location given its latitude and longitude values.
92.         """
93.
94.         self._lat = lat
95.         self._lon = lon
96.
97.     def distanceTo(self, other):
98.         """
99.         Returns the great-circle distance between self and other.
100.         """
101.
102.         return 111 * math.degrees(math.acos(math.sin(math.radians(self._lat)) *
107.                                                         other._lon))))
108.
109.     def __str__(self):
110.         """
111.         Returns a string representation of self.
112.         """
113.
114.         return '(' + str(self._lat) + ', ' + str(self._lon) + ')'
115.
116.
117. # Test client [DO NOT EDIT]. Reads floats lat1, lon1, lat2, lon2 from command
118. # representing two locations on Earth, constructs two Location objects from
119. # them, and writes them along with the great-circle distance between the two.
120. def _main():
121.     lat1, lon1, lat2, lon2 = map(float, sys.argv[1:])
122.     loc1 = Location(lat1, lon1)
123.     loc2 = Location(lat2, lon2)
124.     stdio.writeln('loc1 = ' + str(loc1))
125.     stdio.writeln('loc2 = ' + str(loc2))
126.     stdio.writeln('d(loc1, loc2) = ' + str(loc1.distanceTo(loc2)))
127.
128. if __name__ == '__main__':
129.     _main()
130.
131.
132.
133. import stdio
134. import sys
135.
136.
137. # Returns the GCD of p and q, computed using Euclid's algorithm.
138. def _gcd(p, q):
139.     return p if q == 0 else _gcd(q, p % q)
140.
141.
142. class Rational:
143.     """
144.     Represents a rational number.
145.     """
146.
147.     def __init__(self, x, y=1):
148.         """
149.         Constructs a new rational given its numerator and denominator.
150.         """
151.
152.         d = _gcd(x, y)
153.         self._x = x
154.         self._y = y
155.
157.         """
158.         Returns the sum of self and other.
159.         """
160.         x = (self._x * other._y) + (other._x * self._y)
161.         y = (other._y * self._y)
162.         return Rational(x, y)
163.
164.     def __sub__(self, other):
165.         """
166.         Returns the difference of self and other.
167.         """
168.         x = (self._x * other._y) - (other._x * self._y)
169.         y = (other._y * self._y)
170.         return Rational(x, y)
171.
172.     def __mul__(self, other):
173.         """
174.         Returns the product of self and other.
175.         """
176.         x = (self._x * other._x)
177.         y = (self._y * other._y)
178.         return Rational(x, y)
179.
180.     def __abs__(self):
181.         """
182.         Return the absolute value of self.
183.         """
184.
185.         if (self._x / self._y) >= 0:
186.             return (self._x / self._y)
187.         else:
188.             return ((self._x / self._y) * -1)
189.
190.     def __str__(self):
191.         """
192.         Returns a string representation of self.
193.         """
194.
195.         a, b = self._x, self._y
196.         if a == 0 or b == 1:
197.             return str(a)
198.         if b < 0:
199.             a *= -1
200.             b *= -1
201.         return str(a) + '/' + str(b)
202.
203.
204. # Test client [DO NOT EDIT]. Reads an integer n as command-line argument and
205. # writes the value of PI computed using Leibniz series:
206. #   PI/4 = 1 - 1/3 + 1/5 - 1/7 + ... + (-1)^n/(2n-1).
207. def _main():
208.     n = int(sys.argv[1])
209.     total = Rational(0)
210.     sign = Rational(1)
211.     for i in range(1, n + 1):
212.         total += sign * Rational(1, 2 * i - 1)
213.         sign *= Rational(-1)
214.     stdio.writeln(4.0 * total._x / total._y)
215.
216. if __name__ == '__main__':
217.     _main()
218.
219.
220.
221. import stdio
222. import sys
223. from interval import Interval
224.
225.
226. class Rectangle:
227.     """
228.     Represents a rectangle as two (x and y) intervals.
229.     """
230.
231.     def __init__(self, xint, yint):
232.         """
233.         Constructs a new rectangle given the x and y intervals.
234.         """
235.
236.         self._xint = xint
237.         self._yint = yint
238.
239.     def area(self):
240.         """
241.         Returns the area of self.
242.         """
243.
244.         l = self._xint.rbound() - self._xint.lbound()
245.         w = self._yint.rbound() - self._yint.lbound()
246.         return l * w
247.
248.     def perimeter(self):
249.         """
250.         Returns the perimeter of self.
251.         """
252.
253.         l = self._xint.rbound() - self._xint.lbound()
254.         w = self._yint.rbound() - self._yint.lbound()
255.         return 2*l + 2*w
256.
257.     def contains(self, x, y):
258.         """
259.         Returns True if self contains the point (x, y) and False otherwise.
260.         """
261.
262.         return self._xint.contains(x) and self._yint.contains(y)
263.
264.     def intersects(self, other):
265.         """
266.         Returns True if self intersects other and False othewise.
267.         """
268.
269.         x = other._xint
270.         y = other._yint
271.         return self._xint.intersects(x) or self._yint.intersects(y)
272.
273.     def __str__(self):
274.         """
275.         Returns a string representation of self.
276.         """
277.
278.         a = str(self._xint.lbound())
279.         b = str(self._xint.rbound())
280.         c = str(self._yint.lbound())
281.         d = str(self._yint.rbound())
282.         return '[' + a + ', ' + b + '] x [' + c + ', ' + d + ']'
283.
284.
285. # Test client [DO NOT EDIT]. Reads a floats x and y from the command line and
286. # writes to standard output: all of the rectangles from standard input
287. # (each defined by two pairs of floats) that contain (x, y); and all pairs
288. # of rectangles from standard input that intersect one another.
289. def _main():
290.     x = float(sys.argv[1])
291.     y = float(sys.argv[2])
292.     rectangles = []
293.     while not stdio.isEmpty():
298.         rectangles += [Rectangle(Interval(lbound1, rbound1),
299.                                  Interval(lbound2, rbound2))]
300.     for i in range(len(rectangles)):
301.         stdio.writef('Area(%s) = %f\n', rectangles[i], rectangles[i].area())
302.         stdio.writef('Perimeter(%s) = %f\n', rectangles[i],
303.                      rectangles[i].perimeter())
304.         if rectangles[i].contains(x, y):
305.             stdio.writef('%s contains (%f, %f)\n', rectangles[i], x, y)
306.     for i in range(len(rectangles)):
307.         for j in range(i + 1, len(rectangles)):
308.             if rectangles[i].intersects(rectangles[j]):
309.                 stdio.writef('%s intersects %s\n',
310.                              rectangles[i], rectangles[j])
311.
312. if __name__ == '__main__':
313.     _main()
314.
315.
316.
317. import stdio
318. import sys
319.
320.
321. class Point:
322.     """
323.     Represents a point in 2-dimensional space.
324.     """
325.
326.     def __init__(self, x, y):
327.         """
328.         Constructs a new point given its x and y coordinates.
329.         """
330.
331.         self._x = x
332.         self._y = y
333.
334.     def distanceTo(self, other):
335.         """
336.         Returns the Euclidean distance between self and other.
337.         """
338.
339.         return ((other._x - self._x) ** 2 + (other._y - self._y) ** 2) ** 0.5
340.
341.     def __str__(self):
342.         """
343.         Returns a string representation of self.
344.         """
345.
346.         return '(' + str(self._x) + ', ' + str(self._y) + ')'
347.
348.
349. # Test client [DO NOT EDIT]. Reads floats x1, y1, x2, y2 from command line,
350. # constructs two Point objects from them, and writes them along with the
351. # Euclidean distance between the two.
352. def _main():
353.     x1, y1, x2, y2 = map(float, sys.argv[1:])
354.     p1 = Point(x1, y1)
355.     p2 = Point(x2, y2)
356.     stdio.writeln('p1 = ' + str(p1))
357.     stdio.writeln('p2 = ' + str(p2))
358.     stdio.writeln('d(p1, p2) = ' + str(p1.distanceTo(p2)))
359.
360. if __name__ == '__main__':
361.     _main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top