Sunday, August 24, 2014

Code Hunt Solutions 6

6.01- return false;
6.02 - string r = "";
for(int i = 0; i < s.Length ;i++){
if (i % 2 == 0){
r += s.Substring(i, 1).ToUpper();
}
else
r += s.Substring(i, 1);

}

return r;
6.03 - string r = "";
for(int i = 0, j = 1; i < s.Length; i++, j++){
if(i == s.Length - 1 || s[j] == ' ' ){
r += Char. ToUpper(s[i]);
}
else{
r += s[i];
}
}
return r;
6.04 - return s[x];
6.05 - return two + one;
6.06 - return s.Substring(s.Length/2);
6.07 - string r = "";
r = s.Substring((s.Length/2)+1).ToUpper();
r += s.Substring((s.Length /2));
return r;
6.08 - return Math.Max(b.Length, a.Length); 
updated
6.09 - if(a.Length > b.Length){
return a;
}
if(b.Length > a.Length){
return b;
}
return a + b;
6.10 - return s.Length /3;
6.11- return s.Substring(i,s.Length-i-1) + s.Substring(s.Length-2,1); 
Thanks to Araz
6.12 -string w = "";
char [] charArray = s.ToCharArray();
Array.Reverse( charArray );
w = new string( charArray );
return s+w;

Code Hunt Solutions 5

5.01 - if (s.Length <= 3){
   return "short";
   }
   if (s.Length >= 4 && s.Length <= 7){
   return "average";
   }
   if(s.Length >= 8 && s.Length <= 14){
   return "long";
   }
   return "super long";
5.02 - thx
 if (i % 1111 == 0) return "fancy year";
return "not a fancy year";

5.04 - if (y > 0 && x < 0){
   return (x-y) - 2*(x-y);
   }
   if (x > 0 && y < 0){
   return (x-y);
   }

    if ((x+y) < 0){
return (x+y) - 2*(x+y);
    }
return x + y;
5.05 - return (j == i*i)

Code Hunt Solutions 4

4.01 - if (x == true || y == true){
return true;
}
return false;
4.02 - if(x == false || y == false){
return false;
}
return true;
4.03 - if (x < 50){
return true;
}
return false;
4.04 - if(x >= y){
return false;
    }
return true;
4.05 -  if(i < 0){
return -1;
}
if(i > 0){
return 1;
}
return 0;
4.06 - if(i == j){
return true;
}
return false;
4.07 - if(i > 99){
return 3;
}
return 2;
4.08 - if(i % 2 == 0){
return "even";
}
return "odd";
4.09 - if(i % 5 == 0){
return "multiple of 5";
}
return "not a multiple of 5";
4.10 - if(i % x == 0){
return "multiple of " + x;
}
return "not a multiple of " + x;
4.11 - if(i==(2*j) && j==(2*k))
return "yes!";
else
return "no"; 
4.12 - int output = 21;
        if(i <= 7){
                return 0;
        }
if(i > 7 && i <= 14){
        return 7;
}
return output;

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;

Code Hunt Solutions 2

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;

Code Hunt Solutions 1

1.01 - -x
1.02 - x-2
1.03 - x*x
1.04 - x*3
1.05 - x/3
1.06 - 8 / (x*2)
1.07 - x-y
1.08 - y*2 + x
1.09 - x*y
1.10 - (y/3) + x
1.11 - x/y
1.12 - x%3
1.13 - x%3 + 1
1.14 - 10%x
1.15 - (x+y+z) /3