它是词条吗?


20

使用任何编程语言来查看输入是否为词条的最短方法是什么?

单词是包含长度为1到原始单词长度的单词的单词。例如,

箱子

“我”是单词
“中”是单词
“ bin”是单词

要么,

阶段

'a'是单词
'ta'是单词(是的
'age'是单词
'stag'是单词
'stage'是单词

输入值

您的代码应采用任何合理格式的单词和字典作为输入。

输出量

输出应为一个值,该值指示对或错,以告诉我们该单词是否为单词。

有关单词学家的更多信息,请单击此处

这是我将用作输入和子单词的单词列表。另外,作为对@xnor的响应,它必须包含每个长度的子字,而不是一个子字链。注意,只有一个词将用作输入。


@FryAmTheEggman我不能在这里放整本字典。如果存在任何单词怎么办?
雅克·马赖斯

6
我建议传入字典作为输入。这样,它很容易提出测试用例(因为您可以制作自己的小词典)
Nathan Merrill

2
它是否只需要包含每个长度的子词,还是必须是每个子词在前一个字母后增加一个字母的链条?
xnor

@FryAmTheEggman我编辑了问题以提供所有单词的列表。
雅克·马赖斯

1
@JacquesMarais的概念是取一个单词一个字典,如果单词是单词(根据字典),则返回true
Nathan Merrill

Answers:


4

Pyth,20 16 15 13 11个字节

感谢Leaky Nun节省了4个字节!不幸的是,此后我更改了整个方法,但仍然有帮助。

gl{lM}#zQlz

期望输入为字典,后跟单词。输出True或False。

在这里尝试!

说明:

        lz   Collects the length of the word  input
g             and compares it to:
 l             The length of the following:
     # Q        Select all words from the dictionary that
    } z         are contained within the input word.
  lM            Map them to their respective lengths, and
 {              then remove any duplicates.

如果空字符串""是有效单词,则此功能不起作用。


1
.E可以替换为s
Leaky Nun

1
m}kH可以替换为}RH
Leaky Nun

1
我只想给你的golfed码
漏尼姑

11

Python,52个字节

lambda w,d:len({len(x)for x in d if x in w})==len(w)

一个带单词w和字典的匿名函数d。它使用中的单词d作为的子字符串w,确定它们的长度,然后检查是否有与中的字母一样多的不同长度w


gh,我只是写了完全一样的东西,只不过我用W代替x,用[代替{。+1
丹尼尔(Daniel)

@Dopapp如果您使用[而不是,它将不起作用{{...}是一个集合理解(与相同set([...]))。
mbomb007 '16

@ mbomb007,哦,对了,需要一套
Daniel Daniel

@xnor对不起,您没有选择这个答案,但由于它是代码高尔夫球,所以我必须选择最短的答案……
Jacques Marais

4

Python 3,108个字节

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))])

匿名函数,通过参数将单词w作为字符串输入,将字典d作为字符串列表通过输入返回TrueFalse

怎么运行的

第一步是列表理解,它会生成一个按长度分组的w排除的所有子字符串的列表的列表w。例如,对于'stage',将[['s', 't', 'a', 'g', 'e'], ['st', 'ta', 'ag', 'ge'], ['sta', 'tag', 'age'], ['stag', 'tage']]生成列表。这是通过遍历i每个子字符串长度的所有有效起始索引s,然后s使用来对每个长度的子字符串进行切片来实现的w[i:i+s]。对于此列表中的每个列表,都会检查字典中每个子字符串的存在;any如果找到至少一个给定长度的匹配项,则调用返回一个匹配。最后,打电话all检查是否已找到所有子串长度的匹配项,并返回结果。

在Ideone上尝试


4

Ruby,44个字节

  • @NotThatCharles及其设置的运算符技巧,可节省7个字节!
  • 多亏@Jordan和Ruby 2.3安全导航操作符,才能节省2个字节w[x]&.size:)
->w,d{[*1..w.size]-d.map{|x|w[x]&.size}==[]}

这是一个匿名函数,需要一个单词w和一个字典(单词数组)d。创建两个数组:第一个数组包含数字1,最大长度为w;如果d每个单词都是的子串w,则第二个数组的每个单词都映射为其大小nil。然后,它会设置减法以检查第二个数组是否包含第一个数组的所有元素。


1
使用Ruby 2.3中的“安全导航运算符”,您可以保存几个字节:w[x]&.size==i而不是x.size==i&&w[x]
约旦

哇,谢谢@乔丹。不知道一个,太棒了:)
daniero

1
您可以通过删除uniqand -[p]并使用set减法来在匿名函数(可能还有完整程序)中再保存几个字节:[*1..w.size]-d.map{...}==[]
不是查尔斯(Charles)

@NotthatCharles太好了!谢谢:)
daniero

3

PowerShell的V3 + V2 +,127个 110 70 65字节

param($a,$d)($d|?{$a-match$_}|select length -U).count-eq$a.length

(尽管我是独立开发的,但现在我的方法类似于@xnor的方法)

接受输入的单词$a和字典$d,期望$d作为数组(请参见下面的示例)。循环浏览$d并执行a,Where-Object以提取当前单词$_-match针对输入单词的正则表达式的条目$a(即,当前单词是输入单词的子字符串)。

