Home

Archivos

Buscar

Categorías

Feeds:

RSS / Atom

Top Coder - TwoRotationCypher· 15. December 2008, 00:02

Hace mucho que no hacía uno de Top Coder (mientras tanto voy en el capítulo 8 de Head First C#). Pero ahora hice uno mientras Z veía un concurso en la tele.

Aquí va mi respuesta para el TwoRotationCypher

class TwoRotationCypher
    {
        public String encrypt(int firstSize, int firstRotate, int secondRotate, string message)
        {
            string resultado = "";
            foreach (char caracter in message)
            {
                if (caracter.Equals(' '))
                    resultado += caracter;
                else
                { // Esto jala como yo lo habría hecho de entrada pero si no rotamos 
                  // solo dentro de los grupos decodificar se hace imposible
//                    int rotacion = (int)caracter - 97 < firstSize ? firstRotate : secondRotate;
//                    char c = (char)((((int)caracter - 97 + rotacion) % 26) + 97);
                    char c;
                    if ((int)caracter - 97 < firstSize)
                    {
                        c = (char)((((int)caracter - 97 + firstRotate) % firstSize) + 97);
                    }
                    else
                    {
                        c = (char)((((int)caracter - (97 + firstSize)+secondRotate) % (26-firstSize)) + (97 + firstSize));
                    }
                    resultado += c;
                }
            }
            return resultado;
        }
    }