18
2
Input:
We take two inputs:
- An input
b
with two distinct values:Left
andRight
.† - And a positive integer
n
.
Output:
Based on the Left/Right input, we output either of the following two sequences in the range of 1-n
(in the sequences below the first 125 items are displayed):
Left:
1, 6, 7, 56, 57, 62, 63, 960, 961, 966, 967, 1016, 1017, 1022, 1023, 31744, 31745, 31750, 31751, 31800, 31801, 31806, 31807, 32704, 32705, 32710, 32711, 32760, 32761, 32766, 32767, 2064384, 2064385, 2064390, 2064391, 2064440, 2064441, 2064446, 2064447, 2065344, 2065345, 2065350, 2065351, 2065400, 2065401, 2065406, 2065407, 2096128, 2096129, 2096134, 2096135, 2096184, 2096185, 2096190, 2096191, 2097088, 2097089, 2097094, 2097095, 2097144, 2097145, 2097150, 2097151, 266338304, 266338305, 266338310, 266338311, 266338360, 266338361, 266338366, 266338367, 266339264, 266339265, 266339270, 266339271, 266339320, 266339321, 266339326, 266339327, 266370048, 266370049, 266370054, 266370055, 266370104, 266370105, 266370110, 266370111, 266371008, 266371009, 266371014, 266371015, 266371064, 266371065, 266371070, 266371071, 268402688, 268402689, 268402694, 268402695, 268402744, 268402745, 268402750, 268402751, 268403648, 268403649, 268403654, 268403655, 268403704, 268403705, 268403710, 268403711, 268434432, 268434433, 268434438, 268434439, 268434488, 268434489, 268434494, 268434495, 268435392, 268435393, 268435398, 268435399, 268435448, 268435449
Right:
1, 4, 7, 32, 39, 56, 63, 512, 527, 624, 639, 896, 911, 1008, 1023, 16384, 16415, 16864, 16895, 19968, 19999, 20448, 20479, 28672, 28703, 29152, 29183, 32256, 32287, 32736, 32767, 1048576, 1048639, 1050560, 1050623, 1079296, 1079359, 1081280, 1081343, 1277952, 1278015, 1279936, 1279999, 1308672, 1308735, 1310656, 1310719, 1835008, 1835071, 1836992, 1837055, 1865728, 1865791, 1867712, 1867775, 2064384, 2064447, 2066368, 2066431, 2095104, 2095167, 2097088, 2097151, 134217728, 134217855, 134225792, 134225919, 134471680, 134471807, 134479744, 134479871, 138149888, 138150015, 138157952, 138158079, 138403840, 138403967, 138411904, 138412031, 163577856, 163577983, 163585920, 163586047, 163831808, 163831935, 163839872, 163839999, 167510016, 167510143, 167518080, 167518207, 167763968, 167764095, 167772032, 167772159, 234881024, 234881151, 234889088, 234889215, 235134976, 235135103, 235143040, 235143167, 238813184, 238813311, 238821248, 238821375, 239067136, 239067263, 239075200, 239075327, 264241152, 264241279, 264249216, 264249343, 264495104, 264495231, 264503168, 264503295, 268173312, 268173439, 268181376, 268181503, 268427264, 268427391
How are these sequences generated you ask?
A default sequence from 1 through n=10
would be:
As integer:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
As binary:
1 10 11 100 101 110 111 1000 1001 1010
When we stretch left, the binary will become this:
1, 110, 111, 111000, 111001, 111110, 111111, 1111000000, 1111000001, 1111000110
Why? The last bit is used once; the single-last is used twice; the second-last is used three times; etc.
So `1010` will become (spaces added as clarification): `1111 000 11 0`
And these new left-stretched binary strings are converted back to integers:
1, 6, 7, 56, 57, 62, 63, 960, 961, 966
As for stretched right, the first bit is used once; second twice; third three times; etc. Like this:
As binary:
1, 100, 111, 100000, 100111, 111000, 111111, 1000000000, 1000001111, 1001110000
As integer:
1, 4, 7, 32, 39, 56, 63, 512, 527, 624
Challenge rules:
- † You can take any two distinct values, but state which one you use. So it can be
1/0
,true/false
,null/undefined
,"left"/"right"
, etc. n
is always larger than 0.- You should support a maximum output of at least your language's default integer (which is 32-bit for most languages).
- Output format is flexible. Can be printed or returned as array/list. Can be with a space, comma, pipe, and alike as delimiter. Your call. (Again, please state what you've used.)
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code.
- Also, please add an explanation if necessary.
1Would you consider accepting bitwise-based answers that can only support
n < 128
, so that the results fit in 32-bit integers? – Arnauld – 2017-08-18T16:17:25.193@Arnauld Been doubting about it, but since you asked, why not. Will change the rules for 1000 max to what fits for your language's integer. – Kevin Cruijssen – 2017-08-18T18:33:51.210
@KevinCruijssen Would still recommend limiting that to at least 16 bits - there's at least one language out there that supports only a single bit as a data type. – None – 2018-01-10T21:46:09.447