信箱验证器


28

《纽约时报》每天都有一个名为Letter Boxed的在线游戏(链接位于付费专栏后面;该游戏也在此处进行描述),该游戏在广场上显示如下:

纽约时报的信箱示例

您将获得4组3个字母的字母(每个组对应于图片的一侧);没有字母出现两次。游戏的目的是找到由这12个字母组成的单词(仅这些字母),以便:

  • 每个单词至少3个字母长;
  • 连续字母不能来自同一侧;
  • 一个单词的最后一个字母成为下一个单词的第一个字母;
  • 所有字母至少使用一次(字母可以重复使用)。

在这个挑战中,您会得到字母和单词列表。目的是检查单词列表是否是有效的Boxed解决方案。

输入值

输入包含(1)4组3个字母和(2)单词列表。它可以是任何合适的格式。

输出量

如果单词列表是对那些4×3字母的“盒装”挑战的有效解决方案,则为真值,否则为假值。

测试用例

字母组= {{I,C,O}, {M,R,E}, {G,N,S}, {A,P,L}}

真实价值观

  • 盗版,关闭
  • 作物,航行,瘦肉,无腿,ENIGMA

假值

  • 朝圣,经济(因为他们在同一边,所以不能有CO)
  • 作物,风帆,瘦肉,无腿(未使用G和M)
  • 封条,信封(U不是12个字母之一)
  • ENCLOSE,PILGRIMAGE(第一个单词的最后一个字母不是第二个单词的第一个字母)
  • SCAMS,SO,ORGANISE,ELOPE(所有单词的长度必须至少为3个字母)。

请注意,在此挑战中,我们不在乎单词是否有效(词典的一部分)。

得分:

(最低字节得分)获胜!


4
@TFeldno letter appears twice
feersum

如果单词列表是对那些4×3字母的“盒装”挑战的有效解决方案,则为真值,否则为假值。对于Python(我希望和其他大多数语言),两者[]0都是错误的。我们可以输出还是必须保持输出一致?
Artemis

@ArtemisFowl都可以。
罗宾·赖德

我是这么认为的,但我的问题是:我们可以它们混合吗?
Artemis

@ArtemisFowl是的,您可以将它们混合。
罗宾·赖德

Answers:


6

的JavaScript(ES6), 130个  126字节

将输入作为(letters)(words)。返回或。01个

L=>W=>L.every(a=>a.every(x=>(W+'').match(x,a.map(y=>s+='|'+x+y))),p=s=1)&W.every(w=>w[2]&&p|w[0]==p&!w.match(s,p=w.slice(-1)))

在线尝试!

步骤1

我们首先对进行迭代,以构建由管道组成的,由所有无效字母对组成的字符串。这样做时,我们还要确保每个字母至少每个单词出现一次。大号s

L.every(a =>              // for each group of letter a[] in L[]:
  a.every(x =>            //   for each letter x in a[]:
    (W + '')              //     coerce W[] to a string
    .match(               //     and test whether ...
      x,                  //       ... x can be found in it
      a.map(y =>          //       for each letter y in a[]:
        s += '|' + x + y  //         append '|' + x + y to s
      )                   //       end of map()
    )                     //     end of match()
  ),                      //   end of inner every()
  p = s = 1               //   start with p = s = 1
)                         // end of outer every()

第2步

现在,我们遍历来测试每个单词。w ^

W.every(w =>              // for each word w in W[]:
  w[2] &&                 //   is this word at least 3 characters long?
  p |                     //   is it the first word? (p = 1)
  w[0] == p &             //   or does it start with the last letter of the previous word?
  !w.match(               //   and finally make sure that ...
    s,                    //     ... it doesn't contain any invalid pair of letters
    p = w.slice(-1)       //     and update p to the last letter of w
  )                       //   end of match()
)                         // end of every()

6

果冻30 29字节

FQṢ=Ṣ},i@€€’:3Iʋ,Ẉ>2ɗ,U=ḢɗƝ{Ȧ

在线尝试!

双向链接,将单词列表作为左参数,将文本框中的拼合字母列表作为右参数。它返回1true和0false。

说明

F                               | Flatten the word list
 Q                              | Unique
  Ṣ                             | Sort
   =                            | Is equal to
    Ṣ}                          |   The sorted letterbox letters
      ,        ʋ                | Pair this with the following:
       i@€€                     |   The index of each letter of each word in the letterbox            
           ’                    |   Decrease by 1
            :3                  |   Integer divide by 3
              I                 |   Differences between consecutive ones (will be zero if any two consecutive letters in a word from same side of box)
                ,   ɗ           | Pair everything so far with the following:
                 Ẉ>2            |   Whether length of each input word is greater than 2
                     ,   ɗƝ{    | Pair everything so far with the following, applied to each neighbouring pair of the input word list
                      U         |   Upend (reverse) first word
                       =        | Compare characters to second
                        Ḣ       |   Take first (i.e. last character of first word equals first character of second)
                            Ȧ   | Flatten all of the above and check there are no false values

6

05AB1E37 35 33 32 31 29 28 字节

εk3÷üÊ}DO2@¹ü«εüQO}²{¹˜êQ)˜P

