# Produce n n-squares of integers with rotating *'s in consecuitive corners

The objective is to produce output of n squares (nxn) of random integers (0-9) with a moving * that rotates clockwise around the corners, starting from the top left. The squares should be side by side and separated by a single space.

If n = 0, the output should be empty.

Output for n=1:

*

Output for n=2:

*3 4*
14 07


Output for n=3:

*34 82* 291
453 224 924
145 158 57*


Output for n=4:

*153 135* 0154 0235
2352 5604 3602 2065
2245 6895 3561 7105
7225 5785 479* *662


Notice how the * rotates (around the corners of the square), from left to right, like this: top left, top right, bottom right, bottom left, top left, etc. (clockwise)

The shortest answer (measured in bytes) wins.

• Please halp me understand this – Rohan Jhunjhunwala Sep 14 '16 at 21:11
• Voting to reopen. This is completely clear to me. Have made some minor edits. ("perfect square" sounded too much like the algebra term to me) – Level River St Sep 14 '16 at 21:46
• How "random" do the random numbers need to be? It's probably also worth being explicit in your question about the fact that the numbers need to be exactly N digits long, counting the asterisks as digits. – Jesse Amano Sep 14 '16 at 22:16
• <Shrug> Perhaps I'm being slow, but its still unclear to me. Can you explain the placing of the *s in the n=4 example? Perhaps give some more examples? – Digital Trauma Sep 14 '16 at 23:22
• @DigitalTrauma I couldn't figure it out either until I realized that you're not printing one "clock," you're printing n clocks side-by-side (which was not at all clear from the question). So in the n = 4 example, you see four "clocks"—the first with the top row *153, the second with the top row *135, and so on. – Jordan Sep 15 '16 at 1:06

# 05AB1E, 50 49 bytes

3mF9Ý.R}J¹ô¹ävy¦'*ìN4%©>ir}®iRr}®<iR})ˆ}¯øvyðý}»


Explanation

Examples for input = 4.

First we create a string of input^3 random numbers between 0 and 9.

3mF9Ý.R}J


producing

6799762549425893341317984133999075245812305412010122884262903656


Then we split that into pieces each the size of the input.
That is further split into input pieces.

¹ô¹ä


This gives us a matrix of numbers.

[['6799', '7625', '4942', '5893'],
['3413', '1798', '4133', '9990'],
['7524', '5812', '3054', '1201'],
['0122', '8842', '6290', '3656']]


We then loop over the rows of the matrix, inserting asterisks in the right places.

v                          } # for each row in matrix
y                          # flatten list to stack
¦'*ì                      # replace the first digit of the last number with "*"
N4%©>ir}              # if row-nr % 4 == 0, move the number with "*" to the front
®iRr}         # if row-nr % 4 == 1, move the number with "*" to the front
# and reverse the number, moving "*" to the numbers right side
®<iR}    # if row-nr % 4 == 2, reverse the number, moving "*"
# to the numbers right side
)ˆ  # wrap row in a list and add to global array


Now we have the matrix with a "*" on each row, but we want an asterisk per column.

[['*893', '4942', '7625', '6799'],
['099*', '4133', '1798', '3413'],
['7524', '5812', '3054', '102*'],
['0122', '8842', '6290', '*656']]


So we zip this list turning rows into columns and vice versa.

[['*893', '099*', '7524', '0122'],
['4942', '4133', '5812', '8842'],
['7625', '1798', '3054', '6290'],
['6799', '3413', '102*', '*656']]


All that's left now is to format the output.

vyðý}»


Joining the rows on spaces and the columns on newlines gives us the final result.

*893 099* 7524 0122
4942 4133 5812 8842
7625 1798 3054 6290
6799 3413 102* *656


Try it online!

Old 50 byte solution

F¹Fõ¹F9Ý.R«}}¦'*ì})¹ävyN4%©>iR}®iíÁ}®<ií}})øvyðý}»


# Dyalog APL, 57 bytes

Requires ⎕IO←0 which is default on many systems.

Assuming double-spacing is permitted, as per the OP's first example.

{A←⊃∘⍕¨?10⍴⍨3⍴⍵⋄A[(⍳⍵),¨⍵⍴2↑¨1⍵(⍵ ⍵)(1⍵)-1]←'*'⋄⍉⎕FMT A}


TryAPL online!

### Non-competing 49-byte solution (Dyalog APL 16.0):

{⍉⎕FMT'*'@((⍳⍵),¨⍵⍴2↑¨1⍵(⍵ ⍵)(1⍵)-1)⊃∘⍕¨?10⍴⍨3⍴⍵}


# Java 7, 372370 366 bytes

String c(int i){String s="*",a[][]=new String[i][i],t,r="";int j=0,k,z=i-1,m=(int)Math.pow(10,z);for(;j<i;j++)for(k=0;k<i;a[j][k++]=(new java.util.Random().nextInt((int)Math.pow(10,i)-m+1)+m)+"");for(j=0;j<i;k=j%4,t=a[m=k<2?0:z][k],a[m][j++]=k<1|k>2?s+t.substring(1,i):t.substring(0,z)+s);for(j=0;j<i;j++,r+="\n")for(k=0;k<i;r+=a[j][k++]+" ");return i<1?"":i<2?s:r;}


Can probably be golfed some more by placing the asterisks and creating the output at the same time, instead of one by one.

Ungolfed & test code:

Try it here.

class M{
static String c(int i){
String s = "*",
a[][] = new String[i][i],
t,
r = "";
int j = 0,
k,
z = i-1,
m = (int)Math.pow(10, z);
for(; j < i; j++){
for(k = 0; k < i; a[j][k++] = (new java.util.Random().nextInt((int)Math.pow(10, i) - m + 1) + m)+"");
}
for(j = 0; j < i; k = j % 4,
t = a[m = k < 2 ? 0 : z][k],
a[m][j++] = k < 1 | k > 2
? s + t.substring(1, i)
: t.substring(0, z) + s);

for(j = 0; j < i; j++,
r += "\n"){
for(k = 0; k < i; r += a[j][k++] + " ");
}
return i < 1
? ""
: i < 2
? s
: r;
}

public static void main(String[] a){
for (int i = 0; i < 6; i++) {
System.out.println(c(i));
System.out.println();
}
}
}


Possible output:

(empty)

*

*9 4*
92 47

*25 55* 754
910 197 108
635 439 35*

*512 407* 9646 5017
1663 3847 9772 3149
7796 2997 5494 1362
7283 9720 242* *539

*0726 7743* 52096 50958 *0726
60322 20914 76387 92716 41579
89994 18781 33379 84189 31777
11781 89323 12180 51814 63536
58411 32935 5168* *6597 43216


# PHP ,181 Bytes

for($i=-1;++$i<$c=($n=$argv[1])**3;){echo!($i%$q=$n*$n)?"\n":((!$m=$i%$n)?" ":"");echo(!$m&!($b=$i%$q/$n&3)|$m==$n-1&$b==1)&$i<$q|($m==$n-1&$b==2|!$m&$b==3)&$i>$c-$q?"*":rand(0,9);}

• Can you make a "Try it online!"-version of this? – Forwarding Sep 16 '16 at 3:50
• @forwarding Test it – Jörg Hülsermann Sep 16 '16 at 8:49