用尽可能多的语言创建一系列垃圾邮件生成器


10

挑战的目标是用尽可能多的语言编写解决方案。炫耀您的编程多语言!

在此挑战中,您将创建一个程序,该程序将在输入种子文本的情况下生成垃圾邮件。例如,给定输入

[Thank you/Wow/Great], this [blog/post/writing] [gives/provides/delivers] [great/excellent/wonderful/superior] [content/information/data]. You [may/could] [find/get/retrieve] [similar/related] [content/information/data] at spammysite.com.

可能的输出可能是

Thank you, this writing delivers excellent data. You may find similar content at spammysite.com.

(来自https://security.stackexchange.com/q/37436/29804的想法)

不过有一个陷阱!您还必须以其他语言输出程序。该程序在运行时还将以其他语言输出垃圾邮件文本和程序,也将以其他语言输出垃圾邮件文本和程序...

您的分数将是character count / (number of languages ^ 2)

例如,我可能有一个Ruby程序。它将输入的种子文本作为输入,并输出随机垃圾邮件文本和Python程序。

运行此Python程序(无输入),它将输出另一个随机垃圾邮件文本和一个Perl程序。

运行此Perl程序(无输入),它将输出另一个随机垃圾邮件文本。

在此示例中,我的得分将是原始Ruby程序的长度除以9(我有3种语言,而3 ^ 2为9)。

其他杂项:

  • 您必须至少使用2种不同的编程语言。
  • 请列出每个输出的程序。这样,即使某人没有所有语言,他们仍然可以查看其来源。您可以使用以下简短示例文本:[Please/Kindly] [visit/see] my [site/blog].,也可以仅使用一个占位符(如{insert text here})。
  • 您可以假设除之外[]/.,,将没有任何非字母数字字符以简化某些操作。
  • 不同版本的语言不算作不同语言(例如,Python 2 == Python 3)。
  • 它必须是完全随机的,即不可预测,并且每次运行都输出不同的组合。它还必须能够以任意数量的运行输出每个组合。
  • 由于这既是,因此总分最短的(charcount / (langs ^ 2))将获胜。

如果分布不均匀,例如,第一个单词的选择频率高于另一个单词,可以吗?
mniip 2014年

@mniip Hmm,我从未真正指定过:-PI假设您可以这样做并且可以在保存字符时弯曲规则
Doorknob

Answers:


5

PHP 5.5-> Ruby 2.0-> Python 2.7-> Lua-> C#-> Java-> PdfTeX-> GolfScript 855/8 ^ 2 = 13.359375

主要思想是:

  1. 只需一次仅使用一次,而不必使用所有编程语言中创建第n + 1种语言的逻辑,而是仅使用每种编程语言的基本随机和标准输出编写功能即可
  2. 要缩短模板生成器,只需使用zLib对其进行压缩。使此生成器成为第二种语言
  3. 使用PHP作为第一语言,因为它具有内置的zLib解压缩器

因此,初始代码为:

<?php $x=file_get_contents("php://stdin");echo preg_replace_callback("/\[(.*?)\]/",function($m){$r=explode("/",$m[1]);return $r[array_rand($r)];},$x).gzuncompress("BINARY_DATA")."__END__\n".$x;

BINARY_DATA模板生成器的gzcompressed版本在哪里(例如php代码的结果,即ruby版本)

base64中完整的初始代码(解码后应为855字节):

PD9waHAgJHg9ZmlsZV9nZXRfY29udGVudHMoInBocDovL3N0ZGluIik7ZWNobyBw
cmVnX3JlcGxhY2VfY2FsbGJhY2soIi9cWyguKj8pXF0vIixmdW5jdGlvbigkbSl7
JHI9ZXhwbG9kZSgiLyIsJG1bMV0pO3JldHVybiAkclthcnJheV9yYW5kKCRyKV07
fSwkeCkuZ3p1bmNvbXByZXNzKCJ42o2UwXKbMBCG7zyFZj0doElV55BLiNJmmump
7aHpTA+Yg4xErA6SGCQSZ4jevZIBx3biTHywzO6y/7+fYRm5uf5zjVtOGS5XWjbY
NLWwSZzHKVx4OTa06Z/oE51iRZw6XFzV1FquXiQ/+2TEMKflKsSbViiLfFx4OTbK
puYuajpr0OUlBDmIoqrVErVUMX8I2ejWXCIvL0oejVfm0USznk0y4Fx4YWaxsUx3
Fj+0wvJkKE9mPcVCmYaX1qUpuI+wUODlgvzwjeI4XHg5NtSu8CBnOGeJNthcbsmT
NN0ToWSnHbatx1DEp3Hv/GyZ0KMyzXfaJWen4Z6aqzu7cmmRZvseknQ8c0JyT2FZ
ixKVNTUGUdRPAWOp9ce9Fgz9pEIlqc/dPhrLJf49UGqJ4g9oL1x4OTak2a79Jfl0
JXs5eiHk7AuM5d+0Mrrm+O/G/8zXVKI19hlcXAYXICqUtPgXX9sb7V15OJdzfJ72
R3uYlah2ezheG97DyTKXxQk4D1wiW+YUs64pJiRceGFm9fohFA+THM19hTd5ycDr
1nrId3mBaHtnArx/9J7izop6n99h+N0Iw4M3/JELeAlwATsIlUf4vdbUHhA8bLHP
L7R4P8FtXHhhZloNI6wJUMjeGGS0fIFgMX5EpTp5Pp9fTYGGVZ0SlW4l4/eCWn42
n8/R1qqb6oJLNLmcgpXwbtdkjU1XXHg5Ntn0RlxcvULQW17AYnsj4xWafuvG+lEc
6mf9dnD3IVQHvgjgDQxceDAwLyi8audAq3e+MR3g+O7gXHgwMJ4fxgPMR1TZ8zLc
yiDIw17YJFx4OTZPEPuBXFwMG4FiZ2GEJUJceDAw+dHiEB1cXIx1x4zAdoTlI98U
uciNR1QQ4ndQ5HdeFFZt9B9+DwFHIikuIl9fRU5EX19cbiIuJHg7

让我们用测试输入字符串浏览所有语言 [Thank you/Wow/Great], this is [awesome/wonderful].

的PHP

$ cat ti2 | php a.php
Great, this is wonderful.
d=DATA.read.chomp.split('[').map{|a|a.split(']')}.flatten.map{|a|a.split('/')}
d.each{|a|print a.sample}
puts <<"DATA"

from random import choice
import sys
#{d.map{|a|"sys.stdout.write(choice(#{a.inspect}))"}*"\n"}
print
print '''math.randomseed(os.time())
#{d.map{|a|"a=#{a.inspect.tr('[]','{}')};io.write(a[math.random(1,#{a.length})]);"}*"\n"}
print()
print([==[
public class a {
public static void Main() {
System.Random r=new System.Random();
#{d.map{|a|b=->m{m.length==1?"System.Console.Write(#{m.first.inspect});":"if (r.NextDouble()<0.5){System.Console.Write(#{m.shift.inspect});}else{"+b[m]+"};"};b[a.dup]}*"\n"}
System.Console.WriteLine();
System.Console.WriteLine(@"class a {
public static void main(String[] args) {
java.util.Random r=new java.util.Random();
#{d.map{|a|b=->m{m.length==1?"System.out.print(\"#{m.first.inspect}\");":"if (r.nextFloat()<0.5){System.out.print(\"#{m.shift.inspect}\");}else{"+b[m]+"};"};b[a.dup]}*"\n"}
System.out.println();
#{x="a";d.map{|a|b=->m{m.length==1?m.first: "\\\\\\\\ifnum500>\\\\\\\\pdfuniformdeviate1000 #{m.shift}\\\\\\\\else "+b[m]+"\\\\\\\\fi"};x=x.succ;a.length>1?"System.out.println(\"\"\\\\\\\\def \\\\\\\\opt#{x} {#{b[a.dup]}}%\"\");": ""}*"\n"}
System.out.println(""#{x="a";d.map{|a|x=x.succ;a.length>1?"\\\\\\\\opt#{x}{}": a.first}*""}"");
System.out.println();
System.out.println(""#{d.map{|a|a.length>1? "[#{a.map{|b|"'#{b}'"}*""}]#{a.length}rand=" : "'#{a.first}'"}*""}"");
System.out.println(""\\\\\\\\bye"");
}
}");
}
}
]==]);
'''
DATA
__END__
[Thank you/Wow/Great], this is [awesome/wonderful].

红宝石

$ ruby a.rb
Great, this is awesome.
from random import choice
import sys
sys.stdout.write(choice(["Thank you", "Wow", "Great"]))
sys.stdout.write(choice([", this is "]))
sys.stdout.write(choice(["awesome", "wonderful"]))
sys.stdout.write(choice(["."]))
print
print '''math.randomseed(os.time())
a={"Thank you", "Wow", "Great"};io.write(a[math.random(1,3)]);
a={", this is "};io.write(a[math.random(1,1)]);
a={"awesome", "wonderful"};io.write(a[math.random(1,2)]);
a={"."};io.write(a[math.random(1,1)]);
print()
print([==[
public class a {
public static void Main() {
System.Random r=new System.Random();
if (r.NextDouble()<0.5){System.Console.Write("Thank you");}else{if (r.NextDouble()<0.5){System.Console.Write("Wow");}else{System.Console.Write("Great");};};
System.Console.Write(", this is ");
if (r.NextDouble()<0.5){System.Console.Write("awesome");}else{System.Console.Write("wonderful");};
System.Console.Write(".");
System.Console.WriteLine();
System.Console.WriteLine(@"class a {
public static void main(String[] args) {
java.util.Random r=new java.util.Random();
if (r.nextFloat()<0.5){System.out.print(""Thank you"");}else{if (r.nextFloat()<0.5){System.out.print(""Wow"");}else{System.out.print(""Great"");};};
System.out.print("", this is "");
if (r.nextFloat()<0.5){System.out.print(""awesome"");}else{System.out.print(""wonderful"");};
System.out.print(""."");
System.out.println();
System.out.println(""\\\\def \\\\optb {\\\\ifnum500>\\\\pdfuniformdeviate1000 Thank you\\\\else \\\\ifnum500>\\\\pdfuniformdeviate1000 Wow\\\\else Great\\\\fi\\\\fi}%"");

System.out.println(""\\\\def \\\\optd {\\\\ifnum500>\\\\pdfuniformdeviate1000 awesome\\\\else wonderful\\\\fi}%"");

System.out.println(""\\\\optb{}, this is \\\\optd{}."");
System.out.println();
System.out.println(""['Thank you''Wow''Great']3rand=', this is '['awesome''wonderful']2rand='.'"");
System.out.println(""\\\\bye"");
}
}");
}
}
]==]);
'''

蟒蛇

$ python a.py
Great, this is wonderful.
math.randomseed(os.time())
a={"Thank you", "Wow", "Great"};io.write(a[math.random(1,3)]);
a={", this is "};io.write(a[math.random(1,1)]);
a={"awesome", "wonderful"};io.write(a[math.random(1,2)]);
a={"."};io.write(a[math.random(1,1)]);
print()
print([==[
public class a {
public static void Main() {
System.Random r=new System.Random();
if (r.NextDouble()<0.5){System.Console.Write("Thank you");}else{if (r.NextDouble()<0.5){System.Console.Write("Wow");}else{System.Console.Write("Great");};};
System.Console.Write(", this is ");
if (r.NextDouble()<0.5){System.Console.Write("awesome");}else{System.Console.Write("wonderful");};
System.Console.Write(".");
System.Console.WriteLine();
System.Console.WriteLine(@"class a {
public static void main(String[] args) {
java.util.Random r=new java.util.Random();
if (r.nextFloat()<0.5){System.out.print(""Thank you"");}else{if (r.nextFloat()<0.5){System.out.print(""Wow"");}else{System.out.print(""Great"");};};
System.out.print("", this is "");
if (r.nextFloat()<0.5){System.out.print(""awesome"");}else{System.out.print(""wonderful"");};
System.out.print(""."");
System.out.println();
System.out.println(""\\def \\optb {\\ifnum500>\\pdfuniformdeviate1000 Thank you\\else \\ifnum500>\\pdfuniformdeviate1000 Wow\\else Great\\fi\\fi}%"");

System.out.println(""\\def \\optd {\\ifnum500>\\pdfuniformdeviate1000 awesome\\else wonderful\\fi}%"");

System.out.println(""\\optb{}, this is \\optd{}."");
System.out.println();
System.out.println(""['Thank you''Wow''Great']3rand=', this is '['awesome''wonderful']2rand='.'"");
System.out.println(""\\bye"");
}
}");
}
}
]==]);

a

$ lua a.lua
Great, this is wonderful.
public class a {
public static void Main() {
System.Random r=new System.Random();
if (r.NextDouble()<0.5){System.Console.Write("Thank you");}else{if (r.NextDouble()<0.5){System.Console.Write("Wow");}else{System.Console.Write("Great");};};
System.Console.Write(", this is ");
if (r.NextDouble()<0.5){System.Console.Write("awesome");}else{System.Console.Write("wonderful");};
System.Console.Write(".");
System.Console.WriteLine();
System.Console.WriteLine(@"class a {
public static void main(String[] args) {
java.util.Random r=new java.util.Random();
if (r.nextFloat()<0.5){System.out.print(""Thank you"");}else{if (r.nextFloat()<0.5){System.out.print(""Wow"");}else{System.out.print(""Great"");};};
System.out.print("", this is "");
if (r.nextFloat()<0.5){System.out.print(""awesome"");}else{System.out.print(""wonderful"");};
System.out.print(""."");
System.out.println();
System.out.println(""\\def \\optb {\\ifnum500>\\pdfuniformdeviate1000 Thank you\\else \\ifnum500>\\pdfuniformdeviate1000 Wow\\else Great\\fi\\fi}%"");

System.out.println(""\\def \\optd {\\ifnum500>\\pdfuniformdeviate1000 awesome\\else wonderful\\fi}%"");

System.out.println(""\\optb{}, this is \\optd{}."");
System.out.println();
System.out.println(""['Thank you''Wow''Great']3rand=', this is '['awesome''wonderful']2rand='.'"");
System.out.println(""\\bye"");
}
}");
}
}

C#

$ mcs a.cs
$ mono a.exe
Thank you, this is wonderful.
class a {
public static void main(String[] args) {
java.util.Random r=new java.util.Random();
if (r.nextFloat()<0.5){System.out.print("Thank you");}else{if (r.nextFloat()<0.5){System.out.print("Wow");}else{System.out.print("Great");};};
System.out.print(", this is ");
if (r.nextFloat()<0.5){System.out.print("awesome");}else{System.out.print("wonderful");};
System.out.print(".");
System.out.println();
System.out.println("\\def \\optb {\\ifnum500>\\pdfuniformdeviate1000 Thank you\\else \\ifnum500>\\pdfuniformdeviate1000 Wow\\else Great\\fi\\fi}%");

System.out.println("\\def \\optd {\\ifnum500>\\pdfuniformdeviate1000 awesome\\else wonderful\\fi}%");

System.out.println("\\optb{}, this is \\optd{}.");
System.out.println();
System.out.println("['Thank you''Wow''Great']3rand=', this is '['awesome''wonderful']2rand='.'");
System.out.println("\\bye");
}
}

爪哇

$ javac a.java
$ java a
Wow, this is wonderful.
\def \optb {\ifnum500>\pdfuniformdeviate1000 Thank you\else \ifnum500>\pdfuniformdeviate1000 Wow\else Great\fi\fi}%
\def \optd {\ifnum500>\pdfuniformdeviate1000 awesome\else wonderful\fi}%
\optb{}, this is \optd{}.

['Thank you''Wow''Great']3rand=', this is '['awesome''wonderful']2rand='.'
\bye

PdfTeX

(代码将在a.pdf而不是stdout内。我希望仍然可以)

$ pdftex a.tex
This is pdfTeX, Version 3.1415926-2.4-1.40.13 (TeX Live 2012)
 restricted \write18 enabled.
entering extended mode
(./a.tex [1{/usr/local/texlive/2012/texmf-var/fonts/map/pdftex/updmap/pdftex.ma
p}] )</usr/local/texlive/2012/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.p
fb>
Output written on a.pdf (1 page, 15967 bytes).
Transcript written on a.log.
$ open a.pdf

结果:

pdftex结果

在文字中:

Great, this is wonderful.
['Thank you''Wow''Great']3rand=', this is '['awesome''wonderful']2rand='.'

高尔夫脚本

$ ruby golfscript.rb a.gs
Wow, this is awesome.

笔记

  1. 看起来PHP相当宽容字符串文字中的二进制数据,但十六进制代码AF和96除外。不要问为什么。

  2. 我可能还可以轻松地添加perl和C ++ 11代码(因为它们都支持原始字符串文字),但是8比10更舍入了数字:)

  3. 值的分布不均匀,因为第一个元素比后面的元素出现频率更高。根据评论,这应该可以。

  4. 初始代码实际上是使用以下PHP 5.5代码生成的。orig.rb实际上与没有页脚的Ruby代码相同(__END__以及之后的附加数据)

