13
1
Write a program using the fewest bytes of source code which given a list of strings finds all elements which are still in the list when any character is deleted.
For example, given a list of all English words boats
would be output because oats
, bats
, bots
, boas
, boat
are all English words and thus would be in the list.
Simple Pyhon code example, assuming words
is the input list:
for w in words:
has_all = True
for i in range(len(w)):
test = w[:i] + w[i+1:]
if test not in words:
has_all = False
break
if has_all
print(w)
Example input list with some strings:
aorn (+)
aor (+)
arn
aon
orn (+)
ao
ar
or
rn
on
The strings marked with (+)
are the ones that should be the output.
arn
and aon
shouldn't be output because we can delete the middle character to get an
, which is not in the input list.
You should test that aba is not present on the output of this list:
aba
ba
aa
b
5I would not recommend to copy-paste a large dictionary in the challenge. But I think you should include a test dictionary (say ~100 words) so that the solutions can be easily tested. – Arnauld – 2020-01-30T09:44:18.167
4Edited with a small list of examples. Added
code-golf
tag. – chx – 2020-01-30T09:51:59.1971"when any character is deleted" -> then "arn" and "aon" should be positive I think, you remove "a" or "n" and it's in the list.. or are the first and last char not removeable? in that case should't say "any" – Kaddath – 2020-01-30T10:12:32.060
1@Kaddath when
r
is removed fromarn
, the result isan
, which is not in the list. It should be possible to remove any character, not just one of them. – Grimmy – 2020-01-30T10:58:10.670Might the input have repeated characters? – xnor – 2020-01-30T19:22:28.377
Yes, repeated characters are allowed. Indeed most English examples I am aware of have repeated characters. – chx – 2020-01-30T19:24:57.830
Nice first question! Could I ask you to repeat the information of the title, as well as the relevant bits from [tag:code-golf] into the body of the question? It is how most challenges are written here so it makes this easier to read. In addition the code-golf tag has important rules that can be easily missed by newcomers since they require clicking on a specific element to be found. Thanks. – Post Rock Garf Hunter – 2020-01-30T22:22:16.997
1I have added the requested bits. – chx – 2020-01-30T22:52:39.177
3I think you should add some test cases with repeat characters like
["aba","ab","aa","b"]
to catch solutions that try remove every copy of a character, or always remove the first copy. Having more test cases is good in general. – xnor – 2020-01-31T01:18:22.0102Are we really supposed to support 1-character words? If yes, are we also supposed to support the empty string, which would make 1-character words valid? (cc @xnor) – Arnauld – 2020-01-31T09:03:13.177