大使和翻译


12

两名参加联合国会议的大使想互相讲话,但不幸的是,每个大使只说一种语言,而且他们不是同一种语言。幸运的是,他们可以与多位翻译交流,他们每个人都能听和说几种语言。您的任务是确定最短的翻译链(因为您希望尽可能减少翻译中的损失),从而使两位大使能够相互交谈。

编码

输入:两种语言,即2个字母的小写字符串(每个大使的语言)和语言列表(每个可用翻译器一个列表)

您也可以输入整数而不是2个字母的代码。

输出:按索引或值排列的一系列翻译,是允许两个大使进行交流的最短翻译链中的任何一个。如果没有有效的翻译链,则行为未定义。(您可能会崩溃,输出任意值或表明错误)

一个有效的翻译链是第一个翻译说一种大使的语言,第二个和随后的翻译与前一个翻译共享至少一种语言,最后一个翻译说另一种大使的语言。

例子

使用基于零的索引:

es, en, [
    [es, en]
] ==> [0]

en, en, [] ==> []

en, jp, [
    [en, zh, ko, de],
    [jp, ko]
] ==> [0, 1]

es, ru, [
    [gu, en, py],
    [po, py, ru],
    [po, es]
] ==> [2, 1]

fr, gu, [
    [it, fr, de, es, po, jp],
    [en, ru, zh, ko],
    [jp, th, en],
    [th, gu]
] ==> [0, 2, 3]

fr, ru, [
    [fr, en],
    [en, ko, jp],
    [en, ru]
] ==> [0, 2]

de, jp, [
    [en, fr],
    [ko, jp, zh],
    [fr, po],
    [es, ko, zh],
    [de, en, th],
    [en, es],
    [de, fr]
] ==> [4, 5, 3, 1]

规则与假设

  • 适用标准IO规则(使用任何方便的I / O格式)和禁止的漏洞。
  • 您可能会假设说和理解语言是完全对称的,并且所有语言之间的所有可能翻译都是同样有效的。
  • 没有“足够接近”的语言的概念。例如,在需要西班牙语的一端使用葡萄牙语是不够的。
  • 如果有多个最短的翻译器链,那么任何一个都可以。
  • 如果大使碰巧说相同的语言,则翻译人员列表应为空
  • 哪位大使是第一位无关紧要;转换器列表可以是正向或反向。
  • 大使为了这个挑战只讲一种语言
  • 译者至少会说两种语言
  • 2个字母的语言代码不需要与真实语言相对应
  • 您可能会假设存在有效的翻译顺序
  • 如果按值输出序列,请包括全套可用语言,而不仅仅是相关语言。

高尔夫快乐!


2
为什么对两个字符的字符串进行I / O限制,而整数不那么好吗?
乔纳森·艾伦,

的翻译人员列表列表可以采用csv格式en,fr,sp;en,gr;gr,fr
Quinn

@Quinn标准IO规则说是。
Beefster,

可以在输出的开头和结尾包含大使吗?
肯尼迪

@NickKennedy我要对那个说不。
Beefster '19

Answers:


3

Python 2中138个 126 120 117 113字节

F=lambda a,b,T,*U:a!=b and min([[t]+F(l,b,T,t,*U)for t in T if(t in U)<(a in t)for l in t-{a}]+[2*T],key=len)or[]

在线尝试!

3字节至ArBo

以允许与对话set的语言(即“按值”)形式返回翻译器的最小长度列表。Tab


if t not in U and a in t可以更改为if(a in t)>U.count(t)节省4个字节。
mypetlion,

@mypetition -我也有类似的想法,并挤掉另一个2
查斯布朗

117通过使用*args符号
-ArBo

@ArBo:很好;thx 3个字节。
查斯·布朗

3

果冻19 17字节

ŒPŒ!€Ẏj@€fƝẠ$ƇḢḊṖ

在线尝试!

双向链接,将翻译者列表作为左参数,将大使列表(每个列表双重包装)作为右参数。返回翻译器列表,每个翻译器都是他们使用的语言的列表。

感谢@KevinCruijssen节省了2个字节!

说明

ŒPŒ!€Ẏj@€fƝẠ$ƇḢḊṖ | A dyadic link taking a list of translators as left argument and a list of ambassadors (double-wrapped in lists) as right argument

ŒP                | Power set of translators
  Œ!€             | Permutations of each
     Ẏ            | Tighten, i.e. create a single list of all permutations of any length
      j@€         | Join the ambassadors with each set of translators
            $Ƈ    | Filter those where:
           Ạ      |   all
         fƝ       |   the neighbouring pairs have at least one in common
              Ḣ   | Take the first
               Ḋ  | Drop the first ambassador from the start
                Ṗ | Drop the second ambassador from the end

通过删除按长度排序,您可以节省2个字节,因为powerset +灌注已导致按长度排序的列表。
凯文·克鲁伊森

@KevinCruijssen谢谢,很好!
尼克·肯尼迪

2

05AB1E18 17 字节

怜€`ʒ²š³ªüå€àP}н

受到@NickKennedy的Jelly答案的启发,因此请确保对他进行投票

输出列表本身而不是其索引。

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

说明:

æ                # Get the powerset of the (implicit) input-list of translators
                 #  i.e. [["ef","gh","bc"],["bc","ab"],["ef","cd","de"]]
                 #   → [[],[["ef","gh","bc"]],[["bc","ab"]],[["ef","gh","bc"],["bc","ab"]],[["ef","cd","de"]],[["ef","gh","bc"],["ef","cd","de"]],[["bc","ab"],["ef","cd","de"]],[["ef","gh","bc"],["bc","ab"],["ef","cd","de"]]]
 €œ              # Get the permutations of each
                 #  → [[[]],[[["ef","gh","bc"]]],[[["bc","ab"]]],[[["ef","gh","bc"],["bc","ab"]],[["bc","ab"],["ef","gh","bc"]]],[[["ef","cd","de"]]],[[["ef","gh","bc"],["ef","cd","de"]],[["ef","cd","de"],["ef","gh","bc"]]],[[["bc","ab"],["ef","cd","de"]],[["ef","cd","de"],["bc","ab"]]],[[["ef","gh","bc"],["bc","ab"],["ef","cd","de"]],[["ef","gh","bc"],["ef","cd","de"],["bc","ab"]],[["bc","ab"],["ef","gh","bc"],["ef","cd","de"]],[["bc","ab"],["ef","cd","de"],["ef","gh","bc"]],[["ef","cd","de"],["ef","gh","bc"],["bc","ab"]],[["ef","cd","de"],["bc","ab"],["ef","gh","bc"]]]]
   €`            # Flatten each one level down (4D list becomes 3D list)
                 #  → [[],[["ef","gh","bc"]],[["bc","ab"]],[["bc","ab"],["ef","gh","bc"]],[["ef","gh","bc"],["bc","ab"]],[["ef","cd","de"]],[["ef","cd","de"],["ef","gh","bc"]],[["ef","gh","bc"],["ef","cd","de"]],[["ef","cd","de"],["bc","ab"]],[["bc","ab"],["ef","cd","de"]],[["ef","cd","de"],["bc","ab"],["ef","gh","bc"]],[["ef","cd","de"],["ef","gh","bc"],["bc","ab"]],[["bc","ab"],["ef","cd","de"],["ef","gh","bc"]],[["bc","ab"],["ef","gh","bc"],["ef","cd","de"]],[["ef","gh","bc"],["ef","cd","de"],["bc","ab"]],[["ef","gh","bc"],["bc","ab"],["ef","cd","de"]]]
     ʒ           # Filter this 3D list by:
      ²š         #  Prepend the second input ambassador
                 #   i.e. [["bc","ab"],["ef","gh","bc"]] and "ab"
                 #    → ["ab",["bc","ab"],["ef","gh","bc"]]
        ³ª       #  Append the third input ambassador
                 #   i.e. ["ab",["bc","ab"],["ef","gh","bc"]] and "ef"
                 #    → ["ab",["bc","ab"],["ef","gh","bc"],"ef"]
          ü      #  For each adjacent pair of translator-lists:
           å     #   Check for each item in the second list, if it's in the first list
                 #    i.e. ["bc","ab"] and ["ef","gh","bc"] → [0,0,1]
            ۈ   #   Then check if any are truthy by leaving the maximum
                 #    → 1
              P  #  And then take the product to check if it's truthy for all pairs
                 #   i.e. ["ab",["bc","ab"],["ef","gh","bc"],"ef"] → [1,1,1] → 1
               # After the filter: only leave the first list of translator-lists
                 #  i.e. [[["bc","ab"],["ef","gh","bc"]],[["bc","ab"],["ef","gh","bc"],["ef","cd","de"]]]
                 #   → [["bc","ab"],["ef","gh","bc"]]
                 # (which is output implicitly as result)

1

的JavaScript(ES6), 123个  121字节

需要整数而不是2个字母的代码。

(a,b,l)=>((B=g=(m,s,i)=>m>>b&1?B<i||(o=s,B=i):l.map(a=>a.map(M=c=>M|=1<<c)|M&m&&m^(M|=m)&&g(M,[...s,a],-~i)))(1<<a,[]),o)

在线尝试!

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.