Sunday, October 19, 2014

Code Hunt Solutions 7

7.01 - return two+three+one+one+three+two;
7.02 -  return s.Substring(0, s.Length/2);
7.03 - return a.Replace(b,c);
7.04 - string w = "";
char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
w = new string ( charArray );
return w;
7.05 - string r = "";
for(int i = 0; i < a.Length ;i++){
if (i % 2 == 0){
r += b.Substring(i, 1);
}
else
r += a.Substring(i, 1);

}

return r;
7.06 - return s.Replace(" ", "");
7.07 - return s.Replace("a", "").Replace("e", "").Replace("i", "").Replace("o", "").Replace("u", "");
7.08 - string r = "";
r = input.Replace(a, "").Replace(b, "").Replace(c, "");
if (r == "" && input.Contains(a) && input.Contains(b) && input.Contains(c)){
return true;
}
return false;
7.09 - string r = "";
for(int id = 0; id<i-1; id++){
r += s + " ";
}
        return r + s;
7.10 -(thanks to RainVision for this one)
 using System;
using System.Linq;
public class Program {public static string Puzzle(int t) {return String.Join(" ", Enumerable.Repeat("a b c d e f g h i j k l m n o p q r s t u v w x y z", t)).Substring(0, 52*t-t*2) + "z";}

6 comments:

  1. 7.10
    using System;
    public class Program {
    public static string Puzzle(int t) {
    string output = "";
    int n=25;
    for (int j=0; j<t; j++){
    for (int i = 0; i<n; i++) {
    output += (char)('a' + i);
    output += " ";
    }
    if (j == t-2) n-=(t-1);
    output+="z ";
    }
    return output.Remove(output.Length-1);
    }
    }

    ReplyDelete
  2. 7.10
    String r = "";
    for (int j=0; j<t; j++) {
    for (int i=0; i<25; i++) {
    r+=(char)('a' + i);
    r+=" ";
    if ((j==t-1)&&(i==25-t)) {break;}
    }
    r+="z ";
    }
    return r.trim();

    ReplyDelete
  3. 7.10

    public class Program {
    public static String Puzzle(int t) {
    String output = "";
    for(int i=0; i< 26 * t - t; ++i)
    output += (char)(i % 26 + 'a')+" ";
    return output + "z";
    }
    }

    ReplyDelete
  4. codehunt 7.10

    using System;
    using System.Linq;

    public class Program {
    public static string Puzzle(int t) {
    return String.Join(" ", Enumerable.Repeat("a b c d e f g h i j k l m n o p q r s t u v w x y z", t)).Substring(0, 52*t-t*2) + "z";
    }
    }

    ReplyDelete
  5. Java 8 solution using Stream API (not available yet in CodeHunt though)

    import java.util.stream.Collectors;
    import java.util.stream.IntStream;

    public class Program {

    public static String Puzzle (int t) {
    return IntStream
    .iterate('a', c -> c >= 'a'+25 ? 'a': (c+1))
    .limit(25 * t)
    .mapToObj(c -> (char)c + " ")
    .collect(Collectors.joining()) + "z";
    }
    }

    ReplyDelete