Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
213
Task/Straddling-checkerboard/C/straddling-checkerboard-1.c
Normal file
213
Task/Straddling-checkerboard/C/straddling-checkerboard-1.c
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
#include <glib.h>
|
||||
|
||||
#define ROWS 4
|
||||
#define COLS 10
|
||||
#define NPRX "/"
|
||||
|
||||
/* wikipedia table
|
||||
const char *table[ROWS][COLS] =
|
||||
{
|
||||
{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" },
|
||||
{ "E", "T", NULL, "A", "O", "N", NULL, "R", "I", "S },
|
||||
{ "B", "C", "D", "F", "G", "H", "J", "K", "L", "M" },
|
||||
{ "P", "Q", NPRX, "U", "V", "W", "X", "Y", "Z", "." }
|
||||
};
|
||||
*/
|
||||
|
||||
/* example of extending the table, COLS must be 11
|
||||
const char *table[ROWS][COLS] =
|
||||
{
|
||||
{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":" },
|
||||
{ "H", "O", "L", NULL, "M", "E", "S", NULL, "R", "T", "," },
|
||||
{ "A", "B", "C", "D", "F", "G", "I", "J", "K", "N", "-" },
|
||||
{ "P", "Q", "U", "V", "W", "X", "Y", "Z", ".", NPRX, "?" }
|
||||
};
|
||||
*/
|
||||
|
||||
// task table
|
||||
const char *table[ROWS][COLS] =
|
||||
{
|
||||
{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" },
|
||||
{ "H", "O", "L", NULL, "M", "E", "S", NULL, "R", "T" },
|
||||
{ "A", "B", "C", "D", "F", "G", "I", "J", "K", "N" },
|
||||
{ "P", "Q", "U", "V", "W", "X", "Y", "Z", ".", NPRX }
|
||||
};
|
||||
|
||||
|
||||
GHashTable *create_table_from_array(const char *table[ROWS][COLS], bool is_encoding)
|
||||
{
|
||||
char buf[16];
|
||||
|
||||
GHashTable *r = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
|
||||
size_t i, j, k, m;
|
||||
|
||||
for(i = 0, m = 0; i < COLS; i++)
|
||||
{
|
||||
if (table[1][i] == NULL) m++;
|
||||
}
|
||||
|
||||
const size_t SELNUM = m;
|
||||
|
||||
size_t selectors[SELNUM];
|
||||
size_t numprefix_row, numprefix_col;
|
||||
bool has_numprefix = false;
|
||||
|
||||
// selectors keep the indexes of the symbols to select 2nd and 3rd real row;
|
||||
// nulls must be placed into the 2nd row of the table
|
||||
for(i = 0, k = 0; i < COLS && k < SELNUM; i++)
|
||||
{
|
||||
if ( table[1][i] == NULL )
|
||||
{
|
||||
selectors[k] = i;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
// numprefix is the prefix to insert symbols from the 1st row of table (numbers)
|
||||
for(j = 1; j < ROWS; j++)
|
||||
{
|
||||
for(i = 0; i < COLS; i++)
|
||||
{
|
||||
if (table[j][i] == NULL) continue;
|
||||
if ( strcmp(table[j][i], NPRX) == 0 )
|
||||
{
|
||||
numprefix_col = i;
|
||||
numprefix_row = j;
|
||||
has_numprefix = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// create the map for each symbol
|
||||
for(i = has_numprefix ? 0 : 1; i < ROWS; i++)
|
||||
{
|
||||
for(j = 0; j < COLS; j++)
|
||||
{
|
||||
if (table[i][j] == NULL) continue;
|
||||
if (strlen(table[i][j]) > 1)
|
||||
{
|
||||
fprintf(stderr, "symbols must be 1 byte long\n");
|
||||
continue; // we continue just ignoring the issue
|
||||
}
|
||||
if (has_numprefix && i == (ROWS-1) && j == numprefix_col && i == numprefix_row) continue;
|
||||
if (has_numprefix && i == 0)
|
||||
{
|
||||
snprintf(buf, sizeof(buf), "%s%s%s", table[0][selectors[SELNUM-1]], table[0][numprefix_col], table[0][j]);
|
||||
}
|
||||
else if (i == 1)
|
||||
{
|
||||
snprintf(buf, sizeof(buf), "%s", table[0][j]);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(buf, sizeof(buf), "%s%s", table[0][selectors[i-2]], table[0][j]);
|
||||
}
|
||||
if (is_encoding) g_hash_table_insert(r, strdup(table[i][j]), strdup(buf));
|
||||
else g_hash_table_insert(r, strdup(buf), strdup(table[i][j]));
|
||||
}
|
||||
}
|
||||
if (is_encoding) g_hash_table_insert(r, strdup("mode"), strdup("encode"));
|
||||
else g_hash_table_insert(r, strdup("mode"), strdup("decode"));
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
char *decode(GHashTable *et, const char *enctext)
|
||||
{
|
||||
char *r = NULL;
|
||||
|
||||
if (et == NULL || enctext == NULL || strlen(enctext) == 0 ||
|
||||
g_hash_table_lookup(et, "mode") == NULL ||
|
||||
strcmp(g_hash_table_lookup(et, "mode"), "decode") != 0) return NULL;
|
||||
|
||||
GString *res = g_string_new(NULL);
|
||||
GString *en = g_string_new(NULL);
|
||||
|
||||
for( ; *enctext != '\0'; enctext++ )
|
||||
{
|
||||
if (en->len < 3)
|
||||
{
|
||||
g_string_append_c(en, *enctext);
|
||||
r = g_hash_table_lookup(et, en->str);
|
||||
if (r == NULL) continue;
|
||||
g_string_append(res, r);
|
||||
g_string_truncate(en, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "decoding error\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
r = res->str;
|
||||
g_string_free(res, FALSE);
|
||||
g_string_free(en, TRUE);
|
||||
return r;
|
||||
}
|
||||
|
||||
char *encode(GHashTable *et, const char *plaintext, int (*trasf)(int), bool compress_spaces)
|
||||
{
|
||||
GString *s;
|
||||
char *r = NULL;
|
||||
char buf[2] = { 0 };
|
||||
|
||||
if (plaintext == NULL ||
|
||||
et == NULL || g_hash_table_lookup(et, "mode") == NULL ||
|
||||
strcmp(g_hash_table_lookup(et, "mode"), "encode") != 0) return NULL;
|
||||
|
||||
s = g_string_new(NULL);
|
||||
|
||||
for(buf[0] = trasf ? trasf(*plaintext) : *plaintext;
|
||||
buf[0] != '\0';
|
||||
buf[0] = trasf ? trasf(*++plaintext) : *++plaintext)
|
||||
{
|
||||
if ( (r = g_hash_table_lookup(et, buf)) != NULL )
|
||||
{
|
||||
g_string_append(s, r);
|
||||
}
|
||||
else if (isspace(buf[0]))
|
||||
{
|
||||
if (!compress_spaces) g_string_append(s, buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "char '%s' is not encodable%s\n",
|
||||
isprint(buf[0]) ? buf : "?",
|
||||
!compress_spaces ? ", replacing with a space" : "");
|
||||
if (!compress_spaces) g_string_append_c(s, ' ');
|
||||
}
|
||||
}
|
||||
|
||||
r = s->str;
|
||||
g_string_free(s, FALSE);
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
GHashTable *enctab = create_table_from_array(table, true); // is encoding? true
|
||||
GHashTable *dectab = create_table_from_array(table, false); // is encoding? false (decoding)
|
||||
|
||||
const char *text = "One night-it was on the twentieth of March, 1888-I was returning";
|
||||
|
||||
char *encoded = encode(enctab, text, toupper, true);
|
||||
printf("%s\n", encoded);
|
||||
|
||||
char *decoded = decode(dectab, encoded);
|
||||
printf("%s\n", decoded);
|
||||
|
||||
free(decoded);
|
||||
free(encoded);
|
||||
g_hash_table_destroy(enctab);
|
||||
g_hash_table_destroy(dectab);
|
||||
|
||||
return 0;
|
||||
}
|
||||
88
Task/Straddling-checkerboard/C/straddling-checkerboard-2.c
Normal file
88
Task/Straddling-checkerboard/C/straddling-checkerboard-2.c
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
const char * board = "ET AON RIS"
|
||||
"BCDFGHJKLM"
|
||||
"PQ/UVWXYZ.";
|
||||
|
||||
char encode[128] = {0};
|
||||
char decode[128] = {0};
|
||||
int row[2] = {0};
|
||||
|
||||
void read_table(const char *s)
|
||||
{
|
||||
int i, code;
|
||||
for (i = 0; i < 30; i++) {
|
||||
if (s[i] == '\0') {
|
||||
fprintf(stderr, "Table too short\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (s[i] == ' ') {
|
||||
row[ row[0] ? 1 : 0 ] = i;
|
||||
continue;
|
||||
}
|
||||
|
||||
code = ((i < 10) ? 0 : i < 20 ? row[0] : row[1])
|
||||
* 10 + (i % 10);
|
||||
encode[0 + s[i]] = code; /* guess what 0 + s[i] does, sigh */
|
||||
decode[code] = s[i];
|
||||
}
|
||||
}
|
||||
|
||||
void encipher(const char *in, char *out, int strip)
|
||||
{
|
||||
#define PUTCODE(c) { if (c > 9) {*(out++) = c / 10 + '0'; c %= 10;} *(out++) = c + '0'; }
|
||||
int c, code;
|
||||
while ((c = *(in++)) != '\0') {
|
||||
if (c >= '0' && c <= '9') {
|
||||
code = encode['.'];
|
||||
c -= '0';
|
||||
PUTCODE(code);
|
||||
PUTCODE(c);
|
||||
continue;
|
||||
}
|
||||
|
||||
c &= ~0x20;
|
||||
|
||||
if (c >= 'A' && c <= 'Z') code = encode[c];
|
||||
else if (strip && !c ) continue;
|
||||
else code = encode['/'];
|
||||
|
||||
PUTCODE(code);
|
||||
}
|
||||
*(out++) = '\0';
|
||||
}
|
||||
|
||||
void decipher(const char *in, char *out, int strip)
|
||||
{
|
||||
int c;
|
||||
while ((c = *(in++)) != '\0') {
|
||||
c -= '0';
|
||||
if (c == row[0] || c == row[1])
|
||||
c = c * 10 + *(in++) - '0';
|
||||
|
||||
c = decode[c];
|
||||
|
||||
if (c == '.') c = *(in++);
|
||||
if (c == '/' && !strip) c = ' ';
|
||||
*(out++) = c;
|
||||
}
|
||||
*(out++) = '\0';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
const char *msg = "In the winter 1965/we were hungry/just barely alive";
|
||||
char enc[100] = {0}, dec[100] = {0};
|
||||
read_table(board);
|
||||
|
||||
printf("message: %s\n", msg);
|
||||
encipher(msg, enc, 0); printf("encoded: %s\n", enc);
|
||||
decipher(enc, dec, 0); printf("decoded: %s\n", dec);
|
||||
|
||||
printf("\nNo spaces:\n");
|
||||
encipher(msg, enc, 1); printf("encoded: %s\n", enc);
|
||||
decipher(enc, dec, 1); printf("decoded: %s\n", dec);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
message: In the winter 1965/we were hungry/just barely alive
|
||||
encoded: 85621250626585107626916996966956265062650706225635247676226639162203702867623288640
|
||||
decoded: IN THE WINTER 1965 WE WERE HUNGRY JUST BARELY ALIVE
|
||||
|
||||
No spaces:
|
||||
encoded: 851250658510769169969669562650650702563524767622663912037028673288640
|
||||
decoded: INTHEWINTER1965/WEWEREHUNGRY/JUSTBARELYALIVE
|
||||
Loading…
Add table
Add a link
Reference in a new issue