Square-Random-Symmetrical
Clash Royale CLAN TAG#URR8PPP
up vote
18
down vote
favorite
Challenge
Write a program or a function that returns or prints a square-random-symmetrical matrix.
Input
N: The size of the matrix i.e 6 x 6
Output
The matrix. You can either print it, return it as string (with the newlines) or as a list/array of lists/arrays.
Rules
You need to use at least
N
different characters, whereN
is the size of the square matrix (input). Since we 're using only letter [a, z][A, Z] and digits [0, 9] (and only 1 digit at the time) you can assume thatN < 27
andN > 2
, that is because atN <= 2
you can't have both letters and digits. Last but not least, every letter/digit must have non-zero probability of occurring (uniform distribution is not a necessity). However, the result must have at leastN
different letter/digits.The matrix has to be both horizontally and vertically symmetrical.
Exactly 2 rows and 2 columns must contain strictly one single-digit number (it's position should be random as well). The rest of rows/cols will contain only letters. Consider letters as [a, z] and [A, Z] and of course single-digit numbers as [0, 9].
Just to be easier, you can assume that the case of the letters doesn't matter, as long as the cases are symmetrical which means:
a=A, b=B, etc
.Every possible output must have a non-zero probability of occurring. The random distribution doesn't need to be uniform.
Example
Input: 8
Output:
c r p s s p r c
r k o z z o k r
u t 2 a a 2 t u
y n q z z q n y
y n q z z q n y
u t 2 a a 2 t u
r k o z z o k r
c r p s s p r c
code-golf matrix
add a comment |Â
up vote
18
down vote
favorite
Challenge
Write a program or a function that returns or prints a square-random-symmetrical matrix.
Input
N: The size of the matrix i.e 6 x 6
Output
The matrix. You can either print it, return it as string (with the newlines) or as a list/array of lists/arrays.
Rules
You need to use at least
N
different characters, whereN
is the size of the square matrix (input). Since we 're using only letter [a, z][A, Z] and digits [0, 9] (and only 1 digit at the time) you can assume thatN < 27
andN > 2
, that is because atN <= 2
you can't have both letters and digits. Last but not least, every letter/digit must have non-zero probability of occurring (uniform distribution is not a necessity). However, the result must have at leastN
different letter/digits.The matrix has to be both horizontally and vertically symmetrical.
Exactly 2 rows and 2 columns must contain strictly one single-digit number (it's position should be random as well). The rest of rows/cols will contain only letters. Consider letters as [a, z] and [A, Z] and of course single-digit numbers as [0, 9].
Just to be easier, you can assume that the case of the letters doesn't matter, as long as the cases are symmetrical which means:
a=A, b=B, etc
.Every possible output must have a non-zero probability of occurring. The random distribution doesn't need to be uniform.
Example
Input: 8
Output:
c r p s s p r c
r k o z z o k r
u t 2 a a 2 t u
y n q z z q n y
y n q z z q n y
u t 2 a a 2 t u
r k o z z o k r
c r p s s p r c
code-golf matrix
Comments are not for extended discussion; this conversation has been moved to chat.
â Megoâ¦
Aug 31 at 2:30
add a comment |Â
up vote
18
down vote
favorite
up vote
18
down vote
favorite
Challenge
Write a program or a function that returns or prints a square-random-symmetrical matrix.
Input
N: The size of the matrix i.e 6 x 6
Output
The matrix. You can either print it, return it as string (with the newlines) or as a list/array of lists/arrays.
Rules
You need to use at least
N
different characters, whereN
is the size of the square matrix (input). Since we 're using only letter [a, z][A, Z] and digits [0, 9] (and only 1 digit at the time) you can assume thatN < 27
andN > 2
, that is because atN <= 2
you can't have both letters and digits. Last but not least, every letter/digit must have non-zero probability of occurring (uniform distribution is not a necessity). However, the result must have at leastN
different letter/digits.The matrix has to be both horizontally and vertically symmetrical.
Exactly 2 rows and 2 columns must contain strictly one single-digit number (it's position should be random as well). The rest of rows/cols will contain only letters. Consider letters as [a, z] and [A, Z] and of course single-digit numbers as [0, 9].
Just to be easier, you can assume that the case of the letters doesn't matter, as long as the cases are symmetrical which means:
a=A, b=B, etc
.Every possible output must have a non-zero probability of occurring. The random distribution doesn't need to be uniform.
Example
Input: 8
Output:
c r p s s p r c
r k o z z o k r
u t 2 a a 2 t u
y n q z z q n y
y n q z z q n y
u t 2 a a 2 t u
r k o z z o k r
c r p s s p r c
code-golf matrix
Challenge
Write a program or a function that returns or prints a square-random-symmetrical matrix.
Input
N: The size of the matrix i.e 6 x 6
Output
The matrix. You can either print it, return it as string (with the newlines) or as a list/array of lists/arrays.
Rules
You need to use at least
N
different characters, whereN
is the size of the square matrix (input). Since we 're using only letter [a, z][A, Z] and digits [0, 9] (and only 1 digit at the time) you can assume thatN < 27
andN > 2
, that is because atN <= 2
you can't have both letters and digits. Last but not least, every letter/digit must have non-zero probability of occurring (uniform distribution is not a necessity). However, the result must have at leastN
different letter/digits.The matrix has to be both horizontally and vertically symmetrical.
Exactly 2 rows and 2 columns must contain strictly one single-digit number (it's position should be random as well). The rest of rows/cols will contain only letters. Consider letters as [a, z] and [A, Z] and of course single-digit numbers as [0, 9].
Just to be easier, you can assume that the case of the letters doesn't matter, as long as the cases are symmetrical which means:
a=A, b=B, etc
.Every possible output must have a non-zero probability of occurring. The random distribution doesn't need to be uniform.
Example
Input: 8
Output:
c r p s s p r c
r k o z z o k r
u t 2 a a 2 t u
y n q z z q n y
y n q z z q n y
u t 2 a a 2 t u
r k o z z o k r
c r p s s p r c
code-golf matrix
edited Aug 31 at 17:08
asked Aug 29 at 20:54
DimChtz
601111
601111
Comments are not for extended discussion; this conversation has been moved to chat.
â Megoâ¦
Aug 31 at 2:30
add a comment |Â
Comments are not for extended discussion; this conversation has been moved to chat.
â Megoâ¦
Aug 31 at 2:30
Comments are not for extended discussion; this conversation has been moved to chat.
â Megoâ¦
Aug 31 at 2:30
Comments are not for extended discussion; this conversation has been moved to chat.
â Megoâ¦
Aug 31 at 2:30
add a comment |Â
14 Answers
14
active
oldest
votes
up vote
4
down vote
accepted
Charcoal, 30 bytes
NøEâÂÂâÂÂøâÂÂâÂÂâÂÂøâ½òJâ½âÂÂøâ½âÂÂøIâ½ÃÂâÂÂOOâÂÂâÂÂ﹪øò
Try it online! Link is to verbose version of code. If n
is always even, then for 23 bytes:
NøEâÂÂøâÂÂâÂÂøâ½òJâ½âÂÂøâ½âÂÂøIâ½ÃÂâÂÂCì
Try it online! Link is to verbose version of code. Explanation:
Nø
Input $ n $.
ï¼¥âÂÂøâÂÂâÂÂøâ½ò
Create an $ frac n 2 $ by $ frac n 2 $ array of random lowercase letters. This prints implicitly as a square.
Jâ½âÂÂøâ½âÂÂø
Jump to a random position in the square.
Iâ½ÃÂ
Print a random digit.
âÂÂCì
Reflect horizontally and vertically to complete the matrix.
add a comment |Â
up vote
11
down vote
R, 124 118 bytes
function(n,i=(n+1)/2,j=n%/%2,m="[<-"(matrix(-letters,i,i),j-1,j-1,0:9-1))cbind(y<-rbind(m,m[j:1,]),y[,j:1])
`-`=sample
Try it online!
In R, things that look like operators are just functions that get special treatment from the parser.
If you redefine an operator (like -
) to be some other function, it keeps the special treatment from the parser. Since -
is both prefix and infix, and I need to call the sample
function with both one and two arguments, I can use
`-`=sample
to get what I want.
So the code -letters
is translated to sample(letters)
, which randomly shuffles the letters
built-in. But j-1
is translated to sample(j,1)
, which randomly samples 1
item from the vector 1:j
.
(This behaviour of the sample
function depending on the number of parameters and what the first parameter is, is a huge pain in the butt in production code, so I'm happy to find a great use of its perverse nature here!)
Otherwise the code just makes the top left quadrant of the required result, replaces a random element (the j-1
,j-1
bit) with a random digit (the 0:9-1
bit), and folds it out for the required symmetry. The i
and the j
are needed to deal with the even and odd cases.
I wish I could +2 for the great explanation and also editing the related R golfing tip answer. You can save a few more bytes
â JayCe
Aug 30 at 23:44
What a fantastic solution and explanation!
â J.Doe
Aug 31 at 8:57
add a comment |Â
up vote
6
down vote
Python3, 287 bytes
My first try at golfing something here; I'm sure someone can do far better:
import random as rn, math as m
n=int(input())
x,o=m.ceil(n/2),n%2
c=x-1-o
f=lambda l,n: l.extend((l[::-1], l[:-1][::-1])[o])
q=[rn.sample([chr(i) for i in range(97, 123)],x) for y in range(x)]
q[rn.randint(0,c)][rn.randint(0,c)] = rn.randint(0,9)
for r in q:
f(r, n)
f(q, n)
print(q)
Try it Online!
Thanks to HyperNeurtrino, Ourous and Heiteria this shrunk down to 193 bytes (see comments). However, TFeld correctly pointed out that multiple calls to sample
aren't guaranteeing at least N
different characters.
That stuff in mind, try this new version that should guarantee at least N
different characters per run.
Python3, 265 260 bytes, at least N
distinct characters
from random import *
n=int(input())
x=-(-n//2)
o=n%2
c=x+~o
i=randint
u=[chr(j+97)for j in range(26)]
z,q=u[:],
for y in [1]*x:
shuffle(z)
q+=[z[:x]]
z=z[x:] if len(z[x:])>=x else u[:]
q[i(0,c)][i(0,c)]=i(0,9)
for r in[q]+q:r.extend(r[~o::-1])
print(q)
Try it online!
1
Welcome to PPCG! You can golf a few of the whitespaces out; there's no need to put spaces between symbols and symbols and letters.a[:-1][::-1]
is fundamentally equivalent toa[:-2::-1]
, and you can importrandom
asr
instead ofrn
, and you can move thefor
loop into an inline expression. Try It Online!
â HyperNeutrino
Aug 30 at 1:56
2
You can remove themath
import by using-(-a // 2)
instead ofmath.ceil(a / 2)
which is basically negative floor-div of the negative (effectively ceiling). tio.run/##XY7LagMxDEX3/â¦
â HyperNeutrino
Aug 30 at 1:59
1
You can get it down to 236: Try it online!
â ÃÂurous
Aug 30 at 4:19
1
Even further, at 196: Try it online!
â ÃÂurous
Aug 30 at 4:58
1
The multiplesample()
s don't guarantee that you get at leastN
different characters. I managed to get[['g', 'x', 'x', 'g'], [7, 'x', 'x', 7], [7, 'x', 'x', 7], ['g', 'x', 'x', 'g']]
forN=4
, which only has 3 distinct chars
â TFeld
Aug 30 at 8:35
 |Â
show 5 more comments
up vote
3
down vote
APL (Dyalog Classic), 45 44 43 40 bytes
thanks @Adám for -1 byte
26(âÂÂa,âºâ´âÂÂd)[âÂÂâÂÂâÂÂâ¨âÂÂâÂÂâ½â¨âº+@(?âÂÂâÂÂâµ÷2)?âµâ´âº],â¨
Try it online!
uses âÂÂ
(max) of the matrix with its reflections to make it symmetric, so it's biased towards the latter part of the alphabet
the digit is chosen uniformly from 0...25 mod 10, so it has a small bias to lower values
1
âÂÂ2â´âµ÷2)?âµ âµâ´26]}
âÂÂâÂÂâºâµ÷2)?âºâµâ´26]}â¨
â Adám
Aug 30 at 8:59
@Adám clever!ÂÂ
â ngn
Aug 30 at 9:06
Yeah, I just realised.
â Adám
Aug 30 at 9:09
If I'm not mistaken, you can changeâÂÂâºâµ÷2
âÂÂâºâµ
.
â Adám
Aug 30 at 9:10
@Adám I can't - if N is odd, the digit might end up in the centre and there'd be only 1 row/column containing it
â ngn
Aug 30 at 9:11
 |Â
show 1 more comment
up vote
3
down vote
Japt, 31 bytes (Fixed digit position)
;
/2 c
VÃÂVÃÂBö}ÃÂgT0@Mq9ÃÂîêUvÃÂêUv
Try it online!
Japt, 41 bytes (Random digit position)
;
/2 c
VÃÂVÃÂBö}ÃÂgMqôVà,MqVà@Mq9ÃÂîêUvÃÂêUv
Try it online!
Explanation
; Change to new vars
/2 c set implicit var V equal to implicit var U / 2 rounded up
VÃÂVÃÂBö}ÃÂgT0@Mq9ÃÂîêUvÃÂêUv Main function
VÃÂ Range from 0 to V and map
VÃÂ Range from 0 to V and map
Bö}àreturn random char from alphabet
gT0@ map upper-left corner
Mq9ÃÂ return random number
îêUv horizontal mirror
êUv vertical mirror
Your digits are currently always inserted at the same place. Based on the challenge, the position of the digits should be random as well (and may not be in the middle row and/or column for odd inputs due to rule 4).
â Kevin Cruijssen
Aug 30 at 11:50
@KevinCruijssen I dont see where the challenge say the number position must be random as well, I'll ask OP for clarification though
â Luis felipe De jesus Munoz
Aug 30 at 11:54
1
Ah, you're indeed right. I saw it's random in all other answer, so I might have falsely assumed it's mandatory. We'll see what OP says. I actually hope fixed it allowed, it would make it a lot easier to fix that problem for my prepared answer.. ;)
â Kevin Cruijssen
Aug 30 at 12:03
add a comment |Â
up vote
2
down vote
Python 2, 259 bytes
from random import*
n=input();c=choice;r=range
w,W=n/2,-~n/2
o=n%2
A=map(chr,r(97,123))
l=[c(r(10))]+sample(A,n)+[c(A)for _ in' '*w*w]
l,e=l[:w*w],l[w*w:W*W]
shuffle(l)
l=[l[w*i:w*-~i]+e[i:i+1]for i in range(w)]+[e[-W:]]
for r in l+l[~o::-1]:print r+r[~o::-1]
Try it online!
Is using ints directly allowed? Cool idea on the ~ by the way. I was thinking of that too, but I'm not yet really used to it.
â Teck-freak
Sep 3 at 17:09
add a comment |Â
up vote
2
down vote
05AB1E, 29 40 38 bytes
A.rs;òénâÂÂ9ÃÂéîDnñLÃÂî%ÃÂ}<éÃÂîôûùÃÂi.ú.âÂÂëúâÂÂ
+11 bytes to fix the digit being at a random position while still keeping rule 3 in mind for odd inputs..
-2 bytes thanks to @MagicOctopusUrn, changing îï
to ò
and changing the position of the û
.
Try it online of verify some more test cases.
Old (29 27 bytes) answer where the digit positions where always in the corners:
A.rs;òénâÂÂæ9ÃÂéìîôûùÃÂi.ú.âÂÂëúâÂÂ
Try it online or verify some more test cases.
Explanation:
A # Take the lowercase alphabet
.r # Randomly shuffle it
# i.e. "abcdefghijklmnopqrstuvwxyz" â "uovqxrcijfgyzlbpmhatnkwsed"
s # Swap so the (implicit) input is at the top of the stack
; # Halve the input
# i.e. 7 â 3.5
ò # Bankers rounding to the nearest integer
# i.e. 3.5 â 4
é # And save this number in the register
n # Take its square
# i.e. 4 â 16
â # Shorten the shuffled alphabet to that length
# i.e. "uovqxrcijfgyzlbpmhatnkwsed" and 16 â "uovqxrcijfgyzlbp"
9ÃÂé # Take a random digit in the range [0,9]
# i.e. 3
îDnñ # Take the difference between the saved number and its square:
# i.e. 4 and 16 â 12
L # Create a list in the range [1,n]
# i.e. 12 â [1,2,3,4,5,6,7,8,9,10,11,12]
ÃÂ } # Filter this list by:
î%à# Remove any number that's divisible by the number we've saved
# i.e. [1,2,3,4,5,6,7,8,9,10,11,12] and 4 â [1,2,3,5,6,7,9,10,11]
< # Decrease each by 1 (to make it 0-indexed)
# i.e. [1,2,3,5,6,7,9,10,11] â [0,1,2,3,5,6,7,9,10]
é # Take a random item from this list
# i.e. [0,1,2,3,5,6,7,9,10] â 6
ÃÂ # Replace the character at this (0-indexed) position with the digit
# i.e. "uovqxrcijfgyzlbp" and 3 and 6 â "uovqxr3ijfgyzlbp"
îô # Split the string into parts of length equal to the number we've saved
# i.e. "uovqxr3ijfgyzlbp" and 4 â ["uovq","xr3i","jfgy","zlbp"]
û # Join them by new-lines (this is done implicitly in the legacy version)
# i.e. ["uovq","xr3i","jfgy","zlbp"] â "uovqnxr3injfgynzlbp"
ùÃÂi # If the input is odd:
# i.e. 7 â 1 (truthy)
.ú # Intersect mirror the individual items
# i.e. "uovqnxr3injfgynzlbp"
# â "uovqvounxr3i3rxnjfgygfjnzlbpblz"
.â # And intersect vertically mirror the whole thing
# i.e. "uovqvounxr3i3rxnjfgygfjnzlbpblz"
# â "uovqvounxr3i3rxnjfgygfjnzlbpblznjfgygfjnxr3i3rxnuovqvou"
ë # Else (input was even):
úâ # Do the same, but with non-intersecting mirrors
You could also save 2 bytes with the legacy version since it doesn't require»
â Emigna
Aug 30 at 13:17
@Emigna Verified with OP, and the position should indeed be random as well. Fixed for +11 bytes due to rule 3 with odd inputs.. >.> And 3 bytes could have been saved in the legacy because theï
was done implicitely as well. Unfortunately this doesn't apply to the 40-byte version becauseâÂÂ
would insert instead of replace.
â Kevin Cruijssen
Aug 30 at 14:50
@MagicOctopusUrn The TIO you linked still contained my 29 bytes answer instead of 28, do you have the correct link? As for the failing for2
, the input is guaranteed to be3 <= N <= 26
.
â Kevin Cruijssen
Aug 30 at 15:32
1
@KevinCruijssen you're right, I'm a moron, here's the one I was working out: Try it online!
â Magic Octopus Urn
Aug 30 at 15:36
@MagicOctopusUrn Oh, didn't knew about that bankers rounding. That saves a byte in my current answer as well! :D And first appending a random digit and only then shuffling is a pretty smart approach as well. Not sure if it's 100% valid though, since you will always have the firstn
letters of the alphabet, instead ofn
random letters of the alphabet. And first joining by newlines and only then doing the mirrors saves a byte as well in mine. Thanks for -2 bytes! :) PS: One byte can be saved in your 28-byter by removing the trailing}
. :)
â Kevin Cruijssen
Aug 30 at 15:43
add a comment |Â
up vote
2
down vote
C (gcc), 198 197 bytes
Saved 1 byte thanks to ceilingcat (4 bytes actually but I lost 3 bytes fixing the bug which caused the program to print regular pattern for $N=26$.
#define A(x)(x<n/2?x:n-1-x)
#define R rand()
S(n,x,y)
Try it online!
Explanation:
// Coordinate conversion for symmetry
#define A (x) (x < n / 2 ? x : n - 1 - x)
// Get a random and seed
#define R rand()
S (n, x, y)
Suggestfor(s[R%y+R%y*n]=48+R%10;
instead ofs[R%y+n*(R%y)]=48+R%10;for(;
â ceilingcat
Sep 4 at 23:11
add a comment |Â
up vote
1
down vote
JavaScript (ES6), 213 209 206 bytes
n=>(a=,F=(x=y=d=c=0,R=k=>Math.random()*k|0,g=y=>(r=a[y]=a[y]||)[x]=r[n+~x]=v.toString(36))=>y<n/2?F(g(y,R[v=R(m=~-n/2)<!d&x<m&y<m?R(d=10):R(26)+10]=R[v]||++c,g(n+~y))&&++x<n/2?x:+!++y,R):!d|c<n?F():a)()
Try it online!
Commented
n => ( // n = input
a = , // a = output matrix
F = ( // F = main recursive function taking:
x = y = // (x, y) = current coordinates
d = c = 0, // d = digit flag; c = distinct character counter
R = k => // R() = helper function to get a random value in [0,k[
Math.random() * k | 0, // also used to store characters
g = y => // g() = helper function to update the matrix
(r = a[y] = a[y] || )[x] // with horizontal symmetry
= r[n + ~x] = v.toString(36) // using the base-36 representation of v
) => //
y < n / 2 ? // if we haven't reached the middle row(s) of the matrix:
F( // do a recursive call to F():
g( // invoke g() ...
y, // ... on the current row
R[v = // compute v = next value to be inserted
R(m = ~-n/2) < !d & // we may insert a digit if no digit has been
x < m & // inserted so far and the current coordinates are
y < m ? // compatible: 2 distinct rows / 2 distinct columns
R(d = 10) // if so, pick v in [0, 9] and update d
: // else:
R(26) + 10 // pick v in [10, 35] for a letter
] = R[v] || ++c, // set this character as used; update c accordingly
g(n + ~y) // invoke g() on the mirror row
) && // end of outer call to g()
++x < n / 2 ? // if we haven't reached the middle column(s):
x // use x + 1
: // else
+!++y, // increment y and reset x to 0
R // explicitly pass R, as it is used for storage
) // end of recursive call to F()
: // else:
!d | c < n ? F() : a // either return the matrix or try again if it's invalid
)() // initial call to F()
add a comment |Â
up vote
1
down vote
Clean, 346 312 bytes
will golf more tomorrow
import StdEnv,Data.List,Math.Random,System.Time,System._Unsafe
$n#q=twice(transpose oq=zipWith((++)o reverse o drop(n-n/2*2))q q)[[(['a'..'z']++['0'..'9'])!!(c rem 36)\c<-genRandInt(toInt(accUnsafe(time)))]%(i*n/2,i*n/2+(n-1)/2)\i<-[1..(n+1)/2]]
|length(nub(flatten q))>=n&&sum[1\c<-q|any isDigit c]==2=q= $n
Try it online!
add a comment |Â
up vote
1
down vote
Python 3, 197 bytes
As mentioned by @Emigna, doesn't work for odd values of N
(I didn't understand the question properly)
from random import*
def m(N):M=N//2;E=reversed;R=range;B=[randint(48,57),*(sample(R(97,123),N)*N)][:M*M];shuffle(B);r=R(M);m=[k+[*E(k)]for k in[[chr(B.pop())for i in r]for j in r]];m+=E(m);return m
Try it online!
I do think the calls to randint()
+ sample()
+ shuffle()
are too much, and getting rid of in-place shuffling would be great :)
I'm pretty sure this part (that selects the letters & digit) could be golfed a bit more.
Doesn't seem correct for oddN
.
â Emigna
Aug 30 at 13:50
Damn, I had just assumedN
would always be even since I don't get how the matrix could be symmetrical if it's odd !
â etene
Aug 30 at 13:51
1
These are some examples of odd symmetrical matrices.
â Emigna
Aug 30 at 13:57
Okay, thanks, I hadn't seen it that way ! Well I guess my answer is worthless as is then.
â etene
Aug 30 at 13:59
add a comment |Â
up vote
1
down vote
Python 2, 275 266 bytes
from random import*
def f(n):
R=range;C=choice;A=map(chr,R(97,123));b=N=n-n/2;c=`C(R(10))`;s=[c]+sample(A,n-1)+[C(A)for i in R(N*N-n)]
while b:shuffle(s);i=s.index(c);b=n%2>(i<N*N-N>N-1>i%N)
a=[r+r[~(n%2)::-1]for r in[s[i::N]for i in R(N)]];return a+a[~(n%2)::-1]
Try it online!
Returns the array as a list of lists of characters. To satisfy Rule 1, we set up a pool of characters:
s = [c] # the unique digit...
+ sample(A,n-1) # then sample without replacement `n-1` chars in a-z,
# so we have `n` distinct chars
+ [C(A)for i in R(N*N-n)] # and fill out the rest with any in a-z
The next tricky bit is rule 3: there must be exactly 2 columns and rows having a digit; this means for n
odd, that the chosen digit may not appear in the middle column or middle row. Since we construct the array using a twice reflected square sub array s
, that is accomplished here by using:
while b: # to save a couple bytes, `b` is initialized
# to `N`, which is greater than 0.
shuffle(s) # shuffle at least once...
i = s.index(c) # c is the unique digit used
b = n%2
> # if n is even, 0>(any boolean) will be false,
# so exit the loop; otherwise n odd, and we are
# evaluating '1 > some boolean', which is equivalent
# to 'not (some boolean)'
(i<N*N-N # i is not the last column of s...
> # shortcut for ' and ', since N*N-N is always > N-1
N-1>i%N) # is not the last row of s
i.e., shuffle at least once; and then, if n
is odd, keep looping if the digit is in the last column or the last row of s
.
add a comment |Â
up vote
1
down vote
Pyth, 48 bytes
L+b_<b/Q2JmO/Q2 2jy.eyXWqhJkbeJOT<csm.SGQK.EcQ2K
Try it out online here.
The program is in 3 parts - definition of palindromisation function, choosing location of numeric, and main function.
Implicit: Q=eval(input()), T=10, G=lower case alphabet
L+b_<b/Q2 Palindromisation function
L Define a function, y(b)
/Q2 Half input number, rounding down
<b Take that many elements from the start of the sequence
_ Reverse them
+b Prepend the unaltered sequence
JmO/Q2 2 Choose numeric location
O/Q2 Choose a random number between 0 and half input number
m 2 Do the above twice, wrap in array
J Assign to variable J
jy.eyXWqhJkbeJOT<csm.SGQK.EcQ2K Main function
cQ2 Divide input number by 2
.E Round up
K Assign the above to K
.SG Shuffle the alphabet
sm Q Do the above Q times, concatenate
c K Chop the above into segments of length K
< K Take the first K of the above
.e Map (element, index) as (b,k) using:
qhJk Does k equal first element of J?
W If so...
X b Replace in b...
eJ ...at position <last element of J>...
OT ...a random int less than 10
Otherwise, b without replacement
y Apply palindromisation to the result of the above
y Palindromise the set of lines
j Join on newlines, implicit print
Using several shuffled alphabets should ensure that the number of unique characters is always more then the input number.
add a comment |Â
up vote
1
down vote
Python 2/Python 3, 227 bytes
from random import*
def m(N):n=N-N//2;r=range;C=choice;c=n*[chr(i+97)for i in r(26)];shuffle(c);c[C([i for i in r(n*(N-n))if(i+1)%n+1-N%2])]=`C(r(10))`;R=[c[i*n:i*n+n]+c[i*n:i*n+n-N%2][::-1]for i in r(n)];return R+R[::-1][N%2:]
ungolfing a bit:
from random import * # get 'choice' and 'shuffle'
def matrix(N):
n = ceil(N/2) # get the size of the base block
# get a shuffleable lowercase alphabet
c = [chr(i+97)for i in range(26)]
c = n*c # make it large enough to fill the base-block
shuffle(c) # randomize it
digit = choice('1234567890') # get random digit string
## this is only needed as to prevent uneven side-length matrices
# from having centerline digits.
allowed_indices = [i for i in range( # get all allowed indices
n*(N-n)) # skip those, that are in an unmirrored center-line
if(i+1)%n # only use those that are not in the center column
+1-N%2] # exept if there is no center column
index = choice(allowed_indices) # get random index
c[index]=digit # replace one field at random with a random digit
##
R=
for i in range(n):
r = c[i*n:i*n+n] # chop to chunks sized fit for the base block
R.append(r+r[::-1][N%2:]) # mirror skipping the center line
return R+R[::-1][N%2:] # mirror skipping the center line and return
Older, almost correct versions below:
Python2, Python3, 161 bytes
from random import *
N=26
n=N-N//2
c=[chr(i+97)for i in range(26)]
R=[ r+r[::-1][N%2:]for r in[(shuffle(c),c[:n])[1]for i in range(n)]]
R+=R[::-1][N%2:]
print(R)
It seems N differing elements is only almost guarranteed.
Python 2/Python 3, 170 bytes
from random import*
def m(N):n=N-N//2;r=range;c=n*[chr(i+97)for i in r(26)][:n*n];shuffle(c);R=[_+_[::-1][N%2:]for _ in[c[i*n:i*n+n]for i in r(n)]];return R+R[::-1][N%2:]
It seems I forgot rule 3. Also somehow the [:n*n] slipped in.
Your answer is very clever in the way it constructs the symmetric matrix, but you have not satisfied rule 3 (as you don't have any digits in your result), nor rule 5 (e.g., ifn = 3
, you will never have an output containing a'z'
, so not every output is possible).
â Chas Brown
Sep 2 at 22:30
Well pickle me and ... you're correct @ChasBrown ! Well, the [:n*n] is a remainder from a different approach and frankly it shouldn't be there. But you're correct about rule three. I'll have to correct it. Give me a bit.
â Teck-freak
Sep 3 at 0:57
Tried your solution here, but there was an index error... BTW, TryItOnline is super handy here at PPCG! (Also, this problem is way trickier than I thought at first...)
â Chas Brown
Sep 3 at 5:01
I litterally ran it over 10000 times without any error.
â Teck-freak
Sep 3 at 15:06
found it. a ':' was missing. I copied it directly from my script, but it must have gotten lost. it should be "... :-1][N%2:]for i ..." instead of "... :-1][N%2]for i ...".
â Teck-freak
Sep 3 at 15:11
 |Â
show 1 more comment
14 Answers
14
active
oldest
votes
14 Answers
14
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Charcoal, 30 bytes
NøEâÂÂâÂÂøâÂÂâÂÂâÂÂøâ½òJâ½âÂÂøâ½âÂÂøIâ½ÃÂâÂÂOOâÂÂâÂÂ﹪øò
Try it online! Link is to verbose version of code. If n
is always even, then for 23 bytes:
NøEâÂÂøâÂÂâÂÂøâ½òJâ½âÂÂøâ½âÂÂøIâ½ÃÂâÂÂCì
Try it online! Link is to verbose version of code. Explanation:
Nø
Input $ n $.
ï¼¥âÂÂøâÂÂâÂÂøâ½ò
Create an $ frac n 2 $ by $ frac n 2 $ array of random lowercase letters. This prints implicitly as a square.
Jâ½âÂÂøâ½âÂÂø
Jump to a random position in the square.
Iâ½ÃÂ
Print a random digit.
âÂÂCì
Reflect horizontally and vertically to complete the matrix.
add a comment |Â
up vote
4
down vote
accepted
Charcoal, 30 bytes
NøEâÂÂâÂÂøâÂÂâÂÂâÂÂøâ½òJâ½âÂÂøâ½âÂÂøIâ½ÃÂâÂÂOOâÂÂâÂÂ﹪øò
Try it online! Link is to verbose version of code. If n
is always even, then for 23 bytes:
NøEâÂÂøâÂÂâÂÂøâ½òJâ½âÂÂøâ½âÂÂøIâ½ÃÂâÂÂCì
Try it online! Link is to verbose version of code. Explanation:
Nø
Input $ n $.
ï¼¥âÂÂøâÂÂâÂÂøâ½ò
Create an $ frac n 2 $ by $ frac n 2 $ array of random lowercase letters. This prints implicitly as a square.
Jâ½âÂÂøâ½âÂÂø
Jump to a random position in the square.
Iâ½ÃÂ
Print a random digit.
âÂÂCì
Reflect horizontally and vertically to complete the matrix.
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Charcoal, 30 bytes
NøEâÂÂâÂÂøâÂÂâÂÂâÂÂøâ½òJâ½âÂÂøâ½âÂÂøIâ½ÃÂâÂÂOOâÂÂâÂÂ﹪øò
Try it online! Link is to verbose version of code. If n
is always even, then for 23 bytes:
NøEâÂÂøâÂÂâÂÂøâ½òJâ½âÂÂøâ½âÂÂøIâ½ÃÂâÂÂCì
Try it online! Link is to verbose version of code. Explanation:
Nø
Input $ n $.
ï¼¥âÂÂøâÂÂâÂÂøâ½ò
Create an $ frac n 2 $ by $ frac n 2 $ array of random lowercase letters. This prints implicitly as a square.
Jâ½âÂÂøâ½âÂÂø
Jump to a random position in the square.
Iâ½ÃÂ
Print a random digit.
âÂÂCì
Reflect horizontally and vertically to complete the matrix.
Charcoal, 30 bytes
NøEâÂÂâÂÂøâÂÂâÂÂâÂÂøâ½òJâ½âÂÂøâ½âÂÂøIâ½ÃÂâÂÂOOâÂÂâÂÂ﹪øò
Try it online! Link is to verbose version of code. If n
is always even, then for 23 bytes:
NøEâÂÂøâÂÂâÂÂøâ½òJâ½âÂÂøâ½âÂÂøIâ½ÃÂâÂÂCì
Try it online! Link is to verbose version of code. Explanation:
Nø
Input $ n $.
ï¼¥âÂÂøâÂÂâÂÂøâ½ò
Create an $ frac n 2 $ by $ frac n 2 $ array of random lowercase letters. This prints implicitly as a square.
Jâ½âÂÂøâ½âÂÂø
Jump to a random position in the square.
Iâ½ÃÂ
Print a random digit.
âÂÂCì
Reflect horizontally and vertically to complete the matrix.
answered Aug 30 at 0:07
Neil
75.1k744170
75.1k744170
add a comment |Â
add a comment |Â
up vote
11
down vote
R, 124 118 bytes
function(n,i=(n+1)/2,j=n%/%2,m="[<-"(matrix(-letters,i,i),j-1,j-1,0:9-1))cbind(y<-rbind(m,m[j:1,]),y[,j:1])
`-`=sample
Try it online!
In R, things that look like operators are just functions that get special treatment from the parser.
If you redefine an operator (like -
) to be some other function, it keeps the special treatment from the parser. Since -
is both prefix and infix, and I need to call the sample
function with both one and two arguments, I can use
`-`=sample
to get what I want.
So the code -letters
is translated to sample(letters)
, which randomly shuffles the letters
built-in. But j-1
is translated to sample(j,1)
, which randomly samples 1
item from the vector 1:j
.
(This behaviour of the sample
function depending on the number of parameters and what the first parameter is, is a huge pain in the butt in production code, so I'm happy to find a great use of its perverse nature here!)
Otherwise the code just makes the top left quadrant of the required result, replaces a random element (the j-1
,j-1
bit) with a random digit (the 0:9-1
bit), and folds it out for the required symmetry. The i
and the j
are needed to deal with the even and odd cases.
I wish I could +2 for the great explanation and also editing the related R golfing tip answer. You can save a few more bytes
â JayCe
Aug 30 at 23:44
What a fantastic solution and explanation!
â J.Doe
Aug 31 at 8:57
add a comment |Â
up vote
11
down vote
R, 124 118 bytes
function(n,i=(n+1)/2,j=n%/%2,m="[<-"(matrix(-letters,i,i),j-1,j-1,0:9-1))cbind(y<-rbind(m,m[j:1,]),y[,j:1])
`-`=sample
Try it online!
In R, things that look like operators are just functions that get special treatment from the parser.
If you redefine an operator (like -
) to be some other function, it keeps the special treatment from the parser. Since -
is both prefix and infix, and I need to call the sample
function with both one and two arguments, I can use
`-`=sample
to get what I want.
So the code -letters
is translated to sample(letters)
, which randomly shuffles the letters
built-in. But j-1
is translated to sample(j,1)
, which randomly samples 1
item from the vector 1:j
.
(This behaviour of the sample
function depending on the number of parameters and what the first parameter is, is a huge pain in the butt in production code, so I'm happy to find a great use of its perverse nature here!)
Otherwise the code just makes the top left quadrant of the required result, replaces a random element (the j-1
,j-1
bit) with a random digit (the 0:9-1
bit), and folds it out for the required symmetry. The i
and the j
are needed to deal with the even and odd cases.
I wish I could +2 for the great explanation and also editing the related R golfing tip answer. You can save a few more bytes
â JayCe
Aug 30 at 23:44
What a fantastic solution and explanation!
â J.Doe
Aug 31 at 8:57
add a comment |Â
up vote
11
down vote
up vote
11
down vote
R, 124 118 bytes
function(n,i=(n+1)/2,j=n%/%2,m="[<-"(matrix(-letters,i,i),j-1,j-1,0:9-1))cbind(y<-rbind(m,m[j:1,]),y[,j:1])
`-`=sample
Try it online!
In R, things that look like operators are just functions that get special treatment from the parser.
If you redefine an operator (like -
) to be some other function, it keeps the special treatment from the parser. Since -
is both prefix and infix, and I need to call the sample
function with both one and two arguments, I can use
`-`=sample
to get what I want.
So the code -letters
is translated to sample(letters)
, which randomly shuffles the letters
built-in. But j-1
is translated to sample(j,1)
, which randomly samples 1
item from the vector 1:j
.
(This behaviour of the sample
function depending on the number of parameters and what the first parameter is, is a huge pain in the butt in production code, so I'm happy to find a great use of its perverse nature here!)
Otherwise the code just makes the top left quadrant of the required result, replaces a random element (the j-1
,j-1
bit) with a random digit (the 0:9-1
bit), and folds it out for the required symmetry. The i
and the j
are needed to deal with the even and odd cases.
R, 124 118 bytes
function(n,i=(n+1)/2,j=n%/%2,m="[<-"(matrix(-letters,i,i),j-1,j-1,0:9-1))cbind(y<-rbind(m,m[j:1,]),y[,j:1])
`-`=sample
Try it online!
In R, things that look like operators are just functions that get special treatment from the parser.
If you redefine an operator (like -
) to be some other function, it keeps the special treatment from the parser. Since -
is both prefix and infix, and I need to call the sample
function with both one and two arguments, I can use
`-`=sample
to get what I want.
So the code -letters
is translated to sample(letters)
, which randomly shuffles the letters
built-in. But j-1
is translated to sample(j,1)
, which randomly samples 1
item from the vector 1:j
.
(This behaviour of the sample
function depending on the number of parameters and what the first parameter is, is a huge pain in the butt in production code, so I'm happy to find a great use of its perverse nature here!)
Otherwise the code just makes the top left quadrant of the required result, replaces a random element (the j-1
,j-1
bit) with a random digit (the 0:9-1
bit), and folds it out for the required symmetry. The i
and the j
are needed to deal with the even and odd cases.
edited Aug 31 at 14:49
answered Aug 30 at 20:05
ngm
2,07920
2,07920
I wish I could +2 for the great explanation and also editing the related R golfing tip answer. You can save a few more bytes
â JayCe
Aug 30 at 23:44
What a fantastic solution and explanation!
â J.Doe
Aug 31 at 8:57
add a comment |Â
I wish I could +2 for the great explanation and also editing the related R golfing tip answer. You can save a few more bytes
â JayCe
Aug 30 at 23:44
What a fantastic solution and explanation!
â J.Doe
Aug 31 at 8:57
I wish I could +2 for the great explanation and also editing the related R golfing tip answer. You can save a few more bytes
â JayCe
Aug 30 at 23:44
I wish I could +2 for the great explanation and also editing the related R golfing tip answer. You can save a few more bytes
â JayCe
Aug 30 at 23:44
What a fantastic solution and explanation!
â J.Doe
Aug 31 at 8:57
What a fantastic solution and explanation!
â J.Doe
Aug 31 at 8:57
add a comment |Â
up vote
6
down vote
Python3, 287 bytes
My first try at golfing something here; I'm sure someone can do far better:
import random as rn, math as m
n=int(input())
x,o=m.ceil(n/2),n%2
c=x-1-o
f=lambda l,n: l.extend((l[::-1], l[:-1][::-1])[o])
q=[rn.sample([chr(i) for i in range(97, 123)],x) for y in range(x)]
q[rn.randint(0,c)][rn.randint(0,c)] = rn.randint(0,9)
for r in q:
f(r, n)
f(q, n)
print(q)
Try it Online!
Thanks to HyperNeurtrino, Ourous and Heiteria this shrunk down to 193 bytes (see comments). However, TFeld correctly pointed out that multiple calls to sample
aren't guaranteeing at least N
different characters.
That stuff in mind, try this new version that should guarantee at least N
different characters per run.
Python3, 265 260 bytes, at least N
distinct characters
from random import *
n=int(input())
x=-(-n//2)
o=n%2
c=x+~o
i=randint
u=[chr(j+97)for j in range(26)]
z,q=u[:],
for y in [1]*x:
shuffle(z)
q+=[z[:x]]
z=z[x:] if len(z[x:])>=x else u[:]
q[i(0,c)][i(0,c)]=i(0,9)
for r in[q]+q:r.extend(r[~o::-1])
print(q)
Try it online!
1
Welcome to PPCG! You can golf a few of the whitespaces out; there's no need to put spaces between symbols and symbols and letters.a[:-1][::-1]
is fundamentally equivalent toa[:-2::-1]
, and you can importrandom
asr
instead ofrn
, and you can move thefor
loop into an inline expression. Try It Online!
â HyperNeutrino
Aug 30 at 1:56
2
You can remove themath
import by using-(-a // 2)
instead ofmath.ceil(a / 2)
which is basically negative floor-div of the negative (effectively ceiling). tio.run/##XY7LagMxDEX3/â¦
â HyperNeutrino
Aug 30 at 1:59
1
You can get it down to 236: Try it online!
â ÃÂurous
Aug 30 at 4:19
1
Even further, at 196: Try it online!
â ÃÂurous
Aug 30 at 4:58
1
The multiplesample()
s don't guarantee that you get at leastN
different characters. I managed to get[['g', 'x', 'x', 'g'], [7, 'x', 'x', 7], [7, 'x', 'x', 7], ['g', 'x', 'x', 'g']]
forN=4
, which only has 3 distinct chars
â TFeld
Aug 30 at 8:35
 |Â
show 5 more comments
up vote
6
down vote
Python3, 287 bytes
My first try at golfing something here; I'm sure someone can do far better:
import random as rn, math as m
n=int(input())
x,o=m.ceil(n/2),n%2
c=x-1-o
f=lambda l,n: l.extend((l[::-1], l[:-1][::-1])[o])
q=[rn.sample([chr(i) for i in range(97, 123)],x) for y in range(x)]
q[rn.randint(0,c)][rn.randint(0,c)] = rn.randint(0,9)
for r in q:
f(r, n)
f(q, n)
print(q)
Try it Online!
Thanks to HyperNeurtrino, Ourous and Heiteria this shrunk down to 193 bytes (see comments). However, TFeld correctly pointed out that multiple calls to sample
aren't guaranteeing at least N
different characters.
That stuff in mind, try this new version that should guarantee at least N
different characters per run.
Python3, 265 260 bytes, at least N
distinct characters
from random import *
n=int(input())
x=-(-n//2)
o=n%2
c=x+~o
i=randint
u=[chr(j+97)for j in range(26)]
z,q=u[:],
for y in [1]*x:
shuffle(z)
q+=[z[:x]]
z=z[x:] if len(z[x:])>=x else u[:]
q[i(0,c)][i(0,c)]=i(0,9)
for r in[q]+q:r.extend(r[~o::-1])
print(q)
Try it online!
1
Welcome to PPCG! You can golf a few of the whitespaces out; there's no need to put spaces between symbols and symbols and letters.a[:-1][::-1]
is fundamentally equivalent toa[:-2::-1]
, and you can importrandom
asr
instead ofrn
, and you can move thefor
loop into an inline expression. Try It Online!
â HyperNeutrino
Aug 30 at 1:56
2
You can remove themath
import by using-(-a // 2)
instead ofmath.ceil(a / 2)
which is basically negative floor-div of the negative (effectively ceiling). tio.run/##XY7LagMxDEX3/â¦
â HyperNeutrino
Aug 30 at 1:59
1
You can get it down to 236: Try it online!
â ÃÂurous
Aug 30 at 4:19
1
Even further, at 196: Try it online!
â ÃÂurous
Aug 30 at 4:58
1
The multiplesample()
s don't guarantee that you get at leastN
different characters. I managed to get[['g', 'x', 'x', 'g'], [7, 'x', 'x', 7], [7, 'x', 'x', 7], ['g', 'x', 'x', 'g']]
forN=4
, which only has 3 distinct chars
â TFeld
Aug 30 at 8:35
 |Â
show 5 more comments
up vote
6
down vote
up vote
6
down vote
Python3, 287 bytes
My first try at golfing something here; I'm sure someone can do far better:
import random as rn, math as m
n=int(input())
x,o=m.ceil(n/2),n%2
c=x-1-o
f=lambda l,n: l.extend((l[::-1], l[:-1][::-1])[o])
q=[rn.sample([chr(i) for i in range(97, 123)],x) for y in range(x)]
q[rn.randint(0,c)][rn.randint(0,c)] = rn.randint(0,9)
for r in q:
f(r, n)
f(q, n)
print(q)
Try it Online!
Thanks to HyperNeurtrino, Ourous and Heiteria this shrunk down to 193 bytes (see comments). However, TFeld correctly pointed out that multiple calls to sample
aren't guaranteeing at least N
different characters.
That stuff in mind, try this new version that should guarantee at least N
different characters per run.
Python3, 265 260 bytes, at least N
distinct characters
from random import *
n=int(input())
x=-(-n//2)
o=n%2
c=x+~o
i=randint
u=[chr(j+97)for j in range(26)]
z,q=u[:],
for y in [1]*x:
shuffle(z)
q+=[z[:x]]
z=z[x:] if len(z[x:])>=x else u[:]
q[i(0,c)][i(0,c)]=i(0,9)
for r in[q]+q:r.extend(r[~o::-1])
print(q)
Try it online!
Python3, 287 bytes
My first try at golfing something here; I'm sure someone can do far better:
import random as rn, math as m
n=int(input())
x,o=m.ceil(n/2),n%2
c=x-1-o
f=lambda l,n: l.extend((l[::-1], l[:-1][::-1])[o])
q=[rn.sample([chr(i) for i in range(97, 123)],x) for y in range(x)]
q[rn.randint(0,c)][rn.randint(0,c)] = rn.randint(0,9)
for r in q:
f(r, n)
f(q, n)
print(q)
Try it Online!
Thanks to HyperNeurtrino, Ourous and Heiteria this shrunk down to 193 bytes (see comments). However, TFeld correctly pointed out that multiple calls to sample
aren't guaranteeing at least N
different characters.
That stuff in mind, try this new version that should guarantee at least N
different characters per run.
Python3, 265 260 bytes, at least N
distinct characters
from random import *
n=int(input())
x=-(-n//2)
o=n%2
c=x+~o
i=randint
u=[chr(j+97)for j in range(26)]
z,q=u[:],
for y in [1]*x:
shuffle(z)
q+=[z[:x]]
z=z[x:] if len(z[x:])>=x else u[:]
q[i(0,c)][i(0,c)]=i(0,9)
for r in[q]+q:r.extend(r[~o::-1])
print(q)
Try it online!
edited Aug 30 at 20:58
answered Aug 30 at 1:51
souldeux
1614
1614
1
Welcome to PPCG! You can golf a few of the whitespaces out; there's no need to put spaces between symbols and symbols and letters.a[:-1][::-1]
is fundamentally equivalent toa[:-2::-1]
, and you can importrandom
asr
instead ofrn
, and you can move thefor
loop into an inline expression. Try It Online!
â HyperNeutrino
Aug 30 at 1:56
2
You can remove themath
import by using-(-a // 2)
instead ofmath.ceil(a / 2)
which is basically negative floor-div of the negative (effectively ceiling). tio.run/##XY7LagMxDEX3/â¦
â HyperNeutrino
Aug 30 at 1:59
1
You can get it down to 236: Try it online!
â ÃÂurous
Aug 30 at 4:19
1
Even further, at 196: Try it online!
â ÃÂurous
Aug 30 at 4:58
1
The multiplesample()
s don't guarantee that you get at leastN
different characters. I managed to get[['g', 'x', 'x', 'g'], [7, 'x', 'x', 7], [7, 'x', 'x', 7], ['g', 'x', 'x', 'g']]
forN=4
, which only has 3 distinct chars
â TFeld
Aug 30 at 8:35
 |Â
show 5 more comments
1
Welcome to PPCG! You can golf a few of the whitespaces out; there's no need to put spaces between symbols and symbols and letters.a[:-1][::-1]
is fundamentally equivalent toa[:-2::-1]
, and you can importrandom
asr
instead ofrn
, and you can move thefor
loop into an inline expression. Try It Online!
â HyperNeutrino
Aug 30 at 1:56
2
You can remove themath
import by using-(-a // 2)
instead ofmath.ceil(a / 2)
which is basically negative floor-div of the negative (effectively ceiling). tio.run/##XY7LagMxDEX3/â¦
â HyperNeutrino
Aug 30 at 1:59
1
You can get it down to 236: Try it online!
â ÃÂurous
Aug 30 at 4:19
1
Even further, at 196: Try it online!
â ÃÂurous
Aug 30 at 4:58
1
The multiplesample()
s don't guarantee that you get at leastN
different characters. I managed to get[['g', 'x', 'x', 'g'], [7, 'x', 'x', 7], [7, 'x', 'x', 7], ['g', 'x', 'x', 'g']]
forN=4
, which only has 3 distinct chars
â TFeld
Aug 30 at 8:35
1
1
Welcome to PPCG! You can golf a few of the whitespaces out; there's no need to put spaces between symbols and symbols and letters.
a[:-1][::-1]
is fundamentally equivalent to a[:-2::-1]
, and you can import random
as r
instead of rn
, and you can move the for
loop into an inline expression. Try It Online!â HyperNeutrino
Aug 30 at 1:56
Welcome to PPCG! You can golf a few of the whitespaces out; there's no need to put spaces between symbols and symbols and letters.
a[:-1][::-1]
is fundamentally equivalent to a[:-2::-1]
, and you can import random
as r
instead of rn
, and you can move the for
loop into an inline expression. Try It Online!â HyperNeutrino
Aug 30 at 1:56
2
2
You can remove the
math
import by using -(-a // 2)
instead of math.ceil(a / 2)
which is basically negative floor-div of the negative (effectively ceiling). tio.run/##XY7LagMxDEX3/â¦â HyperNeutrino
Aug 30 at 1:59
You can remove the
math
import by using -(-a // 2)
instead of math.ceil(a / 2)
which is basically negative floor-div of the negative (effectively ceiling). tio.run/##XY7LagMxDEX3/â¦â HyperNeutrino
Aug 30 at 1:59
1
1
You can get it down to 236: Try it online!
â ÃÂurous
Aug 30 at 4:19
You can get it down to 236: Try it online!
â ÃÂurous
Aug 30 at 4:19
1
1
Even further, at 196: Try it online!
â ÃÂurous
Aug 30 at 4:58
Even further, at 196: Try it online!
â ÃÂurous
Aug 30 at 4:58
1
1
The multiple
sample()
s don't guarantee that you get at least N
different characters. I managed to get [['g', 'x', 'x', 'g'], [7, 'x', 'x', 7], [7, 'x', 'x', 7], ['g', 'x', 'x', 'g']]
for N=4
, which only has 3 distinct charsâ TFeld
Aug 30 at 8:35
The multiple
sample()
s don't guarantee that you get at least N
different characters. I managed to get [['g', 'x', 'x', 'g'], [7, 'x', 'x', 7], [7, 'x', 'x', 7], ['g', 'x', 'x', 'g']]
for N=4
, which only has 3 distinct charsâ TFeld
Aug 30 at 8:35
 |Â
show 5 more comments
up vote
3
down vote
APL (Dyalog Classic), 45 44 43 40 bytes
thanks @Adám for -1 byte
26(âÂÂa,âºâ´âÂÂd)[âÂÂâÂÂâÂÂâ¨âÂÂâÂÂâ½â¨âº+@(?âÂÂâÂÂâµ÷2)?âµâ´âº],â¨
Try it online!
uses âÂÂ
(max) of the matrix with its reflections to make it symmetric, so it's biased towards the latter part of the alphabet
the digit is chosen uniformly from 0...25 mod 10, so it has a small bias to lower values
1
âÂÂ2â´âµ÷2)?âµ âµâ´26]}
âÂÂâÂÂâºâµ÷2)?âºâµâ´26]}â¨
â Adám
Aug 30 at 8:59
@Adám clever!ÂÂ
â ngn
Aug 30 at 9:06
Yeah, I just realised.
â Adám
Aug 30 at 9:09
If I'm not mistaken, you can changeâÂÂâºâµ÷2
âÂÂâºâµ
.
â Adám
Aug 30 at 9:10
@Adám I can't - if N is odd, the digit might end up in the centre and there'd be only 1 row/column containing it
â ngn
Aug 30 at 9:11
 |Â
show 1 more comment
up vote
3
down vote
APL (Dyalog Classic), 45 44 43 40 bytes
thanks @Adám for -1 byte
26(âÂÂa,âºâ´âÂÂd)[âÂÂâÂÂâÂÂâ¨âÂÂâÂÂâ½â¨âº+@(?âÂÂâÂÂâµ÷2)?âµâ´âº],â¨
Try it online!
uses âÂÂ
(max) of the matrix with its reflections to make it symmetric, so it's biased towards the latter part of the alphabet
the digit is chosen uniformly from 0...25 mod 10, so it has a small bias to lower values
1
âÂÂ2â´âµ÷2)?âµ âµâ´26]}
âÂÂâÂÂâºâµ÷2)?âºâµâ´26]}â¨
â Adám
Aug 30 at 8:59
@Adám clever!ÂÂ
â ngn
Aug 30 at 9:06
Yeah, I just realised.
â Adám
Aug 30 at 9:09
If I'm not mistaken, you can changeâÂÂâºâµ÷2
âÂÂâºâµ
.
â Adám
Aug 30 at 9:10
@Adám I can't - if N is odd, the digit might end up in the centre and there'd be only 1 row/column containing it
â ngn
Aug 30 at 9:11
 |Â
show 1 more comment
up vote
3
down vote
up vote
3
down vote
APL (Dyalog Classic), 45 44 43 40 bytes
thanks @Adám for -1 byte
26(âÂÂa,âºâ´âÂÂd)[âÂÂâÂÂâÂÂâ¨âÂÂâÂÂâ½â¨âº+@(?âÂÂâÂÂâµ÷2)?âµâ´âº],â¨
Try it online!
uses âÂÂ
(max) of the matrix with its reflections to make it symmetric, so it's biased towards the latter part of the alphabet
the digit is chosen uniformly from 0...25 mod 10, so it has a small bias to lower values
APL (Dyalog Classic), 45 44 43 40 bytes
thanks @Adám for -1 byte
26(âÂÂa,âºâ´âÂÂd)[âÂÂâÂÂâÂÂâ¨âÂÂâÂÂâ½â¨âº+@(?âÂÂâÂÂâµ÷2)?âµâ´âº],â¨
Try it online!
uses âÂÂ
(max) of the matrix with its reflections to make it symmetric, so it's biased towards the latter part of the alphabet
the digit is chosen uniformly from 0...25 mod 10, so it has a small bias to lower values
edited Aug 30 at 9:29
answered Aug 30 at 8:54
ngn
6,11812256
6,11812256
1
âÂÂ2â´âµ÷2)?âµ âµâ´26]}
âÂÂâÂÂâºâµ÷2)?âºâµâ´26]}â¨
â Adám
Aug 30 at 8:59
@Adám clever!ÂÂ
â ngn
Aug 30 at 9:06
Yeah, I just realised.
â Adám
Aug 30 at 9:09
If I'm not mistaken, you can changeâÂÂâºâµ÷2
âÂÂâºâµ
.
â Adám
Aug 30 at 9:10
@Adám I can't - if N is odd, the digit might end up in the centre and there'd be only 1 row/column containing it
â ngn
Aug 30 at 9:11
 |Â
