3.01 - int r = 1;
for(int i = 0; i < power; i++){
r = r *number;
}
return r;
3.02 - int r = 1;
for(int a = i; a > 0 ;a-- ){
r = r * a;
}
return r;
3.03 - int r = 1;
for(int i = lowerBound; i <= upperBound; i++){
r = r*i;
}
return r;
3.04 - if (n < 3){
return 0;
}
int r = 0;
for(int i = 0; (i*2) < n; i++){
r = r + (i*2);
}
return r;
3.05 - int r = 0;
for(int i = 0; i <= upperBound; i++){
for(int a = 0; a < i; a++){
r += a;
}
r += i;
}
return r;
3.06 - string r = "";
for(int i = 0; i < word.Length; i++){
if (i == word.Length - 1)
r = r + "_";
else
r = r + "_ ";
}
return r;
3.07 - int m = 5;
char[] c = new char[s.Length];
for(int i = 0; i < s.Length; i++)
c[i] = (char)((s[i] + m - (int)'a') % 26 + (int)'a');
return new String(c);
3.08 - string r = "";
r = x.ToString(r);
return r.Length;
For 3.07 is a nicer solution
ReplyDeletepublic static String Puzzle(String s) {
int m = 5;
char[] c = new char[s.length()];
for(int i = 0; i < s.length(); i++)
c[i] = (char)((s.charAt(i) + m - (int)'a') % 26 + (int)'a');
return new String(c);
}
That may work but currently it is in the wrong syntax. s.length() needs to be s.Length no parenthesis. Also, charAt is not an extension method
DeleteShorter 3.07 fixed:
ReplyDeleteint m = 5;
char[] c = new char[s.Length];
for(int i = 0; i < s.Length; i++)
c[i] = (char)((s[i] + m - (int)'a') % 26 + (int)'a');
return new String(c);
3.07 Ain't working.
ReplyDeleteSolution 3.01 isn't an elegant solution. Do anyone have an elegant code?
ReplyDeleteIn Java I did.
ReplyDeletereturn (int)(long) Math.pow(number, power);
This comment has been removed by the author.
ReplyDelete3.08
ReplyDeletebest variant)
return(x+"").length()