TOPCODER Practice Room Solution – 250 Problem


using System.Text.RegularExpressions;
using System;

public class HowEasy
{
public int pointVal(String param)
 {
 Regex reg = new Regex(@"\b[a-zA-Z]+\b(?![0-9]|[.]{2,})", RegexOptions.IgnoreCase);
 MatchCollection matches = reg.Matches(param);

int rtotal = 0, avg = 0;

foreach (Match match in matches)
 rtotal += match.Length;

 avg = rtotal / matches.Count;

 if(avg < 3)
 {
 return 250;
 }else if(avg == 4 | avg == 5)
 {
 return 500;
 }else if(avg >= 6)
 {
 return 1000;
 }
 return 0;
 }
}

This is an example of how to use word boundaries, negative lookahead and quantifier repetition. [Regex Tut]

Advertisements