Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Next permutation

by LanX (Saint)
on Feb 25, 2019 at 18:25 UTC ( [id://1230513]=note: print w/replies, xml ) Need Help??


in reply to Next permutation

You can use Algorithm::Permute for that.

You'll need to split the ciphers into a list and join them again.

There is more to say about it ... but your question is too sparse to go into details of non-repeating and non-module implementation...

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: Next permutation
by aartist (Pilgrim) on Feb 25, 2019 at 19:28 UTC
    Thanks for the answer. I am looking for non-modules solution. I don't want to find all the solution. I just want to find next number in sequence. We should be able to derive from current number. Foe example : ( swap till you hit the largest number as first. If you hit the largest number as first, get the next number on left side and then place remaining number in sorted order ) : 117 => 171 => 711 or : 1234 =>(swap) 12.43 =>(largest 4) : 13.24 =>(swap) 13.42 => (largest) 14.23 =>(swap) :14.32 etc..

      How is what you want to do different than a Bubble Sort where you stop on the first swap? A bubble sort moves the highest element up by one position for each pass (here a digit). What do you want to happen if the highest number is already first in the order?

      Update:
      Probably a better way to get the index of max value...But is this what you want?
      Code should run fairly quickly albeit a bit wordy for Perl.

      #!/usr/bin/perl use strict; use warnings; my @array = split '', "117"; print @array, "\n"; print @array,"\n" while rot_max_left(\@array); @array = split '', "1234"; print "\n",@array,"\n"; print @array,"\n" while rot_max_left(\@array); sub rot_max_left { my $array_ref = shift; my $maxi=0; return 0 unless ($maxi = get_index_of_max($array_ref)); ($array_ref->[$maxi-1],$array_ref->[$maxi]) = ($array_ref->[$maxi] +,$array_ref->[$maxi-1]); return $maxi; } sub get_index_of_max { my $array_ref = shift; my $maxi = 0; foreach (my $i=1;$i<@$array_ref;$i++) { $maxi = $i if ($array_ref->[$i] > $array_ref->[$maxi]) } return $maxi; } __END__ 117 171 711 1234 1243 1423 4123
        If you hit the highest digit as the first digit, then:
        1. Sorting of remaining digits.
        2. if you cannot sort any further, (ie.. it is already in reverse order) than sort from the beginning position. ( to return to the original number. 321 -> 123)
      Your requirements are cryptic for me.

      Please note that you can easily implement any "swap" with array slices, like

      @a[0,1]=@a[1,0]

      and @a=split//,$number

      will initialize it.

      Now you know everything needed to implement your ideas.

      HTH! :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1230513]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-23 15:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found