Python Forum
Frog Puzzle Random To Custom
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Frog Puzzle Random To Custom
#2
I think what you want to do is a bubble sort. Or maybe you want to know how sorted() works?

Your code doesn't work and seems complicated.

This implementation of the bubble sort may help you visualise the way bubble sort works.

from random import randint

frogs = [randint(0, 20) for i in range(10)]
#repeating loop len(frogs)(number of elements) number of times
moves = 0
for j in range(len(frogs)):
    #initially swapped is false
    swapped = False
    i = 0
    while i<len(frogs)-1:
        #comparing the adjacent elements
        if frogs[i]>frogs[i+1]:
            #swapping
            frogs[i],frogs[i+1] = frogs[i+1],frogs[i]
            #Changing the value of swapped
            swapped = True
            moves +=1
        i = i+1
        print (f'frogs = {frogs}, i = {i}, moves = {moves}')
    #if swapped is false then the list is sorted
    #we can stop the loop
    if swapped == False:
        break
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,439 Jan-09-2021, 06:35 PM
Last Post: deanhystad
  Slider puzzle captcha resolver, how do this? dw0rd1337 0 3,515 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,817 Apr-18-2020, 10:05 AM
Last Post: Shahmadhur13
  how to program robot to pass wise man puzzle steven12341234 0 1,973 Dec-02-2018, 08:31 AM
Last Post: steven12341234
  Need help with this coding puzzle torchtopher 1 2,119 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