Jump to content

Recommended Posts

Posted (edited)

I want to create a function out of any number of variables that would generate a non repeat output value, sort of like indexing.

 

So to being with one variable you got x, and you set x to 0,1,2,3,4,5,6,7,8,9,10 slowly incrementing values and is positive with non repeating outputs.

 

Now with 2 variables x and y, how do I make it the same as above?

 

Now 3 variables 4, or more, how do I do it?


orz

 

http://stackoverflow.com/questions/14013373/finding-the-index-of-a-given-permutation

Edited by fredreload
Posted (edited)

What is the domain of your function? In your example of setting x = 0, 1, 2, 3 ... are those the inputs or the outputs?

 

If you mean you have some function [math]f : \mathbb N \rightarrow \mathbb N[/math] with [math]f(0) = 0[/math], [math]f(1) = 1[/math], etc., then there's an easy way to get unique outputs for any number of variables.

 

For example given two positive integers [math]m, n[/math] we can map that pair to [math]2^m 3^n[/math].

 

Likewise given any finite set of [math]n[/math] positive integer input variables [math]n_i[/math], we can map them to [math]\displaystyle \Pi_{i=1}^n p_i^{n_i}[/math] where [math]p_i[/math] is the [math]i[/math]-th prime.

 

Uniqueness of the output is guaranteed by the fundamental theorem of arithmetic. Do you see how this works?

 

Is this what you are asking?

Edited by wtf
Posted
I found the answer in the link I posted, pretty much what I am looking for, indexing out of permutations

 

Would you care to enlighten us. I certainly haven't a clue what you were on about from your first post. I shouldn't have to click on an external link. Is it something like unique passenger ticket generation?? Or is it placing things in alphabetical/numerical order lexicographical indexing like in the stackoverflow link??

<?php

$passeneger_ticket_numbers = array("1212","2413","5135","51326","315256");
$passengers = array(array("derek",""),array("bob",""),array("joe",""),array("marie",""),array("anna",""));

if(count($passengers)===5){
    for($i=0;$i<=count($passengers)-1;$i=$i+1){
        $random = rand(0, count($passeneger_ticket_numbers)-1);
        $passengers[$i][1]= $passeneger_ticket_numbers[$random];
        unset($passeneger_ticket_numbers[$random]);
        $passeneger_ticket_numbers = array_values($passeneger_ticket_numbers);
    }
}

var_dump($passengers);

?>

The above take O(1) time and space because my boat holds x number of passengers and always try to have the maximum number of passengers before departure.

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.