68
\$\begingroup\$

Usually, it is said that "Doing X without Y" can be a trap to beginners writing challenges (source). However, I am cocky and think that I can definitely make an X without any Ys. Randomly. Oh yes, this will be good.

Challenge: Given an odd integer n greater than or equal to 1, output an ex of side length n made of random printable ascii characters sans "y" and "Y", and the space. All allowed characters must have a nonzero chance of occurring, but not necessarily uniform. This is a so the shortest code in bytes wins. You should, however, randomize each char--that is, the struts of the ex shouldn't be equal, unless if by chance.

The chars to appear

!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXZ[\]^_`abcdefghijklmnopqrstuvwxz{|}~"

Constructing the ex

Side length 1:

x

Side length 3:

x x
 x
x x

Side length 5:

x   x
 x x
  x
 x x
x   x

etc.

Example outputs

input
output
empty line

3
h 2
 ^
9 5

1
:

5
D   1
 W z
  W
 q j
W   1

Example implementation

You do not need to handle invalid inputs.

function generate(sideLength){
    var result = "";
    var INNER = "@"
    for(var i = 0; i < sideLength; i++){
        var p = i < sideLength / 2 ? i : sideLength - i - 1;
        result += " ".repeat(p) + ((sideLength / 2 | 0) == p ? "" : INNER) + " ".repeat(Math.max(sideLength - p * 2 - 2, 0)) + INNER + "\n";
    }
    
    return result.replace(RegExp(INNER, "g"), function(e){
        let c = "y";
        while(c === "y" || c === "Y"){
            c = String.fromCharCode((Math.random() * 94) + 33 | 0);
        }
        return c;
    });
}

function print(v){
    output.innerHTML = "";
    output.appendChild(document.createTextNode(v));
}

function update(){
    var n = Number(input.value);
    if(n !== Math.floor(n)) print("error: " + n + " is not an integer.");
    else if(n % 2 === 0)    print("error: " + n + " is not odd.");
    else if(n < 1)          print("error: " + n + "is less than one.");
    else                    print(generate(n));
}

input.onchange = update;

update();
* {
  font-family: Consolas, monospace;
}

#output {
  white-space: pre;
}
<input id="input" min=1 value=1 type=Number>
<div id="output"></div>

\$\endgroup\$
9
  • \$\begingroup\$ Which characters exactly must be eligible for appearing? \$\endgroup\$
    – xnor
    Jul 27, 2016 at 1:01
  • \$\begingroup\$ @xnor Characters from ! to ~ sans y and Y \$\endgroup\$ Jul 27, 2016 at 1:02
  • \$\begingroup\$ @LegionMammal978 No, because non-Y characters include y and . \$\endgroup\$
    – Leaky Nun
    Jul 27, 2016 at 1:05
  • 8
    \$\begingroup\$ Hmm.. random... \$\endgroup\$ Jul 27, 2016 at 2:26
  • 16
    \$\begingroup\$ Wait!? We're allowed to use "Y" and "y" in the our code? \$\endgroup\$
    – Adám
    Jul 27, 2016 at 5:35

31 Answers 31

1
2
1
\$\begingroup\$

Vyxal (canvas), 121 bitsv2, 15.125 bytes

½⌈?dkP‛YyFƈṅ\xø∧

Try it Online!

Bitstring:

0001001010010001111011001101111101001110110100101000000011001100001010010000110110000101011011000011110111101111101100110
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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