大码…为您服务!


32

在《黑暗骑士》崛起的开幕式上,中央情报局特工与小人贝恩之间进行了相当尴尬的对话

中央情报局特工: “如果我把那个[面具]拉下来,你会死吗?”

贝恩: “这将非常痛苦……”

中央情报局特工: “你是个大个子。”

贝恩: “……给你。”

不确定贝恩是否打算说“为您感到痛苦”或“为您大佬”。让我们通过代码高尔夫彻底解决这个问题!

挑战

您的任务是编写一个程序,该程序根据输入的整数重新排列上述对话框的顺序。

CIA特工对话的话是: If I pull that off will you die? You're a big guy.

贝恩的对话词是: It would be extremely painful... for you!

请注意die?You’repainful...并且you!被认为是单个单词。

  1. 给定整数n作为输入,将其转换为二进制
  2. 从左到右读取二进制数字,如果是1,则从CIA代理对话框中输出下一个单词,如果是,则从Bane对话框中输出下一个单词0。单词应以空格分隔。扬声器更改时,添加换行符。另外,在每个对话行前加上演讲者姓名(BANE:CIA:)。

您可以假设输入始终以二进制形式的1开头,并具有12个1和7个0。

522300

  1. 转换为二进制: 1111111100000111100

  2. 数字以8个开头,因此我们从CIA的座席对话中输出前8个字,并在它们前面加上CIA: CIA: If I pull that off will you die?

  3. 然后我们得到了5个零,所以我们从Bane的对话中得到了5个头文字: BANE: It would be extremely painful...

  4. 然后有4个,因此我们输出下四个CIA单词: CIA: You’re a big guy.

  5. 然后2个零: BANE: for you!

最后结果:

CIA: If I pull that off will you die?
BANE: It would be extremely painful...
CIA: You’re a big guy.
BANE: for you!

更多测试用例:

494542

CIA: If I pull that
BANE: It would be
CIA: off
BANE: extremely
CIA: will you die? You're
BANE: painful... for
CIA: a big guy.
BANE: you!

326711

CIA: If
BANE: It would
CIA: I pull that off will you
BANE: be extremely painful... for
CIA: die? You're
BANE: you!
CIA: a big guy.

每行可以在一个空格处结束吗?
xnor

@xnor是的,没关系。
Arnaud

Bane语音的第一个单词“ that”和“ it”在测试用例/引用中不
对齐

@Mistah固定为“它”,谢谢!
Arnaud

3
是我还是有人以祸根的声音朗读对话。
Gurupad Mamadapur '17

Answers:


10

Pyth-138字节

我应该研究压缩电影脚本。

K_cc"guy big a You're die? you will off that pull I If you! for painful... extremely be would That"d12jmj;++@c"BANECIA"4ed\:m.)@Kedhdr8jQ2

测试套件


7

的JavaScript(ES6),203个 201字节

编辑:通过trim()ETHproductions答案中无耻地借用这个想法节省了2个字节

n=>"If I pull that off will you die? You're a big guy. It would be extremely painful... for you!".split` `.map((_,i,a)=>[`
BANE: `,`
CIA: `,' '][j^(j=n>>18-i&1)?j:2]+a[k[j]++],k=[j=12,0]).join``.trim()

测试用例


7

的JavaScript(ES6),209个 202 201字节

(n,a="It would be extremely painful... for you! If I pull that off will you die? You're a big guy.".split` `,b=[6,18])=>(F=(s,p=n%2)=>n?F((p^(n>>=1)%2?p?`
CIA: `:`
BANE: `:` `)+a[b[p]--]+s):s.trim())``

旧方法:

(n,a="If I pull that off will you die? You're a big guy. It would be extremely painful... for you!".split` `,b=[12,0])=>(F=p=>p^n%2?F(n%2)+(p?`
CIA:`:`
BANE:`):n?F(p,n>>=1)+" "+a[b[p]++]:``)(n%2).trim()

测试片段