我们收集所有这些子字符串单词,并将它们通过管道传递Select-Objectlength参数和-Unique约束。这将拉出每个子字符串的唯一长度。例如,对于输入单词comb,这将是(4,2)for 的数组('comb','om')

我们采用该.count结果数组的,并将其与输入单词的比较.length。如果等于,则意味着每个子串长度都在字典中,所以$TRUE,否则我们至少缺少一个,所以$FALSE。该布尔值保留在管道上,并且输出是隐式的。

注意:这应该在v2 +中有效,因为-in不再存在该运算符,但是我尚未测试该版本。

例子

PS C:\Tools\Scripts\golfing> .\is-it-a-wordinian.ps1 'stage' (gc .\words.txt)
True

PS C:\Tools\Scripts\golfing> .\is-it-a-wordinian.ps1 'metal' (gc .\words.txt)
True

PS C:\Tools\Scripts\golfing> .\is-it-a-wordinian.ps1 'comb' (gc .\words.txt)
False

2

Perl,86个字节

需要 -E在不增加成本。

chop(($s,@d)=<>);for$=(1..($x=length$s)){$-+=!!grep$s=~/$_/,grep$===y///c,@d}say$-==$x

接受通过STDIN的所有输入。首先输入是目标词,其余输入是字典。1成功时打印,失败时为空字符串。

用法

perl -E 'chomp(($s,@d)=<>);for$=(1..($x=length$s)){$-+=!!grep$s=~/$_/,grep$===y///c,@d}say$-==$x' <<< 'stage
a
ta
age
stag
stage'
1
perl -E 'chomp(($s,@d)=<>);for$=(1..($x=length$s)){$-+=!!grep$s=~/$_/,grep$===y///c,@d}say$-==$x' <<< 'stage
a
at
age
stag
stage'

perl -E 'chomp(($s,@d)=<>);for$=(1..($x=length$s)){$-+=!!grep$s=~/$_/,grep$===y///c,@d}say$-==$x' <<< 'bin
i
in
bin'
1

2

Mathematica,90个字节

Sort[MemberQ[DictionaryWordQ/@StringPartition[#,t,1],True]~Table~{t,StringLength@#}][[1]]&

使用Mathematica的内置DictionaryWordQ

将输入d作为字典要短5个字节,但对于长列表则要慢得多:

m=MemberQ;Sort[m[d~m~#&/@StringPartition[#,t,1],True]~Table~{t,StringLength@#}][[1]]&

2

MATL,15字节

使用@xnor的答案中的想法节省了1个字节。

XXgc32>!suz1Gn=

输出10用于truthy或falsy。

在线尝试!

XX      % Take the two inputs implicitly. Apply the second as a regex into the
        % first. Since the second input is a cell array, each of its contents is
        % applied separately as a regex. So for each dictionary word ("sub-word") 
        % this outputs the sub-word if found in the original word, or else an 
        % empty array. Gives a cell array of cells of strings
g       % Remove one level of nestedness
c       % Convert to char. This concatenates all found sub-words as rows of a 2D 
        % char array, padding with spaces as needed
32>!s   % For each row, count how many non-space characters there are. This is 
        % the length of each sub-word
uz      % Number of distinct non-zero elements
1Gn     % Push length of the original word
=       % Are they equal? Implicitly display

1

Perl,42岁 41字节

包括+2 -p0

输入单词,然后在STDIN上输入字典:

(echo stage; cat dictionary.txt) | ./wordinian.pl

(在UNIX上进行测试时,请确保dictionary.txt \n用作行终止符,而不是\r\n

wordinian.pl

#!/usr/bin/perl -p0
s%\G.%!/^.*(.{$+[0]})\H*
\1
/%eg;$_=!//

1

JavaScript(Firefox 30-57),68个字节

(w,a)=>new Set((for(x of a)if(~w.search(x))x.length)).size==w.length

使用生成器理解可避免创建中间数组。73字节的ES6版本:

(w,a)=>new Set(a.filter(x=>~w.search(x)).map(x=>x.length)).size==w.length

1

05AB1E,8 个字节

ŒÃ€gZLåP

单词作为第一个输入,字典列表作为第二个输入。

在线尝试验证更多测试用例

说明:

Π        # Get all possible substrings of the (implicit) input-string
 Ã        # Only keep the ones which are also in the (implicit) dictionary-list
  g      # Get the length of each remaining string
    Z     # Push the maximum length (without popping the list)
     L    # Pop and push a list in the range [1, maximum-length]
      å   # Check for each value if it's in the list of lengths
       P  # And check if this is truthy for all
          # (then output the result implicitly as result)


0

合格证,147字节

使用文件功能格式:

params["w","d"];a=[];c=count w;for"i"from 1 to c do{a=a+[""];for"j"from 0 to c-i do{q=w select[j,i];if(q in d)then{a set[i-1,q]}}};c==count(a-[""])

致电为: ["WORD", DICTIONARY] call NAME_OF_COMPILED_FUNCTION

取消高尔夫:

//name parameters
params["w", "d"];
a = []; c = count w;
//for each length of subword
for "i" from 1 to c do {
    //add entry to the `a`
    a = a + [""];
    //for each starting position for that length
    for "j" from 0 to c - i do {
        //get subword
        q = w select [j, i];
        //check if in dictionary
        if(q in d) then {
            //set the entry to the wubword
            a set [i - 1, q]
        }
    }
};
//check if length of word is equal to number of subwords
c == count (a - [""])
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.