Enklactify these strings

42

5

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:
""

James

Posted 2018-05-05T02:14:18.570

Reputation: 54 537

Can we return a list of valid strings? – LyricLy – 2018-05-05T02:25:46.330

@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. – James – 2018-05-05T02:36:01.523

Can we guarantee none of the inputted strings are empty? – musicman523 – 2018-05-05T02:44:44.040

@musicman523 Yes. – James – 2018-05-05T02:48:28.413

Can we take input as an array of character arrays? – Shaggy – 2018-05-05T05:34:22.207

@Shaggy the default is "yes" Relevant meta

– JungHwan Min – 2018-05-05T07:08:40.317

6Can the consistent falsy value be an error of consistent type? – Stewie Griffin – 2018-05-05T08:18:14.100

2

Excuse me, but I think the correct verb is enklactate.

– Erik the Outgolfer – 2018-05-05T13:11:13.617

@shaggy As far as I'm concerned, a character array is a string, so an array of characters arrays is perfectly fine. – James – 2018-05-05T16:02:07.320

@stewie I don't know... I feel like crashing instead of returning empty or false isn't really in the spirit. – James – 2018-05-05T16:04:32.357

Answers

8

Python 2, 68 67 61 bytes

lambda a:`{0:j for j in zip(*a)if len(set(j))==len(j)}`[6::5]

Try it online!

Improvements

  • From 68 bytes to 67 bytes by Jo King
  • From 66 to 65 bytes by Lynn

If output would not have to be a string:

Python 3, 49 bytes

lambda a:[j for j in zip(*a)if len({*j})==len(j)]

Try it online!

Neil

Posted 2018-05-05T02:14:18.570

Reputation: 2 417

67 bytes – Jo King – 2018-05-05T05:13:54.537

@JoKing Elegant and implemented. – Neil – 2018-05-05T05:37:01.833

I think max(`j`[2::5]for j in[""]+zip(*a)if len(set(j))==len(j)) works, to save a byte. – Lynn – 2018-05-05T09:00:31.480

@Lynn thanks and updated. – Neil – 2018-05-06T07:52:22.843

8

Python3, 59 bytes

lambda l:{len({*d}):''.join(d)for d in zip(*l)}.get(len(l))

Returns a string with the enklact, None otherwise

PieCot

Posted 2018-05-05T02:14:18.570

Reputation: 1 039

7

Retina, 43 32 bytes

+/^(.).+^\1|^$/ms&m`^.

|""Lm`^.

Try it online! Edit: Saved 11 bytes thanks to @MartinEnder. Explanation:

+

Repeat while the input changes...

/^(.).+^\1|^$/ms&

...only if one line is empty or two lines begin with the same character...

m`^.

...delete the first character of every line. The repeat therefore stops if either a) all the lines begin with different characters, in which case the condition fails and the input does not get changed, or b) at least one line becomes empty, in which case all the characters eventually get deleted, at which point the input stops changing.

|""L`^.

Collect the first character of every line. (If there was no solution, the above loop will have deleted everything and there will be nothing to collect.)

Neil

Posted 2018-05-05T02:14:18.570

Reputation: 95 035

Regex options also take regex modifiers (by writing them directly after the closing delimiter): https://tio.run/##K0otycxLNPz/P1dDP05DT1NPOy7GUD@3WDshTo@Ly7E4QU8rTkVPi6tGSckHKPT/f0BRfnpRYm5uZl46V0BpVVVOajGXc35KKpd7fk4aAA

– Martin Ender – 2018-05-06T07:33:52.657

Actually, that lets you get rid of the second stage entirely: https://tio.run/##K0otycxLNPz/X1s/TkNPU087LsawJk5FP7dYLTchTo@Lq0ZJyQfE@v8/oCg/vSgxNzczL50roLSqKie1mMs5PyWVyz0/Jw0A (somehow can't apply m with a group here, it looks like the conditional stage doesn't propagate the option).

– Martin Ender – 2018-05-06T07:42:39.673

Ah, of course, loop a conditional instead of a conditional loop, that solves the infinite loop bug in your first version. Very neat! – Neil – 2018-05-06T09:35:37.147

5

Haskell, 71 bytes