f=(n,a="It would be extremely painful... for you! If I pull that off will you die? You're a big guy.".split` `,b=[6,18])=>(F=(s,p=n%2)=>n?F((p^(n>>=1)%2?p?`
CIA: `:`
BANE: `:` `)+a[b[p]--]+s):s.trim())``

console.log(f(522300))
console.log(f(494542))
console.log(f(326711))


我无耻地借用你的trim()想法。我确定有比这更好的东西,slice(1)但无法弄清楚。
Arnauld

@Arnauld很好,现在我们都被捆绑了。我敢打赌,有一种方法可以在200
以内

6

C ++ 11(GCC),298293字节

#include<sstream>
[](int i){std::stringstream b("It would be extremely painful... for you!"),c("If I pull that off will you die? You're a big guy.");std::string s,w;int n=0,t=i,p;while(t/=2)n++;for(;n>=0;p=t)((t=i>>n&1)?c:b)>>w,s+=p^t?t?"CIA: ":"BANE: ":" ",s+=t^(i>>--n)&1?w+"\n":w;return s;}

一个lambda函数,它接受一个整数并以形式返回对话std::string。您可以在此处查看它的运行情况。

非高尔夫版本(有一些解释):

#include<sstream>

[](int i) {
    std::stringstream bane("It would be extremely painful... for you!"),
                      cia("If I pull that off will you die? You're a big guy.");
    std::string s, w;
    int n = 0, t = i, p;
    // Find the position of the most significant bit (n)
    while (t/=2) n++;

    for (; n>=0; p=t) {
        t = i>>n&1; // Current bit
        // Append the speaker name if the previous bit was different
        if (t != p) s += (t ? "CIA: " : "BANE: ");
        else s += " ";

        // Read the next word from one of the streams
        if (t) cia >> w;
        else bane >> w;

        s += w;

        if (t != ((i>>--n)&1)) // Append a newline if the next bit is different
            s += "\n";
    }

    return s;
}

4

的JavaScript(ES6),252个227 226字节

n=>[...n.toString(2,b="If I pull that off will you die? You're a big guy.".split` `,c="It would be extremely painful... for you!".split` `)].map((a,i,j)=>(a!=j[i-1]?+a?i?`
CIA: `:`CIA: `:`
BANE: `:``)+(+a?b:c).shift()).join` `

用法

f=n=>[...n.toString(2,b="If I pull that off will you die? You're a big guy.".split` `,c="It would be extremely painful... for you!".split` `)].map((a,i,j)=>(a!=j[i-1]?+a?i?`
CIA: `:`CIA: `:`
BANE: `:``)+(+a?b:c).shift()).join` `
f(522300)

笔记

这是正在进行的高尔夫运动,我想我仍然可以删除一些字节,但随时在评论中留下建议。


4

Python 3.6,232字节

from itertools import*
c="you! for painful... extremely be would It".split(),"guy. big a You're die? you will off that pull I If".split()
for n,r in groupby(f'{int(input()):b}',int):
 print("BCAINAE::"[n::2],*[c[n].pop()for _ in r])

编辑,等效地:

from itertools import*
c="It would be extremely painful... for you!".split(),"_ If I pull that off will you die? You're a big guy.".split()
for n,r in groupby(map(int,f'{int(input()):b}')):
 print("BCAINAE::"[n::2],*map(c[n].pop,r))

3

Japt,121个字节

