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 - int r = 0;
foreach(char x in string s){
r++;
}
return r;
02.04: need in code shape
ReplyDelete02.06 Also need in code shape
ReplyDeletefor 2.01 the 3 point solution is
ReplyDeleteEnumerable.Range(0, n).ToArray();
You'll have to add System.Linq
2.02 - 3 point solution:
ReplyDeleteSystem.Linq
Enumerable.Range(0, n).ToArray().Select(x => (x * n)).ToArray();
02.04
ReplyDeletereturn v.Aggregate((sum, i) => unchecked(sum + i));
error "," was not expected
Delete02.06 the elegant solution is:
ReplyDeleteusing System.Text.RegularExpressions;
return Regex.Matches(s, "a").Count;
Similarly, 02.07 is:
ReplyDeleteusing System.Text.RegularExpressions;
return Regex.Matches(s, x.ToString()).Count;
This comment has been removed by the author.
ReplyDelete2.04
ReplyDeleteint result = 0;
for(int i:v){
result += i;
}
return result;
DeleteRight! Thanks for that.
DeleteEfficient method for 2.06
ReplyDeletereturn s.length()-s.replace(a,"").length();