Melden

Teilen

•0 gefällt mir•14 views

1. If you are in a city and have to walk from where you are, having planar coordinates (0, 0), to a destination having planar coordinates (x, y), then there are two ways of computing the distance you have to travel. One is called the euclidean distance, r1 = sqrt( x^2 + y^2) Note that this distance might require you to cut across lawns and through buildings. There is another way to measure distance called the taxi–cab distance, r2 = |x| + |y| which is the distance a taxi–cab would have to travel along the city blocks. Write a program which reads the destination coordinates, (x, y), and calls a single function, distance, which computes both the euclidean distance and the taxi–cab distance. The main program (not the function) must then print both distances to the screen. Solution import math #importing math to use sqrt function #distance function def distance(x, y): euclidean_distance = math.sqrt(x**2 + y**2) #calculating euclidean_distance taxi_cab_distance = abs(x) + abs(y) #calculating taxi_cab_distance return euclidean_distance, taxi_cab_distance #returning euclidean_distance and taxi_cab_distance coord = input(\"Enter the coordinates: \") #taking input x and y as a tuple x = coord[0] y = coord[1] euclidean_distance, taxi_cab_distance = distance(x, y) #calling the function #print statements print \"Euclidean Distance: \", euclidean_distance print \"Taxi Cab Distance: \", taxi_cab_distance.

•0 gefällt mir•14 views

Melden

Teilen

1. If you are in a city and have to walk from where you are, having planar coordinates (0, 0), to a destination having planar coordinates (x, y), then there are two ways of computing the distance you have to travel. One is called the euclidean distance, r1 = sqrt( x^2 + y^2) Note that this distance might require you to cut across lawns and through buildings. There is another way to measure distance called the taxi–cab distance, r2 = |x| + |y| which is the distance a taxi–cab would have to travel along the city blocks. Write a program which reads the destination coordinates, (x, y), and calls a single function, distance, which computes both the euclidean distance and the taxi–cab distance. The main program (not the function) must then print both distances to the screen. Solution import math #importing math to use sqrt function #distance function def distance(x, y): euclidean_distance = math.sqrt(x**2 + y**2) #calculating euclidean_distance taxi_cab_distance = abs(x) + abs(y) #calculating taxi_cab_distance return euclidean_distance, taxi_cab_distance #returning euclidean_distance and taxi_cab_distance coord = input(\"Enter the coordinates: \") #taking input x and y as a tuple x = coord[0] y = coord[1] euclidean_distance, taxi_cab_distance = distance(x, y) #calling the function #print statements print \"Euclidean Distance: \", euclidean_distance print \"Taxi Cab Distance: \", taxi_cab_distance.

- 1. 1. If you are in a city and have to walk from where you are, having planar coordinates (0, 0), to a destination having planar coordinates (x, y), then there are two ways of computing the distance you have to travel. One is called the euclidean distance, r1 = sqrt( x^2 + y^2) Note that this distance might require you to cut across lawns and through buildings. There is another way to measure distance called the taxi–cab distance, r2 = |x| + |y| which is the distance a taxi–cab would have to travel along the city blocks. Write a program which reads the destination coordinates, (x, y), and calls a single function, distance, which computes both the euclidean distance and the taxi–cab distance. The main program (not the function) must then print both distances to the screen. Solution import math #importing math to use sqrt function #distance function def distance(x, y): euclidean_distance = math.sqrt(x**2 + y**2) #calculating euclidean_distance taxi_cab_distance = abs(x) + abs(y) #calculating taxi_cab_distance return euclidean_distance, taxi_cab_distance #returning euclidean_distance and taxi_cab_distance coord = input("Enter the coordinates: ") #taking input x and y as a tuple x = coord[0] y = coord[1] euclidean_distance, taxi_cab_distance = distance(x, y) #calling the function #print statements print "Euclidean Distance: ", euclidean_distance print "Taxi Cab Distance: ", taxi_cab_distance