| Bytes | Lang | Time | Link |
|---|---|---|---|
| 008 | Vyxal | 241021T014740Z | emanresu |
| 027 | MATL | 170122T182038Z | Suever |
| 006 | Canvas | 201209T121446Z | Razetime |
| 081 | Python 2 | 170123T050017Z | infmagic |
| nan | 170123T202810Z | user1893 | |
| 010 | 05AB1E | 170121T230104Z | Adnan |
| 025 | J | 170122T020346Z | miles |
| 016 | 05AB1E | 170123T180833Z | Magic Oc |
| 171 | C# | 170121T215902Z | CSharpie |
| 157 | PHP | 170122T221151Z | Cave Joh |
| 084 | MATLAB | 170121T214844Z | flawr |
| 100 | Haskell | 170122T114655Z | Laikoni |
| 011 | Jelly | 170122T045617Z | Jonathan |
| 083 | JavaScript ES6 | 170122T012604Z | Neil |
| 091 | Befunge | 170121T223344Z | James Ho |
| 014 | Charcoal | 170121T231507Z | DLosc |
| 087 | Python 2 | 170121T225813Z | xnor |
| 088 | Perl 6 | 170121T221049Z | smls |
| 047 | J | 170121T214149Z | miles |
| 177 | Python 3 | 170121T221851Z | Henke |
| 011 | Pyke | 170121T221305Z | Blue |
| 098 | Python 2 | 170121T214825Z | Rod |
| 030 | Pyth | 170121T215037Z | TheBikin |
Vyxal, 8 bytes
ɾ?(∞)×↲§
ɾ # Range from 1 to n
?( ) # n times
∞ # palindromise
×↲ # Pad "*" to the left to each length
§ # And transpose, filling with spaces
Canvas, 6 bytes
o×/L[│
Explanation
o×/L[|
o× repeat o input times
/ make an antidiagonal using it
L get width without popping
[| palindromize width times
Python 2, 83 81 bytes
n=input()
i=0
exec"s=' '*n+'o'+' '*i;i+=1;print(s[i:-1]+s[:i:-1])*2**~-n+s[i];"*n
Java 8, 254 bytes
Golfed:
n->{if(n==1)return"o";int k,x,y,m=n+n-2;char[][]p=new char[n][m];for(y=0;y<n;++y)for(x=0;x<m;)p[y][x++]=' ';for(k=0;k<m;++k)p[k<n?n-k-1:k-n+1][k]='o';String s="";for(y=0;y<n;++y){for(k=0;k<1<<(n-1);++k)for(x=0;x<m;)s+=p[y][x++];if(y==n-1)s+='o';s+='\n';}
Ungolfed:
import java.util.function.*;
public class LeapingKangaroos {
public static void main(final String[] args) {
for (int i = 1; i <= 4; ++i) {
System.out.println(toString(n -> {
if (n == 1) {
return "o";
}
int k, x, y, m = (n + n) - 2;
char[][] p = new char[n][m];
for (y = 0; y < n; ++y) {
for (x = 0; x < m;) {
p[y][x++] = ' ';
}
}
for (k = 0; k < m; ++k) {
p[k < n ? n - k - 1 : (k - n) + 1][k] = 'o';
}
String s = "";
for (y = 0; y < n; ++y) {
for (k = 0; k < (1 << (n - 1)); ++k) {
for (x = 0; x < m;) {
s += p[y][x++];
}
}
if (y == (n - 1)) {
s += 'o';
}
s += '\n';
}
return s;
} , i));
System.out.println();
System.out.println();
}
}
private static String toString(final IntFunction<String> func, final int level) {
return func.apply(level);
}
}
Program output:
o
o o
o o o
o o o o
o o o o o o o o
o o o o o
o o o o o o o o
o o o o o o o o o o o o o o o o
o o o o o o o o o o o o o o o o
o o o o o o o o o
05AB1E, 12 10 bytes
Îj¹FÐvû},À
Explanation:
Î # Push zero and input
j # Prepend input - 1 spaces
¹F # Input times do..
Ð # Triplicate the string
v } # Length times do..
û # Palindromize
, # Pop and print with a newline
À # Rotate the string on to the right
Uses the CP-1252 encoding. Try it online!
J, 28 25 bytes
' o'{~]_&(](|.,}.)"1)=@i.
Saved 3 bytes thanks to @Conor O'Brien.
This is based on the palindrome trick from @muddyfish's solution.
Explanation
' o'{~]_&(](|.,}.)"1)=@i. Input: integer n
i. Form the range [0, 1, ..., n-1]
=@ Equality table with itself.
Creates an identity matrix of order n
] Get n
_&( ) Repeat n times on x = identity matrix
( )"1 For each row
|. Make a reversed copy
}. Get a copy with the head removed
, Append them
] Use that as the new value of x
' o'{~ Index into the char array
05AB1E, 16 bytes
L<¹Fû}ð×'o«.BøR»
Why and how?
# Example input of n=2.
L< # [0,1] (Push [1..a], decrement).
¹Fû} # [0,1,0,1,0] (Palindromize n times).
ð×'o« # ['o',' o','o',' o','o'] (Push n spaces, append o's).
.Bø # ['o ',' o','o ',' o','o '] (Pad with spaces into 2D array, transpose).
R» # Reverse, join and print.
C#, 180, 173 171 bytes
Wont win this, posting for other C# contestants as something they can beat.
n=>{var s=new string[n];for(int a=-1,j=0,i,m=n-1,x=m;j<=m*(Math.Pow(2,n)*n+1);){i=j++%n;s[i]+=x==i?"o":"_";if(i==m&n>1){x+=a;a*=x%m==0?-1:1;}}return string.Join("\n",s);};
complete program:
using System;
public class P
{
public static void Main()
{
Func<int, string> _ = n =>
{
var s = new string[n];
for (int a = -1, j = 0, i, m = n - 1, x = m; j <= m * (Math.Pow(2, n) * n + 1);)
{
i = j++ % n;
s[i] += x == i ? "o" : "_";
if (i == m & n > 1)
{
x += a;
a *= x % m == 0 ? -1 : 1;
}
}
return string.Join("\n", s);
};
Console.Write(_(4));
Console.ReadKey();
}
}
edit: -7 bytes thanks to @KevinCruijssen
edit: -2 bytes, simplified if
PHP, 157 bytes
for($i=$n=$argv[1],$r=str_repeat;$i>0;)echo$r($r(' ',$i-1).'o'.$r(' ',2*$n-2*$i-1).($i==$n|$i==1?'':'o').$r(' ',$i-2),2**($n-1)).($i--==1&$n!=1?'o':'')."\n";
Ungolfed:
for($i=$n=$argv[1];$i>0;) {
// Spacing from beginning of pattern to first 'o'
$o = str_repeat(' ',$i-1);
// First 'o' for the ascent
$o .= 'o';
// Spacing between ascent and descent
$o .= str_repeat(' ',2*$n-2*$i-1);
// Second 'o' for the descent, unless we are at the apex or the bottom
$o .= ($i==$n|$i==1?'':'o');
// Spacing to the end of the pattern
$o .= str_repeat(' ',$i-2);
// Repeat the pattern 2^(n-1) times
echo str_repeat($o, 2**($n-1));
// Output final 'o' if we are at the bottom in the last pattern
echo $i--==1&$n!=1?'o':'';
// End of line
echo "\n";
}
MATLAB, 92 90 86 84 bytes
n=input('');p=eye(n)+32;A=repmat([fliplr(p),p,''],1,2^n/2);A(:,n+1:n:end)=[];disp(A)
eye creates an identity matrix. If we flip it and concatenate the original i.e. [fliplr(p),p] we get (for n=3):
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1
With repmat(...,1,2^n/2) we repeat this 2^(n-1) times and get
0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0
0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 ...
1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1
From this we just delete the unnecessary columns, with A(:,n+1:n:end)=[];
Haskell, 100 bytes
k 1="o"
k n|n<-n-1,m<-n*2=unlines[[last$' ':['o'|mod c m`elem`[m-r,r]]|c<-[0..m*2^n]]|r<-[n,n-1..0]]
Try it online! Usage: k 3.
Explanation:
Given a row r, a column c and m = 2(n-1) an o is set if c mod m equals r or m-r. The outermost list comprehension sets the range of r from n-1 to 0, the next one sets the range of c from 0 to m*2^(n-1) and the innermost acts as conditional returning 'o' if the above formula is fulfilled and ' ' otherwise. This yields a list of strings which is turned into a single newline separated string by unlines. For n=1 the function produces a division-by-zero error, so this case is handled explicitly in the first line.
Jelly, 11 bytes
ŒḄ¡ḶUz1Ṛa⁶Y
How?
The printable character used is 0.
Builds upon the method of Dennis's answer to his previous question on the subject of kangaroos.
ŒḄ¡ḶUz1Ṛa⁶Y - Main link: n e.g. 3
ŒḄ - bounce, initial implicit range(n) e.g. [1,2,3,2,1]
¡ - repeat n times e.g. [1,2,3,2,1,2,3,2,1,2,3,2,1,2,3,2,1]
i.e. [1,2,3,2,1] bounced to [1,2,3,2,1,2,3,2,1] bounced to [1,2,3,2,1,2,3,2,1,2,3,2,1,2,3,2,1]
Ḷ - lowered range (vectorises) e.g. [[0],[0,1],[0,1,2],[0,1],[0],[0,1],[0,1,2],[0,1],[0],[0,1],[0,1,2],[0,1],[0],[0,1],[0,1,2],[0,1],[0]]
U - upend (vectorises) e.g. [[0],[1,0],[2,1,0],[1,0],[0],[1,0],[2,1,0],[1,0],[0],[1,0],[2,1,0],[1,0],[0],[1,0],[2,1,0],[1,0],[0]]
z1 - transpose with filler 1
Ṛ - ...and reverse e.g. [[1,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,1],
[1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1],
[0,1,2,1,0,1,2,1,0,1,2,1,0,1,2,1,0]]
a⁶ - logical and with space character (all non-zeros become spaces)
Y - join with line feeds e.g. 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0
JavaScript (ES6), 83 bytes
f=
n=>` `.repeat(n).replace(/ /g,"$'o$`-$`o$'-".repeat(1<<n-1)+`
`).replace(/-.?/g,``)
<input type=number min=1 oninput=o.textContent=f(this.value)><pre id=o>
Befunge, 98 91 bytes
This uses a , in place of the o, since that enables us to save a couple of bytes.
&::1>\1-:v
+\:v^*2\<_$\1-2*::!+00p*1
:-1_@v0\-g01:%g00:-1<:\p01
,:^ >0g10g--*!3g,:#^_$\55+
Explanation
Given the stage number, n, we start by calculating the following three parameters of the pattern:
jump_count = 2 ^ (n - 1)
jump_len = (n - 1) * 2
width = (jump_len * jump_count) + 1
The jump_len is normalised to avoid it being zero for a stage 1 kangaroo with:
jump_len += !jumplen
We can then output the jump pattern by iterating over the x and y coordinates of the output area, and calculating the appropriate charater to output for each location. The y coordinate counts down from n - 1 to 0, and the x coordinate counts down from width - 1 to 0. We determine whether a dot needs to be shown with the following formula:
jump_off = x % jump_len
show_dot = (jump_off == y) or (jump_off == (jump_len-y))
The show_dot boolean is used as a table index to determine actual character to output at each location. To save on space, we use the start of the last line of source as that table, which is why our o character ends up being a ,.
Charcoal, 14 bytes
NλP^×λoF⁻λ¹‖O→
Explanation
Nλ inputs an integer into λ. P^ is a multidirectional print (SE and SW) of ×λo (string multiplication of λ with o). Then F⁻λ¹ runs a for loop λ - 1 times, in which ‖O→ reflects the whole thing to the right with overlap.
Python 2, 87 bytes
n=input()
for i in range(n):print''.join(' o'[abs(j%(2*n)-n)==i]for j in range(1,n<<n))
Uses a formula for the coordinates (i,j) that contain a circle, then joins and prints the grid. There's a lot of golf smell here -- ''.join, two nested ranges, for over exec, so there's likely to be improvements.
Perl 6, 104 93 88 bytes
->\n{my @a;@a[$_;$++]="o" for [...] |(n-1,0,n-1)xx 2**n/2;say .join for @a».&{$_//" "}}
Inserts o's into a 2D array, and then prints it.
J, 58 47 bytes
' o'{&:>~[:(,.}."1)&.>/(2^<:)#<@(|.,.}."1)@=@i.
Saved 11 bytes using the identity matrix idea from @flawr's solution.
A straightforward application of the definition.
Explanation
For n = 3, creates the identity matrix of order n.
1 0 0
0 1 0
0 0 1
Then mirror it to make
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1
Repeat that 2n-1 times and drop the head of each row on the duplicates
0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
Use those values as indices into the char array [' ', 'o'] to output a 2d char array
o o o o
o o o o o o o o
o o o o o
Python 3, 177 bytes
n=5;f=n-1;w=''
for i in range(n):
s='';a=0;d='\n'
if i==f:w='';a=-1;d=''
for _ in range(2**f):
s+=' '*(f-i)+'o'+' '*(2*i-1)+w+' '*(n-i-2+a)
print(s,end=d);w='o'
print('o')
Pyke, 11 bytes
XFd*\o+Q^Vs
F - for i in range(input)
d*\o+ - " "*i+"o"
Q^ - ^.lpad(input)
Vs - repeat len(^): palindromise()
X - print(reversed(^))
Python 2, 115 113 108 98 bytes
lambda n:'\n'.join(map(''.join,zip(*[' '*abs(i)+'o'+~-n*' 'for i in range(-n+1,n-1)*2**~-n])))+'o'
Using range(-n+1,n-1) to create the absolute number of spaces between the bottom and the o to generate
o
o
o
o
and then appending more copies, rotating 90º everything and appending the last o at bottom right
Pyth, 30 bytes
jC.<V*]+*dtQNh*tQ^2Q*+JUQtP_J^
A program that takes input of an integer and prints the result. Uses a quote mark " instead of o.
How it works
jC.<V*]+*dtQNh*tQ^2Q*+JUQtP_J^ Program. Input: Q
jC.<V*]+*dtQNh*tQ^2Q*+JUQtP_J^QQ Implicit input fill
] Yield a one-element list, A
*dtQ cotaining Q-1 spaces
+ N appended with a quote mark.
h*tQ^2Q Yield 1+(Q-1)*2^Q
* Repeat A that many times, giving B
UQ Yield [0, 1, 2, ..., Q-1]
J (Store that in J)
+ tP_J Append the reverse of J, discarding the first and last
elements
* ^QQ Repeat the above Q^Q times, giving C
V Vectorised map. For each pair [a,b] from B and C:
.< Cyclically rotate a left by b characters
C Transpose
j Join on newlines
Implicitly print