Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding upgrade
#1
Question 
minimize frog steps as possible, trying to minimize and efficient the frog step.
The rule is given the last step 0 must be at most left, then big number to smaller number, its seems there is some inefficient on my coding
I also want to make it able to input my custom random number like i want to put [0,55,12,74,12,39] and make it work

Note : Frog sorting program. Can input frog numbers randomly. Prepare a list/place to input frog numbers during the presentation later. The frogs at the start are arranged randomly, with the empty/0 spot on the far left.

Here is my try:

frog_count = int(input('Enter the number of frogs: '))

print()

min_interval = int(input('Enter the minimum interval: '))
max_interval = int(input('Enter the maximum interval: '))

print()

from random import randint
frog_list = [0] if frog_count >= 1 else []
sorted_list = []

for index in range(1, frog_count + 1):
    random_value = randint(min_interval, max_interval)
    frog_list.append(random_value)
    sorted_list.append(random_value)

print(frog_list)

sorted_list.sort(reverse=True)

move_count = 0
position = 0
direction = 0

print(frog_list)
if frog_count == 1:
    move_count = 0
else:
    while frog_list[1:] != sorted_list:
        if direction == 0:
            if frog_list[position] == frog_list[-2]:
                frog_list[position], frog_list[position + 1] = frog_list[position + 1], frog_list[position]
                position += 1
            elif frog_list[position] == frog_list[-1]:
                direction = 1
            elif frog_list[position + 2] > frog_list[position + 1]:
                frog_list[position], frog_list[position + 2] = frog_list[position + 2], frog_list[position]
                position += 2
            else:
                frog_list[position], frog_list[position + 1] = frog_list[position + 1], frog_list[position]
                position += 1
        elif direction == 1:
            if frog_list[position] == frog_list[1]:
                frog_list[position], frog_list[position - 1] = frog_list[position - 1], frog_list[position]
                position -= 1
            elif frog_list[position] == frog_list[0]:
                direction = 0
            elif frog_list[position - 2] < frog_list[position - 1]:
                frog_list[position], frog_list[position - 2] = frog_list[position - 2], frog_list[position]
                position -= 2
            else:
                frog_list[position], frog_list[position - 1] = frog_list[position - 1], frog_list[position]
                position -= 1
        
        move_count += 1
        print(frog_list)

    move_count = move_count

print('Minimum number of moves: ', move_count)
........................output

Output:
Enter the number of frogs: 5 Enter the minimum interval: 1 Enter the maximum interval: 100 [0, 58, 66, 97, 51, 82] [0, 58, 66, 97, 51, 82] [66, 58, 0, 97, 51, 82] [66, 58, 97, 0, 51, 82] [66, 58, 97, 82, 51, 0] [66, 58, 97, 82, 51, 0] [66, 58, 97, 82, 0, 51] [66, 58, 97, 0, 82, 51] [66, 0, 97, 58, 82, 51] [0, 66, 97, 58, 82, 51] [0, 66, 97, 58, 82, 51] [97, 66, 0, 58, 82, 51] [97, 66, 82, 58, 0, 51] [97, 66, 82, 58, 51, 0] [97, 66, 82, 58, 51, 0] [97, 66, 82, 58, 0, 51] [97, 66, 82, 0, 58, 51] [97, 0, 82, 66, 58, 51] [0, 97, 82, 66, 58, 51] Minimum number of moves: 17
[Program finished]

#homework
deanhystad write Mar-22-2024, 03:40 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Please don't double post.
Reply
#2
You should post a description of what the program is supposed to do. This was missing from your previous post also.

Is the goal to sort the frogs in decreasing order by their interval? What is a "step"? Is comparing two intervals a step, or is a step when you swap two frogs? Is there a restriction on how frogs can be swapped?
MoreMoney likes this post
Reply
#3
(Mar-22-2024, 03:56 PM)deanhystad Wrote: You should post a description of what the program is supposed to do. This was missing from your previous post also.

Is the goal to sort the frogs in decreasing order by their interval? What is a "step"? Is comparing two intervals a step, or is a step when you swap two frogs? Is there a restriction on how frogs can be swapped?
Sorry, the first thing i want to do is make it also have option to asking for custom input or generating random number like etc: i want to enter [0,10,7,1,6,4] instead of randomly generating number, i already tried modified it but ended in error, do you have code to make it asking the number instead of random?
Reply
#4
Look at str.split() This can be used to split a str like this:
Output:
0 10 7 1 6 4
Int a list of str like this:
Output:
['0', '10', '7', '1', '6', '4']
Then you can convert the individual number strings to ints like you are already doing in your program.
MoreMoney likes this post
Reply
#5
(Mar-22-2024, 04:16 PM)deanhystad Wrote: Look at str.split() This can be used to split a str like this:
Output:
0 10 7 1 6 4
Int a list of str like this:
Output:
['0', '10', '7', '1', '6', '4']
Then you can convert the individual number strings to ints like you are already doing in your program.

Thank you, but already try to putting your code but it cause error, maybe i dont know where to put it correctly, can you put it in my code, or maybe you have entirely whole better and more efficient code?

Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to upgrade this code. valentino1337 2 489 Feb-28-2024, 07:44 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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