Sector Two

2.01 - int [] a = new int [n];
for(int i = 0; i < n; i++){
a [i] = i;
}
return a;
2.02 -  int [] a = new int [n];
for(int i = 0; i < n; i++){
a [i] = i*n;
}

return a;
2.03 -  int [] a = new int [n];
   for(int i = 0; i < n; i++){
   a [i] = i*i;
   }

   return a;
2.04 - int result = 0;
foreach(var i in v){
result += i;
}

return result;
2.05 - int r = 0;
 for(int i = 0; i < n; i++)
r += (i*i);

return r;
2.06 -  int count = 0;
foreach(char c in s){
if (c == 'a'){
count++;
}
}
return count;
2.07 - //UPDATED//
using System;
using System.Text.RegularExpressions;
public class Program {
    public static int Puzzle(string s, char x) {
    return Regex.Matches(s, x.ToString()).Count;
    }
}

1 comment:

  1. 2.01 Enumerable.Range(0,n).ToArray()
    2.02 Enumerable.Range(0, n).Select(x => x * n).ToArray()
    2.03 Enumerable.Range(0, n).Select(x => x * x).ToArray()
    2.04 v.Aggregate((x,y) => x+y)
    2.05 (n-1)*(n)*(2*n-1)/6
    2.06 s.Length - s.Replace("a", "").Length
    2.07 s.Length - s.Replace(""+x, "").Length

    ReplyDelete