挑战在于将零到10(含零)的数字输出到您的终端,窗口,画布或屏幕。每个输出的数字必须显示为4位宽的半字节,因此零必须显示为0000,依此类推。
您可以使用空格,逗号或回车符分隔输出的每个数字。最小的解决方案获胜,但是只要序列中没有重复的数字,数字就可以按照您喜欢的任何顺序显示。
如果不可能用逗号或空格输出(例如,标准输出仅限于二进制,或者您的解决方案是用于较早的计算机套件),则使用低级二进制语言的条目无需担心逗号或空格分隔符例如具有受限数字显示的KIM-1)。
挑战在于将零到10(含零)的数字输出到您的终端,窗口,画布或屏幕。每个输出的数字必须显示为4位宽的半字节,因此零必须显示为0000,依此类推。
您可以使用空格,逗号或回车符分隔输出的每个数字。最小的解决方案获胜,但是只要序列中没有重复的数字,数字就可以按照您喜欢的任何顺序显示。
如果不可能用逗号或空格输出(例如,标准输出仅限于二进制,或者您的解决方案是用于较早的计算机套件),则使用低级二进制语言的条目无需担心逗号或空格分隔符例如具有受限数字显示的KIM-1)。
Answers:
0:10YB
在MATL Online上尝试
说明
0:10 % Create the array [0...10]
YB % Convert this array to a binary string where each number is
% placed on a new row
% Implicitly display the result
T # push 10
4ã # cartesian product repeat with 4
R # reverse list
T>£ # take the first 11 elements of the list
» # join by newline and display
GôA,_¤Å
在这里,我认为Japt注定要比其他所有高尔夫语言都要长...
GôA,_¤Å // Implicit: A = 10, G = 16
GôA // Create the inclusive range [G...G+A].
_ // Map each item Z to Z
¤ // .toString(2)
Å // .slice(1).
// Implicit: output result of last expression
通常情况下逗号可以Japt被删除,但是这一次是因为有一个错误的:_正常手段function(Z){Z,但由于某些原因,编译器认为A_手段function(A,Z){Z。
Aô_¤
2Bṗ4ṫ6Y
(如果允许半字节的尾部行,则为5个字节,2Bṗ4Y)
按降序打印。
2Bṗ4ṫ6Y - Main link, no arguments
2B - 2 converted to binary -> [1,0]
ṗ4 - Cartesian 4th power -> [[1,1,1,1], [1,1,1,0], ..., [0,0,0,0]]
i.e. 16 , 15 ..., 0
ṫ6 - tail from 6th item -> [[1,0,1,0], [1,0,0,1], ..., [0,0,0,0]]
i.e. 10 , 9 , ..., 0
Y - join with line feeds
- implicit print
一种替代7- byter是2ṗ4Ịṫ6Y,所述[1,0]被替换[1,2]并且Ị是“是微不足道的”单子(abs(z)<=1),转换2s到0秒。
B{G+2b1>}%N*
笛卡尔幂方法本来是我的选择,但已被采用。
因此,这会生成从0到10的数字,并且每个数字都会加上16,然后转换为二进制。加16可以确保生成所需的前导零,并删除多余的前导零。
B e# Push 11
{ }% e# Map over "11", implicitly converted to the array [0 1 ... 10]
G+ e# Add 16. This makes sure there will be 5 binary digits: a leading 1
e# which will have to be removed and the remaining, valid digits
2b e# Convert to array of binary digits
1> e# Remove first digit
N* e# Join by newlines. Implicitly converts arrays to strings
⁴r26BḊ€Y
我的果冻不怎么好,所以我会接受任何提示!
这使用了Emigna的第一个算法
感谢Dennis减少了两个字节,这使我可以自己回答。:P
说明:
Ḋ€ # Return all but the first element of each item in the list:
⁴r26 # [16, 17, 18, ... 26]
B # Converted to binary
Y # And joined with newlines
Ḋ€ saves a byte.
⁴r27 saves another one.
n=16;exec"print bin(n)[3:];n+=1;"*11
Thanks to @DJMcMayhem for golfing off 2 bytes!
for n in range(11):print bin(n+16)[3:] also at 38 bytes.
n=16;exec"print bin(n)[3:];n+=1;"*11 is two shorter
2Ḷṗ4ḣ11Y
2Ḷṗ4ḣ11Y Main link.
2Ḷ Unlength 2; yield [0, 1].
ṗ4 Take the fourth Cartesian power.
ḣ11 Head 11; discard all but the first eleven elements.
Y Join, separating by linefeeds.
~16.aL1{2B26q}:
This has been a very good modivation to add a pad function. The entirety of ]L4\-'0'\m\., more than half the code, is to pad.
_Saved 6 bytes thanks to @ETHProductions, that's the pad function cut in half.
~16.aL1{2B26q}:
~ # Zero Space Segment
16. # The literal number 16
aL # The length of the Alphabet
1 # The literal number 1
{ }: # For each number between 16 and 26 inclusive
2B # Convert to base 2
26q # Get the characters between 2 and 6 inclusive.
length of the Alphabet Nice way to save a byte ;-)
%%%%
+`(^|\b)%
0$%'¶$%`1
11!`\d+
Explanation
%%%%
Replace the empty (non-existent) input with %%%%.
+`(^|\b)%
0$%'¶$%`1
On the first run of this stage, it will match ^% and essentially replace the text %%%% with the two lines 0%%% and 1%%%. The stage will loop until the output stops changing. On the second run, it will match \b% (since digits count as word characters and % doesn't), and replace the groups by duplicating them and adding 0 to one copy and 1 to the other: 0%%% becomes the lines 00%% and 01%% (and the same sort of thing for 1%%%). Through this loop all 16 bitstrings will be produced, linefeed separated.
11!`\d+
The first 11 matches of \d+ (a run of at least 1 digit) are retrieved. The matches are output in a linefeed-separated list.
0$%'¶$%1` line works. What do $%, `1, '¶ represent?
$%` represents everything before the match on the same line, and $%' is everything after the match on the same line. ¶ is a literal linefeed. So basically the replacement matches the first % on a line and replaces it with 0 plus the rest of the line it was on, a newline, the beginning of the line it was on, and a 1. Of course, the beginning and end of the line it was on are untouched by the replacement because they weren't part of the match.
G11` as the last line of the regex instead
,....>,.<...+.>.<-..+.-.>.<..+..>.<-.+.-..>.<.+.-.+.>.<-.+..-.>.<.+...>.<.-...>.<+.-..+.>.<.-.+.-.!0
Requires a trailing newline. Makes use of ! symbol (so, check the box that says !) with this interpreter (try it online!).
Potentially 51 bytes if each operator was considered as 4 bits
! checkbox being enabled.
Golfed
()=>{for(int i=0;i++<11;)System.Console.WriteLine(System.Convert.ToString(i,2).PadLeft(4,'0'));}
Ungolfed
() => {
for( int i = 0; i++ < 1; )
System.Console.WriteLine( System.Convert.ToString( i, 2 ).PadLeft( 4, '0' ) );
}
Full code
using System;
namespace Namespace {
class Program {
static void Main( string[] args ) {
m();
Console.ReadLine();
}
static void m() {
for( Int32 i = 0; i++ < 11; )
Console.WriteLine(
Convert.ToString( i, 2 ). // Converts the number to binary code
PadLeft( 4, '0' ) ); // Fills the number with the missing '0's
}
}
}
Releases
96 bytes - Initial solution.n,i,m,k[4]={0};f(){for(m=0;m<=10;m++){n=m;i=0;for(;n;i++){k[i]=n;n/=2;}for(i=4;i>0;i--)printf("%d",k[i-1]%2);puts("");}}
Ungolfed version:
void f()
{
int n,i,m,k[4]={0};
for(m=0;m<=10;m++)
{
n=m;
i=0;
for(;n;i++)
{
k[i]=n;
n/=2;
}
for(i=4;i>0;i--)
printf("%d",k[i-1]%2);
puts("");
}
}
Can definitely be shortened!?
@Ahemone Awesome idea, Thanks!
Should work now! Try it online!
for loop in your golfed version should go to 4 rather than 3, but that doesn't matter because the loop can be eliminated entirely and the second for loop can start from 0. You can also just use while(n), but compacting the while loop down into a for loop saves more again. n/=2 will also save you a byte over the shift. You're also missing a terminating } on the golfed version causing an error on compilation.
} and improved the code, 50 bytes shorter based on your idea.
We can use intToBin function from the R.utils package:
R.utils::intToBin(0:10)
[1] "0000" "0001" "0010" "0011" "0100" "0101" "0110" "0111" "1000" "1001" "1010"
Approach 1: 75 73 74 bytes
m;p(i){putchar(i?m&i?49:48:9);}r(){for(m=11;m--;p(4),p(2),p(1),p(0))p(8);}
Approach 2: 68 69 bytes
m,n,o;f(){for(m=11;m--;)for(n=m,o=5;o--;n*=2)putchar(o?n&8?49:48:9);}
m,n;f(o) instead of m,n,o;f()
for x in range(11):print bin(x)[2:].zfill(4)
This uses the zfill function which works like rjust except it always padds with 0 so you don't waste bytes on an argument.
lambda k,l:' '*(len(k)-l)+k) Wow... +1 just because of this :D
TFw0+b2t
TFw0+b2t - for i in range(10):
w0+ - i+16
b2 - bin(^)
t - ^[:-1]
Also 8 bytes:
TF 4@b2t
4@ - set_bit(4, i)
11:>[2 baserep'0'4 pad out]map
11:> is a range from 0 to 10. The rest is rather self-explanatory.
Other solutions that I've found:
11:>[bits 4 dpad''join out]map
11:>[bits 4 dpad$tostrmap]map out
11~>15+[bits behead''join out]map
16 26|>[bits behead''join out]map
11.times{|i|puts i.to_s(2).rjust 4,?0}
11.times{|i|puts i.to_s(2).rjust 4,?0}
I'm sure this can be shortened--it's pretty much my first BF golf.
++++++++++[>+>+>+++++>>>+++++>>>+++++>>>+++++[<<<]>>>-]>>+>[-->>+>]<<<[<<<]>>[>[>>-[<<+.->]<[>]>-[<<.>]<[>]>++>]<-[<<<-]++<<[<<<]>.>-]
Try it online! Assumes a tape infinite in both directions, like the interpreter at TIO uses. An interpreter where < at the left end of the tape is a no-op would save three bytes.
More than half of the code (the first 77 bytes, to be precise) is spent initializing the tape. The steps go like this:
++++++++++
10|
[>+>+>+++++>>>+++++>>>+++++>>>+++++[<<<]>>>-]
0|10|10|50| 0| 0|50| 0| 0|50| 0| 0|50|
>>+>[-->>+>]<<<[<<<]>>
0|10|11|48| 0| 1|48| 0| 1|48| 0| 1|48| 0| 1|
The cells initialized to 1 store the bits of our number plus 1: 1 represents a zero bit and 2 represents a one bit.
The initialization phase ended with the pointer on the 11. Now we use this cell to run 11 iterations of our loop:
[> Move to the first 48
[>>- While we're still on a 48, move 2 cells over and decrement
[ The cell's value now equals the bit it represents; if it's not 0:
<<+.- Move to the 48, increment, output, and decrement again
> Move to the next cell, which holds a 0
] Leave the loop
<[>]> Pointer shenanigans to get back on the cell representing the bit
- Decrement again: cell is 255 for a zero bit, 0 for a one bit
[ If cell is not 0:
<<.> Move to the 48, output, and move to the 0 cell
]
<[>]>++ Get back on the bit's cell; increment back to original value
> Move to the next 48
] Loop exits once we've output all four bits
Now we increment the binary number: a one bit turns into a zero bit and
carries; a zero bit turns into a one bit and doesn't carry
<- Move back to the rightmost bit cell and decrement
[ If it is not 0, it must represent a one
<<<- Leave it decremented, go to the next bit cell and decrement it too
] Loop exits on a bit cell that represented a zero
++ Increment it twice (to represent a one)
<<[<<<] Move back to first cell on tape
>. Move to 10 cell and output (newline)
>- Move to loop counter cell and decrement
]