snippetcsharpMinor
Generate Braille characters from a dictionary
Viewed 0 times
braillegeneratecharactersdictionaryfrom
Problem
Problem
I would like to generate Braille characters from a dictionary as below which store the n-th active bits:-
in which the a Braille character is a string with length of 6 while 0 and 1 represents bumps and dots.
Algorithm 1
```
private static void PrintBrailleString1()
{
foreach (var entry in BrailleDictionary)
{
var output = new StringBuilder();
for (int i = 1; i -1 ? '1' : '0');
}
Console.WriteLine($"{ entry.Key } => { output
I would like to generate Braille characters from a dictionary as below which store the n-th active bits:-
private static readonly int BrailleStringSize = 6;
Dictionary BrailleDictionary = new Dictionary
{
{ 'a', new int[] { 1 } }, // 100000
{ 'b', new int[] { 1, 2 } }, // 110000
{ 'c', new int[] { 1, 4 } }, // 100100
{ 'd', new int[] { 1, 4, 5 } }, // 100110
{ 'e', new int[] { 1, 5 } }, // 100010
{ 'f', new int[] { 1, 2, 4 } }, // 110100
{ 'g', new int[] { 1, 2, 4, 5 } }, // 110110
{ 'h', new int[] { 1, 2, 5 } }, // 110010
{ 'i', new int[] { 2, 4 } }, // 010100
{ 'j', new int[] { 2, 4, 5 } }, // 010110
{ 'k', new int[] { 1, 3 } }, // 101000
{ 'l', new int[] { 1, 2, 3 } }, // 111000
{ 'm', new int[] { 1, 3, 4 } }, // 101100
{ 'n', new int[] { 1, 3, 4, 5 } }, // 101110
{ 'o', new int[] { 1, 3, 5 } }, // 101010
{ 'p', new int[] { 1, 2, 3, 4 } }, // 111100
{ 'q', new int[] { 1, 2, 3, 4, 5 } }, // 111110
{ 'r', new int[] { 1, 2, 3, 5 } }, // 111010
{ 's', new int[] { 2, 3, 4 } }, // 011100
{ 't', new int[] { 2, 3, 4, 5 } }, // 011110
{ 'u', new int[] { 1, 3, 6 } }, // 101001
{ 'v', new int[] { 1, 2, 3, 6 } }, // 111001
{ 'w', new int[] { 2, 4, 5, 6 } }, // 010111
{ 'x', new int[] { 1, 3, 4, 6 } }, // 101101
{ 'y', new int[] { 1, 3, 4, 5, 6 } }, // 101111
{ 'z', new int[] { 1, 3, 5, 6 } } // 101011
};in which the a Braille character is a string with length of 6 while 0 and 1 represents bumps and dots.
Algorithm 1
```
private static void PrintBrailleString1()
{
foreach (var entry in BrailleDictionary)
{
var output = new StringBuilder();
for (int i = 1; i -1 ? '1' : '0');
}
Console.WriteLine($"{ entry.Key } => { output
Solution
Bit mashing might be fastest
Assign to variable 1,000,000 times in under 10 seconds
Assign to variable 1,000,000 times in under 10 seconds
Stopwatch sw = new Stopwatch();
sw.Start();
Dictionary BrailleDictionary = new Dictionary
{ { 'a', new int[] { 1 } }, // 100000
{ 'b', new int[] { 1, 2 } }, // 110000
{ 'c', new int[] { 1, 4 } }, // 100100
{ 'd', new int[] { 1, 4, 5 } }, // 100110
{ 'e', new int[] { 1, 5 } }, // 100010
{ 'f', new int[] { 1, 2, 4 } }, // 110100
{ 'g', new int[] { 1, 2, 4, 5 } }, // 110110
{ 'h', new int[] { 1, 2, 5 } }, // 110010
{ 'i', new int[] { 2, 4 } }, // 010100
{ 'j', new int[] { 2, 4, 5 } }, // 010110
{ 'k', new int[] { 1, 3 } }, // 101000
{ 'l', new int[] { 1, 2, 3 } }, // 111000
{ 'm', new int[] { 1, 3, 4 } }, // 101100
{ 'n', new int[] { 1, 3, 4, 5 } }, // 101110
{ 'o', new int[] { 1, 3, 5 } }, // 101010
{ 'p', new int[] { 1, 2, 3, 4 } }, // 111100
{ 'q', new int[] { 1, 2, 3, 4, 5 } }, // 111110
{ 'r', new int[] { 1, 2, 3, 5 } }, // 111010
{ 's', new int[] { 2, 3, 4 } }, // 011100
{ 't', new int[] { 2, 3, 4, 5 } }, // 011110
{ 'u', new int[] { 1, 3, 6 } }, // 101001
{ 'v', new int[] { 1, 2, 3, 6 } }, // 111001
{ 'w', new int[] { 2, 4, 5, 6 } }, // 010111
{ 'x', new int[] { 1, 3, 4, 6 } }, // 101101
{ 'y', new int[] { 1, 3, 4, 5, 6 } }, // 101111
{ 'z', new int[] { 1, 3, 5, 6 } } // 101011
};
sw.Start();
for (int i = 0; i c in BrailleDictionary)
{
//9,570
int b = 0;
foreach (int pos in c.Value)
{
b |= 1 { letter }");
//}
}
}
sw.Stop();
Debug.WriteLine(sw.ElapsedMilliseconds.ToString("N0"));Code Snippets
Stopwatch sw = new Stopwatch();
sw.Start();
Dictionary<char, int[]> BrailleDictionary = new Dictionary<char, int[]>
{ { 'a', new int[] { 1 } }, // 100000
{ 'b', new int[] { 1, 2 } }, // 110000
{ 'c', new int[] { 1, 4 } }, // 100100
{ 'd', new int[] { 1, 4, 5 } }, // 100110
{ 'e', new int[] { 1, 5 } }, // 100010
{ 'f', new int[] { 1, 2, 4 } }, // 110100
{ 'g', new int[] { 1, 2, 4, 5 } }, // 110110
{ 'h', new int[] { 1, 2, 5 } }, // 110010
{ 'i', new int[] { 2, 4 } }, // 010100
{ 'j', new int[] { 2, 4, 5 } }, // 010110
{ 'k', new int[] { 1, 3 } }, // 101000
{ 'l', new int[] { 1, 2, 3 } }, // 111000
{ 'm', new int[] { 1, 3, 4 } }, // 101100
{ 'n', new int[] { 1, 3, 4, 5 } }, // 101110
{ 'o', new int[] { 1, 3, 5 } }, // 101010
{ 'p', new int[] { 1, 2, 3, 4 } }, // 111100
{ 'q', new int[] { 1, 2, 3, 4, 5 } }, // 111110
{ 'r', new int[] { 1, 2, 3, 5 } }, // 111010
{ 's', new int[] { 2, 3, 4 } }, // 011100
{ 't', new int[] { 2, 3, 4, 5 } }, // 011110
{ 'u', new int[] { 1, 3, 6 } }, // 101001
{ 'v', new int[] { 1, 2, 3, 6 } }, // 111001
{ 'w', new int[] { 2, 4, 5, 6 } }, // 010111
{ 'x', new int[] { 1, 3, 4, 6 } }, // 101101
{ 'y', new int[] { 1, 3, 4, 5, 6 } }, // 101111
{ 'z', new int[] { 1, 3, 5, 6 } } // 101011
};
sw.Start();
for (int i = 0; i < 1000000; i++)
{
foreach (KeyValuePair<char, int[]> c in BrailleDictionary)
{
//9,570
int b = 0;
foreach (int pos in c.Value)
{
b |= 1 << 6 - pos;
}
string br = Convert.ToString(b, 2).PadLeft(6, '0');
//Debug.WriteLine("{0} {1}", c.Key, Convert.ToString(b, 2).PadLeft(6, '0'));
//var zeros = new string('0', 6);
//foreach (var entry in BrailleDictionary)
//{
// //11,340
// var letter = new StringBuilder(zeros);
// foreach (var bump in entry.Value)
// {
// letter[bump - 1] = '1';
// }
// string br = letter.ToString();
// //Debug.WriteLine($"{ entry.Key } => { letter }");
//}
}
}
sw.Stop();Context
StackExchange Code Review Q#162683, answer score: 3
Revisions (0)
No revisions yet.