Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import stdio
- import sys
- class Interval:
- """
- Represents a 1-dimensional interval [lbound, rbound].
- """
- def __init__(self, lbound, rbound):
- """
- Constructs a new interval given its lower and upper bounds.
- """
- self._lbound = lbound
- self._rbound = rbound
- def lbound(self):
- """
- Returns the lower bound of the interval.
- """
- return self._lbound
- def rbound(self):
- """
- Returns the upper bound of the interval.
- """
- return self._rbound
- def contains(self, x):
- """
- Returns True if self contains the point x and False otherwise.
- """
- return (self._lbound <= x <= self._rbound)
- def intersects(self, other):
- """
- Returns True if self intersects other and False othewise.
- """
- # test if both bounds are between bounds of self
- return self.contains(other._lbound) or self.contains(other._rbound)
- def __str__(self):
- """
- Returns a string representation of self.
- """
- return '[' + str(self._lbound) + ', ' + str(self._rbound) + ']'
- # Test client [DO NOT EDIT]. Reads a float x from the command line and
- # writes to standard output: all of the intervals from standard input
- # (each defined by a pair of floats) that contain x; and all pairs
- # of intervals from standard input that intersect one another.
- def _main():
- x = float(sys.argv[1])
- intervals = []
- while not stdio.isEmpty():
- lbound = stdio.readFloat()
- rbound = stdio.readFloat()
- intervals += [Interval(lbound, rbound)]
- for i in range(len(intervals)):
- if intervals[i].contains(x):
- stdio.writef('%s contains %f\n', intervals[i], x)
- for i in range(len(intervals)):
- for j in range(i + 1, len(intervals)):
- if intervals[i].intersects(intervals[j]):
- stdio.writef('%s intersects %s\n', intervals[i], intervals[j])
- if __name__ == '__main__':
- _main()
- import math
- import stdio
- import sys
- class Location:
- """
- Represents a location on Earth.
- """
- def __init__(self, lat, lon):
- """
- Constructs a new location given its latitude and longitude values.
- """
- self._lat = lat
- self._lon = lon
- def distanceTo(self, other):
- """
- Returns the great-circle distance between self and other.
- """
- return 111 * math.degrees(math.acos(math.sin(math.radians(self._lat)) *
- math.sin(math.radians(other._lat)) +
- math.cos(math.radians(self._lat)) *
- math.cos(math.radians(other._lat)) *
- math.cos(math.radians(self._lon -
- other._lon))))
- def __str__(self):
- """
- Returns a string representation of self.
- """
- return '(' + str(self._lat) + ', ' + str(self._lon) + ')'
- # Test client [DO NOT EDIT]. Reads floats lat1, lon1, lat2, lon2 from command
- # representing two locations on Earth, constructs two Location objects from
- # them, and writes them along with the great-circle distance between the two.
- def _main():
- lat1, lon1, lat2, lon2 = map(float, sys.argv[1:])
- loc1 = Location(lat1, lon1)
- loc2 = Location(lat2, lon2)
- stdio.writeln('loc1 = ' + str(loc1))
- stdio.writeln('loc2 = ' + str(loc2))
- stdio.writeln('d(loc1, loc2) = ' + str(loc1.distanceTo(loc2)))
- if __name__ == '__main__':
- _main()
- import stdio
- import sys
- # Returns the GCD of p and q, computed using Euclid's algorithm.
- def _gcd(p, q):
- return p if q == 0 else _gcd(q, p % q)
- class Rational:
- """
- Represents a rational number.
- """
- def __init__(self, x, y=1):
- """
- Constructs a new rational given its numerator and denominator.
- """
- d = _gcd(x, y)
- self._x = x
- self._y = y
- def __add__(self, other):
- """
- Returns the sum of self and other.
- """
- x = (self._x * other._y) + (other._x * self._y)
- y = (other._y * self._y)
- return Rational(x, y)
- def __sub__(self, other):
- """
- Returns the difference of self and other.
- """
- x = (self._x * other._y) - (other._x * self._y)
- y = (other._y * self._y)
- return Rational(x, y)
- def __mul__(self, other):
- """
- Returns the product of self and other.
- """
- x = (self._x * other._x)
- y = (self._y * other._y)
- return Rational(x, y)
- def __abs__(self):
- """
- Return the absolute value of self.
- """
- if (self._x / self._y) >= 0:
- return (self._x / self._y)
- else:
- return ((self._x / self._y) * -1)
- def __str__(self):
- """
- Returns a string representation of self.
- """
- a, b = self._x, self._y
- if a == 0 or b == 1:
- return str(a)
- if b < 0:
- a *= -1
- b *= -1
- return str(a) + '/' + str(b)
- # Test client [DO NOT EDIT]. Reads an integer n as command-line argument and
- # writes the value of PI computed using Leibniz series:
- # PI/4 = 1 - 1/3 + 1/5 - 1/7 + ... + (-1)^n/(2n-1).
- def _main():
- n = int(sys.argv[1])
- total = Rational(0)
- sign = Rational(1)
- for i in range(1, n + 1):
- total += sign * Rational(1, 2 * i - 1)
- sign *= Rational(-1)
- stdio.writeln(4.0 * total._x / total._y)
- if __name__ == '__main__':
- _main()
- import stdio
- import sys
- from interval import Interval
- class Rectangle:
- """
- Represents a rectangle as two (x and y) intervals.
- """
- def __init__(self, xint, yint):
- """
- Constructs a new rectangle given the x and y intervals.
- """
- self._xint = xint
- self._yint = yint
- def area(self):
- """
- Returns the area of self.
- """
- l = self._xint.rbound() - self._xint.lbound()
- w = self._yint.rbound() - self._yint.lbound()
- return l * w
- def perimeter(self):
- """
- Returns the perimeter of self.
- """
- l = self._xint.rbound() - self._xint.lbound()
- w = self._yint.rbound() - self._yint.lbound()
- return 2*l + 2*w
- def contains(self, x, y):
- """
- Returns True if self contains the point (x, y) and False otherwise.
- """
- return self._xint.contains(x) and self._yint.contains(y)
- def intersects(self, other):
- """
- Returns True if self intersects other and False othewise.
- """
- x = other._xint
- y = other._yint
- return self._xint.intersects(x) or self._yint.intersects(y)
- def __str__(self):
- """
- Returns a string representation of self.
- """
- a = str(self._xint.lbound())
- b = str(self._xint.rbound())
- c = str(self._yint.lbound())
- d = str(self._yint.rbound())
- return '[' + a + ', ' + b + '] x [' + c + ', ' + d + ']'
- # Test client [DO NOT EDIT]. Reads a floats x and y from the command line and
- # writes to standard output: all of the rectangles from standard input
- # (each defined by two pairs of floats) that contain (x, y); and all pairs
- # of rectangles from standard input that intersect one another.
- def _main():
- x = float(sys.argv[1])
- y = float(sys.argv[2])
- rectangles = []
- while not stdio.isEmpty():
- lbound1 = stdio.readFloat()
- rbound1 = stdio.readFloat()
- lbound2 = stdio.readFloat()
- rbound2 = stdio.readFloat()
- rectangles += [Rectangle(Interval(lbound1, rbound1),
- Interval(lbound2, rbound2))]
- for i in range(len(rectangles)):
- stdio.writef('Area(%s) = %f\n', rectangles[i], rectangles[i].area())
- stdio.writef('Perimeter(%s) = %f\n', rectangles[i],
- rectangles[i].perimeter())
- if rectangles[i].contains(x, y):
- stdio.writef('%s contains (%f, %f)\n', rectangles[i], x, y)
- for i in range(len(rectangles)):
- for j in range(i + 1, len(rectangles)):
- if rectangles[i].intersects(rectangles[j]):
- stdio.writef('%s intersects %s\n',
- rectangles[i], rectangles[j])
- if __name__ == '__main__':
- _main()
- import stdio
- import sys
- class Point:
- """
- Represents a point in 2-dimensional space.
- """
- def __init__(self, x, y):
- """
- Constructs a new point given its x and y coordinates.
- """
- self._x = x
- self._y = y
- def distanceTo(self, other):
- """
- Returns the Euclidean distance between self and other.
- """
- return ((other._x - self._x) ** 2 + (other._y - self._y) ** 2) ** 0.5
- def __str__(self):
- """
- Returns a string representation of self.
- """
- return '(' + str(self._x) + ', ' + str(self._y) + ')'
- # Test client [DO NOT EDIT]. Reads floats x1, y1, x2, y2 from command line,
- # constructs two Point objects from them, and writes them along with the
- # Euclidean distance between the two.
- def _main():
- x1, y1, x2, y2 = map(float, sys.argv[1:])
- p1 = Point(x1, y1)
- p2 = Point(x2, y2)
- stdio.writeln('p1 = ' + str(p1))
- stdio.writeln('p2 = ' + str(p2))
- stdio.writeln('d(p1, p2) = ' + str(p1.distanceTo(p2)))
- if __name__ == '__main__':
- _main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement