43
\$\begingroup\$

Inspired by this comment chain...

I wanna enklact my way out of this challenge but I can't...

@ETHproductions to enklact (v): to implement a lookup table using a subsection consisting of unique elements.


Enklacting is a very useful way to compress a lookup table. For example, let's say you had the following list of colors:

red
green
blue
yellow
purple
orange

If you want to be able to take a color as input and return it's index in this list, there's obviously the straightforward way:

["red", "green", "blue", "yellow", "purple", "orange"].index(input())

But there's a way we could do this in way less bytes:

"rgbypo".index(input()[0])

This works because the first (or 0'th) index of each string is unique. This example is obvious, but sometimes it's a little bit harder. What if we wanted to make a lookup table for this list?

Sweet Onion Chicken Teriyaki
Oven Roasted Chicken
Turkey Breast
Italian BMT
Tuna
Black Forest Ham
Meatball Marinara

In this case, we can't do this:

"SOTITBM".index(input()[0])

because there are two different inputs that start with a 'T', namely "Tuna" and "Turkey". We must look at a different index. If you look at the 4th index of each string, you'll notice that they are all unique. So we can do this...

"enklact".index(input()[3])

In this case, the "enklaction string" is "enklact".

That leads us to today's challenge...

Given a list of strings, return any valid enklaction string. Or in other words, given a list of strings, return any new string where each letter is unique, and the string is formed by joining the i'th letter of each string.

If there is no valid enklaction string, your submission must return an empty string or a consistent falsy value instead. As usual, either functions or full programs are allowed, and the input/output formats are permissive (within reason).

Each string will only contain printable ASCII, and this challenge is case sensitive.

This is , so try to write the shortest program possible in your language of choice!

Test cases

Input:
Programming
Puzzles
Code
Golf

Output (any one of these):
"ozdl"
"gzef"


Input:
the quick
brown fox
jumped over
lazy dogs

Output:
"tbjl"
"hrua"
"eomz"
" wpy"
"qne "
"if o"
"kxvs"

Note that "u dd" and "coog" are not valid.


Input:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Output:
""


Input:
AbC
aBc
bbC

Output:
"Aab"


Input:
@#$%^_
Hello_World
How are you?

Output:
"#eo"
"$lw"
"%l "
"^oa"


Input:
a
ab
ac

Output:
""
\$\endgroup\$
10
  • \$\begingroup\$ Can we return a list of valid strings? \$\endgroup\$
    – LyricLy
    May 5 '18 at 2:25
  • \$\begingroup\$ @LyricLy Hmm, now that I think about it, that would have made more sense. But since there are already answers and it isn't too much boilerplate to return the first one, I'm going to say no, it should be any one valid string. \$\endgroup\$
    – DJMcMayhem
    May 5 '18 at 2:36
  • \$\begingroup\$ Can we guarantee none of the inputted strings are empty? \$\endgroup\$ May 5 '18 at 2:44
  • 7
    \$\begingroup\$ Can the consistent falsy value be an error of consistent type? \$\endgroup\$ May 5 '18 at 8:18
  • 5
    \$\begingroup\$ Excuse me, but I think the correct verb is enklactate. \$\endgroup\$ May 5 '18 at 13:11

33 Answers 33

1
2
0
\$\begingroup\$

APL+WIN, 35 33 bytes

2 bytes saved thanks to Adám

Prompts for the lines of text as a character matrix:

⊃((↑⍴¨a)=+/¨((a⍳¨a)=⍳¨⍴¨a))/a←,⌿⎕

Try it online! Courtesy of Dyalog Classic

Explanation:

a←,⌿⎕ prompts for input and creates a nested vector of the input matrix columns

((a⍳¨a)=⍳¨⍴¨a) creates a binary vector for each nested element with a 1 for each unique element

((↑⍴¨a)=+/¨ sums each binary vector and compares to number of characters in each element

(...)/a←⊂[1]⎕ selects only those elements where number of uniques = column length

⊃ converts nested vector back to a matrix of each valid enklaction string 
\$\endgroup\$
1
0
\$\begingroup\$

C# (.NET Core), 167 bytes (149 + 18 for using System.Linq;)

p=>{int l=p.Count(),m=p.Min(s=>s.Length);for(int i=-1;i++<m;){var d=p.Select(s=>s[i]).Distinct();if(d.Count()==l)return string.Join("",d);}return"";}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Scala, 97 bytes

l=>l.flatMap(h=>(0 to l.map(_.size).min-1).find(i=>l.map(_(i)).toSet.size==l.size)map(h))mkString

input is List[String] and returns an empty String if no solution found.

Try it online.

Explanation:

l                                    // List("Programming", "Puzzles", "Code", "Golf")
  .flatMap(                          //
    h =>                             // "Programming"
      (0 to l.map(_.size).min-1)     // Range(0, 3) range from 0 to size of smallest word ("Code".size - 1 = 3)
        .find(                       // returns an Option[Int]: if for a given index, all words have a different char, return Some(i)
          i =>                       // index in words, let's say i = 2
            l.map(_(i)).toSet.size   // Set('o', 'z', 'd', 'l').size = 4; checks the nbr of unique chars for the given index
              == l.size              // and compare it to the nbr of words (4)
        )                            // 
        map(h)                      // Option(i).map(i => h(i)) which gives Some(2).map(h) = Some('o') and for another index: None.map(h) = None; this is why the begining is l.flatMap and not l.map.
  )mkString                         // joins the List[Char] into a String
\$\endgroup\$
2
  • \$\begingroup\$ Hello and welcome to the site. I don't know scala but this seems that this takes input from a predefined variable. This is not an accepted form of output. It looks like you can probably fix this by adding l=>. \$\endgroup\$
    – Grain Ghost
    May 7 '18 at 16:06
  • \$\begingroup\$ @WhatWizard Wasn't sure which convention to use to define the core of the code. I hope that's better. \$\endgroup\$ May 7 '18 at 16:28
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.