Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Backstory

Disclaimer: May contain made up information about kangaroos.

Kangaroos traverse several stages of development. As they grow older and stronger, they can jump higher and longer, and they can jump more times before they get hungry.

In stage 1, the kangaroo is very little and cannot jump at all. Despite this, is constantly requires nourishment. We can represent a stage 1 kangaroo's activity pattern like this.

o

In stage 2, the kangaroo can make small jumps, but not more than 2 before it gets hungry. We can represent a stage 2 kangaroo's activity pattern like this.

 o o
o o o

After stage 2 the kangaroo improves quickly. In each subsequent stage, the kangaroo can jump a bit higher (1 unit in the graphical representation) and twice as many times. For example, a stage 3 kangaroo's activity pattern looks like this.

  o   o   o   o
 o o o o o o o o
o   o   o   o   o

For stage n, the activity pattern consists of 2n-1 V-shaped jumps of height n.

For example, for stage 4, there are 8 jumps of height 4.

   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

Task

Write a full program or a function that takes a positive integer n as input and prints or returns the ASCII art representation of a stage n kangaroo's activity pattern.

Surrounding whitespace and ANSI escape codes are allowed, as long as the pattern looks exactly as depicted above.

If you choose a function that returns the output, it must return a single string or character array that displays the proper output when printed. Returning an array of strings is not allowed.

You can use any printable, non-whitespace character instead of o, as long as it is consistent within the activity pattern and across all patterns in your answer.

This is ; may the shortest answer in bytes win!

share|improve this question
    
I need some clarification. You said you can use any character to represent o. Can you also use any character to represent the spaces (as long as they are different?)? – Kodos Johnson 6 hours ago
    
The spaces have to be blank. You can use actual spaces or you can use control codes to move the cursor around, but you cannot use printable non-space characters. – Dennis 6 hours ago

19 Answers 19

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)

Try it online!

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)=[];

share|improve this answer
7  
Congrats on 20k!! – Luis Mendo yesterday
    
​​​​​​​​​​​​​​​Thank​​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​​you​​​​​​​​​​​​​​​!​​​​​​​​​​​​​​​ – flawr yesterday

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))

Try it online!

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.

share|improve this answer

Charcoal, 14 bytes

NλP^×λoF⁻λ¹‖O→

Try it online!

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.

share|improve this answer

05AB1E, 12 10 bytes

Îj¹FÐvû},À

Uses the CP-1252 encoding. Try it online!

share|improve this answer

Pyke, 11 bytes

XFd*\o+Q^Vs

Try it here!

 F          -  for i in range(input)
  d*\o+     -     " "*i+"o"
       Q^   -    ^.lpad(input)
         Vs -   repeat len(^): palindromise()
X           - print(reversed(^))
share|improve this answer

J, 28 bytes

' o'{~[:|:]_1&(}.,A.)|.@=@i.

This is based on the palindrome trick from @muddyfish's solution.

Try it online!

Explanation

This example uses n = 3. Create the identity matrix of order n and reverse the rows.

0 0 1
0 1 0
1 0 0

Drop the last row and append the reverse of itself. Repeat this action n times. Then transpose the result.

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'] and output as a 2d char array.

  o   o   o   o  
 o o o o o o o o 
o   o   o   o   o

Walkthrough of the code

' o'{~[:|:]_1&(}.,A.)|.@=@i.  Input: integer n
                          i.  Form the range [0, 1, ..., n-1]
                        =@    Equality table with itself.
                              Creates an identity matrix of order n
                     |.@      Reverse
          ]                   Get n
           _1&(     )         Repeat n times on x = reversed identity matrix
           _1  }.               Drop the last row
                  A.            Get the permutation at index -1
                                This is the reverse
                 ,              Append them and use it as the new value of x
      [:|:                    Transpose x
' o'{~                        Index into the char array
share|improve this answer

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.

Try it online!

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
share|improve this answer

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'

Try it online!

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

share|improve this answer

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+

Try it online!

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 ,.

share|improve this answer

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>

share|improve this answer

Jelly, 11 bytes

ŒḄ¡ḶUz1Ṛa⁶Y

TryItOnline!

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
share|improve this answer
1  
Nice. This ties with my reference solution, ŒḄ¡Ṭ€z0o⁶ṚY. – Dennis yesterday

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.

share|improve this answer
    
I really like the ['o'|mod c m`elem`[m-r,r]] part! – flawr 19 hours ago

C#, 180 bytes

Wont win this, posting for other C# contestants as something they can beat.

(n)=>{var s=new string[n];int x=0,a=1,j;for(j=0;j<=Math.Pow(2,n)*(n*n-n);j++){int i=j%n;s[n-i-1]+=x%n==i?'o':' ';if(i==n-1){x+=a;a*=x==n-1|x==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];
          int x = 0, a = 1, j;
          for (j = 0; j <= Math.Pow(2, n) * (n * n - n); j++)
          {
              int i = j % n;
              s[n - i - 1] += x % n == i ? 'o' : ' ';

              if (i == n - 1)
              {
                  x += a;
                  a *= x == n - 1 || x == 0 ? -1 : 1;
              }
          }
          return string.Join("\n", s);
        };

        Console.Write(_(4));
        Console.ReadKey();
    }
}
share|improve this answer

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')

Try it online!

share|improve this answer

J, 58 47 bytes

' o'{&:>~[:(,.}."1)&.>/(2^<:)#<@(|.,.}."1)@=@i.

Saved 11 bytes using the identity matrix idea from @flawr's solution.

Try it online!

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
share|improve this answer

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.

share|improve this answer

MATL, 27 bytes

XyPt3LZ)2&Pht4LZ)lGqX"h48*c

Try it out at MATL Online

share|improve this answer

Python 2, 83 bytes

n=input()-1
for i in range(n+1):s=' '*n+'o'+' '*i;print(s[i:-1]+s[:i:-1])*2**n+s[i]

Try it online!

share|improve this answer
    
Welcome to PPCG! Nice first post! – Easterly Irk 1 hour ago

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"; 

}
share|improve this answer
    
You can replace every 'o' with 1 and every '' with 0. Hope that works, Also, the spaces can be replaced by O or 9. The important is the pattern, according to the rules. But verify first – Ismael Miguel 7 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.