0

I have the following String:

String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

I need to create two strings from it:

  1. A string obtained simply by reordering the characters;

  2. A string obtained by selecting 10 characters and reordering them.

So for (1) I would get, for example:

String characters = "jkDEF56789hisGHIbdefpqraXYZ1234txyzABCcglmnoRSTUVWuvwJKLMNOPQ0";

And for (2) I would get, for example:

String shortList = "8GisIbH9hd";

THE PROBLEM

I could just change to Char Array and order by randomly by a Guid.

However I want to specify some kind of key (maybe a guid?) and for that key the result or reordering and of selecting the shortList must be the same.

Does this make sense?

brz
  • 5,926
  • 1
  • 18
  • 18
Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • your problem is not clear. How can you "reorder" your first string "based on a Guid". I think you chose the wrong word. Could you explain what you really want step by step and what is your problem now? – artragis Sep 21 '14 at 14:24
  • what is the exact output of (1) and (2)? – Mehdi Khademloo Sep 21 '14 at 14:30
  • 1
    Are you looking to encode an arbitrary reordering of (1)62 and (2)10 characters in a Guid? You have 62 unique characters, so each could be represented by a 6 bit number. Thus an arbitrary reordering of 10 characters requires no more than 60 bits, and so could be encoded in a Guid (128 bits). An arbitrary reordering of all 62 characters requires more than 128 bits however, so your Guid would need to index into a table. – dbc Sep 21 '14 at 15:02
  • Sorry, for the delay. I just updated my answer. Basically I want to reorder a string characters and selecting a portion of that string also reordered but the results would always be same according to a key ... So if I use as key "ksjdhkjhurer" then the results would only change if I change that key. – Miguel Moura Sep 22 '14 at 17:01

2 Answers2

0

you could convert your GUID string to an int array of its ascii/utf/whatever codes like here Getting The ASCII Value of a character in a C# string. then iterate over this array with something along lines of this (note: this is pseudocode):

 string res="";
 for(elem in intconvertedGUIDstring) res+= characters[elem%(characters.count)];

for the task [2] you could reverse your Characters i.e. like here Best way to reverse a string and use the c# string function left() to truncate it before running it through the same procedure

Community
  • 1
  • 1
0

You can use a hash function with a good distribution value as seed for comparison between elements. Here's a sample:

static ulong GetHash(char value, ulong seed)
{
   ulong hash = seed * 3074457345618258791ul;
   hash += value;
   hash *= 3074457345618258799ul;
   return hash;
}

And use this function for comparison:

static void Main()
{
   var seed = 53ul;
   var str = "ABCDEFHYUXASPOIMNJH";
   var shuffledStr = new string(str.OrderBy(x => GetHash(x, seed)).ToArray());
   Console.WriteLine(shuffledStr);
}

Now every time you order by seed 53 you'll get the same result, and if you seed by 54 you'll get a different result.

brz
  • 5,926
  • 1
  • 18
  • 18