generator.php

<?php
$d=file_get_contents('orig.rb');
$dc = gzcompress($d,9);
echo '<?php $x=file_get_contents("php://stdin");echo preg_replace_callback("/\\[(.*?)\\]/",function($m){$r=explode("/",$m[1]);return $r[array_rand($r)];},$x)';
?><?php
echo ".gzuncompress(\"";
for ($i=0;$i<strlen($dc);$i++) {
  switch($dc[$i]) {
  case '"':
    echo "\\\"";break;
  case "\n":
    echo "\\n";break;
  case '\\':
    echo "\\\\";break;
  case "\x00":case "\xaf":case "\x96":
    echo sprintf("\\x%02x",ord($dc[$i]));
    break;
  default:
    echo $dc[$i];
  }
}
echo "\").\"__END__\\n\".\$x;";

(您可能会使用此代码zLib压缩PHP解决方案以应对其他挑战)


1
...哇。那就是我要说的:O +1
Doorknob

您可以在lua中省略分号,并且可以print()通过在之后放置简单的换行符来替换虚假调用[==[
mniip 2014年

4

Ruby 1.9-> Perl 5-> Python 2,227/9 = 25.22222 ...

$><<(g=gets).gsub(/\[(.*?)\]/){$1.split(?/).sample}+"say'#{g.chop}'=~s|\\[(.*?)\\]|@a=split'/',$1;$a[rand@a]|reg;say'import re,random;print re.sub(\"\\[(.*?)\\]\",lambda m:random.choice(m.group(1).split(\"/\")),\"#{g.chop}\")'"

输出为[Please/Kindly] [visit/see] my [site/blog].

Please see my site.
say'[Please/Kindly] [visit/see] my [site/blog].'=~s|\[(.*?)\]|@a=split'/',$1;$a[rand@a]|reg;say'import re,random;print re.sub("\[(.*?)\]",lambda m:random.choice(m.group(1).split("/")),"[Please/Kindly] [visit/see] my [site/blog].")'

Perl程序(我的第一个!:-P)输出:

Kindly see my blog.
import re,random;print re.sub("\[(.*?)\]",lambda m:random.choice(m.group(1).split("/")),"[Please/Kindly] [visit/see] my [site/blog].")

Python程序输出:

Kindly visit my site.
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.