patterncsharpMinor
Compact Braille lookup
Viewed 0 times
lookupbraillecompact
Problem
A related question got me thinking about a compact Braille dictionary.
It certainly would be easier with Int16 or string but was going for most compact.
I don't think there is a case insensitive char so went with dual entries.
It certainly would be easier with Int16 or string but was going for most compact.
I don't think there is a case insensitive char so went with dual entries.
Dictionary braile = new Dictionary()
{ { 'a', 0x20 }, { 'b', 0x30 }, { 'c', 0x24 }, { 'd', 0x26 }, { 'e', 0x22 }, { 'f', 0x34 }
, { 'g', 0x36 }, { 'h', 0x32 }, { 'i', 0x14 }, { 'j', 0x16 }, { 'k', 0x28 }, { 'l', 0x38 }
, { 'm', 0x2c }, { 'n', 0x2e }, { 'o', 0x2a }, { 'p', 0x3c }, { 'q', 0x3e }, { 'r', 0x3a }
, { 's', 0x1c }, { 't', 0x1e }, { 'u', 0x29 }, { 'v', 0x39 }, { 'w', 0x17 }, { 'x', 0x2d }
, { 'y', 0x2f }, { 'z', 0x2b }
, { 'A', 0x20 }, { 'B', 0x30 }, { 'C', 0x24 }, { 'D', 0x26 }, { 'E', 0x22 }, { 'F', 0x34 }
, { 'G', 0x36 }, { 'H', 0x32 }, { 'I', 0x14 }, { 'J', 0x16 }, { 'K', 0x28 }, { 'L', 0x38 }
, { 'M', 0x2c }, { 'N', 0x2e }, { 'O', 0x2a }, { 'P', 0x3c }, { 'Q', 0x3e }, { 'R', 0x3a }
, { 'S', 0x1c }, { 'T', 0x1e }, { 'U', 0x29 }, { 'V', 0x39 }, { 'W', 0x17 }, { 'X', 0x2d }
, { 'Y', 0x2f }, { 'Z', 0x2b }};
foreach (KeyValuePair c in braile)
{
Debug.WriteLine("{0} {1}", c.Key, c.Value.ToBraile());
}
Debug.WriteLine("{0} {1}", 'w', braile['w'].ToBraile());
Debug.WriteLine("{0} {1}", 'B', braile['B'].ToBraile());
public static class MyExtensions
{
public static string ToBraile(this byte b)
{
return Convert.ToString(b, 2).PadLeft(6, '0');
}
}Solution
Just turning some of the comments into an answer, more specific I'm going to address these issues in your code:
This leads to the following code (extending on the
- No need to double the lookup table – By using
char.ToLower(c)you could half the size of the lookup table.
- Shift the offset by
(char) 'a'– If you shift the offset by the value ofayou get a zero-based index.
- No need longer for the
chardictionary – Since we now have a zero-based index we could simply do a lookup in a byte table. (Update: And it could be a simple array also, no need for aList)
- Since we shifted the offset, no need to include empty entries – By shifting the offset, we don't need a sparse 256 byte array. It does however, fail miserably like your code if trying to convert non-alpha characters.
- Wouldn't it be nice to convert longer texts also? – Wouldn't you need to convert longer texts, and not only single characters? For good measure, I've added a
ToString(string)to the code below.
This leads to the following code (extending on the
char value):using System.Text;
public static class MyExtensions
{
static byte[] braille = new byte[]
{ 0x20, 0x30, 0x24, 0x26, 0x22, 0x34, 0x36, 0x32, 0x14, 0x16,
0x28, 0x38, 0x2c, 0x2e, 0x2a, 0x3c, 0x3e, 0x3a, 0x1c, 0x1e,
0x29, 0x39, 0x17, 0x2d, 0x2f, 0x2b
};
public static string ToBraille(this char c) {
return Convert.ToString(braille[ (byte) char.ToLower(c) - (byte) 'a'], 2).PadLeft(6, '0');
}
public static string ToBraille(this string s) {
StringBuilder result = new StringBuilder();
foreach (char c in s) {
result.Append(c.ToBraille()).Append(" ");
}
return result.ToString();
}
}Code Snippets
using System.Text;
public static class MyExtensions
{
static byte[] braille = new byte[]
{ 0x20, 0x30, 0x24, 0x26, 0x22, 0x34, 0x36, 0x32, 0x14, 0x16,
0x28, 0x38, 0x2c, 0x2e, 0x2a, 0x3c, 0x3e, 0x3a, 0x1c, 0x1e,
0x29, 0x39, 0x17, 0x2d, 0x2f, 0x2b
};
public static string ToBraille(this char c) {
return Convert.ToString(braille[ (byte) char.ToLower(c) - (byte) 'a'], 2).PadLeft(6, '0');
}
public static string ToBraille(this string s) {
StringBuilder result = new StringBuilder();
foreach (char c in s) {
result.Append(c.ToBraille()).Append(" ");
}
return result.ToString();
}
}Context
StackExchange Code Review Q#162777, answer score: 2
Revisions (0)
No revisions yet.