11
\$\begingroup\$

Objective

Write a program or function (or equivalent) that sorts out and returns the odd letter in the matrix of random size.

Details

You will be passed a matrix (as a string) as input of random dimensions such as this.

bbbbbbbbbb
bbbbbdbbbb
bbbbbbbbbb
bbbbbbbbbb
bbbbbbbbbb

Your job is to find the letter that doesn't match the rest (in this case, it is d, found at line 2, col 6) and to return that letter as output. The matrix will consist of letters A-Z, a-z, newlines (\n, only on ends of rows) and have dimensions ranging from 5x5 to 10x10 (25-100 letters).

Standard loopholes apply. This is a code golf challenge; entry with code of least bytes wins.

Input

Input will be passed in through standard input as a string if it is a program or as an argument if a function (or similar).

Output

A single character that is the "odd" in the matrix or None, nil, NUL, or the string "None" if there is no "odd" character.

More Examples

AAAAAAA
AAAAAAA
AAAAAAA
AAAIAAA
AAAAAAA

Answer: I

vvqvvvvvvv
vvvvvvvvvv
vvvvvvvvvv
vvvvvvvvvv
vvvvvvvvvv

Answer: q

puuuuuuuu
uuuuuuuuu
uuuuuuuuu
uuuuuuuuu
uuuuuuuuu
uuuuuuuuu
uuuuuuuuu
uuuuuuuuu
uuuuuuuuu
uuuuuuuuu

Answer: p

Generator

Here is a random matrix generator written in Python that you can use to test your program. Note: There is a slight chance that it could make a mistake and not put in an odd letter.

Instructions

1. Copy this code into a file called `matrix_gen.py`.
2. Run it with `python matrix_gen.py`.

---

from random import randint

rows = randint(5,10)
cols = randint(5,10)

charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
neg = charset[randint(0,51)]
pos = charset[randint(0,51)]
p_used = 0

comp = 0
matrix = ""

while comp < rows:
  row = ""
  while len(row) < cols:
    if not p_used and not randint(0,10):
      p_used = 1
      row = row + pos
    else:
      row = row + neg
  row = row + "\n"
  matrix = matrix + row
  comp += 1

print matrix[:-1]

\$\endgroup\$
  • 1
    \$\begingroup\$ Here is a literal translation of your Python code into JS. \$\endgroup\$ – Arnauld Nov 28 '17 at 10:09
  • 1
    \$\begingroup\$ @juniorRubyist "removing the bonus" isn't the same as "making the bonus mandatory". By moving the part that was optional so far into the requirements of the challenge, you've invalidated a large part of the existing answers. \$\endgroup\$ – Martin Ender Nov 30 '17 at 8:50

34 Answers 34

1
2
0
\$\begingroup\$

PHP, 74 bytes

normal program, 57 bytes:

<?=chr(array_search(min($a=count_chars($argv[1],1)),$a));

(prints a newline if the is no odd character)

modified program, 74 bytes:

<?=count($a=count_chars($argv[1],1))<4?None:chr(array_search(min($a),$a));

(assumes Windows linebreaks; replace 4 with 3 for input with Unix linebreaks)

Run with php <scriptname> '<matrix>' or try then online.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

R, 61 bytes

cat(names(sort(table(unlist(strsplit(scan(,""),""))),T))[-1])

Try it online!

Takes input from stdin, outputs the letter (or the empty string) to stdout.

Explanation:

x=scan(,"")                     # read in input
x=unlist(strsplit(x,""))        # split into characters
x=table(x)                      # tabulate character counts
x=sort(x,T)                     # sort into decreasing order
x=names(x)                      # get the names (the characters)
x=x[-1]                         # remove the first element
cat(x)                          # print out the remaining letter (or nothing if none exist)

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Ruby, 33 bytes

->s{s.chars.min_by{|c|s.count c}}

Try it online!

Uses the same idea as Mr. Xcoder's great Python answer.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Java (OpenJDK 8), 93 98 108 bytes

a->{char[]r=a.toCharArray();java.util.Arrays.sort(r);return String.valueOf(r).replaceAll("(.)\\1+|\\n","");}

Try it online!

The input is converted to an array of characters, which is then sorted. We have to make a string again so we can replace all occurences matching the regex.

I first tried to simply do it with one "replaceAll" but I did not manage to create a regex that would delete the first a in "abaa" for example.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I think what you're looking for in the first two statements is char[] r = a.toCharArray(); Arrays.sort(r); a = new String(r);; the existing code doesn't actually do anything to a. This should fix your solution for cases like abaa. \$\endgroup\$ – Jakob Nov 29 '17 at 3:00
  • \$\begingroup\$ @Jakob the code does something. It converts a string to a char array and sorts it, then it changes it back to a string and replaces everything appearing more than once. If I managed to create a regex matching cases like abaaI could leave out the part converting the string to an array etc. \$\endgroup\$ – Luca H Nov 29 '17 at 7:28
  • \$\begingroup\$ Nope. The first statement sorts a copy of the string's character array and discards it, and the second statement passes a through String.valueOf(Object), which leaves it unchanged. \$\endgroup\$ – Jakob Nov 29 '17 at 17:37
  • \$\begingroup\$ @Jakob I don't have any words for this... I am sure I tested it and it was working in my tests but I simply feel stupid now, of course it discards the copy... Thank you \$\endgroup\$ – Luca H Nov 30 '17 at 10:23
  • \$\begingroup\$ @Jakob fixed, but it costs 5 bytes... I should stop trying to submit Java solutions, in part because I seem to be an idiot :D \$\endgroup\$ – Luca H Nov 30 '17 at 10:32
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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