-2通过采取的灵感字节ê方式@Emigna在他05AB1E答案使用
-3个字节,感谢@Grimy

将单词的字符列表作为第一个输入,将十二个字母的拼合列表作为第二个输入。

在线尝试验证所有测试用例

说明:

ε         # Map over the character-lists `y` of the (implicit) input-list of words:
 k        #  Get the index of each character in the (implicit) input-list of letters
  3÷      #  Integer-divide each index by 3
    üÊ    #  Check for each overlapping pair of integers that they are NOT equal
}D        # After the map: duplicate the resulting list
  O       #  Get the sum of each inner list of truthy/falsey values
   2@     #  And check that each is larger than 2 (so all words had at least 3 letters)
¹ü        # Get all overlapping pairs of character-lists from the input-list of words:
  «       #  And merge them together to a flattened list of characters
   ε   }  # Map over those merged character lists:
    üQ    #  Check for each overlapping pair of characters in the list that they are equal
      O   #  And take the sum of this (where we'd expect 1/truthy if the last character of
          #  the first word and the first character of the second word are equal)
          #  (NOTE: This could fail for inputs with identical adjacent characters,
          #   but the earlier check of `εk3÷üÊ}` already covers for this)
²{        # Push the input-list of letters, and sort them
  ¹˜      # Push the input-list of list of word-letters, flattened,
    ê     # and then uniquified and sorted as well
     Q    # And check if both lists of characters are the same
        # Then wrap everything on the stack into a list, and deep flatten it
  P       # And check if everything is truthy by taking the product
          # (which is output implicitly as result)

1
@Grimy啊,那第一个评论确实是显而易见的。我刚刚将其更改为字符数组,因此现在可以在单词仍然是字符串的情况下使用了。合并的第二种方法,检查对等,总和相当出色!:D谢谢(一如既往)。
凯文·克鲁伊森

1
另一个-1:¹€g3@-> DO2@在第一次检查(TIO)之后
Grimmy

1
@Grimy另一个好人,谢谢。我们现在低于Jelly的答案29::)
Kevin Cruijssen


4

Python 2,171字节

lambda l,w:(set(sum(l,[]))==set(''.join(w)))*all(a[-1]==b[0]for a,b in zip(w,w[1:]))*all((a in g)+(b in g)<2for x in w for a,b in zip(x,x[1:])for g in l)*min(map(len,w))>2

在线尝试!



4

Haskell,231字节

import Data.List
l&w=all((>2).length)w&&c w&&all(l!)w&&(h l)%(h w)
h=concat
l%w=null[x|x<-l,x`notElem`w]
l!(a:b:c)=a#l?(b#l)&&l!(b:c)
l!_=1>0
Just a?Just b=a/=b
_?_=1<0
c#l=findIndex(elem c)l
c(a:b:t)=last a==head b&&c(b:t)
c _=1>0

在线尝试!

不是最好的成绩。一些Haskell专家可能会获得100字节以下的内容。

用法

["ICO","MRE","GNS","APL"]&["CROPS", "SAIL", "LEAN", "NOPE", "ENIGMA"]

说明

import Data.List
l&w = all((>2).length)w &&      -- Every word has length > 2
      c w &&                    -- Every word ends with the same letter as the next one starts with
      all(l!)w &&               -- For every word: Consecutive letters are on different sides (and must exist on a side)
      (h l)%(h w)               -- All letters are used

h=concat                        -- Just a shorthand

l%w=null[x|x<-l,x`notElem`w]    -- The letters of l, with all letters of w removed, is empty

l!(a:b:c)=a#l?(b#l)&&l!(b:c)    -- Sides of the first two letters are different, recurse from second letter
l!_=1>0                         -- Until fewer than 2 letters remain

Just a?Just b=a/=b              -- Both sides must be different
_?_=1<0                         -- And must exist

c#l=findIndex(elem c)l          -- Find the side of letter c

c(a:b:t)=last a==head b&&c(b:t) -- Last letter of the first word must be same as first letter of second word, recurse starting from second word
c _=1>0                         -- Until there are fewer than 2 words

4

Haskell,231字节

不同的Haskell变体,大小与@Paul Mutser的相同:)

import Data.List
f x=filter(\a->length a>1)$concatMap subsequences x
g=nub.concat.f
p l(x:y)=foldl(\(m,n)c->(c,n&&length c>2&&(not$any(`isInfixOf`c)(f l))&&last m==head c))(x,True)y
z l w=null(g l\\g w)&&null(g w\\g l)&&(snd$p l w)

