Python Forum
Frog Puzzle Random To Custom
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Frog Puzzle Random To Custom
#3
Following your private message:

I think perhaps you are concentrating on a list with only 4 elements, but that is not helpful.

You need to be able to sort any list.

Try this way to bubble sort:

from random import randint

# Helper variables
moves = 0
# set q = True or the while loop will not start
q = True
# make a random list
# you can change the numbers here: randint(0, 20) and here: range(10)
frogs = [randint(0, 20) for i in range(10)]
print(f'Start situation frogs = {frogs}')
# the while loop will keep going until there is nothing to swap around
# then q = False and the while loop will stop
# don't set: while True because True is always True, the while loop will not stop
while q:
    q = False  # Reset q for this pass
    # keep looping through the list until there is nothing left to swap
    for i in range(1, len(frogs)):
        if frogs[i - 1] > frogs[i]:
            # Swap the elements
            frogs[i - 1], frogs[i] = frogs[i], frogs[i - 1]
            q = True  # Set q to True if a swap occurs
            moves +=1
        print (f'frogs = {frogs}, i = {i}, moves = {moves}') 
MoreMoney likes this post
Reply


Messages In This Thread
Frog Puzzle Random To Custom - by MoreMoney - Mar-23-2024, 10:39 AM
RE: Frog Puzzle Random To Custom - by Pedroski55 - Mar-24-2024, 07:57 AM
RE: Frog Puzzle Random To Custom - by Pedroski55 - Mar-24-2024, 06:14 PM
RE: Frog Puzzle Random To Custom - by deanhystad - Mar-25-2024, 09:53 PM
RE: Frog Puzzle Random To Custom - by MoreMoney - Mar-26-2024, 08:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Codility Frog River One Westerner 2 2,442 Jan-09-2021, 06:35 PM
Last Post: deanhystad
  Slider puzzle captcha resolver, how do this? dw0rd1337 0 3,517 Jan-04-2021, 11:55 PM
Last Post: dw0rd1337
  I code a program to solve puzzle but i can't make it more dynamic. Shahmadhur13 5 2,821 Apr-18-2020, 10:05 AM
Last Post: Shahmadhur13
  how to program robot to pass wise man puzzle steven12341234 0 1,974 Dec-02-2018, 08:31 AM
Last Post: steven12341234
  Need help with this coding puzzle torchtopher 1 2,125 Jun-22-2018, 01:14 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020