show 1 more comment
1
âÂÂ2â´âµ÷2)?âµ âµâ´26]}
âÂÂâÂÂâºâµ÷2)?âºâµâ´26]}â¨
â Adám
Aug 30 at 8:59
@Adám clever!ÂÂ
â ngn
Aug 30 at 9:06
Yeah, I just realised.
â Adám
Aug 30 at 9:09
If I'm not mistaken, you can changeâÂÂâºâµ÷2
âÂÂâºâµ
.
â Adám
Aug 30 at 9:10
@Adám I can't - if N is odd, the digit might end up in the centre and there'd be only 1 row/column containing it
â ngn
Aug 30 at 9:11
1
1
âÂÂ2â´âµ÷2)?âµ âµâ´26]}
â âÂÂâºâµ÷2)?âºâµâ´26]}â¨
â Adám
Aug 30 at 8:59
âÂÂ2â´âµ÷2)?âµ âµâ´26]}
â âÂÂâºâµ÷2)?âºâµâ´26]}â¨
â Adám
Aug 30 at 8:59
@Adám clever!ÂÂ
â ngn
Aug 30 at 9:06
@Adám clever!ÂÂ
â ngn
Aug 30 at 9:06
Yeah, I just realised.
â Adám
Aug 30 at 9:09
Yeah, I just realised.
â Adám
Aug 30 at 9:09
If I'm not mistaken, you can change
âÂÂâºâµ÷2
â âºâµ
.â Adám
Aug 30 at 9:10
If I'm not mistaken, you can change
âÂÂâºâµ÷2
â âºâµ
.â Adám
Aug 30 at 9:10
@Adám I can't - if N is odd, the digit might end up in the centre and there'd be only 1 row/column containing it
â ngn
Aug 30 at 9:11
@Adám I can't - if N is odd, the digit might end up in the centre and there'd be only 1 row/column containing it
â ngn
Aug 30 at 9:11
 |Â