f x|elem""x=""|y<-head<$>x,and[filter(==a)y==[a]|a<-y]=y|1<2=f$tail<$>x

Try it online!

BMO saved 3 bytes with any null xelem""x.

Ørjan Johansen saved a byte with sum[1|b<-y,a==b]<2filter(==a)[y]==[a].

Explanation

f x|elem""x=""                      -- Once any of the strings is empty, return "".
   |y<-head<$>x                     -- Otherwise, let y be all the first letters...
   ,and[                 |a<-y]     -- If, for all a in y,
        filter(==a)y==[a]           -- a occurs just once in y:
                               =y   -- return y.
   |1<2=f$tail<$>x                  -- Else, chop off all the first letters and recurse.

If throwing an error (Prelude.head: empty list) when there's no solution is OK, |elem""x="" can be erased for 61 bytes.

Lynn

Posted 2018-05-05T02:14:18.570

Reputation: 55 648

1Shorter test: filter(==a)y==[a] – Ørjan Johansen – 2018-05-05T16:13:49.647

4

Pyth, 6 bytes

>1{I#C

Test suite.

The output is a singleton list, as allowed by default; the list [ ] (empty list, falsy) is returned in case the string cannot be enklactified.

Explanation

>1{I#C – Full program.
     C – Transpose the input, cropping absences.
    #  – Filter by:
  {I   – Invariant under deduplication.
>1     – Slice to 1. list[:1] in Python.

Pyth, 5 bytes

This would've been valid if crashing would have counted as a falsy value.

h{I#C

Test suite.

Mr. Xcoder

Posted 2018-05-05T02:14:18.570

Reputation: 39 774

4

Ruby, 38 bytes

->x,*y{x.zip(*y).find{|z|z==z-[p]|[]}}

Try it online!

Thanks to G B for pointing out a bug.

Kirill L.

Posted 2018-05-05T02:14:18.570

Reputation: 6 693

Fails if there is no match and the first string is not the shortest. – G B – 2018-05-07T12:32:40.463

@GB Could you provide an example, please? I modified my last test according to your description, and it worked. – Kirill L. – 2018-05-07T13:22:26.220

Try ["abc", "ac", "acd"] – G B – 2018-05-08T11:09:46.990

Now I see, you're right. Should be fixed. – Kirill L. – 2018-05-08T11:36:54.463

3

Haskell, 76 74 bytes

f t=last$"":(id=<<foldr(zipWith(#))([[]]<$t)t)
x#[s]=[x:s|all(/=x)s]
x#e=e

Try it online! Returns the last valid lookup string or an empty string if no such string exists.


71 69 bytes

If throwing a consistent exception as falsy value is allowed:

f t=head$id=<<foldr(zipWith(#))([[]]<$t)t
x#[s]=[x:s|all(/=x)s]
x#e=e

Try it online! Throws an empty list exception if no string is found, returns the first valid string otherwise.

-2 bytes thanks to Ørjan Johansen

Laikoni

Posted 2018-05-05T02:14:18.570

Reputation: 23 676

1notElem x can be shortened to all(/=x). – Ørjan Johansen – 2018-05-05T16:04:52.580

2

Jelly, 7 bytes

z0Q€fZḢ

Returns the integer 0 if the strings cannot be enklactified.

Try it online!

How it works

z0Q€fZḢ  Main link. Argument: A (string array)

z0       Zip/transpose, filling shorter rows with 0.
  Q€     Unique each deduplicate resulting string.
     Z   Zip/transpose, without using a filler.
    f    Filter; keep only string that appear in the results to both sides.
      Ḣ  Head; extract the first string. Returns 0 if the array is empty.

Dennis

Posted 2018-05-05T02:14:18.570

Reputation: 196 637

2

Perl 5, 61 bytes

map$;[$_].=$F[$_],0..@F;$-++}{($_)=grep$-==y///c*!/(.).*\1/,@

Try it online!

Dom Hastings

Posted 2018-05-05T02:14:18.570

Reputation: 16 415

2

Japt, 9 bytes

Takes input as an array of character arrays, returns an array of characters or undefined

y æ_f eZâ

Try it (Add a new line at the start of the programme with the code qR mq to take input as a newline seperated string, to save you the hassle of creating the arrays.)


Explanation

y             :Transpose
  æ_          :Pass each array Z through a function and return the first that returns true
    f         :  Filter nulls (used for padding when transposing)
      e       :  Test for equality with
       Zâ     :  Z deduplicated

Shaggy

Posted 2018-05-05T02:14:18.570

Reputation: 24 623

I tried the challenge out before reading your solution and got a nearly identical solution: z æ_¬eZ¬â – Nit – 2018-05-05T09:16:16.170

Exactly identical, other than the input format. – Shaggy – 2018-05-06T21:13:46.307

2

Stax, 9 8 bytes

åτIⁿs↓µg

Run and debug it

Explanation (unpacked):

M{c0-u=}j Full program, implicit input
          e.g. ["Programming", "Puzzles", "Code", "Golf"]
M         Transpose
                ["PPCG", "ruoo", "ozdl", "gzef", "rl\0\0", "ae\0\0", "ms\0\0", "m\0\0\0", "i\0\0\0", "n\0\0\0", "g\0\0\0"]
 {     }j Find first matching element:
            e.g. "PPCG"
  c0-       Copy and remove zero bytes (padding)
                 "PPCG" "PPCG"
     u      Unique
                 "PPCG" "PCG"
      =     Check if equal:
                 1
          First matching here: "ozdl". If none is found, the stack is empty
          Implicit output if anything on stack

wastl

Posted 2018-05-05T02:14:18.570

Reputation: 3 089

2

R, 127 bytes

function(S,s=sapply(S,substring,x<-1:max(nchar(S)+1),x))cat(rbind(s[!apply(s,1,anyDuplicated)&!rowSums(s==""),],"")[1,],sep="")

Try it online!

sapply typically returns a matrix when all the length(FUN(X[[i]])) are equal, except when length(FUN(X[[i]]))==1, in which case it returns a vector. In order to use the matrix operations, then, we have to substring one further than we need to guarantee a matrix, which is why x extends to max(nchar(S)+1).

We then filter for rows that have no duplicates and no empty strings. Since we can only return a single string, we take the first one, except that when there are no rows that meet the criteria, we would throw an error, so we append an extra row of "" to the end.

Then we print out the enklactified string or the empty string.

Giuseppe

Posted 2018-05-05T02:14:18.570

Reputation: 21 077

2

R, 116 107 95 bytes

R+pryr

pryr::f(for(i in 1:min(nchar(v)))`if`(anyDuplicated(t<-substr(v,i,i)),0,{cat(t,sep="")
break}))

Try it online!

base R

function(v)for(i in 1:min(nchar(v)))`if`(anyDuplicated(t<-substr(v,i,i)),0,{cat(t,sep="")
v=0})

Try it online!

These two variants saving 9 bytes thanks to Giuseppe.

Explanation:

This effectively truncates all stings in the vector v to the length of the shortest and iterates through subsequent indexes. Then checks whether there are any duplicates within chosen letters and if not, pastes them together and prints with cat. If all indexes return duplicated results, this prints empty string.
All wraped in an anonymous pryr function with break to halt the loop, or base R function zeroing the vector to break the loop.

pajonk

Posted 2018-05-05T02:14:18.570

Reputation: 2 480

1

Nice one! This can be golfed to 107 bytes in R+pryr or 107 bytes in base R.

– Giuseppe – 2018-05-05T17:12:13.050

2

05AB1E, 7 bytes

øʒDÙQ}н

Try it online!

Explanation

ø        # Zip the input array
 ʒ   }   # Filter by ...
  DÙQ    # ... an entry is equal to itself deduplicated
      н  # Take the first element

Kaldo

Posted 2018-05-05T02:14:18.570

Reputation: 1 135

1

Python 3, 75 bytes

def f(t):c=[s.pop(0)for s in t];return all(t)and(f(t),c)[len(t)==len({*c})]

Operates on character lists instead of strings. Returns False if no valid enklaction string exists.

Try it online!

musicman523

Posted 2018-05-05T02:14:18.570

Reputation: 4 472

I think this recurses, which would make it invalid unless the f= is included in the byte count. – LyricLy – 2018-05-05T03:01:49.427

@LyricLy Fixed :) – musicman523 – 2018-05-05T03:15:14.377

1

Wolfram Language (Mathematica), 54 bytes

#&@@Select[PadRight@#,#~FreeQ~0&&Union@#==Sort@#&]&

Try it online!

Takes a list of list of characters as input, returns a list of characters. Contains U+F3C7, corresponding to "Transpose" operator.

Returns #1 and throws a bunch of ignorable errors when there is no suitable string.

Explanation:

PadRight@#

Pad the input so that each "string" (list of characters) is the same length. This appends integer 0s (not string "0"s). Then transpose.

Select[ ... ,#~FreeQ~0&&Union@#==Sort@#&]

Select the strings that have no integer 0s in them and have all unique characters.

#&@@

Get the first one.

JungHwan Min

Posted 2018-05-05T02:14:18.570

Reputation: 13 290

1

Charcoal, 23 21 bytes

-2 bytes thanks to @Neil!

§ΦE⌊EθLι⭆θ§λι⬤ι⁼¹№ιλ⁰

Try it online!

ASCII-only

Posted 2018-05-05T02:14:18.570

Reputation: 4 687

Huh, when did a) q stop always being a string b) StringMap start working on non-strings? Anyway, your Range is unnecessary, saving you 2 bytes. – Neil – 2018-05-05T10:11:22.183

@Neil a) When I added array/object input b) >_> I'm not sure. probably around the time when I fixed the while loop (sorry, forgot to mention either of those) – ASCII-only – 2018-05-05T23:34:10.253

1

C (gcc), 161 bytes

f(s,i)char**s;{char**t,a[255],*u=a;for(i=0;memset(a,0,255),u&&~i;i+=!!~i&&u)for(t=s;(u=u?*t++:0)&&~(i=u[i]?i:-1)&&!a[u[i]]++;);while(~i&&(u=*s++))putchar(u[i]);}

Try it online!

Each character position is tested for duplication and skipped if a duplicate is detected; this is continued until the shortest string ends. It's ASCII only, sadly: DBCS/UTF-8 strings break this function badly!

ErikF

Posted 2018-05-05T02:14:18.570

Reputation: 2 149

152 bytes – ceilingcat – 2019-01-01T00:23:28.187

1

Japt, 12 bytes

Returns undefined for non-enklactic strings.

y ·æ_¬n ä¦ e

Try it online!

Explanation:

y ·æ_¬n ä¦ e
y             // Split the input at newlines and transpose
  ·           // Join on newlines 
   æ_         // Return the first item that returns truthy when ran through:
     ¬n       //   Sort
        ä¦    //   Reduce with !=
           e  //   All items are truthy (unique)

Oliver

Posted 2018-05-05T02:14:18.570

Reputation: 7 160

You should be able to save 2 bytes by taking input as an array of character arrays and ditching the two splits. – Shaggy – 2018-05-05T12:07:33.073

1

JavaScript (ES6), 66 bytes

Returns either a string or undefined if no solution exists.

f=(a,i=0)=>a.every(s=>(o[k+=c=s[i],c]^=1)&&c,o=k=[])?k:c&&f(a,i+1)

Try it online!

Commented

f = (           // f = recursive function taking:
  a,            //   a[] = input array
  i = 0         //   i   = pointer
) =>            //
  a.every(s =>  // for each string s in a[]:
    (o[k +=     //   append to the key string k
      c = s[i], //   the character c at position i in s
      c] ^= 1   //   and toggle o[c] (undefined -> 1 -> 0)
    ) && c,     //   additionally, make sure that c is defined
    o = k = []  //   start with o = k = empty array
  ) ?           // every() is true if all characters were distinct and defined:
    k           //   in which case we return k
  :             // else:
    c &&        //   provided that every() didn't fail because of an undefined character,
    f(a, i + 1) //   try again at the next position

Arnauld

Posted 2018-05-05T02:14:18.570

Reputation: 111 334

1

Husk, 9 bytes

ḟS=UḞz:∞ø

Try it online!

Explanation

fS=UḞz:∞ø
    Ḟz:∞ø  Transpose the input dropping characters of longer strings
    Ḟ        Fold right
     z:      Zip with prepend
       ∞ø    Infinite list of empty lists
ḟS=U       Find the first string without duplicates, returns an empty string if none
ḟ            Return first value satisfying predicate
  =          Equal
 S U         to itself with duplicates removed

Fyr

Posted 2018-05-05T02:14:18.570

Reputation: 561

Using ←ġLT over Ḟz:∞ø should save a byte. – ბიმო – 2018-05-17T23:41:28.597

1

Perl 6, 27 bytes

{([Z] $_).first:{.Set==$_}}

Try it online!

After seeing that the ruby version has been upvoted, I have copied the approach and used character lists, instead of strings. I don't like it.

My older, and more correct submission is as below:

Perl 6, 38 bytes

Split, zip, check uniqueness, join.

{[~] ([Z] @_>>.comb).first:{.Set==$_}}

Try it online!

Jarrod Funnell

Posted 2018-05-05T02:14:18.570

Reputation: 121

1

Retina, 81 56 bytes

m`$
$.=*£¢
N$`.
$.%`
¶

~`(.*?¢)+
L`.{$#1}
A`(.).*\1|£|¢

Try it online!

-25 bytes thanks to @Neil


Transposing rectangular text in Retina is surprisingly difficult.

TwiNight

Posted 2018-05-05T02:14:18.570

Reputation: 4 187

I wish I knew a better way of performing rectangular transposition, but in the mean time, save 25 bytes.

– Neil – 2018-05-07T11:07:03.697

@Neil Ahh... eval stage. I always forget retina 1.0 has all these cool new features – TwiNight – 2018-05-07T11:16:09.810

1

C (gcc), 121 113 110 bytes

i;d;f(s)char**s;{char**_=s,x[255]={0},y[99]={0};for(d=i=0;*_;)d+=x[y[i++]=*(*_++)++]++;d=d?*x?0:f(s):puts(y);}

Try it online!

Ungolfed

void enklactify(char *strings[]) {
    int quit = 0;
    while (!quit) {
        char **arg = strings;      // current row
        int exists[255] = {0};     // which characters exist in the column
        char output[99] = {0};     // will hold output string
        int index = 0;             // where to insert in output
        int duplicates = 0;        // have we found any duplicates?
        while (*arg != NULL) {
            char *word = *arg;     // current word
            char first = *word;    // first letter of current word
            if (exists[first])
                duplicates = 1;    // we found a duplicate
            exists[first] = 1;     // mark it as in our string
            output[index] = first; // add it to our output
            index++;
            (*arg)++;              // advances the current word to the next
                                   // character by reference (chops first char)
            arg++;                 // advance to next whole argument
        }

        if (!duplicates) {         // This is a valid solution
            puts(output);
            quit = 1;
        }

        if (exists[0]) {           // We hit the null terminator of one of the
                                   // input strings, so we failed
            quit = 1;
        }
    }
}

-3 thanks to ceilingcat

This abuses the ability to just add 1 to a string pointer in C to get the 'tail' of the string. The main byte savers are:

  • d+=x[y[i++]=*(*_)++]++ which appends the first character of the first string of _ to y, advances the first string of _ to remove its first character, adds the x entry at that character to d, and increments said x entry
  • q=d?*x:puts(y) which prints y if d is non-zero while setting q to a non-zero value, or sets q to non-zero if the first element of x is non-zero (if we were at the end of one of the strings then said element would be non-zero)

Edit: Shaved bytes by switching from while loop to recursive tail call and removing brackets from for loop.

LambdaBeta

Posted 2018-05-05T02:14:18.570

Reputation: 2 499

Suggest for(d=i=0;*_;) instead of for(d=0,i=0;*_;++_) and *(*_++)++]++;d=d?!*x*f(s) instead of *(*_)++]++;d=d?*x?0:f(s) – ceilingcat – 2018-06-21T22:14:48.590

0

Pyth, 13 bytes

e+kf{IT@.TQ.t

Try it here

e+kf{IT@.TQ.t
           .tQ   Transpose the (implicit) input with padding.
        .TQ      Transpose the input without padding.
       @         Take the strings in both.
   f{IT          Find the ones that have no duplicates.
e+k              Get the last, or an empty string.

user48543

Posted 2018-05-05T02:14:18.570

Reputation:

1It seems that returning a list of all valid strings is not valid. – LyricLy – 2018-05-05T02:40:00.770

@LyricLy Fixed. – None – 2018-05-05T02:40:30.840

0

Red, 139 bytes

func[b][m: length? first b foreach a b[m: min m length? a]repeat n m[c: copy[]foreach a b[append c a/(n)]if c = unique c[return rejoin c]]]

Try it online!

Explanation:

Takes the input as a block (list) of strings. Returns the enklaction string or none otherwise.

f: func[b][
    m: length? first b                   ; the minimal length of the first string  
    foreach a b[m: min m length? a]      ; find the minimal length of all strings
    repeat n m[                          ; limit the search to the minimal length
        c: copy[]                        ; an empty block  
        foreach a b[append c a/(n)]      ; for each string append the current char
        if c = unique c[return rejoin c] ; if all chars are unique, return the block
    ]  
]

Galen Ivanov

Posted 2018-05-05T02:14:18.570

Reputation: 13 815

0

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 

Graham

Posted 2018-05-05T02:14:18.570

Reputation: 3 184

⊂[1],⌿ – Adám – 2018-05-07T19:39:58.713

0

Röda, 80 77 bytes

f a{a|seq 0,#_|try{{|i|a|[_[i:i+1]]|orderedUniq|concat|[_]if[#_1=#a]}_|head}}

Try it online!

-1 byte thanks to Cows quack

Explanation:

f a{
  a|         /* Push the strings in a to the stream */
             /* For each string (_): */
  seq 0,#_|     /* Push a range from 0 to the length of _ to the stream */
  try{       /* Ignore errors during the following block */
    {|i|        /* For each i in the stream: */
      a|           /* Push strings in a to the stream */
      [_[i:i+1]]|  /* For each string, push the ith character to the stream */
      orderedUniq| /* Remove duplicate characters */
      concat|      /* Join the characters into a string */
      [_]if        /* Push the string to the stream if */
      [#_1=#a]     /* Its length is the length of a */
    }_|
    head        /* Take the first string in the stream and return it */
  }
}

The try keyword is used to dismiss errors that occur if i is larger than the length of the smallest string in a, or there is no answer and head causes an error.

fergusq

Posted 2018-05-05T02:14:18.570

Reputation: 4 867

You can remove the parens in seq to save a byte – user41805 – 2018-05-05T20:31:54.800

@Cowsquack Thanks! – fergusq – 2018-05-05T20:44:07.513

0

Java 10, 106 bytes

a->{for(int i=0;;i++){var r="";for(var s:a)r+=s[i];if(r.length()==r.chars().distinct().count())return r;}}

Will give an error instead of returning an empty String if no solution can be found. Input is a character-matrix.

Try it online.

Explanation:

a->{                  // Method with character-matrix parameter and String return-type
  for(int i=0;;i++){  //  Loop `i` upwards
    var r="";         //   Result-String, starting empty
    for(var s:a)      //   Loop over the character-arrays of the input
      r+=s[i];        //    And append every `i`'th character to `r`
    if(r.length()==r.chars().distinct().count())
                      //   If `r` only contains unique characters
      return r;}}     //    Return `r` as result

Kevin Cruijssen

Posted 2018-05-05T02:14:18.570

Reputation: 67 575

OP didn't seem to want to allow errors, although admittedly never put it in the post itself. – Ørjan Johansen – 2018-05-07T15:58:16.023

0

Clojure, 59 bytes

#(for[s(apply map list %):when(=(count(set s))(count %))]s)

Returns a list of lists of characters.

NikoNyrh

Posted 2018-05-05T02:14:18.570

Reputation: 2 361

0

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

Xavier Guihot

Posted 2018-05-05T02:14:18.570

Reputation: 223

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=>.

– Post Rock Garf Hunter – 2018-05-07T16:06:58.220

@WhatWizard Wasn't sure which convention to use to define the core of the code. I hope that's better. – Xavier Guihot – 2018-05-07T16:28:25.733

0

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!

Emanuel Vintilă

Posted 2018-05-05T02:14:18.570

Reputation: 111