27
2
(Essentially the inverse of Find the needle in the haystack)
Given two tuples, (w, h)
and (x, y)
, generate a haystack composed of a single random printable ASCII character of w
width and h
height with a needle made of a different random printable ASCII character at (x, y)
when measured from the upper-left.
For example, when given (5,4)
for the width and height, and (3,1)
(zero-indexed) for the location of the needle, a possible haystack could be the following:
#####
###N#
#####
#####
Another possibility could be
*****
***$*
*****
*****
among hundreds of others.
Rules and Clarifications
- Input and output can be given by any convenient method. This means you can take input as a list of list of integers, as a single string, as two integers via command-line and a tuple via function argument, etc.
- You can print the result to STDOUT or return it as a function result.
- Either a full program or a function are acceptable.
- You can choose whether the
(x, y)
tuple is zero-indexed or one-indexed, but please specify in your solution which you're using. - You do not get to pick which characters to use. That's part of the challenge - randomly selecting the characters.
- Every possible output for a given input must have a non-zero chance of appearing, but the randomness doesn't need to be uniform.
- The haystack is guaranteed to be at least 2x2 in size, so it's unambiguous which is the needle and which is the hay.
- There is only ever one needle, and it's only ever one character in size, and it's guaranteed to be within the boundaries of the haystack.
- Standard loopholes are forbidden.
- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
Can we return a flat list of characters? – TFeld – 2019-10-16T15:02:13.737
@TFeld No, you must have
\n
(or equivalent) included where appropriate. – AdmBorkBork – 2019-10-16T15:05:16.540can we return a matrix of characters? – dzaima – 2019-10-16T15:09:54.043
@dzaima But of course. – AdmBorkBork – 2019-10-16T15:11:04.077
4Obligatory xkcd about the choice of the characters. Applies to a few early answers... :p – Arnauld – 2019-10-16T15:34:54.303
2@Arnauld I don't know what was the magic here, but I completely read the challenge as I can use any ASCII printable characters "that I like", instead of "random", and it is funny another person also did same exact thing! I didn't mean to implement the random in such a beautiful way :P – Night2 – 2019-10-16T16:17:03.273
Can the order of elements in the tuples be altered, and can the input format be mangled in any other ways? – Unrelated String – 2019-10-17T07:15:37.910
1@UnrelatedString Yes and yes? Those are standard I/O rules. – AdmBorkBork – 2019-10-17T12:23:21.263
Just a quick question for clarity - the two random printable ASCII characters should be by definition, dissimilar, correct? – Taylor Scott – 2019-11-16T18:37:30.143
1@TaylorScott Yes, that's covered in the opening paragraph. The two characters need to be different. – AdmBorkBork – 2019-11-18T13:34:18.380
@AdmBorkBork- Thanks for the clarification, I can be completely blind sometimes and did not see that! Cheers – Taylor Scott – 2019-11-18T17:28:02.690