10
\$\begingroup\$

Given 3 input items, a list of coordinate pairs, a 2D string, and a single-character string, output whether the character at each coordinate of the 2D string is equal to the single character. You can take the input in any order and the coordinates may be 1-indexed.

You may take the 2D string as a 2D list, a list of lines, or a 2D string.

Example: (0,0), "#_\n__", "#" -> True

The string is

#_
__

The char at the coordinate (0,0) (from the top left) is #. This is equal to the third input item, #, so you output True (or any truthy value)

Example: [(0,0), (1,1)], "#_\n_#", "#" -> True

The string is

#_
_#

The characters at the coordinates of (0,0) and (1,1) are both #, so the output is true.

The output is only true iff every coordinate matches a hash. Not every hash has to have a matching coordinate though. If there are no occurrences of the single char (# in some of the test cases) in the 2D string, the output is still just falsy.

You can assume the coordinates will always be within the bounds of the 2D string.

More test cases: (I put the single char second for ease of readability)

[(0,0), (2,1), (3,0)], #

#_##
#_##

True


[(0,0), (1,1), (3,0)], #

#_##
#_##

False (1,1 is not a hash)



[(1,1)], a

#a##
#a##

True


[(4, 0), (3, 0), (2, 0), (1, 0), (0, 0), (0, 1), (0, 2), (0, 3), (1, 3), (2, 3), (2, 2), (3, 2), (4, 2), (4, 3)], ' '


 ####
 #   
   # 

True

Note the last test case uses spaces as the single char string, and hashes around the spaces.

Related. (inverse of this challenge)

\$\endgroup\$
16
  • \$\begingroup\$ Can we assume that input is a 2d array instead of using "\n"? \$\endgroup\$
    – rahnema1
    Jan 14, 2017 at 15:54
  • \$\begingroup\$ @rahnema1 not a 2D array, but an array/list of lines yes. \$\endgroup\$
    – Riker
    Jan 14, 2017 at 15:54
  • \$\begingroup\$ @EasterlyIrk I believe this falls into the category Cumbersome I/O formats \$\endgroup\$ Jan 14, 2017 at 16:06
  • \$\begingroup\$ In your first example coordinates are in the format (row, column) but in the last example coordinates are in the format (column, row) . \$\endgroup\$
    – rahnema1
    Jan 14, 2017 at 16:07
  • 1
    \$\begingroup\$ Can the coordinates be 1-indexed? \$\endgroup\$
    – user41805
    Jan 14, 2017 at 16:14

12 Answers 12

6
\$\begingroup\$

Python, 39 bytes

Takes the inputs:

  1. a list of (x, y) integer coordinates
  2. b list of strings
  3. c single character string

lambda a,b,c:{b[y][x]for x,y in a}=={c}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ On this site, you don't need to name your functions. You can remove the f=. Welcome to PPCG! \$\endgroup\$
    – Riker
    Jan 14, 2017 at 20:48
  • \$\begingroup\$ Welcome to PPCG, nice first answer! \$\endgroup\$
    – FlipTack
    Jan 15, 2017 at 11:12
4
\$\begingroup\$

JavaScript (ES6), 37 bytes

Takes the inputs:

  1. a array of [x, y] integer coordinates
  2. s array of strings
  3. c single character string

(a,s,c)=>a.every(([x,y])=>s[y][x]==c)
\$\endgroup\$
4
\$\begingroup\$

Octave, 45 38 29 bytes

@(A,B,C)A(1+B*[rows(A);1])==C

A function that takes a 2D array of chars as A and coordinates(0 based) B as a two column matrix of [col row] and the matching character as C. The two element coordinates(using matrix multiplication) converted to linear index.

Note: Previous answer that made use of sparse matrix was wrong.

Other Contributors:

Stewie Griffin for saving 5 bytes noting that [0 1 0] can be regarded as false value!!

Luis Mendo for saving 2 bytes that ~0 == true and notification about sparse matrix.

Try it Online

\$\endgroup\$
8
  • 2
    \$\begingroup\$ Nice :) You can skip all and save three bytes. 1 1 1 is true, and 1 0 1 is false in Octave, so it should be OK. :) \$\endgroup\$ Jan 14, 2017 at 21:22
  • 1
    \$\begingroup\$ Great approach! I like how this exploits the fact that logical indices need not have the same size as the indexed array \$\endgroup\$
    – Luis Mendo
    Jan 15, 2017 at 2:52
  • 1
    \$\begingroup\$ In addition to Stewie's suggestion, you can replace true by ~0 to save 2 bytes \$\endgroup\$
    – Luis Mendo
    Jan 15, 2017 at 3:00
  • \$\begingroup\$ @StewieGriffin Thanks it is really OK:) \$\endgroup\$
    – rahnema1
    Jan 15, 2017 at 3:31
  • \$\begingroup\$ @LuisMendo Good point \$\endgroup\$
    – rahnema1
    Jan 15, 2017 at 3:31
3
\$\begingroup\$

Mathematica, 28 bytes

#3~Extract~#~MatchQ~{#2...}&

1-indexed. Due to how arrays are structured in Mathematica, the input coordinates must be reversed (i.e. (row, column))

Usage

#3~Extract~#~MatchQ~{#2...}&[{{1, 1}, {2, 3}, {1, 4}}, "#", {{"#", "_", "#", "#"}, {"#", "_", "#", "#"}}]

True

