| | |
help with this program
![]() |
•
•
Join Date: Nov 2008
Posts: 1
Reputation:
Solved Threads: 0
I need help with this problem
Generating Random Cities
Write a function called generate_cities(min_x, max_x, min_y, max_y, n, distance) that generates a list of cities with random coordinates. The parameters represent the following:
* min_x, max_ x: the minimum and maximum values for the x coordinate of each city
* min_y max_ y: the minimum and maximum values for the y coordinate of each city
* n: the number of cities to be generated (that is the number of [x, y] lists)
* distance: the minimum distance allowed between any two cities
Here is a brief description of how you can generate your cities while ensuring that they are not too close to each other
This is the algorithm that I have to follow
repeat n times
generate random coordinates for new_city
too_close = True
while too_close == True
too_close = False
for each city in the city list so far
if the new_city < distance from city
too_close = True
generate new random city point
break
add new_city to the results list
This is as far as I got:
def generate_cities(min_x, max_x, min_y, max_y, n, distance):
for steps in range(N):
random_coordinates_x = random.randint(min_x, max_x)
random_coordinates_y = random.randint(min_y, max_y)
new_city = [random_coordinates_x, random_coordinates_y]
print new_city
Generating Random Cities
Write a function called generate_cities(min_x, max_x, min_y, max_y, n, distance) that generates a list of cities with random coordinates. The parameters represent the following:
* min_x, max_ x: the minimum and maximum values for the x coordinate of each city
* min_y max_ y: the minimum and maximum values for the y coordinate of each city
* n: the number of cities to be generated (that is the number of [x, y] lists)
* distance: the minimum distance allowed between any two cities
Here is a brief description of how you can generate your cities while ensuring that they are not too close to each other
This is the algorithm that I have to follow
repeat n times
generate random coordinates for new_city
too_close = True
while too_close == True
too_close = False
for each city in the city list so far
if the new_city < distance from city
too_close = True
generate new random city point
break
add new_city to the results list
This is as far as I got:
def generate_cities(min_x, max_x, min_y, max_y, n, distance):
for steps in range(N):
random_coordinates_x = random.randint(min_x, max_x)
random_coordinates_y = random.randint(min_y, max_y)
new_city = [random_coordinates_x, random_coordinates_y]
print new_city
![]() |
Similar Threads
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- What's the HARDEST program you've written? (Computer Science)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP)
- Program is shutting down right after program is executed (C++)
- 3d Program (Game Development)
Other Threads in the Python Forum
- Previous Thread: Help With this code
- Next Thread: Memory game
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui heads homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython





