首页
学习
活动
专区
圈层
工具
发布

解Anagram
EN

Code Golf用户
提问于 2017-06-19 14:44:18
回答 6查看 685关注 0票数 10

亦见:奶奶爱安娜

您将收到一串小写的ASCII字母。使用这个字典文件 (更新),您的任务是解决字谜。要解决一个字谜,您必须输出所有可以使用输入字符串中的每个字母形成的单词或组,用换行符隔开。按不同顺序排列的同一词组并不是唯一的,不应单独输出;然而,这些词的顺序并不重要。输出解决方案的顺序也不重要。如果输入不能形成单词,则不输出任何内容。

一些有用的测试用例:

代码语言:javascript
复制
Input:  viinlg
Output: living

Input:  fceodglo
Output: code golf

Input:  flogflog
Output: golf golf

Input:  ahwhygi
Output: highway
        high way

Input:  bbbbbb
Output: 

规则/注意事项:

  • 你可以用任何方式访问字典列表。命令行参数、stdin、从文件中读取或从internet读取都是可以接受的。
  • 输入只包含小写ASCII字母。在任何特定情况下都不需要输出结果。
  • 不会给您一个已经形成有效单词的字符串(但是,您可能会得到一个形成多个单词的字符串,如bluehouse)。
  • 允许尾随换行符,但不需要。
  • 适用标准漏洞。

我是密码-高尔夫。以字节为单位的最短代码获胜。祝好运!

EN

回答 6

Code Golf用户

发布于 2023-05-27 07:39:57

Scala,156个字节

金色的版本。在网上试试!

代码语言:javascript
复制
(l:Seq[String],k:String)=>l.filter(w=>w.foldLeft((k,true)){case((r,b),c)=>val i=r.indexOf(c);if(i>=0)(r.substring(0,i)+r.substring(i+1),b)else(r,false)}._2)

非高尔夫版。在网上试试!

代码语言:javascript
复制
object Main {
  def main(args: Array[String]): Unit = {
    val arr = List("living", "code", "golf", "highway", "high", "way")

    def checkAvailability(str: String, keyword: String): Boolean = {
      keyword.foldLeft((str, true)) { case ((remaining, result), ch) =>
        val idx = remaining.indexOf(ch)
        if (idx >= 0) (remaining.substring(0, idx) + remaining.substring(idx + 1), result && true)
        else (remaining, false)
      }._2
    }

    def filterList(arr: List[String], keyword: String): List[String] = {
      arr.filter(word => checkAvailability(keyword, word))
    }

    println(filterList(arr, "viinlg"))
    println(filterList(arr, "fceodglo"))
  }
}
票数 1
EN

Code Golf用户

发布于 2023-05-27 09:41:27

OCaml,224个字节

金色的版本。在网上试试!

代码语言:javascript
复制
let f s t=List.filter(fun w->List.fold_left(fun(x,y)c->let i=String.index_opt x c in match i with Some j->String.sub x 0 j^String.sub x(j+1) (String.length x-j-1),y|None->x,false)(t,true)(String.to_seq w|>List.of_seq)|>snd)s

非高尔夫版。在网上试试!

代码语言:javascript
复制
let filter_strings lst str =
  let rec filter_chars str char =
    let i = String.index_opt str char in
    match i with
    | Some idx -> String.sub str 0 idx ^ String.sub str (idx + 1) (String.length str - idx - 1), true
    | None -> str, false
  in
  List.filter (fun word ->
    let _, ok = List.fold_left (fun (str, b) c -> let str', b' = filter_chars str c in str', b && b') (str, true) (String.to_seq word |> List.of_seq) in
    ok) lst

let () =
  let arr = ["living"; "code"; "golf"; "highway"; "high"; "way"] in
  let result1 = filter_strings arr "viinlg" in
  let result2 = filter_strings arr "fceodglo" in
  print_endline (List.fold_left (fun a b -> if a = "" then b else a ^ "," ^ b) "" result1);
  print_endline (List.fold_left (fun a b -> if a = "" then b else a ^ "," ^ b) "" result2)
票数 0
EN

Code Golf用户

发布于 2023-05-27 09:47:15

灵丹妙药,188个字节

金色的版本。在网上试试!

代码语言:javascript
复制
f=fn(l,k)->Enum.filter(l,fn w->elem(Enum.reduce(String.graphemes(w),{k,true},fn c,{r,b}->if String.contains?(r,c),do: {String.replace(r,c,"",global: false),b},else: {r,false}end),1)end)end

非高尔夫版。在网上试试!

代码语言:javascript
复制
defmodule Main do
  def run do
    filter_strings = fn (lst, k) ->
      Enum.filter(lst, fn w ->
        str = Enum.reduce(String.graphemes(w), {k, true}, fn c, {r, b} ->
          case String.contains?(r, c) do
            true -> {String.replace(r, c, "", global: false), b}
            _ -> {r, false}
          end
        end)
        elem(str, 1)
      end)
    end

    arr = ["living", "code", "golf", "highway", "high", "way"]
    IO.inspect(filter_strings.(arr, "viinlg"))
    IO.inspect(filter_strings.(arr, "fceodglo"))
  end
end

Main.run
票数 0
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/127243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档