Balanced quinary quasiquine
Given an integer $N$ in balanced quinary, output the first $N$ characters of your source code if $N$ is positive, or the last $-N$ characters of your source code if $N$ is negative.
Terminology
Quinary
Standard quinary (base $5$) has digits $0, 1, 2, 3, 4$. In a quinary number a digit $n$ places from the right is worth $5^n$ times that digit's individual value.For example, the quinary number $1234$ converts to decimal like this: $$(1\times5^3)+(2\times5^2)+(3\times5^1)+(4\times5^0)$$ $$(1\times125)+(2\times25)+(3\times5)+(4\times1)$$ $$125+50+15+4$$ $$194$$
Balanced quinary
Balanced quinary works exactly the same as standard quinary, except it uses digits worth $-2,-1,0,1,2$. In a balanced quinary number a digit $n$ places from the right is worth $5^n$ times that digit's individual value, just like in standard quinary. This allows balanced quinary to express all positive and negative integers without the need for a leading minus sign / negation symbol.For the following example we will use $Y$ to represent $-2$ and $Z$ to represent $-1$, so our digits are $Y,Z,0,1,2$.
For example, the balanced quinary number $YZ12$ converts to decimal like this: $$(Y\times5^3)+(Z\times5^2)+(1\times5^1)+(2\times5^0)$$ $$(-2\times125)+(-1\times25)+(1\times5)+(2\times1)$$ $$-250-25+5+2$$ $$-268$$
Input
- The input will be an integer in balanced quinary
- You may choose any $5$ characters to represent the $5$ digits of balanced quinary
- As these digits do not necessarily have to be numeric characters, you are free to choose between taking input as a numeric type, or as a string, including any ordered data structure of characters
- The input will never have leading zeroes
- The input may correspond to a value larger in magnitude (absolute value) than the number of characters in your source code
- The input will never be empty (zero is represented in balanced quinary as the digit $0$)
Output
For input that evaluates to $N$:
- If $N$ is positive, the output is the first $N$ characters of your source code
- If $N$ is negative, the output is the last $-N$ characters of your source code
- If the magnitude (absolute value) of $N$ is greater than the number of characters in your source code, the output consists of your entire source code
- The required characters of your source code must be output in order
- Specifically, if $N$ is negative, the last $-N$ characters must not be in reversed order.
If your source code isQUINTESSENTIAL
the last $9$ characters areESSENTIAL
, notLAITNESSE
- Specifically, if $N$ is negative, the last $-N$ characters must not be in reversed order.
- The output may include an optional trailing newline
- If $N=0$ the output must be empty (apart from the optional trailing newline)
Excluded trivial cases
Using source code of length $3$ or fewer characters is defined to be a challenge-specific loophole, and is not a valid answer.
Detailed justification
Source code with length $3$ characters or fewer meets the requirements trivially, without requiring an implementation of balanced quinary.In order to demonstrate that it is correctly interpreting the balanced quinary base system (not just the individual digits), your code must have at least $2$ different outputs for inputs with $2$ digits.
The smallest magnitude of a $2$ digit balanced quinary number is $3$. This means that source code of $3$ or fewer characters would lead to the full source code being output for all $2$ digit inputs, regardless of value. The shortest source code length that can lead to different outputs for different $2$ digit inputs is therefore length $4$ ($4$ characters - even in cases where that means more than $4$ bytes).
Test cases
These test cases use $Y,Z,0,1,2$ as digits of value $-2,-1,0,1,2$ respectively. In your own code you are free to use whichever character you wish to represent each of these 5 values.
The test cases are in the format
quinary input : decimal equivalent : "output characters"
The output characters are based on your source code being the string QUINTESSENTIAL
and the double quotes do not form part of the output (they just make empty output easier to read for these test cases).
0 : 0 : ""
Z : -1 : "L"
1 : 1 : "Q"
Y : -2 : "AL"
2 : 2 : "QU"
Z2 : -3 : "IAL"
1Y : 3 : "QUI"
Z1 : -4 : "TIAL"
1Z : 4 : "QUIN"
Z0 : -5 : "NTIAL"
10 : 5 : "QUINT"
1000 : 125 : "QUINTESSENTIAL"
Z000 : -125 : "QUINTESSENTIAL"
Scoring
This is a code golf challenge. Your score is the length of your source code in bytes. Lower is better.
Note that your score is in bytes, but the required length of output is measured in characters, in case the two are not equivalent for your chosen language.
Explanations are optional, but I'm more likely to upvote answers that have one.
1 answer
Vyxal D
, 62 bytes
`$f3uV4udV5β$I$:∇₍λ2|62∵Ẏ;ȯ$0<i`$f3uV4udV5β$I$:∇₍λ2|62∵Ẏ;ȯ$0<i
Character map:
4 : -2
3 : -1
0 : 0
1 : 1
2 : 2
Nice, long and yummy quine.
Explained
`$f3uV4udV5β$I$:∇₍λ2|62∵Ẏ;ȯ$0<i`
Push the string "$f3uV4udV5β$I$:∇₍λ2|62∵Ẏ;ȯ$0<i"
to the stack. This is the "data" of the quine. Now onto the code part:
$f3uV4udV5β
$f # Push a list of digits of the input in front of that string
3uV # Replace all 3s with -1
4udV # And replace all 4s with -2
5β # And convert that to base 5. Vyxal is smart enough to know how to handle negative digits in the list.
The above does the balanced quinary conversion.
$I$:∇
$I # Swap the top two stack items to make the stack [quinary number, data string", and quine cheese the string (surround it in backticks and append it to itself)
$:∇ # Swap the top two stack items to make the stack [string, number], duplicate the number and then rotate the stack so it becomes [number, string, number] - this will be used to determine which end to get characters from
₍λ2|62∵Ẏ;ȯ
₍ # Apply the next two things to the same stack:
λ2|62∵Ẏ; # string[0:min(62, number)]
ȯ # string[number:]
This gets number
characters from both the start and end of the string, because there's no built-in to conditionally get either start or end. Also, the lambda is needed to avoid modular indexing with Ẏ
. The stack will now be [number, [string[0:min(62, number)], string[number:]]
.
$0<i
$ # Make the number the top of the stack again
0< # is it negative?
i # index that into the [string[0:min(62, number)], string[number:] list - if number is negative, it'll get string[number:]. Otherwise, it'll get string[0:min(62, number)]
2 comment threads