| | |
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 ansi anti apache application approximation argv array assignment backend beginner binary bluetooth builtin calculator change character converter countpasswordentry curved customdialog dan08 dictionary edit exe file float format function gnu heads homework ideas inches input java keyboard lapse leftmouse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook pointer prime programming py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session software sprite sqlite statictext statistics string strings syntax terminal text thread threading time tlapse tuple twoup ubuntu unicode unit urllib urllib2 variable wordgame write wxpython xlib