show 1 more comment
up vote
3
down vote
Japt, 31 bytes (Fixed digit position)
;
/2 c
VÃÂVÃÂBö}ÃÂgT0@Mq9ÃÂîêUvÃÂêUv
Try it online!
Japt, 41 bytes (Random digit position)
;
/2 c
VÃÂVÃÂBö}ÃÂgMqôVà,MqVà@Mq9ÃÂîêUvÃÂêUv
Try it online!
Explanation
; Change to new vars
/2 c set implicit var V equal to implicit var U / 2 rounded up
VÃÂVÃÂBö}ÃÂgT0@Mq9ÃÂîêUvÃÂêUv Main function
VÃÂ Range from 0 to V and map
VÃÂ Range from 0 to V and map
Bö}àreturn random char from alphabet
gT0@ map upper-left corner
Mq9ÃÂ return random number
îêUv horizontal mirror
êUv vertical mirror
Your digits are currently always inserted at the same place. Based on the challenge, the position of the digits should be random as well (and may not be in the middle row and/or column for odd inputs due to rule 4).
â Kevin Cruijssen
Aug 30 at 11:50
@KevinCruijssen I dont see where the challenge say the number position must be random as well, I'll ask OP for clarification though
â Luis felipe De jesus Munoz
Aug 30 at 11:54
1
Ah, you're indeed right. I saw it's random in all other answer, so I might have falsely assumed it's mandatory. We'll see what OP says. I actually hope fixed it allowed, it would make it a lot easier to fix that problem for my prepared answer.. ;)
â Kevin Cruijssen
Aug 30 at 12:03
add a comment |Â
up vote
3
down vote
Japt, 31 bytes (Fixed digit position)
;
/2 c
VÃÂVÃÂBö}ÃÂgT0@Mq9ÃÂîêUvÃÂêUv
Try it online!
Japt, 41 bytes (Random digit position)
;
/2 c
VÃÂVÃÂBö}ÃÂgMqôVà,MqVà@Mq9ÃÂîêUvÃÂêUv
Try it online!
Explanation
; Change to new vars
/2 c set implicit var V equal to implicit var U / 2 rounded up
VÃÂVÃÂBö}ÃÂgT0@Mq9ÃÂîêUvÃÂêUv Main function
VÃÂ Range from 0 to V and map
VÃÂ Range from 0 to V and map
Bö}àreturn random char from alphabet
gT0@ map upper-left corner
Mq9ÃÂ return random number
îêUv horizontal mirror
êUv vertical mirror
Your digits are currently always inserted at the same place. Based on the challenge, the position of the digits should be random as well (and may not be in the middle row and/or column for odd inputs due to rule 4).
â Kevin Cruijssen
Aug 30 at 11:50
@KevinCruijssen I dont see where the challenge say the number position must be random as well, I'll ask OP for clarification though
â Luis felipe De jesus Munoz
Aug 30 at 11:54
1
Ah, you're indeed right. I saw it's random in all other answer, so I might have falsely assumed it's mandatory. We'll see what OP says. I actually hope fixed it allowed, it would make it a lot easier to fix that problem for my prepared answer.. ;)
â Kevin Cruijssen
Aug 30 at 12:03
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Japt, 31 bytes (Fixed digit position)
;
/2 c
VÃÂVÃÂBö}ÃÂgT0@Mq9ÃÂîêUvÃÂêUv
Try it online!
Japt, 41 bytes (Random digit position)
;
/2 c
VÃÂVÃÂBö}ÃÂgMqôVà,MqVà@Mq9ÃÂîêUvÃÂêUv
Try it online!
Explanation
; Change to new vars
/2 c set implicit var V equal to implicit var U / 2 rounded up
VÃÂVÃÂBö}ÃÂgT0@Mq9ÃÂîêUvÃÂêUv Main function
VÃÂ Range from 0 to V and map
VÃÂ Range from 0 to V and map
Bö}àreturn random char from alphabet
gT0@ map upper-left corner
Mq9ÃÂ return random number
îêUv horizontal mirror
êUv vertical mirror
Japt, 31 bytes (Fixed digit position)
;
/2 c
VÃÂVÃÂBö}ÃÂgT0@Mq9ÃÂîêUvÃÂêUv
Try it online!
Japt, 41 bytes (Random digit position)
;
/2 c
VÃÂVÃÂBö}ÃÂgMqôVà,MqVà@Mq9ÃÂîêUvÃÂêUv
Try it online!
Explanation
; Change to new vars
/2 c set implicit var V equal to implicit var U / 2 rounded up
VÃÂVÃÂBö}ÃÂgT0@Mq9ÃÂîêUvÃÂêUv Main function
VÃÂ Range from 0 to V and map
VÃÂ Range from 0 to V and map
Bö}àreturn random char from alphabet
gT0@ map upper-left corner
Mq9ÃÂ return random number
îêUv horizontal mirror
êUv vertical mirror
edited Aug 30 at 12:35
answered Aug 29 at 22:38
Luis felipe De jesus Munoz
2,9511044
2,9511044
Your digits are currently always inserted at the same place. Based on the challenge, the position of the digits should be random as well (and may not be in the middle row and/or column for odd inputs due to rule 4).
â Kevin Cruijssen
Aug 30 at 11:50
@KevinCruijssen I dont see where the challenge say the number position must be random as well, I'll ask OP for clarification though
â Luis felipe De jesus Munoz
Aug 30 at 11:54
1
Ah, you're indeed right. I saw it's random in all other answer, so I might have falsely assumed it's mandatory. We'll see what OP says. I actually hope fixed it allowed, it would make it a lot easier to fix that problem for my prepared answer.. ;)
â Kevin Cruijssen
Aug 30 at 12:03
add a comment |Â
Your digits are currently always inserted at the same place. Based on the challenge, the position of the digits should be random as well (and may not be in the middle row and/or column for odd inputs due to rule 4).
â Kevin Cruijssen
Aug 30 at 11:50
@KevinCruijssen I dont see where the challenge say the number position must be random as well, I'll ask OP for clarification though
â Luis felipe De jesus Munoz
Aug 30 at 11:54
1
Ah, you're indeed right. I saw it's random in all other answer, so I might have falsely assumed it's mandatory. We'll see what OP says. I actually hope fixed it allowed, it would make it a lot easier to fix that problem for my prepared answer.. ;)
â Kevin Cruijssen
Aug 30 at 12:03
Your digits are currently always inserted at the same place. Based on the challenge, the position of the digits should be random as well (and may not be in the middle row and/or column for odd inputs due to rule 4).
â Kevin Cruijssen
Aug 30 at 11:50
Your digits are currently always inserted at the same place. Based on the challenge, the position of the digits should be random as well (and may not be in the middle row and/or column for odd inputs due to rule 4).
â Kevin Cruijssen
Aug 30 at 11:50
@KevinCruijssen I dont see where the challenge say the number position must be random as well, I'll ask OP for clarification though
â Luis felipe De jesus Munoz
Aug 30 at 11:54
@KevinCruijssen I dont see where the challenge say the number position must be random as well, I'll ask OP for clarification though
â Luis felipe De jesus Munoz
Aug 30 at 11:54
1
1
Ah, you're indeed right. I saw it's random in all other answer, so I might have falsely assumed it's mandatory. We'll see what OP says. I actually hope fixed it allowed, it would make it a lot easier to fix that problem for my prepared answer.. ;)
â Kevin Cruijssen
Aug 30 at 12:03
Ah, you're indeed right. I saw it's random in all other answer, so I might have falsely assumed it's mandatory. We'll see what OP says. I actually hope fixed it allowed, it would make it a lot easier to fix that problem for my prepared answer.. ;)
â Kevin Cruijssen
Aug 30 at 12:03
add a comment |Â
up vote
2
down vote
Python 2, 259 bytes
from random import*
n=input();c=choice;r=range
w,W=n/2,-~n/2
o=n%2
A=map(chr,r(97,123))
l=[c(r(10))]+sample(A,n)+[c(A)for _ in' '*w*w]
l,e=l[:w*w],l[w*w:W*W]
shuffle(l)
l=[l[w*i:w*-~i]+e[i:i+1]for i in range(w)]+[e[-W:]]
for r in l+l[~o::-1]:print r+r[~o::-1]
Try it online!
Is using ints directly allowed? Cool idea on the ~ by the way. I was thinking of that too, but I'm not yet really used to it.
â Teck-freak
Sep 3 at 17:09
add a comment |Â
up vote
2
down vote
Python 2, 259 bytes
from random import*
n=input();c=choice;r=range
w,W=n/2,-~n/2
o=n%2
A=map(chr,r(97,123))
l=[c(r(10))]+sample(A,n)+[c(A)for _ in' '*w*w]
l,e=l[:w*w],l[w*w:W*W]
shuffle(l)
l=[l[w*i:w*-~i]+e[i:i+1]for i in range(w)]+[e[-W:]]
for r in l+l[~o::-1]:print r+r[~o::-1]
Try it online!
Is using ints directly allowed? Cool idea on the ~ by the way. I was thinking of that too, but I'm not yet really used to it.
â Teck-freak
Sep 3 at 17:09
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Python 2, 259 bytes
from random import*
n=input();c=choice;r=range
w,W=n/2,-~n/2
o=n%2
A=map(chr,r(97,123))
l=[c(r(10))]+sample(A,n)+[c(A)for _ in' '*w*w]
l,e=l[:w*w],l[w*w:W*W]
shuffle(l)
l=[l[w*i:w*-~i]+e[i:i+1]for i in range(w)]+[e[-W:]]
for r in l+l[~o::-1]:print r+r[~o::-1]
Try it online!
Python 2, 259 bytes
from random import*
n=input();c=choice;r=range
w,W=n/2,-~n/2
o=n%2
A=map(chr,r(97,123))
l=[c(r(10))]+sample(A,n)+[c(A)for _ in' '*w*w]
l,e=l[:w*w],l[w*w:W*W]
shuffle(l)
l=[l[w*i:w*-~i]+e[i:i+1]for i in range(w)]+[e[-W:]]
for r in l+l[~o::-1]:print r+r[~o::-1]
Try it online!
answered Aug 30 at 9:20
TFeld
11.2k2833
11.2k2833
Is using ints directly allowed? Cool idea on the ~ by the way. I was thinking of that too, but I'm not yet really used to it.
â Teck-freak
Sep 3 at 17:09
add a comment |Â
Is using ints directly allowed? Cool idea on the ~ by the way. I was thinking of that too, but I'm not yet really used to it.
â Teck-freak
Sep 3 at 17:09
Is using ints directly allowed? Cool idea on the ~ by the way. I was thinking of that too, but I'm not yet really used to it.
â Teck-freak
Sep 3 at 17:09
Is using ints directly allowed? Cool idea on the ~ by the way. I was thinking of that too, but I'm not yet really used to it.
â Teck-freak
Sep 3 at 17:09
add a comment |Â
up vote
2
down vote
05AB1E, 29 40 38 bytes
A.rs;òénâÂÂ9ÃÂéîDnñLÃÂî%ÃÂ}<éÃÂîôûùÃÂi.ú.âÂÂëúâÂÂ
+11 bytes to fix the digit being at a random position while still keeping rule 3 in mind for odd inputs..
-2 bytes thanks to @MagicOctopusUrn, changing îï
to ò
and changing the position of the û
.
Try it online of verify some more test cases.
Old (29 27 bytes) answer where the digit positions where always in the corners:
A.rs;òénâÂÂæ9ÃÂéìîôûùÃÂi.ú.âÂÂëúâÂÂ
Try it online or verify some more test cases.
Explanation:
A # Take the lowercase alphabet
.r # Randomly shuffle it
# i.e. "abcdefghijklmnopqrstuvwxyz" â "uovqxrcijfgyzlbpmhatnkwsed"
s # Swap so the (implicit) input is at the top of the stack
; # Halve the input
# i.e. 7 â 3.5
ò # Bankers rounding to the nearest integer
# i.e. 3.5 â 4
é # And save this number in the register
n # Take its square
# i.e. 4 â 16
â # Shorten the shuffled alphabet to that length
# i.e. "uovqxrcijfgyzlbpmhatnkwsed" and 16 â "uovqxrcijfgyzlbp"
9ÃÂé # Take a random digit in the range [0,9]
# i.e. 3
îDnñ # Take the difference between the saved number and its square:
# i.e. 4 and 16 â 12
L # Create a list in the range [1,n]
# i.e. 12 â [1,2,3,4,5,6,7,8,9,10,11,12]
ÃÂ } # Filter this list by:
î%à# Remove any number that's divisible by the number we've saved
# i.e. [1,2,3,4,5,6,7,8,9,10,11,12] and 4 â [1,2,3,5,6,7,9,10,11]
< # Decrease each by 1 (to make it 0-indexed)
# i.e. [1,2,3,5,6,7,9,10,11] â [0,1,2,3,5,6,7,9,10]
é # Take a random item from this list
# i.e. [0,1,2,3,5,6,7,9,10] â 6
ÃÂ # Replace the character at this (0-indexed) position with the digit
# i.e. "uovqxrcijfgyzlbp" and 3 and 6 â "uovqxr3ijfgyzlbp"
îô # Split the string into parts of length equal to the number we've saved
# i.e. "uovqxr3ijfgyzlbp" and 4 â ["uovq","xr3i","jfgy","zlbp"]
û # Join them by new-lines (this is done implicitly in the legacy version)
# i.e. ["uovq","xr3i","jfgy","zlbp"] â "uovqnxr3injfgynzlbp"
ùÃÂi # If the input is odd:
# i.e. 7 â 1 (truthy)
.ú # Intersect mirror the individual items
# i.e. "uovqnxr3injfgynzlbp"
# â "uovqvounxr3i3rxnjfgygfjnzlbpblz"
.â # And intersect vertically mirror the whole thing
# i.e. "uovqvounxr3i3rxnjfgygfjnzlbpblz"
# â "uovqvounxr3i3rxnjfgygfjnzlbpblznjfgygfjnxr3i3rxnuovqvou"
ë # Else (input was even):
úâ # Do the same, but with non-intersecting mirrors
You could also save 2 bytes with the legacy version since it doesn't require»
â Emigna
Aug 30 at 13:17
@Emigna Verified with OP, and the position should indeed be random as well. Fixed for +11 bytes due to rule 3 with odd inputs.. >.> And 3 bytes could have been saved in the legacy because theï
was done implicitely as well. Unfortunately this doesn't apply to the 40-byte version becauseâÂÂ
would insert instead of replace.
â Kevin Cruijssen
Aug 30 at 14:50
@MagicOctopusUrn The TIO you linked still contained my 29 bytes answer instead of 28, do you have the correct link? As for the failing for2
, the input is guaranteed to be3 <= N <= 26
.
â Kevin Cruijssen
Aug 30 at 15:32
1
@KevinCruijssen you're right, I'm a moron, here's the one I was working out: Try it online!
â Magic Octopus Urn
Aug 30 at 15:36
@MagicOctopusUrn Oh, didn't knew about that bankers rounding. That saves a byte in my current answer as well! :D And first appending a random digit and only then shuffling is a pretty smart approach as well. Not sure if it's 100% valid though, since you will always have the firstn
letters of the alphabet, instead ofn
random letters of the alphabet. And first joining by newlines and only then doing the mirrors saves a byte as well in mine. Thanks for -2 bytes! :) PS: One byte can be saved in your 28-byter by removing the trailing}
. :)
â Kevin Cruijssen
Aug 30 at 15:43
add a comment |Â
up vote
2
down vote
05AB1E, 29 40 38 bytes
A.rs;òénâÂÂ9ÃÂéîDnñLÃÂî%ÃÂ}<éÃÂîôûùÃÂi.ú.âÂÂëúâÂÂ
+11 bytes to fix the digit being at a random position while still keeping rule 3 in mind for odd inputs..
-2 bytes thanks to @MagicOctopusUrn, changing îï
to ò
and changing the position of the û
.
Try it online of verify some more test cases.
Old (29 27 bytes) answer where the digit positions where always in the corners:
A.rs;òénâÂÂæ9ÃÂéìîôûùÃÂi.ú.âÂÂëúâÂÂ
Try it online or verify some more test cases.
Explanation:
A # Take the lowercase alphabet
.r # Randomly shuffle it
# i.e. "abcdefghijklmnopqrstuvwxyz" â "uovqxrcijfgyzlbpmhatnkwsed"
s # Swap so the (implicit) input is at the top of the stack
; # Halve the input
# i.e. 7 â 3.5
ò # Bankers rounding to the nearest integer
# i.e. 3.5 â 4
é # And save this number in the register
n # Take its square
# i.e. 4 â 16
â # Shorten the shuffled alphabet to that length
# i.e. "uovqxrcijfgyzlbpmhatnkwsed" and 16 â "uovqxrcijfgyzlbp"
9ÃÂé # Take a random digit in the range [0,9]
# i.e. 3
îDnñ # Take the difference between the saved number and its square:
# i.e. 4 and 16 â 12
L # Create a list in the range [1,n]
# i.e. 12 â [1,2,3,4,5,6,7,8,9,10,11,12]
ÃÂ } # Filter this list by:
î%à# Remove any number that's divisible by the number we've saved
# i.e. [1,2,3,4,5,6,7,8,9,10,11,12] and 4 â [1,2,3,5,6,7,9,10,11]
< # Decrease each by 1 (to make it 0-indexed)
# i.e. [1,2,3,5,6,7,9,10,11] â [0,1,2,3,5,6,7,9,10]
é # Take a random item from this list
# i.e. [0,1,2,3,5,6,7,9,10] â 6
ÃÂ # Replace the character at this (0-indexed) position with the digit
# i.e. "uovqxrcijfgyzlbp" and 3 and 6 â "uovqxr3ijfgyzlbp"
îô # Split the string into parts of length equal to the number we've saved
# i.e. "uovqxr3ijfgyzlbp" and 4 â ["uovq","xr3i","jfgy","zlbp"]
û # Join them by new-lines (this is done implicitly in the legacy version)
# i.e. ["uovq","xr3i","jfgy","zlbp"] â "uovqnxr3injfgynzlbp"
ùÃÂi # If the input is odd:
# i.e. 7 â 1 (truthy)
.ú # Intersect mirror the individual items
# i.e. "uovqnxr3injfgynzlbp"
# â "uovqvounxr3i3rxnjfgygfjnzlbpblz"
.â # And intersect vertically mirror the whole thing
# i.e. "uovqvounxr3i3rxnjfgygfjnzlbpblz"
# â "uovqvounxr3i3rxnjfgygfjnzlbpblznjfgygfjnxr3i3rxnuovqvou"
ë # Else (input was even):
úâ # Do the same, but with non-intersecting mirrors
You could also save 2 bytes with the legacy version since it doesn't require»
â Emigna
Aug 30 at 13:17
@Emigna Verified with OP, and the position should indeed be random as well. Fixed for +11 bytes due to rule 3 with odd inputs.. >.> And 3 bytes could have been saved in the legacy because theï
was done implicitely as well. Unfortunately this doesn't apply to the 40-byte version becauseâÂÂ
would insert instead of replace.
â Kevin Cruijssen
Aug 30 at 14:50
@MagicOctopusUrn The TIO you linked still contained my 29 bytes answer instead of 28, do you have the correct link? As for the failing for2
, the input is guaranteed to be3 <= N <= 26
.
â Kevin Cruijssen
Aug 30 at 15:32
1
@KevinCruijssen you're right, I'm a moron, here's the one I was working out: Try it online!
â Magic Octopus Urn
Aug 30 at 15:36
@MagicOctopusUrn Oh, didn't knew about that bankers rounding. That saves a byte in my current answer as well! :D And first appending a random digit and only then shuffling is a pretty smart approach as well. Not sure if it's 100% valid though, since you will always have the firstn
letters of the alphabet, instead ofn
random letters of the alphabet. And first joining by newlines and only then doing the mirrors saves a byte as well in mine. Thanks for -2 bytes! :) PS: One byte can be saved in your 28-byter by removing the trailing}
. :)
â Kevin Cruijssen
Aug 30 at 15:43
add a comment |Â
up vote
2
down vote
up vote
2
down vote
05AB1E, 29 40 38 bytes
A.rs;òénâÂÂ9ÃÂéîDnñLÃÂî%ÃÂ}<éÃÂîôûùÃÂi.ú.âÂÂëúâÂÂ
+11 bytes to fix the digit being at a random position while still keeping rule 3 in mind for odd inputs..
-2 bytes thanks to @MagicOctopusUrn, changing îï
to ò
and changing the position of the û
.
Try it online of verify some more test cases.
Old (29 27 bytes) answer where the digit positions where always in the corners:
A.rs;òénâÂÂæ9ÃÂéìîôûùÃÂi.ú.âÂÂëúâÂÂ
Try it online or verify some more test cases.
Explanation:
A # Take the lowercase alphabet
.r # Randomly shuffle it
# i.e. "abcdefghijklmnopqrstuvwxyz" â "uovqxrcijfgyzlbpmhatnkwsed"
s # Swap so the (implicit) input is at the top of the stack
; # Halve the input
# i.e. 7 â 3.5
ò # Bankers rounding to the nearest integer
# i.e. 3.5 â 4
é # And save this number in the register
n # Take its square
# i.e. 4 â 16
â # Shorten the shuffled alphabet to that length
# i.e. "uovqxrcijfgyzlbpmhatnkwsed" and 16 â "uovqxrcijfgyzlbp"
9ÃÂé # Take a random digit in the range [0,9]
# i.e. 3
îDnñ # Take the difference between the saved number and its square:
# i.e. 4 and 16 â 12
L # Create a list in the range [1,n]
# i.e. 12 â [1,2,3,4,5,6,7,8,9,10,11,12]
ÃÂ } # Filter this list by:
î%à# Remove any number that's divisible by the number we've saved
# i.e. [1,2,3,4,5,6,7,8,9,10,11,12] and 4 â [1,2,3,5,6,7,9,10,11]
< # Decrease each by 1 (to make it 0-indexed)
# i.e. [1,2,3,5,6,7,9,10,11] â [0,1,2,3,5,6,7,9,10]
é # Take a random item from this list
# i.e. [0,1,2,3,5,6,7,9,10] â 6
ÃÂ # Replace the character at this (0-indexed) position with the digit
# i.e. "uovqxrcijfgyzlbp" and 3 and 6 â "uovqxr3ijfgyzlbp"
îô # Split the string into parts of length equal to the number we've saved
# i.e. "uovqxr3ijfgyzlbp" and 4 â ["uovq","xr3i","jfgy","zlbp"]
û # Join them by new-lines (this is done implicitly in the legacy version)
# i.e. ["uovq","xr3i","jfgy","zlbp"] â "uovqnxr3injfgynzlbp"
ùÃÂi # If the input is odd:
# i.e. 7 â 1 (truthy)
.ú # Intersect mirror the individual items
# i.e. "uovqnxr3injfgynzlbp"
# â "uovqvounxr3i3rxnjfgygfjnzlbpblz"
.â # And intersect vertically mirror the whole thing
# i.e. "uovqvounxr3i3rxnjfgygfjnzlbpblz"
# â "uovqvounxr3i3rxnjfgygfjnzlbpblznjfgygfjnxr3i3rxnuovqvou"
ë # Else (input was even):
úâ # Do the same, but with non-intersecting mirrors
05AB1E, 29 40 38 bytes
A.rs;òénâÂÂ9ÃÂéîDnñLÃÂî%ÃÂ}<éÃÂîôûùÃÂi.ú.âÂÂëúâÂÂ
+11 bytes to fix the digit being at a random position while still keeping rule 3 in mind for odd inputs..
-2 bytes thanks to @MagicOctopusUrn, changing îï
to ò
and changing the position of the û
.
Try it online of verify some more test cases.
Old (29 27 bytes) answer where the digit positions where always in the corners:
A.rs;òénâÂÂæ9ÃÂéìîôûùÃÂi.ú.âÂÂëúâÂÂ
Try it online or verify some more test cases.
Explanation:
A # Take the lowercase alphabet
.r # Randomly shuffle it
# i.e. "abcdefghijklmnopqrstuvwxyz" â "uovqxrcijfgyzlbpmhatnkwsed"
s # Swap so the (implicit) input is at the top of the stack
; # Halve the input
# i.e. 7 â 3.5
ò # Bankers rounding to the nearest integer
# i.e. 3.5 â 4
é # And save this number in the register
n # Take its square
# i.e. 4 â 16
â # Shorten the shuffled alphabet to that length
# i.e. "uovqxrcijfgyzlbpmhatnkwsed" and 16 â "uovqxrcijfgyzlbp"
9ÃÂé # Take a random digit in the range [0,9]
# i.e. 3
îDnñ # Take the difference between the saved number and its square:
# i.e. 4 and 16 â 12
L # Create a list in the range [1,n]
# i.e. 12 â [1,2,3,4,5,6,7,8,9,10,11,12]
ÃÂ } # Filter this list by:
î%à# Remove any number that's divisible by the number we've saved
# i.e. [1,2,3,4,5,6,7,8,9,10,11,12] and 4 â [1,2,3,5,6,7,9,10,11]
< # Decrease each by 1 (to make it 0-indexed)
# i.e. [1,2,3,5,6,7,9,10,11] â [0,1,2,3,5,6,7,9,10]
é # Take a random item from this list
# i.e. [0,1,2,3,5,6,7,9,10] â 6
ÃÂ # Replace the character at this (0-indexed) position with the digit
# i.e. "uovqxrcijfgyzlbp" and 3 and 6 â "uovqxr3ijfgyzlbp"
îô # Split the string into parts of length equal to the number we've saved
# i.e. "uovqxr3ijfgyzlbp" and 4 â ["uovq","xr3i","jfgy","zlbp"]
û # Join them by new-lines (this is done implicitly in the legacy version)
# i.e. ["uovq","xr3i","jfgy","zlbp"] â "uovqnxr3injfgynzlbp"
ùÃÂi # If the input is odd:
# i.e. 7 â 1 (truthy)
.ú # Intersect mirror the individual items
# i.e. "uovqnxr3injfgynzlbp"
# â "uovqvounxr3i3rxnjfgygfjnzlbpblz"
.â # And intersect vertically mirror the whole thing
# i.e. "uovqvounxr3i3rxnjfgygfjnzlbpblz"
# â "uovqvounxr3i3rxnjfgygfjnzlbpblznjfgygfjnxr3i3rxnuovqvou"
ë # Else (input was even):
úâ # Do the same, but with non-intersecting mirrors
edited Aug 30 at 15:49
answered Aug 30 at 12:25
Kevin Cruijssen
29.5k550162
29.5k550162
You could also save 2 bytes with the legacy version since it doesn't require»
â Emigna
Aug 30 at 13:17
@Emigna Verified with OP, and the position should indeed be random as well. Fixed for +11 bytes due to rule 3 with odd inputs.. >.> And 3 bytes could have been saved in the legacy because theï
was done implicitely as well. Unfortunately this doesn't apply to the 40-byte version becauseâÂÂ
would insert instead of replace.
â Kevin Cruijssen
Aug 30 at 14:50
@MagicOctopusUrn The TIO you linked still contained my 29 bytes answer instead of 28, do you have the correct link? As for the failing for2
, the input is guaranteed to be3 <= N <= 26
.
â Kevin Cruijssen
Aug 30 at 15:32
1
@KevinCruijssen you're right, I'm a moron, here's the one I was working out: Try it online!
â Magic Octopus Urn
Aug 30 at 15:36
@MagicOctopusUrn Oh, didn't knew about that bankers rounding. That saves a byte in my current answer as well! :D And first appending a random digit and only then shuffling is a pretty smart approach as well. Not sure if it's 100% valid though, since you will always have the firstn
letters of the alphabet, instead ofn
random letters of the alphabet. And first joining by newlines and only then doing the mirrors saves a byte as well in mine. Thanks for -2 bytes! :) PS: One byte can be saved in your 28-byter by removing the trailing}
. :)
â Kevin Cruijssen
Aug 30 at 15:43
add a comment |Â
You could also save 2 bytes with the legacy version since it doesn't require»
â Emigna
Aug 30 at 13:17
@Emigna Verified with OP, and the position should indeed be random as well. Fixed for +11 bytes due to rule 3 with odd inputs.. >.> And 3 bytes could have been saved in the legacy because theï
was done implicitely as well. Unfortunately this doesn't apply to the 40-byte version becauseâÂÂ
would insert instead of replace.
â Kevin Cruijssen
Aug 30 at 14:50
@MagicOctopusUrn The TIO you linked still contained my 29 bytes answer instead of 28, do you have the correct link? As for the failing for2
, the input is guaranteed to be3 <= N <= 26
.
â Kevin Cruijssen
Aug 30 at 15:32
1
@KevinCruijssen you're right, I'm a moron, here's the one I was working out: Try it online!
â Magic Octopus Urn
Aug 30 at 15:36
@MagicOctopusUrn Oh, didn't knew about that bankers rounding. That saves a byte in my current answer as well! :D And first appending a random digit and only then shuffling is a pretty smart approach as well. Not sure if it's 100% valid though, since you will always have the firstn
letters of the alphabet, instead ofn
random letters of the alphabet. And first joining by newlines and only then doing the mirrors saves a byte as well in mine. Thanks for -2 bytes! :) PS: One byte can be saved in your 28-byter by removing the trailing}
. :)
â Kevin Cruijssen
Aug 30 at 15:43
You could also save 2 bytes with the legacy version since it doesn't require
»
â Emigna
Aug 30 at 13:17
You could also save 2 bytes with the legacy version since it doesn't require
»
â Emigna
Aug 30 at 13:17
@Emigna Verified with OP, and the position should indeed be random as well. Fixed for +11 bytes due to rule 3 with odd inputs.. >.> And 3 bytes could have been saved in the legacy because the
ï
was done implicitely as well. Unfortunately this doesn't apply to the 40-byte version because âÂÂ
would insert instead of replace.â Kevin Cruijssen
Aug 30 at 14:50
@Emigna Verified with OP, and the position should indeed be random as well. Fixed for +11 bytes due to rule 3 with odd inputs.. >.> And 3 bytes could have been saved in the legacy because the
ï
was done implicitely as well. Unfortunately this doesn't apply to the 40-byte version because âÂÂ
would insert instead of replace.â Kevin Cruijssen
Aug 30 at 14:50
@MagicOctopusUrn The TIO you linked still contained my 29 bytes answer instead of 28, do you have the correct link? As for the failing for
2
, the input is guaranteed to be 3 <= N <= 26
.â Kevin Cruijssen
Aug 30 at 15:32
@MagicOctopusUrn The TIO you linked still contained my 29 bytes answer instead of 28, do you have the correct link? As for the failing for
2
, the input is guaranteed to be 3 <= N <= 26
.â Kevin Cruijssen
Aug 30 at 15:32
1
1
@KevinCruijssen you're right, I'm a moron, here's the one I was working out: Try it online!
â Magic Octopus Urn
Aug 30 at 15:36
@KevinCruijssen you're right, I'm a moron, here's the one I was working out: Try it online!
â Magic Octopus Urn
Aug 30 at 15:36
@MagicOctopusUrn Oh, didn't knew about that bankers rounding. That saves a byte in my current answer as well! :D And first appending a random digit and only then shuffling is a pretty smart approach as well. Not sure if it's 100% valid though, since you will always have the first
n
letters of the alphabet, instead of n
random letters of the alphabet. And first joining by newlines and only then doing the mirrors saves a byte as well in mine. Thanks for -2 bytes! :) PS: One byte can be saved in your 28-byter by removing the trailing }
. :)â Kevin Cruijssen
Aug 30 at 15:43
@MagicOctopusUrn Oh, didn't knew about that bankers rounding. That saves a byte in my current answer as well! :D And first appending a random digit and only then shuffling is a pretty smart approach as well. Not sure if it's 100% valid though, since you will always have the first
n
letters of the alphabet, instead of n
random letters of the alphabet. And first joining by newlines and only then doing the mirrors saves a byte as well in mine. Thanks for -2 bytes! :) PS: One byte can be saved in your 28-byter by removing the trailing }
. :)â Kevin Cruijssen
Aug 30 at 15:43
add a comment |Â
up vote
2
down vote
C (gcc), 198 197 bytes
Saved 1 byte thanks to ceilingcat (4 bytes actually but I lost 3 bytes fixing the bug which caused the program to print regular pattern for $N=26$.
#define A(x)(x<n/2?x:n-1-x)
#define R rand()
S(n,x,y)
Try it online!
Explanation:
// Coordinate conversion for symmetry
#define A (x) (x < n / 2 ? x : n - 1 - x)
// Get a random and seed
#define R rand()
S (n, x, y)
Suggestfor(s[R%y+R%y*n]=48+R%10;
instead ofs[R%y+n*(R%y)]=48+R%10;for(;
â ceilingcat
Sep 4 at 23:11
add a comment |Â
up vote
2
down vote
C (gcc), 198 197 bytes
Saved 1 byte thanks to ceilingcat (4 bytes actually but I lost 3 bytes fixing the bug which caused the program to print regular pattern for $N=26$.
#define A(x)(x<n/2?x:n-1-x)
#define R rand()
S(n,x,y)
Try it online!
Explanation:
// Coordinate conversion for symmetry
#define A (x) (x < n / 2 ? x : n - 1 - x)
// Get a random and seed
#define R rand()
S (n, x, y)
Suggestfor(s[R%y+R%y*n]=48+R%10;
instead ofs[R%y+n*(R%y)]=48+R%10;for(;
â ceilingcat
Sep 4 at 23:11
add a comment |Â
up vote
2
down vote
up vote
2
down vote
C (gcc), 198 197 bytes
Saved 1 byte thanks to ceilingcat (4 bytes actually but I lost 3 bytes fixing the bug which caused the program to print regular pattern for $N=26$.
#define A(x)(x<n/2?x:n-1-x)
#define R rand()
S(n,x,y)
Try it online!
Explanation:
// Coordinate conversion for symmetry
#define A (x) (x < n / 2 ? x : n - 1 - x)
// Get a random and seed
#define R rand()
S (n, x, y)
C (gcc), 198 197 bytes
Saved 1 byte thanks to ceilingcat (4 bytes actually but I lost 3 bytes fixing the bug which caused the program to print regular pattern for $N=26$.
#define A(x)(x<n/2?x:n-1-x)
#define R rand()
S(n,x,y)
Try it online!
Explanation:
// Coordinate conversion for symmetry
#define A (x) (x < n / 2 ? x : n - 1 - x)
// Get a random and seed
#define R rand()
S (n, x, y)
edited Sep 3 at 5:44
answered Aug 31 at 14:48
Max Yekhlakov
2415
2415
Suggestfor(s[R%y+R%y*n]=48+R%10;
instead ofs[R%y+n*(R%y)]=48+R%10;for(;
â ceilingcat
Sep 4 at 23:11
add a comment |Â
Suggestfor(s[R%y+R%y*n]=48+R%10;
instead ofs[R%y+n*(R%y)]=48+R%10;for(;
â ceilingcat
Sep 4 at 23:11
Suggest
for(s[R%y+R%y*n]=48+R%10;
instead of s[R%y+n*(R%y)]=48+R%10;for(;
â ceilingcat
Sep 4 at 23:11
Suggest
for(s[R%y+R%y*n]=48+R%10;
instead of s[R%y+n*(R%y)]=48+R%10;for(;
â ceilingcat
Sep 4 at 23:11
add a comment |Â
up vote
1
down vote
JavaScript (ES6), 213 209 206 bytes
n=>(a=,F=(x=y=d=c=0,R=k=>Math.random()*k|0,g=y=>(r=a[y]=a[y]||)[x]=r[n+~x]=v.toString(36))=>y<n/2?F(g(y,R[v=R(m=~-n/2)<!d&x<m&y<m?R(d=10):R(26)+10]=R[v]||++c,g(n+~y))&&++x<n/2?x:+!++y,R):!d|c<n?F():a)()
Try it online!
Commented
n => ( // n = input
a = , // a = output matrix
F = ( // F = main recursive function taking:
x = y = // (x, y) = current coordinates
d = c = 0, // d = digit flag; c = distinct character counter
R = k => // R() = helper function to get a random value in [0,k[
Math.random() * k | 0, // also used to store characters
g = y => // g() = helper function to update the matrix
(r = a[y] = a[y] || )[x] // with horizontal symmetry
= r[n + ~x] = v.toString(36) // using the base-36 representation of v
) => //
y < n / 2 ? // if we haven't reached the middle row(s) of the matrix:
F( // do a recursive call to F():
g( // invoke g() ...
y, // ... on the current row
R[v = // compute v = next value to be inserted
R(m = ~-n/2) < !d & // we may insert a digit if no digit has been
x < m & // inserted so far and the current coordinates are
y < m ? // compatible: 2 distinct rows / 2 distinct columns
R(d = 10) // if so, pick v in [0, 9] and update d
: // else:
R(26) + 10 // pick v in [10, 35] for a letter
] = R[v] || ++c, // set this character as used; update c accordingly
g(n + ~y) // invoke g() on the mirror row
) && // end of outer call to g()
++x < n / 2 ? // if we haven't reached the middle column(s):
x // use x + 1
: // else
+!++y, // increment y and reset x to 0
R // explicitly pass R, as it is used for storage
) // end of recursive call to F()
: // else:
!d | c < n ? F() : a // either return the matrix or try again if it's invalid
)() // initial call to F()
add a comment |Â
up vote
1
down vote
JavaScript (ES6), 213 209 206 bytes
n=>(a=,F=(x=y=d=c=0,R=k=>Math.random()*k|0,g=y=>(r=a[y]=a[y]||)[x]=r[n+~x]=v.toString(36))=>y<n/2?F(g(y,R[v=R(m=~-n/2)<!d&x<m&y<m?R(d=10):R(26)+10]=R[v]||++c,g(n+~y))&&++x<n/2?x:+!++y,R):!d|c<n?F():a)()
Try it online!
Commented
n => ( // n = input
a = , // a = output matrix
F = ( // F = main recursive function taking:
x = y = // (x, y) = current coordinates
d = c = 0, // d = digit flag; c = distinct character counter
R = k => // R() = helper function to get a random value in [0,k[
Math.random() * k | 0, // also used to store characters
g = y => // g() = helper function to update the matrix
(r = a[y] = a[y] || )[x] // with horizontal symmetry
= r[n + ~x] = v.toString(36) // using the base-36 representation of v
) => //
y < n / 2 ? // if we haven't reached the middle row(s) of the matrix:
F( // do a recursive call to F():
g( // invoke g() ...
y, // ... on the current row
R[v = // compute v = next value to be inserted
R(m = ~-n/2) < !d & // we may insert a digit if no digit has been
x < m & // inserted so far and the current coordinates are
y < m ? // compatible: 2 distinct rows / 2 distinct columns
R(d = 10) // if so, pick v in [0, 9] and update d
: // else:
R(26) + 10 // pick v in [10, 35] for a letter
] = R[v] || ++c, // set this character as used; update c accordingly
g(n + ~y) // invoke g() on the mirror row
) && // end of outer call to g()
++x < n / 2 ? // if we haven't reached the middle column(s):
x // use x + 1
: // else
+!++y, // increment y and reset x to 0
R // explicitly pass R, as it is used for storage
) // end of recursive call to F()
: // else:
!d | c < n ? F() : a // either return the matrix or try again if it's invalid
)() // initial call to F()
add a comment |Â
up vote
1
down vote
up vote
1
down vote
JavaScript (ES6), 213 209 206 bytes
n=>(a=,F=(x=y=d=c=0,R=k=>Math.random()*k|0,g=y=>(r=a[y]=a[y]||)[x]=r[n+~x]=v.toString(36))=>y<n/2?F(g(y,R[v=R(m=~-n/2)<!d&x<m&y<m?R(d=10):R(26)+10]=R[v]||++c,g(n+~y))&&++x<n/2?x:+!++y,R):!d|c<n?F():a)()
Try it online!
Commented
n => ( // n = input
a = , // a = output matrix
F = ( // F = main recursive function taking:
x = y = // (x, y) = current coordinates
d = c = 0, // d = digit flag; c = distinct character counter
R = k => // R() = helper function to get a random value in [0,k[
Math.random() * k | 0, // also used to store characters
g = y => // g() = helper function to update the matrix
(r = a[y] = a[y] || )[x] // with horizontal symmetry
= r[n + ~x] = v.toString(36) // using the base-36 representation of v
) => //
y < n / 2 ? // if we haven't reached the middle row(s) of the matrix:
F( // do a recursive call to F():
g( // invoke g() ...
y, // ... on the current row
R[v = // compute v = next value to be inserted
R(m = ~-n/2) < !d & // we may insert a digit if no digit has been
x < m & // inserted so far and the current coordinates are
y < m ? // compatible: 2 distinct rows / 2 distinct columns
R(d = 10) // if so, pick v in [0, 9] and update d
: // else:
R(26) + 10 // pick v in [10, 35] for a letter
] = R[v] || ++c, // set this character as used; update c accordingly
g(n + ~y) // invoke g() on the mirror row
) && // end of outer call to g()
++x < n / 2 ? // if we haven't reached the middle column(s):
x // use x + 1
: // else
+!++y, // increment y and reset x to 0
R // explicitly pass R, as it is used for storage
) // end of recursive call to F()
: // else:
!d | c < n ? F() : a // either return the matrix or try again if it's invalid
)() // initial call to F()
JavaScript (ES6), 213 209 206 bytes
n=>(a=,F=(x=y=d=c=0,R=k=>Math.random()*k|0,g=y=>(r=a[y]=a[y]||)[x]=r[n+~x]=v.toString(36))=>y<n/2?F(g(y,R[v=R(m=~-n/2)<!d&x<m&y<m?R(d=10):R(26)+10]=R[v]||++c,g(n+~y))&&++x<n/2?x:+!++y,R):!d|c<n?F():a)()
Try it online!
Commented
n => ( // n = input
a = , // a = output matrix
F = ( // F = main recursive function taking:
x = y = // (x, y) = current coordinates
d = c = 0, // d = digit flag; c = distinct character counter
R = k => // R() = helper function to get a random value in [0,k[
Math.random() * k | 0, // also used to store characters
g = y => // g() = helper function to update the matrix
(r = a[y] = a[y] || )[x] // with horizontal symmetry
= r[n + ~x] = v.toString(36) // using the base-36 representation of v
) => //
y < n / 2 ? // if we haven't reached the middle row(s) of the matrix:
F( // do a recursive call to F():
g( // invoke g() ...
y, // ... on the current row
R[v = // compute v = next value to be inserted
R(m = ~-n/2) < !d & // we may insert a digit if no digit has been
x < m & // inserted so far and the current coordinates are
y < m ? // compatible: 2 distinct rows / 2 distinct columns
R(d = 10) // if so, pick v in [0, 9] and update d
: // else:
R(26) + 10 // pick v in [10, 35] for a letter
] = R[v] || ++c, // set this character as used; update c accordingly
g(n + ~y) // invoke g() on the mirror row
) && // end of outer call to g()
++x < n / 2 ? // if we haven't reached the middle column(s):
x // use x + 1
: // else
+!++y, // increment y and reset x to 0
R // explicitly pass R, as it is used for storage
) // end of recursive call to F()
: // else:
!d | c < n ? F() : a // either return the matrix or try again if it's invalid
)() // initial call to F()
edited Aug 30 at 10:26
answered Aug 30 at 8:29
Arnauld
63.6k580268
63.6k580268
add a comment |Â
add a comment |Â
up vote
1
down vote
Clean, 346 312 bytes
will golf more tomorrow
import StdEnv,Data.List,Math.Random,System.Time,System._Unsafe
$n#q=twice(transpose oq=zipWith((++)o reverse o drop(n-n/2*2))q q)[[(['a'..'z']++['0'..'9'])!!(c rem 36)\c<-genRandInt(toInt(accUnsafe(time)))]%(i*n/2,i*n/2+(n-1)/2)\i<-[1..(n+1)/2]]
|length(nub(flatten q))>=n&&sum[1\c<-q|any isDigit c]==2=q= $n
Try it online!
add a comment |Â
up vote
1
down vote
Clean, 346 312 bytes
will golf more tomorrow
import StdEnv,Data.List,Math.Random,System.Time,System._Unsafe
$n#q=twice(transpose oq=zipWith((++)o reverse o drop(n-n/2*2))q q)[[(['a'..'z']++['0'..'9'])!!(c rem 36)\c<-genRandInt(toInt(accUnsafe(time)))]%(i*n/2,i*n/2+(n-1)/2)\i<-[1..(n+1)/2]]
|length(nub(flatten q))>=n&&sum[1\c<-q|any isDigit c]==2=q= $n
Try it online!
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Clean, 346 312 bytes
will golf more tomorrow
import StdEnv,Data.List,Math.Random,System.Time,System._Unsafe
$n#q=twice(transpose oq=zipWith((++)o reverse o drop(n-n/2*2))q q)[[(['a'..'z']++['0'..'9'])!!(c rem 36)\c<-genRandInt(toInt(accUnsafe(time)))]%(i*n/2,i*n/2+(n-1)/2)\i<-[1..(n+1)/2]]
|length(nub(flatten q))>=n&&sum[1\c<-q|any isDigit c]==2=q= $n
Try it online!
Clean, 346 312 bytes
will golf more tomorrow
import StdEnv,Data.List,Math.Random,System.Time,System._Unsafe
$n#q=twice(transpose oq=zipWith((++)o reverse o drop(n-n/2*2))q q)[[(['a'..'z']++['0'..'9'])!!(c rem 36)\c<-genRandInt(toInt(accUnsafe(time)))]%(i*n/2,i*n/2+(n-1)/2)\i<-[1..(n+1)/2]]
|length(nub(flatten q))>=n&&sum[1\c<-q|any isDigit c]==2=q= $n
Try it online!
edited Aug 30 at 12:26
answered Aug 30 at 1:36
ÃÂurous
5,2031931
5,2031931
add a comment |Â
add a comment |Â
up vote
1
down vote
Python 3, 197 bytes
As mentioned by @Emigna, doesn't work for odd values of N
(I didn't understand the question properly)
from random import*
def m(N):M=N//2;E=reversed;R=range;B=[randint(48,57),*(sample(R(97,123),N)*N)][:M*M];shuffle(B);r=R(M);m=[k+[*E(k)]for k in[[chr(B.pop())for i in r]for j in r]];m+=E(m);return m
Try it online!
I do think the calls to randint()
+ sample()
+ shuffle()
are too much, and getting rid of in-place shuffling would be great :)
I'm pretty sure this part (that selects the letters & digit) could be golfed a bit more.
Doesn't seem correct for oddN
.
â Emigna
Aug 30 at 13:50
Damn, I had just assumedN
would always be even since I don't get how the matrix could be symmetrical if it's odd !
â etene
Aug 30 at 13:51
1
These are some examples of odd symmetrical matrices.
â Emigna
Aug 30 at 13:57
Okay, thanks, I hadn't seen it that way ! Well I guess my answer is worthless as is then.
â etene
Aug 30 at 13:59
add a comment |Â
up vote
1
down vote
Python 3, 197 bytes
As mentioned by @Emigna, doesn't work for odd values of N
(I didn't understand the question properly)
from random import*
def m(N):M=N//2;E=reversed;R=range;B=[randint(48,57),*(sample(R(97,123),N)*N)][:M*M];shuffle(B);r=R(M);m=[k+[*E(k)]for k in[[chr(B.pop())for i in r]for j in r]];m+=E(m);return m
Try it online!
I do think the calls to randint()
+ sample()
+ shuffle()
are too much, and getting rid of in-place shuffling would be great :)
I'm pretty sure this part (that selects the letters & digit) could be golfed a bit more.
Doesn't seem correct for oddN
.
â Emigna
Aug 30 at 13:50
Damn, I had just assumedN
would always be even since I don't get how the matrix could be symmetrical if it's odd !
â etene
Aug 30 at 13:51
1
These are some examples of odd symmetrical matrices.
â Emigna
Aug 30 at 13:57
Okay, thanks, I hadn't seen it that way ! Well I guess my answer is worthless as is then.
â etene
Aug 30 at 13:59
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Python 3, 197 bytes
As mentioned by @Emigna, doesn't work for odd values of N
(I didn't understand the question properly)
from random import*
def m(N):M=N//2;E=reversed;R=range;B=[randint(48,57),*(sample(R(97,123),N)*N)][:M*M];shuffle(B);r=R(M);m=[k+[*E(k)]for k in[[chr(B.pop())for i in r]for j in r]];m+=E(m);return m
Try it online!
I do think the calls to randint()
+ sample()
+ shuffle()
are too much, and getting rid of in-place shuffling would be great :)
I'm pretty sure this part (that selects the letters & digit) could be golfed a bit more.
Python 3, 197 bytes
As mentioned by @Emigna, doesn't work for odd values of N
(I didn't understand the question properly)
from random import*
def m(N):M=N//2;E=reversed;R=range;B=[randint(48,57),*(sample(R(97,123),N)*N)][:M*M];shuffle(B);r=R(M);m=[k+[*E(k)]for k in[[chr(B.pop())for i in r]for j in r]];m+=E(m);return m
Try it online!
I do think the calls to randint()
+ sample()
+ shuffle()
are too much, and getting rid of in-place shuffling would be great :)
I'm pretty sure this part (that selects the letters & digit) could be golfed a bit more.
edited Aug 30 at 14:02
answered Aug 30 at 12:59
etene
4487
4487
Doesn't seem correct for oddN
.
â Emigna
Aug 30 at 13:50
Damn, I had just assumedN
would always be even since I don't get how the matrix could be symmetrical if it's odd !
â etene
Aug 30 at 13:51
1
These are some examples of odd symmetrical matrices.
â Emigna
Aug 30 at 13:57
Okay, thanks, I hadn't seen it that way ! Well I guess my answer is worthless as is then.
â etene
Aug 30 at 13:59
add a comment |Â
Doesn't seem correct for oddN
.
â Emigna
Aug 30 at 13:50
Damn, I had just assumedN
would always be even since I don't get how the matrix could be symmetrical if it's odd !
â etene
Aug 30 at 13:51
1
These are some examples of odd symmetrical matrices.
â Emigna
Aug 30 at 13:57
Okay, thanks, I hadn't seen it that way ! Well I guess my answer is worthless as is then.
â etene
Aug 30 at 13:59
Doesn't seem correct for odd
N
.â Emigna
Aug 30 at 13:50
Doesn't seem correct for odd
N
.â Emigna
Aug 30 at 13:50
Damn, I had just assumed
N
would always be even since I don't get how the matrix could be symmetrical if it's odd !â etene
Aug 30 at 13:51
Damn, I had just assumed
N
would always be even since I don't get how the matrix could be symmetrical if it's odd !â etene
Aug 30 at 13:51
1
1
These are some examples of odd symmetrical matrices.
â Emigna
Aug 30 at 13:57
These are some examples of odd symmetrical matrices.
â Emigna
Aug 30 at 13:57
Okay, thanks, I hadn't seen it that way ! Well I guess my answer is worthless as is then.
â etene
Aug 30 at 13:59
Okay, thanks, I hadn't seen it that way ! Well I guess my answer is worthless as is then.
â etene
Aug 30 at 13:59
add a comment |Â
up vote
1
down vote
Python 2, 275 266 bytes
from random import*
def f(n):
R=range;C=choice;A=map(chr,R(97,123));b=N=n-n/2;c=`C(R(10))`;s=[c]+sample(A,n-1)+[C(A)for i in R(N*N-n)]
while b:shuffle(s);i=s.index(c);b=n%2>(i<N*N-N>N-1>i%N)
a=[r+r[~(n%2)::-1]for r in[s[i::N]for i in R(N)]];return a+a[~(n%2)::-1]
Try it online!
Returns the array as a list of lists of characters. To satisfy Rule 1, we set up a pool of characters:
s = [c] # the unique digit...
+ sample(A,n-1) # then sample without replacement `n-1` chars in a-z,
# so we have `n` distinct chars
+ [C(A)for i in R(N*N-n)] # and fill out the rest with any in a-z
The next tricky bit is rule 3: there must be exactly 2 columns and rows having a digit; this means for n
odd, that the chosen digit may not appear in the middle column or middle row. Since we construct the array using a twice reflected square sub array s
, that is accomplished here by using:
while b: # to save a couple bytes, `b` is initialized
# to `N`, which is greater than 0.
shuffle(s) # shuffle at least once...
i = s.index(c) # c is the unique digit used
b = n%2
> # if n is even, 0>(any boolean) will be false,
# so exit the loop; otherwise n odd, and we are
# evaluating '1 > some boolean', which is equivalent
# to 'not (some boolean)'
(i<N*N-N # i is not the last column of s...
> # shortcut for ' and ', since N*N-N is always > N-1
N-1>i%N) # is not the last row of s
i.e., shuffle at least once; and then, if n
is odd, keep looping if the digit is in the last column or the last row of s
.
add a comment |Â
up vote
1
down vote
Python 2, 275 266 bytes
from random import*
def f(n):
R=range;C=choice;A=map(chr,R(97,123));b=N=n-n/2;c=`C(R(10))`;s=[c]+sample(A,n-1)+[C(A)for i in R(N*N-n)]
while b:shuffle(s);i=s.index(c);b=n%2>(i<N*N-N>N-1>i%N)
a=[r+r[~(n%2)::-1]for r in[s[i::N]for i in R(N)]];return a+a[~(n%2)::-1]
Try it online!
Returns the array as a list of lists of characters. To satisfy Rule 1, we set up a pool of characters:
s = [c] # the unique digit...
+ sample(A,n-1) # then sample without replacement `n-1` chars in a-z,
# so we have `n` distinct chars
+ [C(A)for i in R(N*N-n)] # and fill out the rest with any in a-z
The next tricky bit is rule 3: there must be exactly 2 columns and rows having a digit; this means for n
odd, that the chosen digit may not appear in the middle column or middle row. Since we construct the array using a twice reflected square sub array s
, that is accomplished here by using:
while b: # to save a couple bytes, `b` is initialized
# to `N`, which is greater than 0.
shuffle(s) # shuffle at least once...
i = s.index(c) # c is the unique digit used
b = n%2
> # if n is even, 0>(any boolean) will be false,
# so exit the loop; otherwise n odd, and we are
# evaluating '1 > some boolean', which is equivalent
# to 'not (some boolean)'
(i<N*N-N # i is not the last column of s...
> # shortcut for ' and ', since N*N-N is always > N-1
N-1>i%N) # is not the last row of s
i.e., shuffle at least once; and then, if n
is odd, keep looping if the digit is in the last column or the last row of s
.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Python 2, 275 266 bytes
from random import*
def f(n):
R=range;C=choice;A=map(chr,R(97,123));b=N=n-n/2;c=`C(R(10))`;s=[c]+sample(A,n-1)+[C(A)for i in R(N*N-n)]
while b:shuffle(s);i=s.index(c);b=n%2>(i<N*N-N>N-1>i%N)
a=[r+r[~(n%2)::-1]for r in[s[i::N]for i in R(N)]];return a+a[~(n%2)::-1]
Try it online!
Returns the array as a list of lists of characters. To satisfy Rule 1, we set up a pool of characters:
s = [c] # the unique digit...
+ sample(A,n-1) # then sample without replacement `n-1` chars in a-z,
# so we have `n` distinct chars
+ [C(A)for i in R(N*N-n)] # and fill out the rest with any in a-z
The next tricky bit is rule 3: there must be exactly 2 columns and rows having a digit; this means for n
odd, that the chosen digit may not appear in the middle column or middle row. Since we construct the array using a twice reflected square sub array s
, that is accomplished here by using:
while b: # to save a couple bytes, `b` is initialized
# to `N`, which is greater than 0.
shuffle(s) # shuffle at least once...
i = s.index(c) # c is the unique digit used
b = n%2
> # if n is even, 0>(any boolean) will be false,
# so exit the loop; otherwise n odd, and we are
# evaluating '1 > some boolean', which is equivalent
# to 'not (some boolean)'
(i<N*N-N # i is not the last column of s...
> # shortcut for ' and ', since N*N-N is always > N-1
N-1>i%N) # is not the last row of s
i.e., shuffle at least once; and then, if n
is odd, keep looping if the digit is in the last column or the last row of s
.
Python 2, 275 266 bytes
from random import*
def f(n):
R=range;C=choice;A=map(chr,R(97,123));b=N=n-n/2;c=`C(R(10))`;s=[c]+sample(A,n-1)+[C(A)for i in R(N*N-n)]
while b:shuffle(s);i=s.index(c);b=n%2>(i<N*N-N>N-1>i%N)
a=[r+r[~(n%2)::-1]for r in[s[i::N]for i in R(N)]];return a+a[~(n%2)::-1]
Try it online!
Returns the array as a list of lists of characters. To satisfy Rule 1, we set up a pool of characters:
s = [c] # the unique digit...
+ sample(A,n-1) # then sample without replacement `n-1` chars in a-z,
# so we have `n` distinct chars
+ [C(A)for i in R(N*N-n)] # and fill out the rest with any in a-z
The next tricky bit is rule 3: there must be exactly 2 columns and rows having a digit; this means for n
odd, that the chosen digit may not appear in the middle column or middle row. Since we construct the array using a twice reflected square sub array s
, that is accomplished here by using:
while b: # to save a couple bytes, `b` is initialized
# to `N`, which is greater than 0.
shuffle(s) # shuffle at least once...
i = s.index(c) # c is the unique digit used
b = n%2
> # if n is even, 0>(any boolean) will be false,
# so exit the loop; otherwise n odd, and we are
# evaluating '1 > some boolean', which is equivalent
# to 'not (some boolean)'
(i<N*N-N # i is not the last column of s...
> # shortcut for ' and ', since N*N-N is always > N-1
N-1>i%N) # is not the last row of s
i.e., shuffle at least once; and then, if n
is odd, keep looping if the digit is in the last column or the last row of s
.
edited Sep 3 at 6:19
answered Sep 3 at 4:58
Chas Brown
4,1361319
4,1361319
add a comment |Â
add a comment |Â
up vote
1
down vote
Pyth, 48 bytes
L+b_<b/Q2JmO/Q2 2jy.eyXWqhJkbeJOT<csm.SGQK.EcQ2K
Try it out online here.
The program is in 3 parts - definition of palindromisation function, choosing location of numeric, and main function.
Implicit: Q=eval(input()), T=10, G=lower case alphabet
L+b_<b/Q2 Palindromisation function
L Define a function, y(b)
/Q2 Half input number, rounding down
<b Take that many elements from the start of the sequence
_ Reverse them
+b Prepend the unaltered sequence
JmO/Q2 2 Choose numeric location
O/Q2 Choose a random number between 0 and half input number
m 2 Do the above twice, wrap in array
J Assign to variable J
jy.eyXWqhJkbeJOT<csm.SGQK.EcQ2K Main function
cQ2 Divide input number by 2
.E Round up
K Assign the above to K
.SG Shuffle the alphabet
sm Q Do the above Q times, concatenate
c K Chop the above into segments of length K
< K Take the first K of the above
.e Map (element, index) as (b,k) using:
qhJk Does k equal first element of J?
W If so...
X b Replace in b...
eJ ...at position <last element of J>...
OT ...a random int less than 10
Otherwise, b without replacement
y Apply palindromisation to the result of the above
y Palindromise the set of lines
j Join on newlines, implicit print
Using several shuffled alphabets should ensure that the number of unique characters is always more then the input number.
add a comment |Â
up vote
1
down vote
Pyth, 48 bytes
L+b_<b/Q2JmO/Q2 2jy.eyXWqhJkbeJOT<csm.SGQK.EcQ2K
Try it out online here.
The program is in 3 parts - definition of palindromisation function, choosing location of numeric, and main function.
Implicit: Q=eval(input()), T=10, G=lower case alphabet
L+b_<b/Q2 Palindromisation function
L Define a function, y(b)
/Q2 Half input number, rounding down
<b Take that many elements from the start of the sequence
_ Reverse them
+b Prepend the unaltered sequence
JmO/Q2 2 Choose numeric location
O/Q2 Choose a random number between 0 and half input number
m 2 Do the above twice, wrap in array
J Assign to variable J
jy.eyXWqhJkbeJOT<csm.SGQK.EcQ2K Main function
cQ2 Divide input number by 2
.E Round up
K Assign the above to K
.SG Shuffle the alphabet
sm Q Do the above Q times, concatenate
c K Chop the above into segments of length K
< K Take the first K of the above
.e Map (element, index) as (b,k) using:
qhJk Does k equal first element of J?
W If so...
X b Replace in b...
eJ ...at position <last element of J>...
OT ...a random int less than 10
Otherwise, b without replacement
y Apply palindromisation to the result of the above
y Palindromise the set of lines
j Join on newlines, implicit print
Using several shuffled alphabets should ensure that the number of unique characters is always more then the input number.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Pyth, 48 bytes
L+b_<b/Q2JmO/Q2 2jy.eyXWqhJkbeJOT<csm.SGQK.EcQ2K
Try it out online here.
The program is in 3 parts - definition of palindromisation function, choosing location of numeric, and main function.
Implicit: Q=eval(input()), T=10, G=lower case alphabet
L+b_<b/Q2 Palindromisation function
L Define a function, y(b)
/Q2 Half input number, rounding down
<b Take that many elements from the start of the sequence
_ Reverse them
+b Prepend the unaltered sequence
JmO/Q2 2 Choose numeric location
O/Q2 Choose a random number between 0 and half input number
m 2 Do the above twice, wrap in array
J Assign to variable J
jy.eyXWqhJkbeJOT<csm.SGQK.EcQ2K Main function
cQ2 Divide input number by 2
.E Round up
K Assign the above to K
.SG Shuffle the alphabet
sm Q Do the above Q times, concatenate
c K Chop the above into segments of length K
< K Take the first K of the above
.e Map (element, index) as (b,k) using:
qhJk Does k equal first element of J?
W If so...
X b Replace in b...
eJ ...at position <last element of J>...
OT ...a random int less than 10
Otherwise, b without replacement
y Apply palindromisation to the result of the above
y Palindromise the set of lines
j Join on newlines, implicit print
Using several shuffled alphabets should ensure that the number of unique characters is always more then the input number.
Pyth, 48 bytes
L+b_<b/Q2JmO/Q2 2jy.eyXWqhJkbeJOT<csm.SGQK.EcQ2K
Try it out online here.
The program is in 3 parts - definition of palindromisation function, choosing location of numeric, and main function.
Implicit: Q=eval(input()), T=10, G=lower case alphabet
L+b_<b/Q2 Palindromisation function
L Define a function, y(b)
/Q2 Half input number, rounding down
<b Take that many elements from the start of the sequence
_ Reverse them
+b Prepend the unaltered sequence
JmO/Q2 2 Choose numeric location
O/Q2 Choose a random number between 0 and half input number
m 2 Do the above twice, wrap in array
J Assign to variable J
jy.eyXWqhJkbeJOT<csm.SGQK.EcQ2K Main function
cQ2 Divide input number by 2
.E Round up
K Assign the above to K
.SG Shuffle the alphabet
sm Q Do the above Q times, concatenate
c K Chop the above into segments of length K
< K Take the first K of the above
.e Map (element, index) as (b,k) using:
qhJk Does k equal first element of J?
W If so...
X b Replace in b...
eJ ...at position <last element of J>...
OT ...a random int less than 10
Otherwise, b without replacement
y Apply palindromisation to the result of the above
y Palindromise the set of lines
j Join on newlines, implicit print
Using several shuffled alphabets should ensure that the number of unique characters is always more then the input number.
answered Sep 3 at 9:38
Sok
2,851722
2,851722
add a comment |Â
add a comment |Â
up vote
1
down vote
Python 2/Python 3, 227 bytes
from random import*
def m(N):n=N-N//2;r=range;C=choice;c=n*[chr(i+97)for i in r(26)];shuffle(c);c[C([i for i in r(n*(N-n))if(i+1)%n+1-N%2])]=`C(r(10))`;R=[c[i*n:i*n+n]+c[i*n:i*n+n-N%2][::-1]for i in r(n)];return R+R[::-1][N%2:]
ungolfing a bit:
from random import * # get 'choice' and 'shuffle'
def matrix(N):
n = ceil(N/2) # get the size of the base block
# get a shuffleable lowercase alphabet
c = [chr(i+97)for i in range(26)]
c = n*c # make it large enough to fill the base-block
shuffle(c) # randomize it
digit = choice('1234567890') # get random digit string
## this is only needed as to prevent uneven side-length matrices
# from having centerline digits.
allowed_indices = [i for i in range( # get all allowed indices
n*(N-n)) # skip those, that are in an unmirrored center-line
if(i+1)%n # only use those that are not in the center column
+1-N%2] # exept if there is no center column
index = choice(allowed_indices) # get random index
c[index]=digit # replace one field at random with a random digit
##
R=
for i in range(n):
r = c[i*n:i*n+n] # chop to chunks sized fit for the base block
R.append(r+r[::-1][N%2:]) # mirror skipping the center line
return R+R[::-1][N%2:] # mirror skipping the center line and return
Older, almost correct versions below:
Python2, Python3, 161 bytes
from random import *
N=26
n=N-N//2
c=[chr(i+97)for i in range(26)]
R=[ r+r[::-1][N%2:]for r in[(shuffle(c),c[:n])[1]for i in range(n)]]
R+=R[::-1][N%2:]
print(R)
It seems N differing elements is only almost guarranteed.
Python 2/Python 3, 170 bytes
from random import*
def m(N):n=N-N//2;r=range;c=n*[chr(i+97)for i in r(26)][:n*n];shuffle(c);R=[_+_[::-1][N%2:]for _ in[c[i*n:i*n+n]for i in r(n)]];return R+R[::-1][N%2:]
It seems I forgot rule 3. Also somehow the [:n*n] slipped in.
Your answer is very clever in the way it constructs the symmetric matrix, but you have not satisfied rule 3 (as you don't have any digits in your result), nor rule 5 (e.g., ifn = 3
, you will never have an output containing a'z'
, so not every output is possible).
â Chas Brown
Sep 2 at 22:30
Well pickle me and ... you're correct @ChasBrown ! Well, the [:n*n] is a remainder from a different approach and frankly it shouldn't be there. But you're correct about rule three. I'll have to correct it. Give me a bit.
â Teck-freak
Sep 3 at 0:57
Tried your solution here, but there was an index error... BTW, TryItOnline is super handy here at PPCG! (Also, this problem is way trickier than I thought at first...)
â Chas Brown
Sep 3 at 5:01
I litterally ran it over 10000 times without any error.
â Teck-freak
Sep 3 at 15:06
found it. a ':' was missing. I copied it directly from my script, but it must have gotten lost. it should be "... :-1][N%2:]for i ..." instead of "... :-1][N%2]for i ...".
â Teck-freak
Sep 3 at 15:11
 |Â
show 1 more comment
up vote
1
down vote
Python 2/Python 3, 227 bytes
from random import*
def m(N):n=N-N//2;r=range;C=choice;c=n*[chr(i+97)for i in r(26)];shuffle(c);c[C([i for i in r(n*(N-n))if(i+1)%n+1-N%2])]=`C(r(10))`;R=[c[i*n:i*n+n]+c[i*n:i*n+n-N%2][::-1]for i in r(n)];return R+R[::-1][N%2:]
ungolfing a bit:
from random import * # get 'choice' and 'shuffle'
def matrix(N):
n = ceil(N/2) # get the size of the base block
# get a shuffleable lowercase alphabet
c = [chr(i+97)for i in range(26)]
c = n*c # make it large enough to fill the base-block
shuffle(c) # randomize it
digit = choice('1234567890') # get random digit string
## this is only needed as to prevent uneven side-length matrices
# from having centerline digits.
allowed_indices = [i for i in range( # get all allowed indices
n*(N-n)) # skip those, that are in an unmirrored center-line
if(i+1)%n # only use those that are not in the center column
+1-N%2] # exept if there is no center column
index = choice(allowed_indices) # get random index
c[index]=digit # replace one field at random with a random digit
##
R=
for i in range(n):
r = c[i*n:i*n+n] # chop to chunks sized fit for the base block
R.append(r+r[::-1][N%2:]) # mirror skipping the center line
return R+R[::-1][N%2:] # mirror skipping the center line and return
Older, almost correct versions below:
Python2, Python3, 161 bytes
from random import *
N=26
n=N-N//2
c=[chr(i+97)for i in range(26)]
R=[ r+r[::-1][N%2:]for r in[(shuffle(c),c[:n])[1]for i in range(n)]]
R+=R[::-1][N%2:]
print(R)
It seems N differing elements is only almost guarranteed.
Python 2/Python 3, 170 bytes
from random import*
def m(N):n=N-N//2;r=range;c=n*[chr(i+97)for i in r(26)][:n*n];shuffle(c);R=[_+_[::-1][N%2:]for _ in[c[i*n:i*n+n]for i in r(n)]];return R+R[::-1][N%2:]
It seems I forgot rule 3. Also somehow the [:n*n] slipped in.
Your answer is very clever in the way it constructs the symmetric matrix, but you have not satisfied rule 3 (as you don't have any digits in your result), nor rule 5 (e.g., ifn = 3
, you will never have an output containing a'z'
, so not every output is possible).
â Chas Brown
Sep 2 at 22:30
Well pickle me and ... you're correct @ChasBrown ! Well, the [:n*n] is a remainder from a different approach and frankly it shouldn't be there. But you're correct about rule three. I'll have to correct it. Give me a bit.
â Teck-freak
Sep 3 at 0:57
Tried your solution here, but there was an index error... BTW, TryItOnline is super handy here at PPCG! (Also, this problem is way trickier than I thought at first...)
â Chas Brown
Sep 3 at 5:01
I litterally ran it over 10000 times without any error.
â Teck-freak
Sep 3 at 15:06
found it. a ':' was missing. I copied it directly from my script, but it must have gotten lost. it should be "... :-1][N%2:]for i ..." instead of "... :-1][N%2]for i ...".
â Teck-freak
Sep 3 at 15:11
 |Â
show 1 more comment
up vote
1
down vote
up vote
1
down vote
Python 2/Python 3, 227 bytes
from random import*
def m(N):n=N-N//2;r=range;C=choice;c=n*[chr(i+97)for i in r(26)];shuffle(c);c[C([i for i in r(n*(N-n))if(i+1)%n+1-N%2])]=`C(r(10))`;R=[c[i*n:i*n+n]+c[i*n:i*n+n-N%2][::-1]for i in r(n)];return R+R[::-1][N%2:]
ungolfing a bit:
from random import * # get 'choice' and 'shuffle'
def matrix(N):
n = ceil(N/2) # get the size of the base block
# get a shuffleable lowercase alphabet
c = [chr(i+97)for i in range(26)]
c = n*c # make it large enough to fill the base-block
shuffle(c) # randomize it
digit = choice('1234567890') # get random digit string
## this is only needed as to prevent uneven side-length matrices
# from having centerline digits.
allowed_indices = [i for i in range( # get all allowed indices
n*(N-n)) # skip those, that are in an unmirrored center-line
if(i+1)%n # only use those that are not in the center column
+1-N%2] # exept if there is no center column
index = choice(allowed_indices) # get random index
c[index]=digit # replace one field at random with a random digit
##
R=
for i in range(n):
r = c[i*n:i*n+n] # chop to chunks sized fit for the base block
R.append(r+r[::-1][N%2:]) # mirror skipping the center line
return R+R[::-1][N%2:] # mirror skipping the center line and return
Older, almost correct versions below:
Python2, Python3, 161 bytes
from random import *
N=26
n=N-N//2
c=[chr(i+97)for i in range(26)]
R=[ r+r[::-1][N%2:]for r in[(shuffle(c),c[:n])[1]for i in range(n)]]
R+=R[::-1][N%2:]
print(R)
It seems N differing elements is only almost guarranteed.
Python 2/Python 3, 170 bytes
from random import*
def m(N):n=N-N//2;r=range;c=n*[chr(i+97)for i in r(26)][:n*n];shuffle(c);R=[_+_[::-1][N%2:]for _ in[c[i*n:i*n+n]for i in r(n)]];return R+R[::-1][N%2:]
It seems I forgot rule 3. Also somehow the [:n*n] slipped in.
Python 2/Python 3, 227 bytes
from random import*
def m(N):n=N-N//2;r=range;C=choice;c=n*[chr(i+97)for i in r(26)];shuffle(c);c[C([i for i in r(n*(N-n))if(i+1)%n+1-N%2])]=`C(r(10))`;R=[c[i*n:i*n+n]+c[i*n:i*n+n-N%2][::-1]for i in r(n)];return R+R[::-1][N%2:]
ungolfing a bit:
from random import * # get 'choice' and 'shuffle'
def matrix(N):
n = ceil(N/2) # get the size of the base block
# get a shuffleable lowercase alphabet
c = [chr(i+97)for i in range(26)]
c = n*c # make it large enough to fill the base-block
shuffle(c) # randomize it
digit = choice('1234567890') # get random digit string
## this is only needed as to prevent uneven side-length matrices
# from having centerline digits.
allowed_indices = [i for i in range( # get all allowed indices
n*(N-n)) # skip those, that are in an unmirrored center-line
if(i+1)%n # only use those that are not in the center column
+1-N%2] # exept if there is no center column
index = choice(allowed_indices) # get random index
c[index]=digit # replace one field at random with a random digit
##
R=
for i in range(n):
r = c[i*n:i*n+n] # chop to chunks sized fit for the base block
R.append(r+r[::-1][N%2:]) # mirror skipping the center line
return R+R[::-1][N%2:] # mirror skipping the center line and return
Older, almost correct versions below:
Python2, Python3, 161 bytes
from random import *
N=26
n=N-N//2
c=[chr(i+97)for i in range(26)]
R=[ r+r[::-1][N%2:]for r in[(shuffle(c),c[:n])[1]for i in range(n)]]
R+=R[::-1][N%2:]
print(R)
It seems N differing elements is only almost guarranteed.
Python 2/Python 3, 170 bytes
from random import*
def m(N):n=N-N//2;r=range;c=n*[chr(i+97)for i in r(26)][:n*n];shuffle(c);R=[_+_[::-1][N%2:]for _ in[c[i*n:i*n+n]for i in r(n)]];return R+R[::-1][N%2:]
It seems I forgot rule 3. Also somehow the [:n*n] slipped in.
edited Sep 3 at 16:57
answered Aug 31 at 22:38
Teck-freak
1314
1314
Your answer is very clever in the way it constructs the symmetric matrix, but you have not satisfied rule 3 (as you don't have any digits in your result), nor rule 5 (e.g., ifn = 3
, you will never have an output containing a'z'
, so not every output is possible).
â Chas Brown
Sep 2 at 22:30
Well pickle me and ... you're correct @ChasBrown ! Well, the [:n*n] is a remainder from a different approach and frankly it shouldn't be there. But you're correct about rule three. I'll have to correct it. Give me a bit.
â Teck-freak
Sep 3 at 0:57
Tried your solution here, but there was an index error... BTW, TryItOnline is super handy here at PPCG! (Also, this problem is way trickier than I thought at first...)
â Chas Brown
Sep 3 at 5:01
I litterally ran it over 10000 times without any error.
â Teck-freak
Sep 3 at 15:06
found it. a ':' was missing. I copied it directly from my script, but it must have gotten lost. it should be "... :-1][N%2:]for i ..." instead of "... :-1][N%2]for i ...".
â Teck-freak
Sep 3 at 15:11
 |Â
show 1 more comment
Your answer is very clever in the way it constructs the symmetric matrix, but you have not satisfied rule 3 (as you don't have any digits in your result), nor rule 5 (e.g., ifn = 3
, you will never have an output containing a'z'
, so not every output is possible).
â Chas Brown
Sep 2 at 22:30
Well pickle me and ... you're correct @ChasBrown ! Well, the [:n*n] is a remainder from a different approach and frankly it shouldn't be there. But you're correct about rule three. I'll have to correct it. Give me a bit.
â Teck-freak
Sep 3 at 0:57
Tried your solution here, but there was an index error... BTW, TryItOnline is super handy here at PPCG! (Also, this problem is way trickier than I thought at first...)
â Chas Brown
Sep 3 at 5:01
I litterally ran it over 10000 times without any error.
â Teck-freak
Sep 3 at 15:06
found it. a ':' was missing. I copied it directly from my script, but it must have gotten lost. it should be "... :-1][N%2:]for i ..." instead of "... :-1][N%2]for i ...".
â Teck-freak
Sep 3 at 15:11
Your answer is very clever in the way it constructs the symmetric matrix, but you have not satisfied rule 3 (as you don't have any digits in your result), nor rule 5 (e.g., if
n = 3
, you will never have an output containing a 'z'
, so not every output is possible).â Chas Brown
Sep 2 at 22:30
Your answer is very clever in the way it constructs the symmetric matrix, but you have not satisfied rule 3 (as you don't have any digits in your result), nor rule 5 (e.g., if
n = 3
, you will never have an output containing a 'z'
, so not every output is possible).â Chas Brown
Sep 2 at 22:30
Well pickle me and ... you're correct @ChasBrown ! Well, the [:n*n] is a remainder from a different approach and frankly it shouldn't be there. But you're correct about rule three. I'll have to correct it. Give me a bit.
â Teck-freak
Sep 3 at 0:57
Well pickle me and ... you're correct @ChasBrown ! Well, the [:n*n] is a remainder from a different approach and frankly it shouldn't be there. But you're correct about rule three. I'll have to correct it. Give me a bit.
â Teck-freak
Sep 3 at 0:57
Tried your solution here, but there was an index error... BTW, TryItOnline is super handy here at PPCG! (Also, this problem is way trickier than I thought at first...)
â Chas Brown
Sep 3 at 5:01
Tried your solution here, but there was an index error... BTW, TryItOnline is super handy here at PPCG! (Also, this problem is way trickier than I thought at first...)
â Chas Brown
Sep 3 at 5:01
I litterally ran it over 10000 times without any error.
â Teck-freak
Sep 3 at 15:06
I litterally ran it over 10000 times without any error.
â Teck-freak
Sep 3 at 15:06
found it. a ':' was missing. I copied it directly from my script, but it must have gotten lost. it should be "... :-1][N%2:]for i ..." instead of "... :-1][N%2]for i ...".
â Teck-freak
Sep 3 at 15:11
found it. a ':' was missing. I copied it directly from my script, but it must have gotten lost. it should be "... :-1][N%2:]for i ..." instead of "... :-1][N%2]for i ...".
â Teck-freak
Sep 3 at 15:11
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f171435%2fsquare-random-symmetrical%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Comments are not for extended discussion; this conversation has been moved to chat.
â Megoâ¦
Aug 31 at 2:30