Is it possible to write a ROT13 in one line?

帅比萌擦擦* 提交于 2019-12-04 08:40:49

What about this? I just happen to have this code lying around, it isn't pretty, but it does the job. Just to make sure: One liners are fun, but they usually do not improve readability and code maintainability... So I'd stick to your own solution :)

static string ROT13(string input)
{
    return !string.IsNullOrEmpty(input) ? new string (input.ToCharArray().Select(s =>  { return (char)(( s >= 97 && s <= 122 ) ? ( (s + 13 > 122 ) ? s - 13 : s + 13) : ( s >= 65 && s <= 90 ? (s + 13 > 90 ? s - 13 : s + 13) : s )); }).ToArray() ) : input;            
}

If you need more clarification, just ask.

Just an alternative version that uses other chars in the comparison to make things more "clear"

static string ROT13(string input)
{
  return !string.IsNullOrEmpty(input) ? new string(input.Select(x => (x >= 'a' && x <= 'z') ? (char)((x - 'a' + 13) % 26 + 'a') : ((x >= 'A' && x <= 'Z') ? (char)((x - 'A' + 13) % 26 + 'A') : x)).ToArray()) : input;           
}

Not really a one liner but still shorter than your original code and more understandable than the other answer:

static string Rot13(string input)
{
    if(input == null)
        return null;
    Tuple<int, int>[] ranges = { Tuple.Create(65, 90), Tuple.Create(97, 122) };
    var chars = input.Select(x =>
    {
        var range = ranges.SingleOrDefault(y => x >= y.Item1 && x <= y.Item2);
        if(range == null)
            return x;
        return (char)((x - range.Item1 + 13) % 26) + range.Item1;
    });

    return string.Concat(chars);
}

Another version that even better expresses what happens in ROT13 is this:

static string Rot13(string input)
{
    var lowerCase = Enumerable.Range('a', 26).Select(x => (char)x).ToArray();
    var upperCase = Enumerable.Range('A', 26).Select(x => (char)x).ToArray();
    var mapItems = new[]
    {
        lowerCase.Zip(lowerCase.Skip(13).Concat(lowerCase.Take(13)), (k, v) => Tuple.Create(k, v)),
        upperCase.Zip(upperCase.Skip(13).Concat(upperCase.Take(13)), (k, v) => Tuple.Create(k, v))
    };
    var map = mapItems.SelectMany(x => x).ToDictionary(x => x.Item1, x => x.Item2);

    return new string(input.Select(x => Map(map, x)).ToArray());
}

static char Map(Dictionary<char, char> map, char c)
{
    char result;
    if(!map.TryGetValue(c, out result))
        return c;
    return result;
}

One more an alternative version:

static string ROT13(string input) { String.Join("", input.Select(x => char.IsLetter(x) ? (x >= 65 && x <= 77) || (x >= 97 && x <= 109) ? (char)(x + 13) : (char)(x - 13) : x)) }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!