*语言名称*真棒!


13

用任何一种语言编写程序,该程序可以从stdin读取输入,并将经过稍微修改的输出输出到stdout。程序应从输入和输出中借用一些字符,并尽可能增加一个前缀,*language-name* is awesome!后跟换行符,然后输入剩余的内容。

  • 输入不包含任何大写字符。
  • 如果字符串中不存在语言名称的第一个字符,则仅应换行符。
  • 如果输入中没有换行符,请输出未修改的输入。
  • 借用哪个可用字符都没有关系。

我在写时\n用作换行符(0x0a),以节省空间。真正的程序应该只关心真正的换行符,而不是\n字符串。

示例:python。
输入:abcdefghijklmnopqrstuvwxyz\n0123456789
输出:python\nabcdefgijklmqrsuvwxz0123456789
由于输入没有空格,所以即使下一个单词有足够的字符,我们也无法继续is

示例:C.
输入:i don't see anything!
输出:i don't see anything!
在字符串中找不到C,因此无法进行修改。另外,不存在换行符。

示例:C ++。
输入:i don't\nsee anything!
输出:\ni don'tsee anything!
在字符串中找不到C,因此无法进行修改。

示例:Obj-C。
输入:objectively, clojure is amazing.\nq.e.d.
输出:obj\nectively, clojure is amazing.q.e.d.
输入包含足以写的字符,obj-缺少。

源代码的字节数减去语言名称的字节数(如果可能)是utf-8编码的分数;最低的胜利!


3
让我们希望有人能找到一种语言,该语言的重复性很强,以得到负分:)
Filip Haglund 2015年

您可以将其i don't\nsee anything!作为测试用例吗?
丹尼斯

很好的测试用例!添加。
Filip Haglund

带有重复字符(名称或输入中)的情况应如何处理?输出中剩余字符的顺序重要吗?
彼得·泰勒

哇!固定它。还阐明了可以借用任何字符,而不仅仅是第一个字符。
菲利普·哈格隆德

Answers:


4

Pyth,37个字节

.-Jjb.zpef!.-TJ+,kb+Rb._"pyth is awesome!

源代码长41个字节在线尝试。

怎么运行的

  Jjb.z                                      Save all user input in J.
                      ._"pyth is awesome!    Compute all prefixes that string:
                                               ["p", "py", ... ]
                   +Rb                       Append a linefeed to each prefix.
               +,kb                          Concatenate ["", "\n"] with the result.
         f                                   Filter the resulting array; for each T:
           .-TJ                                Perform bagwise difference between T
                                               and J (respects multiplicities).
         !                                     Take the logical NOT.
                                             Keep T if ! returned True, i.e., if J
                                             contains all of T's characters.
        e                                    Retrieve the last, longest match.
       p                                     Print it.
.-J                                          Remove its characters from J.
                                             (implicit) Print the result.

2

Python,186-6 = 180

import sys
a=sys.stdin.read()
s="python is awesome!"
r=''
if'\n'not in a:print a;exit()
a=a.replace('\n','',1)
for c in s:
 if c in a:a,r=a.replace(c,'',1),r+c
 else:break
print r+'\n'+a

在线尝试


它仅应借用字符,因此,如果输入中存在多个换行符,则仅需借用其中之一。所有字符仍应存在于输出中,但不必以相同的顺序出现。
菲利普·哈格隆德

1

Python,146个字节

import sys
r=sys.stdin.read();y='\npython is awesome!';a=''
for i in y:
    if i in r:a+=i
    else:break
print a[1:]+'\n'+''.join(b for b in r if not b in a)

@Dennis修复了它-_-
TheDoctor

如果输入中不包含换行符,那么换行符仍然过多(第二个示例)。
丹尼斯

1

锡兰,235-6 = 229

void a(){variable value i="";variable value r="\nceylon is awesome!";while(exists l=process.readLine()){i=i+"\n"+l;}i=i.rest;for(j->c in r.indexed){if(c in i){i=i.replaceLast(c.string,"");}else{r=r[0:j];break;}}print(r.rest+r[0:1]+i);}

这是格式化和注释的版本:

void a() {
    // our target string, with the \n shuffled to the start.
    variable value r = "\nceylon is awesome!";

    // read the whole input line by line
    // (there doesn't seem a way to do this shorter :-/)
    variable value i = "";
    while (exists l = process.readLine()) {
        i = i + "\n" + l;
    }
    // remove first \n:
    i = i.rest;
    for (j->c in r.indexed) {
        if (c in i) {
            // remove some occurence of c
            i = i.replaceLast(c.string, "");
        } else {
            // stop the loop, and take the part of `r` processed so far.
            r = r[0:j];
            break;
        }
    }
    // reshuffle first \n in r to its end.
    // This will result in the empty string if r is empty, i.e. no \n was found.
    print(r.rest + r[0:1] + i);
}

使用replaceLast而不是replaceFirst因为它更短。

一些示例输入和输出,格式与问题相同:

  • abcdefghijklmnopqrstuvwxyz\n0123456789ceylon\nabdfghijkmpqrstuvwxz0123456789
  • i don't see anything!i don't see anything!
  • i don't\nsee anything!\ni don't see anything!
  • objectively, closure is amazing.\nq.e.d.ceylon is a\nobjectivel, sureiamzng.\q..d.

0

JavaScript(ES6)90(100-10)

作为函数返回请求的输出。使用I / O很难实现,因为STDIN的通常替代是prompt(),它在输入字符串内不接受换行符。

作为具有实际输出(使用alert)的函数,字节数为107

f=s=>alert(([...`
javascript is awesome`].every(c=>(z=s.replace(c,''))!=s&&(s=z,q?o+=c:q=c),q=o=''),o+q+z))

在符合EcmaScript 6的浏览器中测试运行以下代码段(实现传播算子和箭头功能-我使用FireFox)

f=s=>([...`
javascript is awesome`].every(c=>(z=s.replace(c,''))!=s&&(s=z,q?o+=c:q=c),q=o=''),o+q+z)

function test()
{
  O.innerHTML=f(I.value)
}

test()
#I {width: 80%}
<textarea id=I>
objectively, clojure is amazing.
q.e.d.</textarea><button onclick="test()">-></button>
<pre id=O></pre>


0

Perl,72-4 = 68个字节

包括2个开关。

perl -0pe 'for$c("\nperl is awesome!"=~/./gs){s/$c//?$p.=$c:last}s/^/$p\n/;s/\n//'

说明:对于字符串中的每个字符,请"\nperl is awesome"从输入字符串($_)中删除相应的字符,直到找到不存在的字符$_。匹配的字符将被存储在$p其中$_,然后被打印。

-0开关在完成输入,而不是行由行读取和-p开关使得读取输入并打印输出隐式的。


0

的JavaScript(ES7),101 107 - 10 = 97

它以前更短,甚至可以用于所有四个测试用例,但是显然我错过了一条规则,所以...。

x=>(i=r=q='',[for(c of`
javascript is awesome`)(y=x.replace(c,''),i||y==x?i=1:(c<' '?q=c:r+=c,x=y))],r+q+x)

在Firefox 42中可以正常工作。它最初以119个字节开始,但是@ edc65的答案中的一个技巧有助于将其大大缩短。我认为仍有一些改进的空间。一如既往,欢迎提出建议!


输入中缺少换行符时失败。测试obj-> job,不得修改(规则3)
edc65

@ edc65嗯,是的,测试案例仅覆盖了边缘案例的75%。我什is awesome至可以删除它,并且仍然可以在所有四个上正常工作。我会尽快修复。
ETHproductions 2015年
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.