A=[`It Ùd ¼ extÚ+ pafª... f y!`¸`If I pªl È     f Øi y ¹e? Y' a big guy.`¸]¢®^T?["
BANE:""
CIA:"]gT=Z :P +S+AgZ vÃx

包含许多不可打印的内容,因此最好在线进行测试


2

Perl 6、211字节

{.put for map |*,zip map {.[0]X .[1].rotor: .[2..*]},zip <CIA: BANE:>,(<If I pull that off will you die? You're a big guy.>,<It would be extremely painful... for you!>),|(.base(2)~~m:g/(.)$0*/)».chars.rotor(2)}

2

C#,398个 390 385 396 389字节

class P{static void Main(string[] a){string s="2"+System.Convert.ToString(int.Parse(a[0]),2),e="CIA: ",r="BANE: ",o="";int k=0,l=0,i=1;string[] c="If I pull that off will you die? You're a big guy.".Split(' '),b="It would be extremely painful... for you!".Split(' ');for(;i<s.Length;i++)o+=(s[i-1]==s[i]?" ":(i<2?"":"\n")+(s[i]>'0'?e:r))+(s[i]>'0'?c[k++]:b[l++]);System.Console.Write(o);}}

以输入编号作为参数启动。

取消高尔夫:

class P
{
    static void Main(string[] a)
    {
        string s = "2" + System.Convert.ToString(int.Parse(a[0]), 2), e = "CIA: ", r = "BANE: ", o = "";
        int k = 0, l = 0, i = 1;

        string[] c = "If I pull that off will you die? You're a big guy.".Split(' '), b = "It would be extremely painful... for you!".Split(' ');
        for (; i < s.Length; i++)
            o += (s[i - 1] == s[i] ? " " : (i<2?"":"\n") + (s[i] > '0' ? e : r))
                + (s[i] > '0' ? c[k++] : b[l++]);
        System.Console.Write(o); 
    }
}

回到396字节,因为我没有注意到“开始时没有换行”规则。


可悲的是,s.Replace("1",c[k++]).Replace("0",b[l++])没有用相应的单词代替。看来,C#一次替换了字符串,然后将其拍打到位。
Scapegrace先生17年

2

Ruby,204 + 1 = 205字节

需要-n标志。

d=[%w"It would be extremely painful... for you!",%w"If I pull that off will you die? You're a big guy."]
n=%w"BANE CIA"
("%b"%$_).scan(/((.)\2*)/).map{|i,b|puts n[k=b.to_i]+': '+d[k].slice!(0,i.size)*' '}

2

PHP,198字节

while($i++<19)echo($t-$s=1&$argv[1]>>19-$i)?"
"[$i<2].($s?CIA:BANE).": ":" ",explode(0,"It0would0be0extremely0painful...0for0you!0If0I0pull0that0off0will0you0die?0You're0a0big0guy.")[$$s+++7*$t=$s];

1

Perl,205个字节

@t=([qw/It would be extremely painful... for you!/],[qw/If I pull that off will you die? You're a big guy./]);$_=sprintf'%b',$0;print$n?'BANE':'CIA',": @{[splice$t[$n=1-$n],0,length$&]}"while s/(.)\1*//;

将其放入名为494542的文件中,并运行如下:

perl -lX 494542

在perl v5.22上测试


1

Clojure,401个字节

(require '[clojure.string :as s])(defn f[n](let[c(map #(-(int %) 48)(Integer/toString n 2))p[(s/split"It would be extremely painful... for you!"#" ")(s/split"If I pull that off will you die? You're a big guy."#" ")]a["BANE" "CIA"]](loop[i[0 0] g"" d c q 2](if(<(count d)1)g(let[b(first d)j(i b)](recur(assoc i b (inc j))(str g(if(= b q)" "(str(when(not= 2 q)"\n")(a b)": "))((p b) j))(rest d) b))))))

调用:

(f 522300)

不打高尔夫球

(require '[clojure.string :as s])
(defn dialogue[num]
    (let [dacode (map #(- (int %) 48) (Integer/toString num 2))
          phrases [(s/split"It would be extremely painful... for you!"#" ")(s/split"If I pull that off will you die? You're a big guy."#" ")]
      actors ["BANE" "CIA"]]
      (loop [idxs [0 0] finaldial "" code dacode prevbit 2]
        (if (< (count code) 1) finaldial
            (let [bit (first code) idx (idxs bit)]
              (recur (assoc idxs bit (inc idx)) (str finaldial (if (= bit prevbit) " " (str (when (not= 2 prevbit) "\n") (actors bit) ": ")) ((phrases bit) idx)) (rest code) bit))))))
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.