Home > database >  How to version string elements?
How to version string elements?

Time:06-16

I have below list of strings available as list

 List<string> available = new List<string> {"C1,C2,C3,C1_V1" };

I have input parameter C1. Now I have to match available strings in available list of strings. Whenever my input is C1 then matching elements are C1,C1_V1 in available so my string should increase by 1 to get C1_v2. I have mentioned clearly in below table

enter image description here

As per the above table,

case 1- Avail is C1,C2,C3 and input is C1 so my destination should be C1_V1 case 2 - avail is C1,C2,C3,C1_V1 and input is C1 but my destination cannot be C1_v1 because it is already available in avail so next version should be C1_v2 and so on.

I am trying to implement this logic in c#. I have started doing this but couldnt get it done

List<string> available = new List<string> {"C1,C2,C3,C1_V1" };

string input = "C1";

string destination = string.Empty;

foreach(var data in available)
{
    destination = $"{input}_V{initialVersion}";
}

Can someone help me to complete this. Any help would be appreciated.

CodePudding user response:

You could count the number of items in available that match the given input, and use that count to determine the version.

A simple matching algorithm might be where a string in available is equal to input or where a string in available starts with "{input}_".

In order to handle cases where input is given with the version part, such as C1_V1 you need to split the input on the version separator, '_', and just look at the "key" part of the input.

public string NextVersion(string input, List<string> available)
{
    // Argument validation omitted.
    if (input.Contains('_'))
    {
        input = input.Split('_')[0];
    }
    int count = available.Count(a => string.Equals(a, input) || a.StartsWith($"{input}_"));
    if (count == 0)
    {
        // The given input doesn't exist in available, so we can just return it as
        // the "next version".
        return input;
    }

    // Otherwise, the next version is the count of items that we found.
    return $"{input}_V{count}";
}

This assumes that '_' is only valid as a version separator. If you can have strings in available such as "C1_AUX" then you'll run into issues trying to get the next version of "C1".

It also assumes that you want to increment to the next version, even if input is a version that doesn't exist. For example, if available is ["C1", "C1_V1"] and input is "C1_V123" than the return value should be "C1_V2".

Poul Bak raises another caveat. If you end up with a situation where available is missing a version. For example, if available is ["C1", "C1_V2"] and input is "C1" then the result of this function is "C1_V2", leading to a duplicate version. In this case, you'd probably have to find every item in available where the "key" part is input's key part, then parse each one to find the next version.

It isn't clear what the constraints are requirements are exactly, so these caveats may or may not be an issue. But they're certainly worth keeping in mind.

  • Related