Question

NEEDS TO BE IN PYTHON: (Central city) Given a set of cities, the central point is...

NEEDS TO BE IN PYTHON:

(Central city)

Given a set of cities, the central point is the city that has the shortest total distance to all other cities. Write a program that prompts the user to enter the number of the cities and the locations of the cities (that is, their coordinates), and finds the central city. Note that the coordinates are entered in one line.

Sample Run

Enter the coordinates of the cities: 2.5 5 5.1 3 1 9 5.4 54 5.5 2.1

The central city is at (2.5, 5)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def distance(cities, i):
    total = 0
    for j in range(len(cities)):
        total += ((cities[i][0] - cities[j][0]) ** 2 + (cities[i][1] - cities[j][1]) ** 2) ** 0.5
    return total


def main():
    cities = []
    points = input("Enter the coordinates of the cities: ").split()
    for i in range(0, len(points), 2):
        cities.append([float(points[i]), float(points[i + 1])])
    central = 0
    for i in range(len(cities)):
        if distance(cities, i) < distance(cities, central):
            central = i
    print("The central city is at (" + str(cities[central][0]) + ", " + str(cities[central][1]) + ")")


main()

Add a comment
Know the answer?
Add Answer to:
NEEDS TO BE IN PYTHON: (Central city) Given a set of cities, the central point is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a...

    NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a point with x- and y-coordinates. The class contains: - Two private data fields x and y that represent the coordinates with get methods. - A constructor that constructs a point with specified coordinates, with default point (0, 0). - A method named distance that returns the distance from this point to another point of the Point type. - A method named isNearBy(p1) that returns...

  • The following table gives the map coordinates and the shipping loads for a set of cities...

    The following table gives the map coordinates and the shipping loads for a set of cities that we wish to connect through a central hub. (3.8) С D E F G City Map Coordinates (x,y) Shipping Load А (5.11) 6 (6.7) 10 16 (8,6) 6 (7.8) 14 (3.2) 11 (2.5) 5 a) For the location of the proposed new central hub, the coordinates should be near (round your response to two decimal places). y-round your response to two decimal places)....

  • In Python 3, Write a program that reads a set of floating-point values. Ask the user...

    In Python 3, Write a program that reads a set of floating-point values. Ask the user to enter the values, then print: The number of values entered. The smallest of the values The largest of the values. The average of the values. The range, that is the difference between the smallest and largest values. If no values were entered, the program should display “No values were entered” The program execution should look like (note: the bold values are sample user...

  • Its java class pratice  problem (Geometry: point in a rectangle?) Write a program that prompts the user...

    Its java class pratice  problem (Geometry: point in a rectangle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5.  For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle. (Hint: A point is in the rectangle if its horizontal distance to (0, 0) is less than or equal to10 / 2 and...

  • Write a program to allow the user to calculate the distance between cities on a map.

    The distances are calculated in map units on the 2D map.Each city is given a set of coordinates relative to some point on the map. We'll call this origin (0,0) .There can only be a finite number of cities in the system at a given time (you decide this maximum).But in order to make the system useful, we'll have to add a few other features:Each city has a name in addition to its coordinates (you can assume a maximum length...

  • using python Instructions You will be given the name of a city followed by a set...

    using python Instructions You will be given the name of a city followed by a set of integers representing the monthly rainfall (in millimeters) that have happened in a given area over the past year. You will then calculate the average (mean) rainfall for the year. The mean is calculated by dividing the sum of the values in the set by the number of elements in the set mean of a set = sum of all elements number of elements...

  • Question 4-6 Please. Python 3.6. def main(). entered by a user. Write a program that finds...

    Question 4-6 Please. Python 3.6. def main(). entered by a user. Write a program that finds the sum and average of a series of numbers he program should first prompt the user to enter total numbers of numbers are to be summed and averaged. It should then as for input for each of the numbers, add them, and print the total of the numbers and their average 2. Write a progra m that finds the area of a circle. The...

  • Create a program in Python that will allow the user to enter a temperature in Fahrenheit...

    Create a program in Python that will allow the user to enter a temperature in Fahrenheit which will then be converted to degrees Celsius. The program will keep asking the user for a Fahrenheit temperature until the user enters Q to quit. After each conversion the program needs to print out the degrees Celsius. The input prompts for this problem need to look like the following: Degrees Fahrenheit: Continue: For these input prompts watch the capitalization and spacing. There are...

  • *In Python please***** This program will display some statistics, a table and a histogram of a...

    *In Python please***** This program will display some statistics, a table and a histogram of a set of cities and the population of each city. You will ask the user for all of the information. Using what you learned about incremental development, consider the following approach to create your program: Prompt the user for information about the table. First, ask for the title of this data set by prompting the user for a title for data, and then output the...

  • please write psedocodes for all of the questions and an algorithm for 2. no coding is...

    please write psedocodes for all of the questions and an algorithm for 2. no coding is required . FIUJELI 95 PIOL 1. (Geometry: Area of a Pentagon) Write a C# program that prompts the user to enter the length from the center of a pentagon to a vertex and computes the area of the pentagon, as shown in the following figure. The formula for computing the area of a pentagon is Area = 2 , where s is the length...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT