Sha256: f7d8ac91d5eb423170922f34aa191fce7f1587bf04cec25c77916677603aaba5

Contents?: true

Size: 1.54 KB

Versions: 208

Compression:

Stored size: 1.54 KB

Contents

using System;
using System.Collections.Generic;
using System.Linq;

public class Crypto
{
    public Crypto(string input)
    {
        this.NormalizePlaintext = GetNormalizedPlaintext(input);
        this.Size = this.CalculateSize();
    }

    public int Size { get; private set; }

    public string NormalizePlaintext { get; private set; }

    public string NormalizeCiphertext()
    {
        return string.Join(" ", Transpose(this.PlaintextSegments()));
    }

    public string Ciphertext()
    {
        return string.Join("", Transpose(this.PlaintextSegments()));
    }

    public IEnumerable<string> PlaintextSegments()
    {
        return Chunks(this.NormalizePlaintext, this.Size);
    }

    private int CalculateSize()
    {
        return (int) Math.Ceiling(Math.Sqrt(this.NormalizePlaintext.Length));
    }

    private static string GetNormalizedPlaintext(string input)
    {
        return new string(input.ToLowerInvariant().Where(char.IsLetterOrDigit).ToArray());
    }

    private static IEnumerable<string> Chunks(string str, int chunkSize)
    {
        return Enumerable.Range(0, (int)Math.Ceiling(str.Length / (double)chunkSize))
                            .Select(i => str.Substring(i * chunkSize, Math.Min(str.Length - i * chunkSize, chunkSize)));
    }
    
    private static IEnumerable<string> Transpose(IEnumerable<string> input)
    {
        return input.SelectMany(s => s.Select((c, i) => Tuple.Create(i, c)))
                    .GroupBy(x => x.Item1)
                    .Select(g => new string(g.Select(t => t.Item2).ToArray()));
    }
}

Version data entries

208 entries across 208 versions & 1 rubygems

Version Path
trackler-2.1.0.53 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.52 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.51 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.50 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.49 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.48 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.47 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.46 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.45 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.44 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.43 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.42 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.41 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.40 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.39 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.38 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.37 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.36 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.34 tracks/csharp/exercises/crypto-square/Example.cs
trackler-2.1.0.33 tracks/csharp/exercises/crypto-square/Example.cs