老switcheroo 2【难度:1级】:
答案1:
using System;
using System.Linq;
public class Kata {
public static string Encode(string str) {
string alpha = "_abcdefghijklmnopqrstuvwxyz";
return String.Concat(str.ToLower().Select((c, i) => Char.IsLetter(c) ? "" + (alpha.IndexOf(c)) : "" + c));
}
}
答案2:
using System.Text.RegularExpressions;
public class Kata
{
public static string Encode(string str)
{
return Regex.Replace(str,"[a-zA-Z]",s=>str[s.Index]-(str[s.Index]>96 ? 96 : 64)+"");
}
}
答案3:
using System;
using System.Collections.Generic;
public class Kata
{
public static string Encode(string str)
{
string ret = string.Empty;
foreach(char chr in str)
{
ret += Char.IsLetter(chr) ? ((int)chr % 32).ToString() : chr.ToString();
}
return ret;
}
}
答案4:
using System;
public class Kata
{
public static string Encode(string str)
{
string res = "";
foreach(char c in str)
{
if(char.IsLetter(c) && !char.IsLower(c)) { res+= c - 64; }
else if (char.IsLetter(c) && char.IsLower(c)) { res+= c - 96; }
else { res+= c;}
}
return res;
}
}
答案5:
using System;
using System.Text;
public class Kata
{
public static string Encode(string str)
{
StringBuilder sb = new StringBuilder();
str = str.ToLower();
for (int i = 0; i < str.Length; i++)
{
if (Char.IsLetter(str[i]))
{
string p = (str[i] - 96).ToString();
sb.Append(p);
}
else
sb.Append(str[i]);
}
return sb.ToString();
}
}
答案6:
using System;
using System.Collections.Generic;
public class Kata
{
public static string Encode(string str)
{
Dictionary<string, int> dictionary = new Dictionary<string, int>
{
{"a",1 },
{"b",2 },
{"c",3 },
{"d",4 },
{"e",5 },
{"f",6 },
{"g",7 },
{"h",8 },
{"i",9 },
{"j",10 },
{"k",11},
{"l",12 },
{"m",13 },
{"n",14 },
{"o",15 },
{"p",16 },
{"q",17 },
{"r",18},
{"s",19 },
{"t",20 },
{"u",21 },
{"v",22 },
{"w",23 },
{"x",24 },
{"y",25 },
{"z",26 },
};
var result = "";
foreach (var val in str.ToLower())
{
if (char.IsLetter(val))
{
result += dictionary[val.ToString()];
}
else
{
result += val;
}
}
return result;
}
}
答案7:
using System;
public class Kata
{
public static string Encode(string str)
{
string strResult = string.Empty;
foreach(char c in str)
{
if (c >= 'a' && c <= 'z')
strResult += (c - 'a' + 1).ToString();
else if (c >= 'A' && c <= 'Z')
strResult += (c - 'A' + 1).ToString();
else
strResult += c.ToString();
}
return strResult;
}
}
答案8:
using System;
using System.Linq;
public class Kata
{
public static string Encode(string cc)
{
return string.Join(string.Empty,
cc.Select(ch => char.IsLetter(ch) ? ((ch - (Char.IsLower(ch) ? 'a' : 'A')) + 1).ToString() : ch.ToString()).ToArray());
}
}
答案9:
using System.Text.RegularExpressions;
public class Kata
{
public static string Encode(string str)
{
return Regex.Replace(str.ToLower(), "[a-z]", m => ((int)char.Parse(m.Value) - 96).ToString());
}
}
答案10:
using System;
using System.Linq;
public class Kata
{
public static string Encode(string str)
{
return string.Concat(str.Select(ch =>
{
if (char.IsLetter(ch))
{
ch = (char)((int)ch | 0x0020);
return ((int)ch - 96).ToString();
}
return ch.ToString();
}));
}
}