思考和测试:报告某事【难度: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 ( ) ) ;
var output = new int [ 5 ] ;
foreach ( var item in input) {
if ( Regex. IsMatch ( item, @"[a-z]" , RegexOptions. IgnoreCase) ) {
output[ 0 ] ++ ;
output[ Regex. IsMatch ( item, @"[aeiou]" , RegexOptions. IgnoreCase) ? 3 : 4 ] ++ ;
}
else if ( Regex. IsMatch ( item, @"\d" ) ) {
output[ 1 ] ++ ;
}
else {
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;
}
}
}