Sunday, August 24, 2014

Code Hunt Solutions 3

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;

8 comments:

  1. For 3.07 is a nicer solution
    public 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);
    }

    ReplyDelete
    Replies
    1. 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

      Delete
  2. Shorter 3.07 fixed:


    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);

    ReplyDelete
  3. Solution 3.01 isn't an elegant solution. Do anyone have an elegant code?

    ReplyDelete
  4. In Java I did.
    return (int)(long) Math.pow(number, power);

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. 3.08

    best variant)
    return(x+"").length()

    ReplyDelete