Return string with some sort of pointers

Viewed 11

So I have a code to decode an encoded string. The function has a list of numbers and another list with randomized letters.

The number list is a pointer that indicates which letter is first in an order. The problem is that the numbers are built using this code -

for (int i = 0; i < count; i++)
{
    Random r = new Random();
    int removeIndex = r.Next(0, splited.Count);
    result.Add(splited[removeIndex]);
    splited.RemoveAt(removeIndex);
    pointers.Add(removeIndex);
}

So basically what happens for example :

Letters = { a, b, c, d}

Pointer = { 2, 0, 1, 0}

Result = { c, a, b, d}

The thing I want to happen is something like in this example or in this example

(Result is the requested return. Remaining code is what left in the randomized list. Initial number and onward is the list generation process.)

What I've tried -

List<char> encodedToChar = new List<char>(encodedText);
List<char> encodedResult = new List<char>();
List<int> whereToRemove = new List<int>();

for (int i = 0; i < length; i++)
{
    encodedResult.Add('☺');
}

Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < length; i++)
{
    whereToRemove.Add(pointerToInt[i]);
    int addCurrentPos = -1;
    for (int a = 0; a < whereToRemove.Count; a++)
    {
        if (pointerToInt[i] >= whereToRemove[a])
        {
            addCurrentPos++;
        }
    }

    encodedResult[pointerToInt[i] + addCurrentPos] = encodedToChar[i];



    Console.Write(" ' " + (int)(pointerToInt[i] + addCurrentPos) + " ' ");
    for (int k = 0; k < length; k++)
        Console.Write(" " + encodedResult[k]);
    Console.Write(" : ");
}

This is the code in action. which almost works. (Top numbers are pointers and below each pointer is the letter which should be in that position)

I've tried multiple solutions but can't figure one that works. since the encoded code is made whilst removing the letter, one thing I can think of on how to get the exact position is to somehow count how many letters were removed but there are more problems, for example, if the last 2 remaining letters are the first letter and the last letter (which it is possible) , the pointer can show 0 , 0. and therefor you cant know which one is the last and which one is the first unless you use some kind of a trick which I cant figure out..

(Or maybe it's just not possible and I should change the code generation method)

0 Answers
Related