Good day,
So, I have a class that converts arabic numerals to roman numerals. I have a loop that executes, if the user selected "Arabisch"(arabic) in the dropdownlist.
If the user selected "römisch"(roman) I want to do the same loop but convert the numerals to roman. In following you can see the code of the both classes. Whats the best way to initialize the method of the "ArabicToRoman" class in my loop to convert the numbers to roman?
namespace Friedhof_Generator
{
class ArabicToRoman
{
private string[] Tausender =
{ "", "M", "MM", "MMM" };
private string[] Hunderter =
{ "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
private string[] Zehner =
{ "", "X", "XX", "XXX", "XL", "L", "LX" , "LXX", "LXXX", "XC" };
private string[] Einer =
{ "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
private string ArabicRoman(int arabic)
{
if (arabic >= 4000)
{
int tausend = arabic / 1000;
arabic %= 1000;
return "(" + ArabicRoman(tausend) + ")" +
ArabicRoman(arabic);
}
string result = "";
int num;
num = arabic / 1000;
result += Tausender[num];
arabic %= 1000;
num = arabic / 100;
result += Hunderter[num];
arabic %= 100;
num = arabic / 10;
result += Zehner[num];
arabic %= 10;
result += Einer[arabic];
return result;
}
}
}
Above the ArabicToRoman class. Below the class with the loops.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Friedhof_Generator
{
public partial class frm_Grabnummer : Form
{
public frm_Grabnummer()
{
InitializeComponent();
ddl_Feld.Items.Add("Arabisch");
ddl_Feld.Items.Add("Römisch");
ddl_Feld.Items.Add("Alphabetisch");
ddl_Reihe.Items.Add("Arabisch");
ddl_Reihe.Items.Add("Römisch");
ddl_Reihe.Items.Add("Alphabetisch");
ddl_Nr.Items.Add("Arabisch");
ddl_Nr.Items.Add("Römisch");
ddl_Nr.Items.Add("Alphabetisch");
}
private void ddl_Feld_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void btn_Start_Click(object sender, EventArgs e)
{
int FeldVon, FeldBis, ReiheVon, ReiheBis, NrVon, NrBis;
string Friedhof;
FeldVon = int.Parse(tb_Von_Feld.Text);
FeldBis = int.Parse(tb_Bis_Feld.Text);
ReiheVon = int.Parse(tb_Von_Reihe.Text);
ReiheBis = int.Parse(tb_Bis_Reihe.Text);
NrVon = int.Parse(tb_Von_Nr.Text);
NrBis = int.Parse(tb_Bis_Nr.Text);
Friedhof = tb_Friedhof.Text;
if (ddl_Feld.Text == "Arabisch")
{
for (int iFeld = FeldVon; iFeld <= FeldBis; iFeld++)
{
for (int iReihe = ReiheVon; iReihe <= ReiheBis; iReihe++)
{
for (int iNr = NrVon; iNr <= NrBis; iNr++)
{
lb_Ausgabe.Items.Add(string.Format("{0} - {1} - {2} - {3}", Friedhof, iFeld, iReihe, iNr));
}
}
}
}
else if(ddl_Feld.Text == "Römisch")
{
}
}
}
}
The ArabicToRoman class seems like a typical helper class, in days gone by you would just easily make it public and static
public static class ArabicToRoman
{
private static string[] Tausender = { "", "M", "MM", "MMM" };
private static string[] Hunderter = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
private static string[] Zehner = { "", "X", "XX", "XXX", "XL", "L", "LX" , "LXX", "LXXX", "XC" };
private static string[] Einer = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
public static string Convert(int arabic)
{
...
}
}
Usage
You can call this from anywhere that access to the assembly
ArabicToRoman.Convert(234);
You could also just make your classes and methods public
public class ArabicToRoman
{
private string[] Tausender = { "", "M", "MM", "MMM" };
private string[] Hunderter = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
private string[] Zehner = { "", "X", "XX", "XXX", "XL", "L", "LX" , "LXX", "LXXX", "XC" };
private string[] Einer = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
public string Convert(int arabic)
{
...
}
}
Usage
Once again you call this anywhere that can access the assembly it lives in
var converter = new ArabicToRoman();
converter.Convert(345);
However these days, there is a big push for Dependency Injection DI and Service architecture, You might make it a singleton Service and inject your converters. However this is debatable as well.
For your case and your level of understanding static classes or an instantiable class seem to be a good fit
Further Reading
static (C# Reference)
Use the static modifier to declare a static member, which belongs to
the type itself rather than to a specific object. The static modifier
can be used with classes, fields, methods, properties, operators,
events, and constructors, but it cannot be used with indexers,
finalizers, or types other than classes.
Related
This question already has answers here:
Random name generator in c#
(8 answers)
Closed 1 year ago.
using System;
namespace npcnames
{
class Program
{
static string RandomVowel()
{
Random rand = new Random();
string[] Vowels = new string[5] { "a", "e", "i", "o", "u" };
int index = rand.Next(Vowels.Length);
string Vowel = Vowels[index];
return Vowel;
}
static string RandomConsonant()
{
Random rand = new Random();
string[] Consonants = new string[21] { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" };
int index = rand.Next(Consonants.Length);
string Consonant = Consonants[index];
return Consonant;
}
static string MaleName()
{
string malename = RandomConsonant() + RandomVowel() + RandomConsonant() + RandomVowel() + RandomConsonant();
return MaleName();
}
static string FemaleName()
{
string femalename = RandomVowel() + RandomConsonant() + RandomVowel() + RandomConsonant() + RandomVowel();
return FemaleName();
}
static void generateFemaleName(int from, int to, int step)
{
for (int a = from; a <= to; a = a + step)
{
Console.WriteLine("Female:");
Console.WriteLine(a + FemaleName());
}
}
static void generateMaleName(int from, int to, int step)
{
for (int b = from; b <= to; b = b + step)
{
Console.WriteLine("Male:");
Console.WriteLine(b + MaleName());
}
}
static void Main(string[] args)
{
generateFemaleName(1,10,1);
Console.WriteLine();
generateMaleName(1,10,1);
}
}
Hi guys I'm new to coding and everything, if anyone can help me how to resolve this issue I would really appreciate it. Problem is that in my code I keep getting stack overflow and I don't know how to prevent it to execute my program normally. The purpose of the program is to generate male and female names of the random selected vowels and consonants in the list of 10 for each.
The issue is here:
static string FemaleName()
{
string femalename = RandomVowel() + RandomConsonant() + RandomVowel() + RandomConsonant() + RandomVowel();
return FemaleName(); // <== bam!
}
Change:
return FemaleName();
to:
return femalename;
The short story is the method is continually calling itself. It's exactly the same for MaleName().
Also, while we are fixing things, Random initialization should be outside the method:
private static Random rand = new Random();
static string RandomConsonant()
{
string[] Consonants = new string[21] { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" };
int index = rand.Next(Consonants.Length);
string Consonant = Consonants[index];
return Consonant;
}
It will be fixed if you change
return MaleName();
to
return malename;
and the same for
return FemaleName();
to
return femalename;
It is the string that you want to return.
As for my requirement i want to change string revision format newly. Based on below format.
E.g:
______________
|Test-A |
| ... +
|Test-Z | ==>IF Test-Z then Next time i need to create Revision "AA"
| ... +
|Test-AA |
| ... +
|Test-AZ |==>IF Test-AZ then Next time i need to create Revision "BA"
| ... +
|Test-BA |
| ... +
|Test-BZ |==>IF Test-BZ then Next time i need to create Revision "CA"
| ... +
|Test-CA |
| ... +
|Test-CZ |==>IF Test-CZ then Next time i need to create Revision "DA"
+_____________+
E.g2 :
A, B ....Z & AA, BB....AZ then I need to change BA...BB, BC....BZ & CA, CB....CZ.
Please help me to solve this task.
I would think that you need two methods to do so:
1.: A String to Integer method which returns you a number where you can add 1.
2.: A method which converts back the number into a string.
I'm currently on my tablet, so here just some untested code:
private int StringToInt(string version)
{
int returnValue =0;
// Iterate through the given string char by char and convert it to an integer
// (A=65 in the ascii table, therefore we nned to substract it)
for(int i=0; i<version.Length;i++)
{
// The alphabet has 26 chars so we just think of your version
// as a base26 number and convert it to a base10 number.
retunValue+=Math.Pow(26,i)*((int)version[version.Length-1-i]-65);
}
return returnValue;
}
and
private string IntToSting(int value)
{
String returnValue=String.Empty;
while(value /26>0)
{
// % is the modulo operator. eg if value was 27, value%26 == 1
returnValue=((char)(value%26+65)).ToString()+returnValue;
value-=value%26;
}
returnValue=((char)(value%26+65)).ToString() +returnValue;
}
Note that these methods only work for capital letters and that they do not handle special cases as for example IntToString(0);
You can now simply use it by calling the methods and adding 1 to the result of StringToInteger before calling IntToString.
version = IntToString(StringToInteger(version)+1);
Sounds like homework:
public class RevisionNumber
{
private static readonly string[] _Representations = new[] { "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" };
private int _Value;
public void Increment()
{
_Value++;
}
public override string ToString()
{
var result = String.Empty;
var value = _Value;
while (value >= _Representations.Length)
{
result = _Representations[(int)value % _Representations.Length] + result;
value /= _Representations.Length;
value--;
}
return _Representations[(int)value] + result;
}
}
Usage:
using System;
namespace ConsoleApplication
{
internal class Program
{
private static void Main(string[] args)
{
var revision = new RevisionNumber();
for (int i = 0; i < 100; i++)
{
Console.WriteLine(revision);
revision.Increment();
}
Console.ReadKey();
}
}
}
The corresponding fiddle is here.
I am building up some easy stuff but already got an error.
In summary : when my code running user will write some sentence to the cmd and my code will separate it to words.
It is alright so far. After separating, I want to make arrays for each word
for example :
cmd screen =" hello world "
seperating to words = hello , world
making them arrays (program should do these aoutomatically)
string[] hello = new string[5]
and
string[] world = new string[5]
Here is where the problem starts. I want to name those new arrays after running. You will write "apple" to cmd and new array with name "apple" should pop out. Already asked for my teacher about this and he says it can be done with dynamic valuables (with var etc). But I don't know how. Here my codes so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> tümkelimeler = new List<string>();
for (int i = 1; i < 100; i++)
{
tümkelimeler.Insert(i, "null");
}
int ü = 0;
while (ü == 0)
{
string s = Console.ReadLine();
string[] kelimeler = s.Split(' ');
// this is where i seperate words from sentence .
}
Console.ReadKey() ;
}
}
}
#Complete Code
I'm showing one way of doing it using dynamic ExpandoObject. We keep adding dynamic properties with string name and array values.
Showing for one sentence, you can run a loop and do it for as many sentences you want.
using System.Dynamic;
string s = "hello world"; //example
IDictionary<string, object> dynamicArrays = new ExpandoObject();
string[] words = s.Split(' '); //hello, world
foreach(var word in words)
{
dynamicArrays[word] = word.Select(c => new String(new char[] { c })).ToArray();
}
What it does in the loop is: For each word, it creates a new property in the object with the word (e.g. "hello") as the name. Then it breaks down the same word as a letter (string) array (h, e, l, l, o) and adds that as the value of the same property.
If you see the dynamic object dynamicArrays, it will have following properties
hello: ['h', 'e', 'l', 'l', 'o']
world: ['w', 'o', 'r', 'l', 'd']
See the ExpandoObject reference here.
Update
If you want to do that for multiple user input, you can do like this.
static void Main(string[] args)
{
string s = string.Empty;
IDictionary<string, object> dynamicArrays = new ExpandoObject();
Console.WriteLine("Keep entering words/sentences. Enter blank/empty line to end.");
s = Console.ReadLine();
while (!string.IsNullOrEmpty(s))
{
string[] words = s.Split(' '); //hello, world
foreach (var word in words)
{
dynamicArrays[word] = word.Select(c => new String(new char[] { c })).ToArray();
}
Console.WriteLine("Enter next sentence : ");
s = Console.ReadLine();
}
// do something with dynamicArrays
return;
}
Here, if you enter three values : hello world, apple, green tiger then this is what you'll get in dynamicArrays object.
{
"hello": [ "h", "e", "l", "l", "o" ],
"world": [ "w", "o", "r", "l", "d" ],
"apple": [ "a", "p", "p", "l", "e" ],
"green": [ "g", "r", "e", "e", "n" ],
"tiger": [ "t", "i", "g", "e", "r" ]
}
The Peptide Encoding Problem consists on Finding substrings of a genome encoding a given amino acid sequence.
The genetic code describes the translation of an RNA 3-mer (codon) into one of 20 different amino acids. The first three circles, moving from the inside out, represent the 1st, 2nd, and 3rd nucleotides of a given codon. The 4th, 5th, and 6th circles define the translated amino acid in three ways: the amino acid’s full name, its 3-letter abbreviation, and its single-letter abbreviation. Three of the 64 total RNA codons are stop codons, which halt translation and implicitly add a 21st stop symbol to the amino acid alphabet.
The problem consists on having
Input: A DNA string Text and an amino acid string Peptide.
and
Output: All substrings of Text encoding Peptide (if any such substrings exist).
Sample Input:
ATGGCCATGGCCCCCAGAACTGAGATCAATAGTACCCGTATTAACGGGTGA
MA
Sample Output:
ATGGCC
GGCCAT
ATGGCC
the result is gotten by reading triplets so, ATGGCC encodes MA, GGCCAT inverse would be CCGGTA written backwards so it is in fact ATGGCC (CCGGTA.reverse)
the complement of genetic code is C-G, G-C, T-A and A-T
So to solve this problem I first use already defined arrays
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
using System.Collections;
class Program
{
private static string[] CODONS = {
"TTT", "TTC", "TTA", "TTG", "TCT",
"TCC", "TCA", "TCG", "TAT", "TAC", "TGT", "TGC", "TGG", "CTT",
"CTC", "CTA", "CTG", "CCT", "CCC", "CCA", "CCG", "CAT", "CAC",
"CAA", "CAG", "CGT", "CGC", "CGA", "CGG", "ATT", "ATC", "ATA",
"ATG", "ACT", "ACC", "ACA", "ACG", "AAT", "AAC", "AAA", "AAG",
"AGT", "AGC", "AGA", "AGG", "GTT", "GTC", "GTA", "GTG", "GCT",
"GCC", "GCA", "GCG", "GAT", "GAC", "GAA", "GAG", "GGT", "GGC",
"GGA", "GGG", };
private static string[] AMINOS_PER_CODON = {
"F", "F", "L", "L", "S", "S",
"S", "S", "Y", "Y", "C", "C", "W", "L", "L", "L", "L", "P", "P",
"P", "P", "H", "H", "Q", "Q", "R", "R", "R", "R", "I", "I", "I",
"M", "T", "T", "T", "T", "N", "N", "K", "K", "S", "S", "R", "R",
"V", "V", "V", "V", "A", "A", "A", "A", "D", "D", "E", "E", "G",
"G", "G", "G", };
private static string[] AMINO_ABBREVIATIONS = {
"F", "L", "I", "M", "V",
"S", "P", "T", "A", "Y", "H", "Q", "N", "K", "D", "E", "C", "W",
"R", "G" };
private static string[] FULL_NAMES = {
"phenylalanine", "leucine",
"isoleucine", "methionine", "valine", "serine", "proline",
"threonine", "alanine", "tyrosine", "histidine", "glutamine",
"asparagine", "lysine", "aspartic acid", "glutamic acid",
"cysteine", "tryptophan", "arginine", "glycine" };
static void Main()
{
//then input is DNA
string dna = "TTCGAATCCGTATTGCTTTGGTTGGTTGAGAGAGCGACCTACATTGCTAGTCAGAATAGGTGATTCACACAAAGCTTAGCACCTGGGCAGCACCCCGTGATGTAAACCTATGGGAACTAAGGGAGTCCTGCGGTTTTAGCCAGCAAGCGAGCCGGCAGGAACACTCATACCATCGGACGCGTTTGACGCCTCCCCGGAAAGGAAGTATTTGAGCCTCATTATTACGTATTGCCCGTTAGTCGACAAATCAAGCCCTCGTACGCAGCTTATTCGTACGACGTGGAGGCGTTCCCACGGGCCTAACACGATTGGAACACCACCATAGTAGTGTGGTTCAAATACCTCCTTTGGAGATCTAGAGCTTCACTCTGATTCTAGAGGCAACTTTACAATCGCTCTACGAAATTGTATGGACATCATCAACCGGATATTCTGGGGCGGTAGAATTTCTTTTGTTCGAATCGCTCTAGGCCAGGATCAAATTAATTGAATTGCGGACTCAAGGATCGCGATAGCCGACACATCGGACGCTGTAGAAAGCCAGTCTCTGGATTTAATCCACCCTCTATGTTTGACAAAGCACTAAAACGGGATAGTTTCGGGTGGTATAAGTTTCCCAAGACGATTGCATCGCAATTCATCAACAACCATGAACTTACTGTTTTAGTACTTCCACACACCTTGTTAAATTACGCCTTTACTTCATGTTGCGGTGTGTGTTAGATAGTGTGCAGCTACAAGTCTACCGCCATCGCAGCTCGGGATACCGGCAGATGAGATGGTCCTGAGCTCGTACCGGACTCAAACTTTTTCCTTTACTACCTAGGAATCGCCCATGCGAATTTGTCGGACACACACCATTACATTAACGTCACAACAGCTACTGTTAGAATTTTGCTCTTGCAAATCCTGGAAAGAGTTAAAAAAACTCTTCCGCGCGCCAATAGGGTAAATAATAGATAGCCAGACGGCTGTAAGAGGTGATGACATTTGCAACAATCATGCTGTCGCATCTTCCGCAAGTTCATGTCGCGCCTAGGCAATGGATCTGCGAATGGGGGCCACGGGGTATGAACTACGGAATTCTAAGAAAGTTGCCATCCAGAGTTAAGGGTTTGAGGCTAGTTGCATCGCTGGTAACGAACTACCTCATTACTTGGACGCGCAGTGTGACTTCACTCCTGTATAGCGATGATGCCAAGCAGGAATTAGCAAATCTGAAGAGCGTTTCCAAACTGGCCACTTGGACTGACACCTATCGCGGGGGATTTCAGCGCGTGTCGCTCTCACATGAGAGCTGCCGTCAGGAGCGGTAGAGTTTAGAGAGGAATGCGACAAACTCCCTATTCACCTCTCTGGTGATGTAAGGATATTTACGCTTAGTTCTATGCCAGGCTTAGGGCCTCTCGGAACTTTGGTGAGTCCTTATTAATTGATGCTACCTCTCCCTTACCTTCGCCCCAAGTCACGTAGAAGTACTCAATCCTGCTACATGATAATCAAATATTTCCAACGTTGGGAAATCGGTGACATCACATACTAGTTAAGAAACCACTGTCAGTGAACTTATATCCGGGGGAGAAAATCTACTAACTTACATACGCTGTGCGAGCAGTTTTCATTATAAGAAAATATACTCCCGAGGTACCGCATCAAGCACGACATTCCCGGAGAGCATAACATTTCGGTGCACCTGCTTTTGTGCGCTTGCTTGCGGTTATTTATAAACTACGCACAAGGCGCAAACCGCAGTGCGCATGTTTTCTCCGCCTGGCTAGAACTCGACATTCTCGTCAACGCCAATCTATGTGAGAGGATTTAGACCTCTGTGAAAACGAGTCCCTCTATAGAATAAATACCCAGATGCCAATGGGGGTTCTATCCGATGGCAGTGCATGGAGTGGTGGCTCCAGATTAAGGATGAGGAGAGGTAAAGATAACAGTTCGGTCGCCACGACGCGTTGCCAATCGAAATATCAGTACTAAAAGGCCCACCGCTCCGCTTTAGTCCGACTTTACATCCTGTGGAAATTGTCGAACGGAGGCTACATCGGGCTATATGAGTGTGAAAACCTATACTTCTCGCGTCGTTACTCAGTGCCGGTCTCCTGTTTCCCCCAGTCTTACGTACCCTTATTGATATTTGCTTCACGTTGAAACGTCCTAACGCAGCGTAAAGAGGTGTTTGAACCTCATTACTATAAAATCGCGATCGAAGGTAGACTACATACGCAAACGCCGAAACCCTCAGTTGGCCTTGTTGCAAGTATGGAACGTTGTAAAATTTTTCCTAGACGTTGAGCTATCGGTACAAGGTCGTTAGCGTCCTTACCCTTCACTTATATGCCCGACAAAACGCGGGTCCTAGTGCAGTGGTGGGAGCTTGGAATCCCGCAATACAAGGACAACCTGTATCTCGTTCGGCGTTCCGCGATCACTCGATCCCGAACCACTCCAAGCCTGGTTGATCAGCAAAAGCGGAAGGATGGATAAAGGGCTACTGGTTAATGGATGTAAACTTCCAATGATGAAATCCTGGAAACGAGGGATCGGGTTACGGTGGCGAACGGGGTACGGCAACGTGGCTATCTAGAGCCCGACGTTACGACTCATGTACATGCTGCTACGTGGTTGAAGCTGACGTTCAGATGAAGCAGTACTGAGTCCTAGGGCTTACTACTACTCCAATAGGTCTGGCCGGCCAGATACAAAAGTTCGTGGCGGCTCACCCCCTTTCTGGCGGGTGTAGCTTGCTGACCGGTTTGCTCGATAACACAGGCTAGCGAATAGTAATGAGGTTCGAAAACCTCTTTCCAACGACTGAAAGGGTCTACACGAACTATCTACATTTCCCCGCCCATGTCCTTCCGTCTGGTTGCTTCTGGAGATCCTTTCGCATTATACCGCAGCGTAGTGGCTCTGGCATATATGAAAAAATCCTTCTGTGGGTATTTGTGCCATCACTTATTGTTCGTACCGATATGGGATTACAAGTGCGATGTGATAATAAGCGAAGAAGCCAACATGTTACACTGTTCATGCGCTCCGGGTAATGTGCGGGCACCATGCTCAGTTCCCGCTCGCAGTTGTCACTGTCCCTGTTTCGGCACCATAATCAACATTTCCACGGCCACGCTGGTGAATAACCGAGGATACCGAAGTACAGCAAGAATGAGAGCGGGACTCCTCCATCTGCTTGTAATACGCCTTCAAGATAGTCCATAAAACGGTCGGGGTCTTGTTTCGGACTAGCCGCTTTGAAACGGTGCATAGTTGTGTCAAGTGTGGACATTGGCTTTCTATCCTCGTCAGCGATCCTCGGAAAGACTCGGGCAGTCGCCCCGAATCGTAATTAGGTAGTAGTGCGGCTCAAAAACTTCCTTCGACCTAACCGCTATAATGTTCGTAGATATAAATTTCGTTTCAGTATTAACAGGCGCACCGTATATATACGGAATGGTGTCGCCCCATTAGCTGCTCGCCAATATTTATCTAAGACCGCGCGCGTCTAGCGCCTTTAGTAGTTGCACCCGAGTATAGTAATGGGGTTCGAAGACTTCCTTCGCAAGGCTGCCATACTGTATCACAAGTACTGACGGAGCCCCGGAGGAGTGCAGGATACGGCAAAGGAGACCATTACCGGGGCATGAGTCCAAGTTAGCCCGTTAGGTGAAGGACGCTGATACAATAGTGAATCCGTTACTGAAAGGTTTAGAAGACCGGGGGGCTCGCACTAGGTCCAAATATTATGAACCCTACTCCTGCAACTGAATTGGCCGTCCAGGCGATATTTAAAAGGGGTTACTAGCAGGTTCATCGGAGCCCGTACTCCTTCCGGGCATAGTCGTTCGACGGGTAGAAATTCATCCAGTCGTGCCGGATACCCCGAGAATACCCCTATTTTTTGATCCTTCACCATCATCGTCCGCGGACTCATCTAAGTACCTCAGACCGAAACTGTTATCGTAGCGAAGAGCGAACTCGAATGACATCGCTTGTCCAACAGGGAAAATATGTAAAGTATATGCAGATTATTATAGGAAGATCACAAACTCCATCGCGCCTAGGCCAAAGACTTGCCAGAACAACATCTCTTCCAGAGCAAGGAAGTGTTTGAACCTCACTATTATCGAGAGAAGTCCCATGAATTTATAATAGTGAGGCTCAAAAACTTCCTTCATCGTCGGGCGCTGGGGCGAGCTAGGCTTCCCTAGCCGTCATTACTGTACCCACGCCAAATATTATCGAGTATGACTACGAAGCCTTCACAAGGGGCTGCAGTAACAGACTAACTCACGCACACCGCAACTACTATCCTAAATTGAGTAAGAAAACCCTCGACTATAGCCCTTGAATTAAATTCGCTATTTAAGGAAGACCGCGCTTCCGCTCGCCCGCGTATAGTTTACCAGTCCCAAGAAACATGTGTGGCCAGCCTACCTGAAGAGCTGTGTAAAGATAGATTCTGACATCCTCAAAAAGAAGTTTTCGAGCCGCACTACTACGCACGGAGCTCCGTTATTCAAGGCATGTCGAAGTACAGCGTGGTGGCCTCTCCTGTTCTCCACCCCAGCTAAACCCACCTTGTTCGAATTCGCGCAACTGTATATGACATGAACACTTACAGGGGTTAGAAGTTACTGCAACCAAGATAGAGCTCGTCGAAGTAATAGTGCGGTTCAAAAACTTCCTTCAATTGGTCTCATCACTTAAATTTAAGAGCTATTGTGAGTACAGGTACGGATGCGGCTTCAGTGGATCTTCAGCATTCATTCCTTGTAGTAATGGGGTTCGAAGACTTCCTTGCCAGGGTACCAAACAAGTCTTGCGCATCCTCCTCCCTAAGGAGGTATTTGAACCCCACTATTACCCACGATAGAACATGCAGGGTTTGATAGTGGAACACCTTTTAGAATCTGGGGATAAATTCCCAGGACTAATGTATGGCTGTAGTAATGAGGTTCAAAAACCTCCTTTTCAGGTGGATCGCAGGCCGTGCTGCCTCACAAGCTGGGACGCCGTCCACGGTATAGCCGGCGTCGGCAGTTACTGTGAAATAGCGGAAACTCGATCCCAATATATCATCTTACGTTTGGCGCCCAATAGTCGCCCAGTACCCGTTGACAGTTCTTTAACTCGGCTTAGAACTACTAGACAGGTTCAACCGAACCTTGCCCTAGTTCCCACTCCCGTAATTCATTTGGGTTTGCATCTAGAATACTGGAGGGTGCATAGACGAAACGTGTACGTCGGAGAAAACGTAAGAAAATGACCTAGACTCATAGTAGTGAGGTTCGAAGACTTCCTTTCAGTGAAATCGATCCACCACTCGCCGCGAAGAGATAATAGCATAGAGCACAAGTGCGCGAGTAGAGAAAAAGGCTATCCCAACCGGGCACGTCCTTCGTGTTTGGCGTTTACATACGGCACCCCGTTTCTGCACGTTAACCGTCTAGTATCCAACGGTGGATGGCGGACGCTAGACTATAGATATGAGATATCGAGACCTGGAGCTGGGTGTGGCTGCAGCCCGGGTCATTGCGGGCTGTGAATTCAAGGGCATGTAAACAAGCGTATATCGAACAGTGGATGGGCACCTGCAATACTCACGGTAGAGTTAGCTCACAGGATTCACGTTGAGGACTATGAGTCCCTCTTCGCTAGCAGTCTGGGGGGATATGGAGTTTAATAGCTTGACGTAGTAATGGGGCTCAAACACCTCTTTGTGTGAGCACAGCTACTTGCATTAAGAGATTCTAAACAGCGATCATCTCGGCTATTTCGGGCCAGCCTTTTCGGCAGGATGTTATGTAGCATTTCTGGAAGCTTCCCCCTCGAATCTACTAGTGGTGAGAAGATGCCCCACCGATATTACTCTTTAATCTTGAGAAACCTAAAACCGATCTGACCTCAGACGGGCGGCTCCCACCCGAGGATAAACTCGTCAATAATAATGTGGCTCGAACACTTCTTTTCTCACTAGGCTTTTACGACACGCCACATGTATTTAAGCATCTACCTAACTTGTGTCTGCTGATATACAGCGCATTCTACGCCCAACCTACCAATTACTTCAACGTAGTGCGTGGCTAAAATTCAGGGGAGCTTCATCTCTGTCTTAATTTGAAGGTTCTTCCGGGGCGTTTGGGAATCTTCGTGCCTTTTGCGAGGTTAAGGTATCAAAGAAGTTTTCGAACCACATTATTACCGCCTTAAGCACCGGCGCATCCTGCTCGTGACAACTCTACCCTGCCCTGATAAAGGCACTGAACGTTCCAGAGAGTGCATCATTGACACGCGAGCAGGCCACAGTAGCCACAAGACGTATGGGTGATTATAGAATTGGTGGAGGTGTTGTTAACGATCAGGAGGACATTAGTGGGAGTTAGGAAAGACCCTATGTTCTCTCTATCGCGGACTTGTAACTTGACAAGCAAAAGGGTAAGAGAGCTGCACACCGAAGCAGGCCCTTCCTATACCTGTTTTTCCTACGCGTAGAGAGGAATCCAGAAAGGTGATAATTGGCATTCGATGAAAAAACAGTGTGCCACTGACTTAGTTCTATATGTGAAGAGCCTGTTAGCACGTGACGGCGGCCTTGGTATAGAGCCCTTAATGGTCTCCATCGCGTAGTAATGGGGCTCGAAAACCTCCTTACTTGGGATTGCGTGGCCTCCTTGTGAGTCATACACAAGGCTTAGGGCTATGGGGCGATACACTCCTTTTCGCGGCGCATGGGGCGGTGATGCCTACATAGTAGTAGTGACTGCCTTTCTGGGGGGCTATTTGTGGATGACCAACACCTGACCAGCGATGCAATCGCTAGGGGAGGTACACCTCTCATATGTTACAACAATCACCGAATTGTGTTTCGAATTCGAATCAAGTTTGCGGTGTCGACCAGATCTGGTCTTGCTGCCATACCGGGTTCGCCGCCTCCGGTGGATAGAACTGCATCTTAAGACATCTGGACCCAGCGGTAAGTAGCGGGAAGAGTTTAGAGTCATTCGTACAACTACAGGCTAAGGGCTTACTGGGGAGTTGTTGTAGGGCATAAAGATCGCCCCATGACTTTTCGTACTTTCCCCGATAGTTCACTCGCAGCGAGCTGCGGCTGGGCTTCGCCACACGAGTACGGGCAACATTTATCTCCTCTAATCACTGGGCACCGCGCGAGGAAATAGAAAACCCTAATCAGTGCTCATGGGCGCATCTATTGGTCTCCGCATGCACGATGCCGCGGAGTGCTTAGTTGTCCCTGCATAATCTTCGTAGATGTATAAGAGATTACCTATTTATTCGGTTTCGGTTCTAGACGTACCTTGCCGCATGAGTATAGGCTAATGAACTGAGTTGGCGCCAGAGGGAAAGGCATAATAATGCGGCTCGAATACTTCCTTAAGGAAGTATTCGAACCACATTACTAT";
//The given peptide
string givenPeptide = "KEVFEPHYY";
char[] givenPeptideArray = givenPeptide.ToCharArray() ;
string codon="";
string auxCodon="";
string convertedCodon = "";
string clean = "";
int pivot = 0;
int foundFlag = 0;
string cleanReverse = "";
int foundFlagReverse = 0;
string convertedCodonReverse = "";
//so the idea I was working was to loop dna string until getting to 3*givenPeptide.Length
//because dna is red 3 by 3...
//then get givenPeptide.Length and compare each char of given peptide with encoded
//letter of scanned triplet
//so for example first givenpeptide is 'M' so I would search for 'ATG'
//then, if found increment a counter and then search for next 'GCC' etc...
//however When testing with longer string I do not get complete result
//I have
for (int pos = 0; pos < dna.Length - (3*givenPeptide.Length) ; pos++) {
pivot = pos;
for (int codonPos = 0; codonPos < givenPeptide.Length; codonPos++)
{
codon = dna.Substring(pivot, 3);
auxCodon = givenPeptideArray[codonPos].ToString();
convertedCodon = codonToAminoAcid(codon);
convertedCodonReverse = codonToAminoAcid(DNAComplement(codon));
if (auxCodon.Equals(convertedCodon) )
{
foundFlag++;
clean += codon;
}
else
{
foundFlag = 0;
clean = "";
}
if (foundFlag == givenPeptide.Length)
{
Console.WriteLine(clean);
foundFlag = 0;
clean = "";
}
//reverse
if (auxCodon.Equals(convertedCodonReverse))
{
foundFlagReverse++;
cleanReverse += codon;
}
else
{
foundFlagReverse = 0;
cleanReverse = "";
}
if (foundFlagReverse == givenPeptide.Length)
{
Console.WriteLine(cleanReverse);
foundFlagReverse = 0;
cleanReverse = "";
}
pivot+=3;
}
}
}//end main
public static string DNAComplement(string dna)
{
char[] array = dna.ToCharArray();
for (int i = 0; i < array.Length; i++)
{
char let = array[i];
if (let == 'A') array[i] = 'T';
else if (let == 'T') array[i] = 'A';
else if (let == 'C') array[i] = 'G';
else if (let == 'G') array[i] = 'C';
}
Array.Reverse(array);
return new string(array);
}
public static string codonToAminoAcid(String codon)
{
for (int k = 0; k < CODONS.Length ; k++)
{
if (CODONS[k].Equals (codon))
{
return AMINOS_PER_CODON[k];
}
}
// never reach here with valid codon
return "X";
}
}//end class
I should get
AAGGAAGTATTTGAGCCTCATTATTAC
AAAGAGGTGTTTGAACCTCATTACTAT
AAGGAGGTATTTGAACCCCACTATTAC
AAAGAAGTTTTCGAACCACATTATTAC
AAGGAAGTGTTTGAACCTCACTATTAT
AAAGAAGTTTTCGAGCCGCACTACTAC
AAGGAAGTATTCGAACCACATTACTAT
ATAATAATGCGGCTCGAATACTTCCTT
GTAGTAATGGGGCTCGAAAACCTCCTT
GTAGTAATGAGGTTCAAAAACCTCCTT
GTAGTAATGGGGTTCGAAGACTTCCTT
ATAATAGTGAGGCTCAAAAACTTCCTT
ATAGTAATGGGGTTCGAAGACTTCCTT
GTAGTAGTGCGGCTCAAAAACTTCCTT
ATAGTAATGAGGTTCGAAAACCTCTTT
ATAATAATGTGGCTCGAACACTTCTTT
GTAGTAATGGGGCTCAAACACCTCTTT
ATAGTAGTGAGGTTCGAAGACTTCCTT
GTAATAGTGCGGTTCAAAAACTTCCTT
ATAGTAGTGTGGTTCAAATACCTCCTT
However I am only getting:
AAGGAAGTATTTGAGCCTCATTATTAC
AAAGAGGTGTTTGAACCTCATTACTAT
AAGGAAGTGTTTGAACCTCACTATTAT
AAAGAAGTTTTCGAGCCGCACTACTAC
AAGGAGGTATTTGAACCCCACTATTAC
AAAGAAGTTTTCGAACCACATTATTAC
I was thinking on problem and It seems the problem is I am not looping all string each time...
It looks like the problem is two-fold. Your outer for loop doesn't go far enough to catch the very end of the dna string. Secondly, it appears that reversing individual codons doesn't work and that you have to take the entire (givenPeptideLength * 3) character string before getting the complement. The following changes output the strings you put in your example so I'm hopeful that it does what you want. It has the benefit of being much more concise as well.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
using System.Collections;
namespace DNA
{
class Program
{
private static string[] CODONS = {
"TTT", "TTC", "TTA", "TTG", "TCT",
"TCC", "TCA", "TCG", "TAT", "TAC", "TGT", "TGC", "TGG", "CTT",
"CTC", "CTA", "CTG", "CCT", "CCC", "CCA", "CCG", "CAT", "CAC",
"CAA", "CAG", "CGT", "CGC", "CGA", "CGG", "ATT", "ATC", "ATA",
"ATG", "ACT", "ACC", "ACA", "ACG", "AAT", "AAC", "AAA", "AAG",
"AGT", "AGC", "AGA", "AGG", "GTT", "GTC", "GTA", "GTG", "GCT",
"GCC", "GCA", "GCG", "GAT", "GAC", "GAA", "GAG", "GGT", "GGC",
"GGA", "GGG", };
private static string[] AMINOS_PER_CODON = {
"F", "F", "L", "L", "S", "S",
"S", "S", "Y", "Y", "C", "C", "W", "L", "L", "L", "L", "P", "P",
"P", "P", "H", "H", "Q", "Q", "R", "R", "R", "R", "I", "I", "I",
"M", "T", "T", "T", "T", "N", "N", "K", "K", "S", "S", "R", "R",
"V", "V", "V", "V", "A", "A", "A", "A", "D", "D", "E", "E", "G",
"G", "G", "G", };
private static string[] AMINO_ABBREVIATIONS = {
"F", "L", "I", "M", "V",
"S", "P", "T", "A", "Y", "H", "Q", "N", "K", "D", "E", "C", "W",
"R", "G" };
private static string[] FULL_NAMES = {
"phenylalanine", "leucine",
"isoleucine", "methionine", "valine", "serine", "proline",
"threonine", "alanine", "tyrosine", "histidine", "glutamine",
"asparagine", "lysine", "aspartic acid", "glutamic acid",
"cysteine", "tryptophan", "arginine", "glycine" };
public static string DNAComplement(string dna)
{
char[] array = dna.ToCharArray();
for (int i = 0; i < array.Length; i++)
{
char let = array[i];
if (let == 'A') array[i] = 'T';
else if (let == 'T') array[i] = 'A';
else if (let == 'C') array[i] = 'G';
else if (let == 'G') array[i] = 'C';
}
Array.Reverse(array);
return new string(array);
}
public static string StringToAminoAcid(String input)
{
string result = "";
for (int i = 0; i < input.Length; i += 3)
{
result += codonToAminoAcid(input.Substring(i, 3));
}
return result;
}
public static string codonToAminoAcid(String codon)
{
for (int k = 0; k < CODONS.Length; k++)
{
if (CODONS[k].Equals(codon))
{
return AMINOS_PER_CODON[k];
}
}
// never reach here with valid codon
return "X";
}
static void Main()
{
//then input is DNA
string dna = "TTCGAATCCGTATTGCTTTGGTTGGTTGAGAGAGCGACCTACATTGCTAGTCAGAATAGGTGATTCACACAAAGCTTAGCACCTGGGCAGCACCCCGTGATGTAAACCTATGGGAACTAAGGGAGTCCTGCGGTTTTAGCCAGCAAGCGAGCCGGCAGGAACACTCATACCATCGGACGCGTTTGACGCCTCCCCGGAAAGGAAGTATTTGAGCCTCATTATTACGTATTGCCCGTTAGTCGACAAATCAAGCCCTCGTACGCAGCTTATTCGTACGACGTGGAGGCGTTCCCACGGGCCTAACACGATTGGAACACCACCATAGTAGTGTGGTTCAAATACCTCCTTTGGAGATCTAGAGCTTCACTCTGATTCTAGAGGCAACTTTACAATCGCTCTACGAAATTGTATGGACATCATCAACCGGATATTCTGGGGCGGTAGAATTTCTTTTGTTCGAATCGCTCTAGGCCAGGATCAAATTAATTGAATTGCGGACTCAAGGATCGCGATAGCCGACACATCGGACGCTGTAGAAAGCCAGTCTCTGGATTTAATCCACCCTCTATGTTTGACAAAGCACTAAAACGGGATAGTTTCGGGTGGTATAAGTTTCCCAAGACGATTGCATCGCAATTCATCAACAACCATGAACTTACTGTTTTAGTACTTCCACACACCTTGTTAAATTACGCCTTTACTTCATGTTGCGGTGTGTGTTAGATAGTGTGCAGCTACAAGTCTACCGCCATCGCAGCTCGGGATACCGGCAGATGAGATGGTCCTGAGCTCGTACCGGACTCAAACTTTTTCCTTTACTACCTAGGAATCGCCCATGCGAATTTGTCGGACACACACCATTACATTAACGTCACAACAGCTACTGTTAGAATTTTGCTCTTGCAAATCCTGGAAAGAGTTAAAAAAACTCTTCCGCGCGCCAATAGGGTAAATAATAGATAGCCAGACGGCTGTAAGAGGTGATGACATTTGCAACAATCATGCTGTCGCATCTTCCGCAAGTTCATGTCGCGCCTAGGCAATGGATCTGCGAATGGGGGCCACGGGGTATGAACTACGGAATTCTAAGAAAGTTGCCATCCAGAGTTAAGGGTTTGAGGCTAGTTGCATCGCTGGTAACGAACTACCTCATTACTTGGACGCGCAGTGTGACTTCACTCCTGTATAGCGATGATGCCAAGCAGGAATTAGCAAATCTGAAGAGCGTTTCCAAACTGGCCACTTGGACTGACACCTATCGCGGGGGATTTCAGCGCGTGTCGCTCTCACATGAGAGCTGCCGTCAGGAGCGGTAGAGTTTAGAGAGGAATGCGACAAACTCCCTATTCACCTCTCTGGTGATGTAAGGATATTTACGCTTAGTTCTATGCCAGGCTTAGGGCCTCTCGGAACTTTGGTGAGTCCTTATTAATTGATGCTACCTCTCCCTTACCTTCGCCCCAAGTCACGTAGAAGTACTCAATCCTGCTACATGATAATCAAATATTTCCAACGTTGGGAAATCGGTGACATCACATACTAGTTAAGAAACCACTGTCAGTGAACTTATATCCGGGGGAGAAAATCTACTAACTTACATACGCTGTGCGAGCAGTTTTCATTATAAGAAAATATACTCCCGAGGTACCGCATCAAGCACGACATTCCCGGAGAGCATAACATTTCGGTGCACCTGCTTTTGTGCGCTTGCTTGCGGTTATTTATAAACTACGCACAAGGCGCAAACCGCAGTGCGCATGTTTTCTCCGCCTGGCTAGAACTCGACATTCTCGTCAACGCCAATCTATGTGAGAGGATTTAGACCTCTGTGAAAACGAGTCCCTCTATAGAATAAATACCCAGATGCCAATGGGGGTTCTATCCGATGGCAGTGCATGGAGTGGTGGCTCCAGATTAAGGATGAGGAGAGGTAAAGATAACAGTTCGGTCGCCACGACGCGTTGCCAATCGAAATATCAGTACTAAAAGGCCCACCGCTCCGCTTTAGTCCGACTTTACATCCTGTGGAAATTGTCGAACGGAGGCTACATCGGGCTATATGAGTGTGAAAACCTATACTTCTCGCGTCGTTACTCAGTGCCGGTCTCCTGTTTCCCCCAGTCTTACGTACCCTTATTGATATTTGCTTCACGTTGAAACGTCCTAACGCAGCGTAAAGAGGTGTTTGAACCTCATTACTATAAAATCGCGATCGAAGGTAGACTACATACGCAAACGCCGAAACCCTCAGTTGGCCTTGTTGCAAGTATGGAACGTTGTAAAATTTTTCCTAGACGTTGAGCTATCGGTACAAGGTCGTTAGCGTCCTTACCCTTCACTTATATGCCCGACAAAACGCGGGTCCTAGTGCAGTGGTGGGAGCTTGGAATCCCGCAATACAAGGACAACCTGTATCTCGTTCGGCGTTCCGCGATCACTCGATCCCGAACCACTCCAAGCCTGGTTGATCAGCAAAAGCGGAAGGATGGATAAAGGGCTACTGGTTAATGGATGTAAACTTCCAATGATGAAATCCTGGAAACGAGGGATCGGGTTACGGTGGCGAACGGGGTACGGCAACGTGGCTATCTAGAGCCCGACGTTACGACTCATGTACATGCTGCTACGTGGTTGAAGCTGACGTTCAGATGAAGCAGTACTGAGTCCTAGGGCTTACTACTACTCCAATAGGTCTGGCCGGCCAGATACAAAAGTTCGTGGCGGCTCACCCCCTTTCTGGCGGGTGTAGCTTGCTGACCGGTTTGCTCGATAACACAGGCTAGCGAATAGTAATGAGGTTCGAAAACCTCTTTCCAACGACTGAAAGGGTCTACACGAACTATCTACATTTCCCCGCCCATGTCCTTCCGTCTGGTTGCTTCTGGAGATCCTTTCGCATTATACCGCAGCGTAGTGGCTCTGGCATATATGAAAAAATCCTTCTGTGGGTATTTGTGCCATCACTTATTGTTCGTACCGATATGGGATTACAAGTGCGATGTGATAATAAGCGAAGAAGCCAACATGTTACACTGTTCATGCGCTCCGGGTAATGTGCGGGCACCATGCTCAGTTCCCGCTCGCAGTTGTCACTGTCCCTGTTTCGGCACCATAATCAACATTTCCACGGCCACGCTGGTGAATAACCGAGGATACCGAAGTACAGCAAGAATGAGAGCGGGACTCCTCCATCTGCTTGTAATACGCCTTCAAGATAGTCCATAAAACGGTCGGGGTCTTGTTTCGGACTAGCCGCTTTGAAACGGTGCATAGTTGTGTCAAGTGTGGACATTGGCTTTCTATCCTCGTCAGCGATCCTCGGAAAGACTCGGGCAGTCGCCCCGAATCGTAATTAGGTAGTAGTGCGGCTCAAAAACTTCCTTCGACCTAACCGCTATAATGTTCGTAGATATAAATTTCGTTTCAGTATTAACAGGCGCACCGTATATATACGGAATGGTGTCGCCCCATTAGCTGCTCGCCAATATTTATCTAAGACCGCGCGCGTCTAGCGCCTTTAGTAGTTGCACCCGAGTATAGTAATGGGGTTCGAAGACTTCCTTCGCAAGGCTGCCATACTGTATCACAAGTACTGACGGAGCCCCGGAGGAGTGCAGGATACGGCAAAGGAGACCATTACCGGGGCATGAGTCCAAGTTAGCCCGTTAGGTGAAGGACGCTGATACAATAGTGAATCCGTTACTGAAAGGTTTAGAAGACCGGGGGGCTCGCACTAGGTCCAAATATTATGAACCCTACTCCTGCAACTGAATTGGCCGTCCAGGCGATATTTAAAAGGGGTTACTAGCAGGTTCATCGGAGCCCGTACTCCTTCCGGGCATAGTCGTTCGACGGGTAGAAATTCATCCAGTCGTGCCGGATACCCCGAGAATACCCCTATTTTTTGATCCTTCACCATCATCGTCCGCGGACTCATCTAAGTACCTCAGACCGAAACTGTTATCGTAGCGAAGAGCGAACTCGAATGACATCGCTTGTCCAACAGGGAAAATATGTAAAGTATATGCAGATTATTATAGGAAGATCACAAACTCCATCGCGCCTAGGCCAAAGACTTGCCAGAACAACATCTCTTCCAGAGCAAGGAAGTGTTTGAACCTCACTATTATCGAGAGAAGTCCCATGAATTTATAATAGTGAGGCTCAAAAACTTCCTTCATCGTCGGGCGCTGGGGCGAGCTAGGCTTCCCTAGCCGTCATTACTGTACCCACGCCAAATATTATCGAGTATGACTACGAAGCCTTCACAAGGGGCTGCAGTAACAGACTAACTCACGCACACCGCAACTACTATCCTAAATTGAGTAAGAAAACCCTCGACTATAGCCCTTGAATTAAATTCGCTATTTAAGGAAGACCGCGCTTCCGCTCGCCCGCGTATAGTTTACCAGTCCCAAGAAACATGTGTGGCCAGCCTACCTGAAGAGCTGTGTAAAGATAGATTCTGACATCCTCAAAAAGAAGTTTTCGAGCCGCACTACTACGCACGGAGCTCCGTTATTCAAGGCATGTCGAAGTACAGCGTGGTGGCCTCTCCTGTTCTCCACCCCAGCTAAACCCACCTTGTTCGAATTCGCGCAACTGTATATGACATGAACACTTACAGGGGTTAGAAGTTACTGCAACCAAGATAGAGCTCGTCGAAGTAATAGTGCGGTTCAAAAACTTCCTTCAATTGGTCTCATCACTTAAATTTAAGAGCTATTGTGAGTACAGGTACGGATGCGGCTTCAGTGGATCTTCAGCATTCATTCCTTGTAGTAATGGGGTTCGAAGACTTCCTTGCCAGGGTACCAAACAAGTCTTGCGCATCCTCCTCCCTAAGGAGGTATTTGAACCCCACTATTACCCACGATAGAACATGCAGGGTTTGATAGTGGAACACCTTTTAGAATCTGGGGATAAATTCCCAGGACTAATGTATGGCTGTAGTAATGAGGTTCAAAAACCTCCTTTTCAGGTGGATCGCAGGCCGTGCTGCCTCACAAGCTGGGACGCCGTCCACGGTATAGCCGGCGTCGGCAGTTACTGTGAAATAGCGGAAACTCGATCCCAATATATCATCTTACGTTTGGCGCCCAATAGTCGCCCAGTACCCGTTGACAGTTCTTTAACTCGGCTTAGAACTACTAGACAGGTTCAACCGAACCTTGCCCTAGTTCCCACTCCCGTAATTCATTTGGGTTTGCATCTAGAATACTGGAGGGTGCATAGACGAAACGTGTACGTCGGAGAAAACGTAAGAAAATGACCTAGACTCATAGTAGTGAGGTTCGAAGACTTCCTTTCAGTGAAATCGATCCACCACTCGCCGCGAAGAGATAATAGCATAGAGCACAAGTGCGCGAGTAGAGAAAAAGGCTATCCCAACCGGGCACGTCCTTCGTGTTTGGCGTTTACATACGGCACCCCGTTTCTGCACGTTAACCGTCTAGTATCCAACGGTGGATGGCGGACGCTAGACTATAGATATGAGATATCGAGACCTGGAGCTGGGTGTGGCTGCAGCCCGGGTCATTGCGGGCTGTGAATTCAAGGGCATGTAAACAAGCGTATATCGAACAGTGGATGGGCACCTGCAATACTCACGGTAGAGTTAGCTCACAGGATTCACGTTGAGGACTATGAGTCCCTCTTCGCTAGCAGTCTGGGGGGATATGGAGTTTAATAGCTTGACGTAGTAATGGGGCTCAAACACCTCTTTGTGTGAGCACAGCTACTTGCATTAAGAGATTCTAAACAGCGATCATCTCGGCTATTTCGGGCCAGCCTTTTCGGCAGGATGTTATGTAGCATTTCTGGAAGCTTCCCCCTCGAATCTACTAGTGGTGAGAAGATGCCCCACCGATATTACTCTTTAATCTTGAGAAACCTAAAACCGATCTGACCTCAGACGGGCGGCTCCCACCCGAGGATAAACTCGTCAATAATAATGTGGCTCGAACACTTCTTTTCTCACTAGGCTTTTACGACACGCCACATGTATTTAAGCATCTACCTAACTTGTGTCTGCTGATATACAGCGCATTCTACGCCCAACCTACCAATTACTTCAACGTAGTGCGTGGCTAAAATTCAGGGGAGCTTCATCTCTGTCTTAATTTGAAGGTTCTTCCGGGGCGTTTGGGAATCTTCGTGCCTTTTGCGAGGTTAAGGTATCAAAGAAGTTTTCGAACCACATTATTACCGCCTTAAGCACCGGCGCATCCTGCTCGTGACAACTCTACCCTGCCCTGATAAAGGCACTGAACGTTCCAGAGAGTGCATCATTGACACGCGAGCAGGCCACAGTAGCCACAAGACGTATGGGTGATTATAGAATTGGTGGAGGTGTTGTTAACGATCAGGAGGACATTAGTGGGAGTTAGGAAAGACCCTATGTTCTCTCTATCGCGGACTTGTAACTTGACAAGCAAAAGGGTAAGAGAGCTGCACACCGAAGCAGGCCCTTCCTATACCTGTTTTTCCTACGCGTAGAGAGGAATCCAGAAAGGTGATAATTGGCATTCGATGAAAAAACAGTGTGCCACTGACTTAGTTCTATATGTGAAGAGCCTGTTAGCACGTGACGGCGGCCTTGGTATAGAGCCCTTAATGGTCTCCATCGCGTAGTAATGGGGCTCGAAAACCTCCTTACTTGGGATTGCGTGGCCTCCTTGTGAGTCATACACAAGGCTTAGGGCTATGGGGCGATACACTCCTTTTCGCGGCGCATGGGGCGGTGATGCCTACATAGTAGTAGTGACTGCCTTTCTGGGGGGCTATTTGTGGATGACCAACACCTGACCAGCGATGCAATCGCTAGGGGAGGTACACCTCTCATATGTTACAACAATCACCGAATTGTGTTTCGAATTCGAATCAAGTTTGCGGTGTCGACCAGATCTGGTCTTGCTGCCATACCGGGTTCGCCGCCTCCGGTGGATAGAACTGCATCTTAAGACATCTGGACCCAGCGGTAAGTAGCGGGAAGAGTTTAGAGTCATTCGTACAACTACAGGCTAAGGGCTTACTGGGGAGTTGTTGTAGGGCATAAAGATCGCCCCATGACTTTTCGTACTTTCCCCGATAGTTCACTCGCAGCGAGCTGCGGCTGGGCTTCGCCACACGAGTACGGGCAACATTTATCTCCTCTAATCACTGGGCACCGCGCGAGGAAATAGAAAACCCTAATCAGTGCTCATGGGCGCATCTATTGGTCTCCGCATGCACGATGCCGCGGAGTGCTTAGTTGTCCCTGCATAATCTTCGTAGATGTATAAGAGATTACCTATTTATTCGGTTTCGGTTCTAGACGTACCTTGCCGCATGAGTATAGGCTAATGAACTGAGTTGGCGCCAGAGGGAAAGGCATAATAATGCGGCTCGAATACTTCCTTAAGGAAGTATTCGAACCACATTACTAT";
//The given peptide
string givenPeptide = "KEVFEPHYY";
int resultCount = 0;
int candidateLength = givenPeptide.Length * 3;
string forward = "";
string backward = "";
for (int pos = 0; pos < dna.Length - candidateLength + 1; pos++) // Added the "+ 1"
{
forward = dna.Substring(pos, candidateLength);
backward = DNAComplement(forward);
// Unremark to get a glimpse into what is happening...
//Console.WriteLine("pos: {0}, forward: {1}, backward: {2}, forwardTranslation: {3}, backwardTranslation: {4}",
// pos, forward, backward, StringToAminoAcid(forward), StringToAminoAcid(backward));
if (
(StringToAminoAcid(forward) == givenPeptide)
||
(StringToAminoAcid(backward) == givenPeptide)
)
{
resultCount++;
Console.WriteLine(String.Format("Result {0,3} at position {1,6}... {2}", resultCount, pos, forward));
}
}
Console.WriteLine("Done");
Console.ReadLine();
}//end main
}//end class
}
I'm sorry if the title is misleading but I have to develop the format for a barcode (either 1D or 2D, doesn't matter). The barcode needs to include a Purchase Order #, Bill of lading #, as well as the Line #, product ID # and quantity of each item that appears on the purchase order.
Anyone have any ideas for doing this?
Here is what I've come up with so far:
PurchaseOrder # - Bill of Lading # | Product1ID Line# Qty | Product2ID Line# Qty| Product3...
What the string for this idea may actually look like:
VO12340000-APX132 * 65465 1 12 * 897965 2 5 * 98464 3 88 *
This idea uses "*" as a break character to tell the difference between items as well as having a required length for each specific trait. To me it seems like there must be a better way to accomplish this. This idea would be very hard to have one of my company's suppliers implement as it is very complicated and precise. Any insight or ideas on how to accomplish this would be greatly appreciated.
EDIT: Although I am using VB.NET. This is more of a general programming logic question.
We're going with QR Codes here. In your application you could use the plain text format and then process them with Regex. Also, they have redundancy and lots of free/open source software to support generation and reading.
There are other complex bar codes like HIBC LIC and HIBC PAS with implementations using Code39 or Code128 (http://www.hibcc.org/autoidupn/standards.htm).
What is important is to use a symbology that will be able to generate a condensed bar code, in order to be readable by common bar code scanners. So you can go with Code39 or Code128 (GS1 family) and replace * with whatever (AIs/FUNC).
If I was you I would go with Code93 Extended symbology leaving the content spec as it is.
The following code is from a library I made in the past for almost all symbologies, that will give you what is needed to implement Code93 Extended. It's in C# but it's easy to convert it to VB.NET. Have in mind that you will have to modify it for your needs, and that it only generates a sequence of bars without drawing them.
private static int[]
C93Table = { 131112, 111213, 111312, 111411, 121113, 121212, 121311, 111114, 131211, 141111, 211113, 211212, 211311, 221112, 221211, 231111, 112113, 112212, 112311, 122112, 132111, 111123, 111222, 111321, 121122, 131121, 212112, 212211, 211122, 211221, 221121, 222111, 112122, 112221, 122121, 123111, 121131, 311112, 311211, 321111, 112131, 113121, 211131, 121221, 312111, 311121, 122211 };
static string[]
C93TableX = { "bU", "aA", "aB", "aC", "aD", "aE", "aF", "aG", "aH", "aI", "aJ", "aK", "aL", "aM", "aN", "aO", "aP", "aQ", "aR", "aS", "aT", "aU", "aV", "aW", "aX", "aY", "aZ", "bA", "bB", "bC", "bD", "bE", " ", "cA", "cB", "cC", "cD", "cE", "cF", "cG", "cH", "cI", "cJ", "cK", "cL", "cM", "cN", "cO", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "cZ", "bF", "bG", "bH", "bI", "bJ", "bV", "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", "bK", "bL", "bM", "bN", "bO", "bW", "dA", "dB", "dC", "dD", "dE", "dF", "dG", "dH", "dI", "dJ", "dK", "dL", "dM", "dN", "dO", "dP", "dQ", "dR", "dS", "dT", "dU", "dV", "dW", "dX", "dY", "dZ", "bP", "bQ", "bR", "bS", "bT" };
static string C93Set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%abcd";
private void Code93X()
{
string source = request.Data;
Error valid = Validate(source, 197, C93Set);
if (valid != Error.None)
{
encoded = new Encoded() { Error = valid };
return;
}
StringBuilder adapt = new StringBuilder();
for (int i = 0; i < source.Length; i++)
adapt.Append(C93TableX[(byte)source[i]]);
Code93(adapt.ToString());
}
private void Code93(string source)
{
StringBuilder bars = new StringBuilder();
bars.Append(111141);
int c = 0, k = 0;
int[] values = new int[source.Length + 1];
for (int i = 0; i < source.Length; i++)
{
values[i] = C93Set.IndexOf(source[i]);
bars.Append(C93Table[values[i]]);
}
if (request.AddChecksum)
{
int weight = 1;
for (int i = source.Length - 1; i >= 0; i--)
{
c += values[i] * weight;
if (++weight == 21)
weight = 1;
}
c %= 47;
values[source.Length] = c;
bars.Append(C93Table[c]);
source += C93Set[c];
weight = 1;
for (int i = source.Length - 1; i >= 0; i--)
{
k += values[i] + weight;
if (++weight == 16)
weight = 1;
}
k %= 47;
bars.Append(C93Table[k]);
if (request.DisplayChecksum)
source += C93Set[k];
}
bars.Append(1111411);
source = string.Concat("*", source, "*");
encoded = new Encoded(bars.ToString(), source);
}
private Error Validate(string source, int maxLength, string set)
{
Error valid = Validate(source, maxLength);
if (valid == Error.None)
for (var i = 0; i < source.Length; i++)
if (set.IndexOf(source[i]) < 0)
{
valid = Error.InvalidCharacters;
break;
}
return valid;
}