Is it a wordinian?
Python, 52 bytes
lambda w,d:len({len(x)for x in d if x in w})==len(w)
An anonymous function that takes a word w
and dictionary d
. It takes the words in d
that are substrings of w
, makes a set of their lengths, and then checks that there as many distinct lengths as there are letters in w
.
Python 3, 108 bytes
lambda w,d,r=range:all(any(i in d for i in j)for j in[[w[i:i+s]for i in r(len(w)+1-s)]for s in r(1,len(w))])
An anonymous function that takes input, via argument, of a word w
as a string and a dictionary d
as a list of strings and returns True
or False
.
How it works
The first step is a list comprehension that generates a list of lists of all substrings of w
excluding w
, grouped by length. For example, for 'stage'
, the list [['s', 't', 'a', 'g', 'e'], ['st', 'ta', 'ag', 'ge'], ['sta', 'tag', 'age'], ['stag', 'tage']]
is generated. This is achieved by looping over all valid start indices i
for each substring length s
, and slicing every s
-length substring using w[i:i+s]
. For each list in this list, the presence of each substring in the dictionary is checked; calling any
returns a hit if at least one match for a given length is found. Finally, calling all
checks if a match has been found for all substring lengths, and the result of this is returned.
Try it on Ideone
Ruby, 44 bytes
- 7 Bytes off thanks to @NotThatCharles and his set operator tricks!
- 2 bytes off thanks to @Jordan with the Ruby 2.3 safe navigation operator trick
w[x]&.size
:)
->w,d{[*1..w.size]-d.map{|x|w[x]&.size}==[]}
It's an anonymous functions which takes a word w
and a dictionary (array of words) d
. Creates two arrays: The first containing the numbers 1 up to and including the length of w
; The second array is d
with each word mapped to their size if they are a substring of w
, otherwise nil
. Then it does set substraction to check whether the second array contains all element of the first array.