Login Register


[Challenge] All your bijective base are belong to us filter_list
Author
Message
[Challenge] All your bijective base are belong to us #1
This challenge was loosely inspired by another user on the forum, Dijkstra.

Just to give you all a run down.. A bijective base b numeration, where b is a positive integer, is a bijective positional notation that makes use of b symbols with associated values of 1 to b.

Unlike its non-bijective counterpart, no symbol has a value of 0. This way, each non-negative integer n has a unique representation in bijective base b.

Popular bijective numerations include unary, bijective base 2 (used in run-length encoding) and bijective base 26 (used to number columns in spreadsheets).

The Challenge

In this challenge, we define the set M of symbols as

Code:
123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz<=>

and a function i from M to the natural number such that i('1') = 1, …, i('>') = 64.

Given a base b between 1 and 64 (both inclusive), we define that each non-negative integer n corresponds to the string ak…a0, consisting of symbols of M, such that n = bki(ak)+…+b0i(a0).

This correspondence is well-defined and bijective. Since an empty sum is defined as 0, the integer 0 can be encoded as an empty string.

The Task

Accept three strings as input:

An input base b between 1 and 64, encoded as a bijective base 64 string.

A non-negative integer n, encoded as a bijective base b string.

An output base B between 1 and 64, encoded as a bijective base 64 string.

Given these three inputs, encode n as a bijective base B string.

Test Case

All test cases specify the input in the order b, n, B.

Code:
Input:  "4" "" "8" Output: "" Input:  "A" "16" "2" Output: "1112" Input:  "2" "122" "A" Output: "A" Input:  "3" "31" "1" Output: "1111111111" Input:  ">" "Fe" "a" Output: "RS"

The Rules
  • You can read the three strings in any convenient order, as such, an array of strings, a string representation thereof, concatenated or separated by single-character delimiters of your choice.
  • If you choose to print the output to STDOUT, you may only print the symbols and (optionally) a trailing newline.
  • Base conversion built-ins of all kinds are allowed.

No special tricks in here really, except for the [2::5] slicing to get the charset at a lower byte count. Here's my solution written in Python 2 (167 bytes):

Code:
x=range;A=`map(chr,x(49,58)+x(65,91)+x(97,123))`[2::5]+'<=>' r=A.find b,n,B=input() B=r(B)+1 d=0;s='' for c in n:d=d*-~r(b)+r(c)+1 while d:d-=1;s=A[d%B]+s;d/=B print s

Tests:

Code:
"4","","8"     >>> (empty string) ">","Fe","a"   >>> RS "3","31","1"   >>> 1111111111 "A","16","2"   >>> 1112 "2","122","A"  >>> A

Reply

RE: [Challenge] All your bijective base are belong to us #2
I am confused asf xD

Reply

RE: [Challenge] All your bijective base are belong to us #3
(08-31-2015, 01:46 PM)Versified Wrote: I am confused asf xD

I posted this under the impression that those taking part have some sort of prior knowledge.
Don't worry, though. I'll be posting other challenges in the future.

Reply

RE: [Challenge] All your bijective base are belong to us #4
(08-31-2015, 01:49 PM)FUSUL Wrote:
(08-31-2015, 01:46 PM)Versified Wrote: I am confused asf xD

I posted this under the impression that those taking part have some sort of prior knowledge.
Don't worry, though. I'll be posting other challenges in the future.
okay ^^

Reply

RE: [Challenge] All your bijective base are belong to us #5
Here's a quick solution in C:
Code:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define MAX_BASE 64 static char M[] = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz<=>"; const char *base10_to_b(int b, int n, char *buf) { int i = MAX_BASE - 1; while (n) { --n; buf[i--] = M[n % b]; n /= b; } buf[MAX_BASE] = 0; return &buf[i] + 1; } int baseb_to_10(int b, const char *buf) { int result = 0; const char *pch = NULL, *p = buf; int e = strlen(buf); while (*p) { if ((pch = strchr(M, *p))) result += (pch - M + 1) * (int)pow(b, --e); ++p; } return result; } int main(void) { int inbase, outbase; char input_n[256]; char *pch = NULL; char outbuf[MAX_BASE] = { 0 }; inbase = '3'; strcpy(input_n, "31"); outbase = '1'; if (!(pch = strchr(M, inbase))) exit(1); inbase = pch - M + 1; if (!(pch = strchr(M, outbase))) exit(1); outbase = pch - M + 1; puts(base10_to_b(outbase, baseb_to_10(inbase, input_n), outbuf)); exit(0); }

Left out user input for the values as I didn't see it as trivial to the main code itself.
- mostly braindead monkeys on this forum.

Reply

RE: [Challenge] All your bijective base are belong to us #6
(08-31-2015, 01:46 PM)Versified Wrote: I am confused asf xD

Ditto. Wink

Reply







Users browsing this thread: