C#练习题答案: 思考和测试:报告某事【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

思考和测试:报告某事【难度:2级】:

答案1:

namespace myjinxin
{
    using System;
    using System.Text.RegularExpressions;
    
    public class Kata
    {
        public Int32[] Testit(String Input){
          return new int[]{
            Regex.Replace(Input, "[^a-z]", String.Empty, RegexOptions.IgnoreCase).Length,
            Regex.Replace(Input, "[^0-9]", String.Empty).Length,
            Regex.Replace(Input, "[a-z0-9]", String.Empty, RegexOptions.IgnoreCase).Length,
            Regex.Replace(Input, "[^aeiou]", String.Empty, RegexOptions.IgnoreCase).Length,
            Regex.Replace(Input, "[^b-df-hj-np-tv-z]", String.Empty, RegexOptions.IgnoreCase).Length
          };
        }
    }
}

答案2:

namespace myjinxin
{
    using System;
    using System.Text.RegularExpressions;
    using System.Linq;
    
    public class Kata
    {
        public int[] Testit(string s){
          var input = s.ToCharArray().Select(x => x.ToString()); //split each character in string, convert chars to sep strings so it can match w/ regex
          var output = new int[5];
          foreach (var item in input){
            if (Regex.IsMatch(item, @"[a-z]", RegexOptions.IgnoreCase)){ //matches letters, case insensitive
              output[0]++;
              output[Regex.IsMatch(item, @"[aeiou]", RegexOptions.IgnoreCase) ? 3 : 4]++; //if it's a vowel, increment 3, else increment 4
            }
            else if (Regex.IsMatch(item, @"\d")){ //matches any digit (0-9)
              output[1]++;
            }
            else { // matches anything else such as whitespace and non-letter characters
              output[2]++;
            }
          }
          return output;
        }
    }
}

答案3:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public int[] Testit(string s){
            int[] result = new int[] { 0, 0, 0, 0, 0 };
            foreach (var item in s)
            {
                if (char.IsLetter(item))
                {
                    result[0]++;
                    if ("aeiouAEIOU".Contains(item)) result[3]++;
                    else result[4]++;
                }
                else if (char.IsDigit(item))
                {
                    result[1]++;
                }
                else result[2]++;
            }
            return result;
        }
    }
}

答案4:

namespace myjinxin
{
    using System;
    using System.Text.RegularExpressions;
    using System.Linq;
    
    public class Kata
    {
        public int[] Testit(string s){
          var input = s.ToCharArray().Select(x => x.ToString());
          var output = new int[5];
          foreach (var item in input){
            if (Regex.IsMatch(item, @"\d")){
              output[1] += 1;
            }
            else if (Regex.IsMatch(item, @"[aeiou]", RegexOptions.IgnoreCase)){
              output[0] += 1;
              output[3] += 1;
            }
            else if (Regex.IsMatch(item, @"[a-z]", RegexOptions.IgnoreCase)){
              output[0] += 1;
              output[4] += 1;
            }
            else {
              output[2] += 1;
            }
          }
          return output;
        }
    }
}

答案5:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public int[] Testit(string s)
        {
            var arr = new[] {0, 0, 0, 0, 0};
            foreach (var t in s)
            {
                if (char.IsDigit(t)) arr[1]++;
                else if (char.IsLetter(t))
                {
                    arr[0]++;
                    arr["aeiouAEIOU".IndexOf(t) >= 0 ? 3 : 4]++;
                }
                else arr[2]++;
            }
            return arr;
        }
    }
}

答案6:

namespace myjinxin
{
    using System;

    public class Kata
    {
        public int[] Testit(string s)
        {
            int[] res = new int[] { 0, 0, 0, 0, 0 };
            s = s.ToLower();
            if (!String.IsNullOrEmpty(s))
            {
                for (int i = 0; i < s.Length; i++)
                {
                    int startCode = (int)s[i];

                    if (startCode >= 48 &amp;&amp; startCode <= 57)  res[1]++;
                    else if (startCode == 97 || startCode == 101 || startCode == 105 || startCode == 111 || startCode == 117) { res[0]++; res[3]++; }
                    else if (startCode >= 97 &amp;&amp; startCode <= 122) { res[0]++; res[4]++; }
                    else res[2]++;
                }
            }
            return res;
        }
    }
}

答案7:

namespace myjinxin
{
    using System;
    using System.Linq;
    
    public class Kata
    {
        public int[] Testit(string s){
            return string.IsNullOrEmpty(s)?new int[5]:new int[]{s.Count(x=>char.IsLetter(x)),s.Count(x=>char.IsDigit(x)),s.Count(x=>!char.IsLetter(x)&amp;&amp;!char.IsDigit(x)),s.Count(x=>"aeiouAEIOU".Contains(x)),s.Count(x=>char.IsLetter(x)&amp;&amp;!"aeiouAEIOU".Contains(x))};
        }
    }
}

答案8:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;

    public class Kata
    {
        static readonly HashSet<char> vowels = new HashSet<char>() { 'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u' };

        public int[] Testit(string s)
        {
            int[] ct = new int[5];

            foreach (char x in s)
            {
                if (char.IsLetter(x))
                {
                    ct[0]++;
                    if (vowels.Contains(x))
                        ct[3]++;
                    else
                        ct[4]++;
                }
                else if (char.IsDigit(x))
                    ct[1]++;
                else
                    ct[2]++;
            }

            return ct;
        }
    }
}

答案9:

namespace myjinxin
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public class Kata
    {
        public int[] Testit(string s)
        {
          return s==""?new int[5]:s.Select(x=>GetArr(x)).Aggregate((a,b)=>a.Zip(b,(x,y)=>x+y)).ToArray();
        }
        public IEnumerable<int> GetArr(char s)
        {
          if(Char.IsDigit(s))
          return new[]{0,1,0,0,0};
          if(Char.IsLetter(s)&amp;&amp;"aeiouAEIOU".Contains(s))
          return new[]{1,0,0,1,0};
          if(Char.IsLetter(s))
          return new[]{1,0,0,0,1};
          else
          return new[]{0,0,1,0,0};
        }
    }
}

答案10:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public int[] Testit(string s){
          if(s==""||s==null){
            return new int[5];
          }
          int[] a = new int[5] {0,0,0,0,0};
          foreach (char item in s){
            int b;          
            bool parsed = char.IsNumber(item); 
            if (parsed){
              a[1] += 1;
            } else if (Char.IsLetter(item)) {
              char d = char.ToLower(item);
              a[0]+=1;
              if(d=='a'||d=='e'||d=='i'||d=='o'||d=='u'){
                a[3]+=1;
              } else{
                a[4]+=1;
              }
            } else {
              a[2] +=1;            }
          }  
          return a;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值