Pigeon Poem (DNA Decoder)

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
					
public class Program
{
	public static void Main()
	{
		Console.WriteLine("Input DNA Genome");
		//step1 stores the DNA code
		string step1 = Console.ReadLine();
		//step2 will be where the binary is stored
		string step2 = "";
		int splitCounter = 0;
		foreach (char base4i in step1) {//base4i = each character in the DNA base 4. (ATCG)
			/*If it's A or T, translate to 0, but if it's C or G, translate to 1.
			When I was originally solving this, I misunderstood it-
			I thought that by "base pairs" it meant that A = 00, C = 01,
			and T or G = 10 or 11. I ended up with a jumbled mess of nothing.
			i forgot how dna works :D*/
			if (base4i.ToString().ToLower() == "a" || base4i.ToString().ToLower() == "t") {
				step2 += '0';
			}
			else {
				step2 += '1';
			}
			//splitCounter and logic separates each series of 8 bits by a space.
			splitCounter++;
			if (splitCounter == 8) {
				splitCounter = 0;
				step2 = step2 + " ";
			}
		}
		//Displays the DNA decoded to Binary to the console
		Console.WriteLine("Finished decoding to binary: " + step2);
		//step3 just turns step2 into a list/array by separating by the spaces
		List<string> step3 = new List<string>();
		step3 = step2.Split(' ').ToList<string>();
		//step4 will store the ASCII as numbers
		List<int> step4 = new List<int>();
		//This removes the last item in step3. Because, for some reason, we always end up with an extra item.
		//I added that and it worked all the sudden. Sometimes programming works like that. I have no clue haha
		step3.RemoveAt(step3.Count - 1);
		Console.WriteLine();
		Console.WriteLine("Decoding to numbers:");
		foreach (string binaryi in step3) {
			int n = 0;
			//Converts the binary into ASCII as numbers
			try {
				for (int c = 0; c < 8; c++) {
					int ascii = binaryi[7 - c] - '0';
					ascii = ascii * (int)Math.Pow(2, c);
					n += ascii;
				}
			}
			/*I added a try/catch loop here because I felt this error might be common.
			It's possible for users to enter a genome that is not divisible by 8 in character length,
			so I added this to explain what they did wrong instead of just throwing them
			a plain error.*/
			catch (IndexOutOfRangeException e) {
				Console.WriteLine("\n\nInvalid length of DNA code. Should be divisible by 8!");
				Console.WriteLine("Please press \"run\" again.");
				throw(e);
			}
			step4.Add(n);
			Console.Write(n + " ");
		}
		//asciiTo is a list of characters that could be used in the poem
		//asciiFrom is a list of numbers that the characters correspond to
		List<char> asciiTo = new List<char>{' ', '\n', ',', '.', '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', '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', ':', ';', '-'};
		List<int> asciiFrom = new List<int>{32,  10,   44,  46,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  97,  98,  99,  100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 58,  59,  45};
		string finished = "";
		Console.WriteLine();
		foreach (int num in step4) {
			//translates ascii into text
			if (asciiFrom.IndexOf(num) != -1) {
				finished += asciiTo[asciiFrom.IndexOf(num)];
			}
			//If the number is not included in the list of known characters (in asciiTo),
			//it just displays "?"
			else {
				finished += '?';
			}
		}
		//Prints the finished product!
		Console.WriteLine();
		Console.WriteLine(finished);
		
		//I've also put this program into a Repl on Replit, if you would rather view from that site:
		//https://replit.com/@rgtuma19/Pigeon-Poem-DNA-Decoder?v=1#main.cs
		//Please note that Replit viewers cannot run my code without forking it first,
		//so this site is better for just checking out the program.
	}
}
;