在线尝试!

不打高尔夫球

-- generate all invalid substrings
f :: [String] -> [String] 
f xs = filter (\x -> length x > 1) $ concatMap subsequences xs

-- utility function to flatten and remove duplicates
g :: [String] -> String
g  = nub $ concat $ f

-- verify that all conditions are satisfied along the list
p :: [String] -> [String] -> (String, Bool)
p l (x:xs) = foldl (\(m,n) c -> (c , n && length c > 2 && (not $ any (`isInfixOf` c)(f l)) && last m == head c)) (x, True) xs

-- put all the pieces together and consume input
z :: [String] -> [String] -> Bool
z l w = null (g l \\ g w) && null (g w \\ g l) && (snd $ p l w)

3

红宝石,126字节

->l,w{(/(_|^)..(_|$)/!~s=w*?_)&&!!s.chars.uniq[12]&&/__|^_|_$|(_.*)\1/!~s.gsub(/(.)_\1/,'\1').chars.map{|x|l.grep(/#{x}/)}*?_}

在线尝试!


很好,当我第一次看到挑战时,我尝试做类似的事情,但是在140个独立实体中放弃了某个分数。顺便说一句,在后面加上括号可以节省一个字节grep
Kirill L.

当最后一个单词的长度为1或2个字母时(例如puts f[l,['PILGRIMAGE','ENCLOSE','EG']]返回true而不是),此功能将无效false
罗宾·赖德

1
你是正确的,固定的。
GB

3

Java(JDK),188个字节

g->w->{var v=0<1;int x=0,l,i=0,j,p,z,y=w[0][0];for(;i<w.length;i++)for(l=w[i].length,v&=y==w[i][0]&l>2,j=0,p=-9;j<l;v&=z>=0&z/3!=p/3,x|=2<<(p=z))z=g.indexOf(y=w[i][j++]);return v&x==8190;}

在线尝试!

说明

g->w->{     // Lambda accepting letter groups as a string and a list of words, in the form of an array of char arrays.
 var v=0<1;     // Validity variable
 int x=0,       // The letter coverage (rule 4)
     l,         // The length of w[i]
     i=0,       // The w iterator
     j,         // The w[i] iterator
     p,         // The previous group
     z,         // The current group
     y=w[0][0]; // The previous character
 for(;i<w.length;i++) // For each word...
  for(
     l=w[i].length,     // make a shortcut for the length
     v&=y==w[i][0]&l>2, // check if the last character of the previous word is the same as the first of the current.
                        // Also, check if the length is at least 3
     j=0,               // Reset the iteration
     p=-9               // Set p to an impossible value.
    ;
     j<l                // 
    ;
     v&=z>=0&z/3!=p/3,  // Check that each letter of the word is in the letter pool,
                        //  and that the current letter group isn't the same as the previous one.
     x|=2<<(p=z)      // After the checks, assign z to p,
                        //  and mark the letter of the pool as used.
   )
   z=g.indexOf(y=w[i][j++]); // Assign the current letter to y so that it contains the last at the end of the loop.
                             //  and fetch the position of the letter in the pool.
 return v&x==8190; // Return true if all matched
                   //  and if the rule 4 is enforced.
}

学分

  • -2个字节归功于ceilingcat

2

木炭,63字节

⌊⁺⁺⁺⭆η›Lι²⭆⪫ηω№⪫θωι⭆⪫θω№⪫ηωι⭆η⭆ι⎇μ¬⁼Φθ№νλΦθ№ν§ι⊖μ∨¬κ⁼§ι⁰§§η⊖κ±¹

在线尝试!链接是详细版本的代码。说明:

⌊⁺⁺⁺

连接以下表达式并输出,0如果其中任何一个包含0else 1

⭆η›Lι²

对于解决方案输出中的每个单词,其长度是否至少为3。

⭆⪫ηω№⪫θωι

对于解决方案输出中的每个字母,是否出现在拼图中。

⭆⪫θω№⪫ηωι

对于拼图输出中的每个字母,它是否出现在解决方案中。

⭆η⭆ι⎇μ¬⁼Φθ№νλΦθ№ν§ι⊖μ∨¬κ⁼§ι⁰§§η⊖κ±¹

对于解决方案中的每个字母,请确保前一个字母不在同一组中,除非它是单词的第一个字母,否则请检查它是否等于前一个单词的最后一个字母,除非它是第一个字母解决方案的字母,在这种情况下,请忽略它。


0

Python 2中168个 156字节

lambda l,w,J=''.join:(set(J(w))==set(J(l)))*all((v<1or u[-1]==v[0])*u[2:]*(2>(x in p)+(y in p))for u,v in zip(w,w[1:]+[0])for x,y in zip(u,u[1:])for p in l)

在线尝试!

返回1真假,0假。

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.