\$\endgroup\$
2
\$\begingroup\$

Haskell, 27 bytes

s!c=all(\(x,y)->s!!y!!x==c)

Usage example: ( ["#_##","#_##"] ! '#' ) [(0,0), (2,1), (3,0)] -> True.

\$\endgroup\$
2
\$\begingroup\$

Jelly, 10 bytes

ịṪ⁸ịḢð€Q⁼⁵

This only works as a full program. Input order is indices, string array, singleton string.

Try it online!

How it works

ịṪ⁸ịḢð€Q⁼⁵  Main link.
            Left argument:  P (array of coordinate pairs)
            Right argument: S (array of strings)
            Third argument: C (singleton string)

     ð€     Combine the links to the left into a dyadic chain and call it with each
            p = (x, y) in P as left argument and S as the right one.
ị             Unindex; retrieve the strings of S at indices x and y.
 Ṫ            Tail; yield s, the string of S at index y.
  ⁸ị          Unindex; retrieve the characters of s at indices x and y.
    Ḣ         Head; yield the character of s at index x.
       Q    Unique; deduplicate the resulting string/array of characters.
        ⁼⁵  Compare the result with the third argument.
\$\endgroup\$
2
\$\begingroup\$

Perl 6, 41 40 bytes

->\c,\h,\n{all map {n eq h[.[0];.[1]]},c}

->$_,\h,\n{all .map:{n eq h[.[0];.[1]]}}

Expects the 2D string as a 2D list.

Thanks to b2gills for -1 byte.

\$\endgroup\$
2
  • \$\begingroup\$ If you used $_ instead of \c you could use .map:{…} saving one byte \$\endgroup\$ Jan 14, 2017 at 21:10
  • \$\begingroup\$ @BradGilbertb2gills: Oh, I didn't realize the space was optional in .map: {…}. That's useful to know. Also, it's a shame that prefix || isn't implemented yet, it could make the inner lambda simply n eq h[||$_]... \$\endgroup\$
    – smls
    Jan 15, 2017 at 7:58
2
\$\begingroup\$

C#, 80 77 bytes

Saved 3 bytes, thanks to pinkfloydx33

a=>b=>c=>{foreach(var i in a){if(b[i[0]][i[1]]!=c){return 1<0;}}return 1>0;};

a is the pairs of coordinate, b is the list of lines, and c is the single-character string.

\$\endgroup\$
1
  • \$\begingroup\$ You can replace false with 1<0 and true with 1>0 and save 3 bytes. \$\endgroup\$ Jan 15, 2017 at 9:16
1
\$\begingroup\$

Dyalog APL, 8 bytes

Prompts for list of coordinate pairs (row,column), then 2D array, then character.

∧/⎕=⎕[⎕]

[⎕] prompt for coordinates and use them to scatter pick from

prompted input (2D array)

= compare the selected elements to

input (the character)

∧/ check if all are true (AND-reduction)

Test cases (⎕IO←0 to match examples, but this is not necessary):

First example

Second example

Third example

Fourth example

Fifth example

\$\endgroup\$
1
\$\begingroup\$

Haskell, 72 63 bytes

c [] _ _ =1<2;c ((f,s):t) m n |n/=lines m!!s!!f=1>2|1>0=c t m n

Input of c [(0,0), (1,0), (3,0)] "#_##\n#_##" '#' Outputs False

Input c [(4, 0), (3, 0), (2, 0), (1, 0), (0, 0), (0, 1), (0, 2), (0, 3), (1, 3), (2, 3), (2, 2), (3, 2), (4, 2), (4, 3)] " \n ####\n # \n # " ' '

Output True

UnGolfed

checkfunc :: [(Int,Int)] -> String -> Char -> Bool
checkfunc [] _ _ = True
checkfunc (x:xs) string char | char /= ((lines string)!!(snd x))!!(fst x)= False  -- Checks first coordinates and returns False if no match
                             | otherwise = checkfunc xs string char --Otherwise iterate over remaining coordinate pairs
\$\endgroup\$
3
  • \$\begingroup\$ There is still unnecessary whitespace left: c[]_ _=1<2;c((f,s):t)m n|n/=lines m!!s!!f=1>2|1>0=c t m n \$\endgroup\$
    – Laikoni
    Jan 15, 2017 at 14:09
  • \$\begingroup\$ Also as you are doing boolean logic, the implicit conditional if n/=lines m!!s!!f then False else c t m n can be replaced by n/=lines m!!s!!f&&c t m n. \$\endgroup\$
    – Laikoni
    Jan 15, 2017 at 14:13
  • \$\begingroup\$ Finally, as the OP says You may take the 2D string as a 2D list, a list of lines, or a 2D string. you can drop the lines and directly take a list of lines as input. \$\endgroup\$
    – Laikoni
    Jan 15, 2017 at 14:19
1
\$\begingroup\$

Scala, 68 bytes

def f(l:(Int,Int)*)(s:String*)(c:Char)=l forall{x=>s(x._2)(x._1)==c}
\$\endgroup\$
1
\$\begingroup\$

Clojure, 39 bytes

#(apply = %3(map(fn[[x y]]((%2 y)x))%))

Example (string input is a vec of vec of characters):

(def f #(apply = %3(map(fn[[x y]]((%2 y)x))%)))
(f [[0 0] [1 1] [3 0]] (mapv vec ["#_##" "#_##"]) \#)
\$\endgroup\$

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.