创建一个随机显示符合指定正则表达式的文本的应用程序

irc*_*ell 32 language-agnostic string random

好吧,所以本着Code-Golf的精神,我在这里尝试新的东西:Code-Bowling.

在高尔夫中,你试图获得最低分(最小的应用,最优雅的等).在鲍灵,你试图获得最高分.因此,如果您遵循,Code-Bowling挑战的目标是制作最大,最卑鄙,最难维护的代码仍然能够满足挑战的要求.然而,仅仅为了它而使源更长是没有意义的.它似乎需要增加的长度来自设计而不仅仅是填充.

这是挑战:

用您选择的语言编写一个程序,创建一行文本输出并终止.创建的输出必须与此正则表达式匹配:

/^Good (Morning|Afternoon|Evening|Night)$/
Run Code Online (Sandbox Code Playgroud)

输出可能是随机的(使用语言或您自己的实现)或混乱(确定性,但不是平凡的).

Fre*_*Foo 30

/*
 * TODO: write manpage
 */

#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define ERROR_MAX   2048    /* arbitrary, must account for argv[0] */

#define MSG_SIZE    (5+9+1) /* +1 for newline, +1 for NUL */

#if defined(linux) || defined(BSD) && BSD > 199300L
extern char const *__progname;
# define progname __progname
#else
static char *progname;
#endif

typedef enum _Progerr {
    IO_ERR = 1,
    RND_ERR = 2
} progerr_t;

static const char GREET_START[5] = "Good";  /* XXX move to Makefile? */

static const char *TIMES_OF_DAY[5] = {
    "Morning",
    "Afternoon",
    "Evening",
    "Night",
    NULL
};

int main()
{
    char errbuf[ERROR_MAX];
    char msgbuf[MSG_SIZE];
    char *slash;
    const char **time_of_day;
    int fd, rnd;
    size_t greet_len;

#ifndef progname
    /* we want proper error messages */
    progname = argv[0];
    if ((slash = strrchr(progname, '/')) != NULL)
        progname = slash+1;
#endif

    /* get REAL randomness; can't trust rand(3).
     * avoid stdio, it's slow. */
#ifdef DEBUG
    write(STDERR_FILENO, "getting random data\n", sizeof("getting random data\n")-1);
#endif
    if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
        if ((fd = open("/dev/random", O_RDONLY)) == -1)
            rnd = rand();   /* last resort, for MSYS etc. */
    }

    if (fd >= 0 && read(fd, &rnd, sizeof(int)) != sizeof(int)) {
        close(fd);
        goto rngerr;
    }

    /* higher bits of rand() have better entropy */
    assert(sizeof(int) >= 4);   /* should be compile-time assert */
    rnd = (rnd >> 24) & 0x03;

    for (time_of_day = TIMES_OF_DAY; *time_of_day && rnd; time_of_day++, rnd--)
        ;
    if (!time_of_day)
        goto rngerr;

    sprintf(msgbuf, "%s %s", GREET_START, *time_of_day);
    greet_len = strlen(msgbuf);
    msgbuf[greet_len] = '\n';
    if (write(STDOUT_FILENO, msgbuf, greet_len+1) == -1)
        goto write_err;

    return 0;

rngerr:
    sprintf(errbuf, "%s: cannot get random data\n", progname);
    write(STDERR_FILENO, errbuf, strlen(errbuf));
    return (int)RND_ERR;

write_err:
    sprintf(errbuf, "%s: cannot write to stdout\n", progname);
    write(STDERR_FILENO, errbuf, strlen(errbuf));
    return (int)IO_ERR;
}
Run Code Online (Sandbox Code Playgroud)

  • @ircmaxell,你以为我只是在做这个? (22认同)
  • +1 ...可悲的是,你实际上可能会在生产中找到类似的代码...... (9认同)

use*_*637 19

Java + Spring:所有接口都可注入单元测试!

package stackoverflow.codebowling;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ByteArrayResource;

import java.io.*;
import java.util.Random;
import java.util.logging.Logger;

public class Main {

    /*
    TODO: Add javadoc
    */

    static public final Logger logger = Logger.getLogger(Main.class.getName());

    public static void main(String[] args) throws IOException {
        ByteArrayResource byteArrayResource =
            new ByteArrayResource(spring_config_xml.getBytes());
        XmlBeanFactory beanFactory = new XmlBeanFactory(byteArrayResource);
        MessageWriter messageWriter = beanFactory.getBean("MessageWriterBean", MessageWriter.class);
        try {
            Writer writer = new PrintWriter(System.out);
            messageWriter.writeMessage(writer);
            writer.flush();
        } catch(IOException ex) {
            logger.severe(ex.getMessage());
            throw ex;
        }
    }

    /*
    Using a visitor pattern to avoid casting or case statements.
    If you see a case statement your are probably not doing real OOP
     */
    static abstract public class TimeOfDay {

        public abstract void visit(TimeOfDayVisitor visitor);

        static final class Morning extends TimeOfDay {

            @Override
            public void visit(TimeOfDayVisitor visitor) {
                visitor.morning(this);
            }
        }

        static final class Afternoon extends TimeOfDay {

            @Override
            public void visit(TimeOfDayVisitor visitor) {
                visitor.afternoon(this);
            }
        }

        static final class Evening extends TimeOfDay {

            @Override
            public void visit(TimeOfDayVisitor visitor) {
                visitor.evening(this);
            }
        }

        static final class Night extends TimeOfDay {

            @Override
            public void visit(TimeOfDayVisitor visitor) {
                visitor.night(this);
            }
        }

        static public final TimeOfDay[] ALL = {
                new Morning(),
                new Afternoon(),
                new Evening(),
                new Night()
        };

        static public interface TimeOfDayVisitor {
            public void morning(TimeOfDay timeOfDay);
            public void afternoon(TimeOfDay timeOfDay);
            public void evening(TimeOfDay timeOfDay);
            public void night(TimeOfDay timeOfDay);
        }

    }

    static public interface MessageWriter {
        void writeMessage(Writer writer) throws IOException;
    }

    static public class MessageWriterImpl implements MessageWriter {

        private TimeOfDayChooser timeOfDayChooser;

        private TimeOfDayGreetingsFormatter timeOfDayGreetingsFormatter;

        public void writeMessage(Writer writer) throws IOException {
            TimeOfDay timeOfDay = timeOfDayChooser.choose();
            writer.write(timeOfDayGreetingsFormatter.format(timeOfDay));
        }

        public void setTimeOfDayChooser(TimeOfDayChooser timeOfDayChooser) {
            this.timeOfDayChooser = timeOfDayChooser;
        }

        public void setTimeOfDayGreetingsFormatter(TimeOfDayGreetingsFormatter timeOfDayGreetingsFormatter) {
            this.timeOfDayGreetingsFormatter = timeOfDayGreetingsFormatter;
        }
    }

    static public interface TimeOfDayGreetingsFormatter {
        String format(TimeOfDay timeOfDay);
    }

    static public class TimeOfDayGreetingsFormatterImpl implements TimeOfDayGreetingsFormatter {

        public String format(TimeOfDay timeOfDay) {
            final StringBuilder builder = new StringBuilder();
            builder.append("Good ");
            timeOfDay.visit(new TimeOfDay.TimeOfDayVisitor() {
                public void morning(TimeOfDay timeOfDay) {
                    builder.append("Morning");
                }

                public void afternoon(TimeOfDay timeOfDay) {
                    builder.append("Afternoon");
                }

                public void evening(TimeOfDay timeOfDay) {
                    builder.append("Evening");
                }

                public void night(TimeOfDay timeOfDay) {
                    builder.append("Night");
                }
            });
            return builder.toString();
        }
    }

    static public interface TimeOfDayChooser {
        TimeOfDay choose();
    }

    static public class RandomTimeOfDayChooserImpl implements TimeOfDayChooser {

        // *** injected by Spring
        private RandomService randomService;

        public synchronized TimeOfDay choose() {
            int range = TimeOfDay.ALL.length;
            int index = randomService.rand(range);
            return TimeOfDay.ALL[index];
        }

        public void setRandomService(RandomService randomService) {
            this.randomService = randomService;
        }
    }

    static public class ChaoticTimeOfDayChooserImpl implements TimeOfDayChooser {

        // *** injected by Spring
        private RandomService randomService;

        // *** this is initialized in the setter for randomService
        private int currentIndex;

        public synchronized TimeOfDay choose() {
            int range = TimeOfDay.ALL.length;
            this.currentIndex = this.currentIndex + 1 + randomService.rand(range - 1);
            return TimeOfDay.ALL[this.currentIndex];
        }

        public void setRandomService(RandomService randomService) {
            this.randomService = randomService;
            int range = TimeOfDay.ALL.length;
            this.currentIndex = randomService.rand(range);
        }
    }

    static public interface RandomService {
        int rand(int range);
    }

    static public class RandomServiceImpl implements RandomService {

        // *** initialized by Spring
        private long seed;

        // *** initialized by setSeed
        private Random random;

        public int rand(int range) {
            return (int)(random.nextInt(range));
        }

        /*
        A seed of < 0 indicates a random seed. For testing, set a positive long value
        which will guarantee reproducible results.
         */
        public void setSeed(long seed) {
            this.seed = seed;
            if (seed >= 0) {
                this.random = new Random(seed);
            } else {
                this.random = new Random();
            }
        }
    }

    static public final String spring_config_xml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<beans xmlns=\"http://www.springframework.org/schema/beans\"\n" +
                "       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "       xmlns:aop=\"http://www.springframework.org/schema/aop\"\n" +
                "       xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\">\n" +
                "   <bean id=\"MessageWriterBean\" class=\"stackoverflow.codebowling.Main.MessageWriterImpl\">\n" +
                "      <property name=\"timeOfDayChooser\" ref=\"RandomTimeOfDayChooserBean\" />\n" +
                "      <property name=\"timeOfDayGreetingsFormatter\" ref=\"TimeOfDayGreetingsFormatterBean\" />\n" +
                "   </bean>\n" +
                "   <bean id=\"RandomTimeOfDayChooserBean\" class=\"stackoverflow.codebowling.Main.RandomTimeOfDayChooserImpl\">\n" +
                "      <property name=\"randomService\" ref=\"RandomServiceBean\" />\n" +
                "   </bean>\n" +
                "   <bean id=\"ChaoticTimeOfDayChooserBean\" class=\"stackoverflow.codebowling.Main.ChaoticTimeOfDayChooserImpl\">\n" +
                "      <property name=\"randomService\" ref=\"RandomServiceBean\" />\n" +
                "   </bean>\n" +
                "   <bean id=\"RandomServiceBean\" class=\"stackoverflow.codebowling.Main.RandomServiceImpl\">\n" +
                "      <property name=\"seed\" value=\"-1\" />\n" +
                "   </bean>\n" +
                "   <bean id=\"TimeOfDayGreetingsFormatterBean\" class=\"stackoverflow.codebowling.Main.TimeOfDayGreetingsFormatterImpl\" />\n" +
                "</beans>\n";

}
Run Code Online (Sandbox Code Playgroud)

  • +1,你生病了.哪里是网络服务?:) (6认同)

Ivo*_*zel 18

好吧,你去吧...... 现在快点给我一些钱,这样我就可以离开这个国家,住在一个随机的太平洋岛屿上,我在那里建造陷阱并躲在丛林中,这样我就可以安全地离开克罗克福德了....

// give out greeting program
rand = Math.random;
tims = Array()
timeString = "morning ,afternoon,evening ,night" // our times of day
timesCount=4 // set timesCount to 4

// shorthand for creating array dont have to type tims[...] = ... all the time
makeArray(timeString, timesCount)
greeting = null

// this saves typing
String['prototype']['getChar'] =function(big,p){
return String.fromCharCode(big!=false? this.charCodeAt(p)-32: this.charCodeAt(p)) }

function makeArray(data,max){
    ex = regeExFromStr('[a-zA-Z]+', "g")
    while(m= ex.exec(data))if(tims.length< max)tims.push(m[0])
    return tims }

function formatGreting(){
    mesage ="Good " + randm;
    msglength=0

    // get string length should be fast this way
     while(mesage[msglength])++msglength
    captialisised=""
        // check if we need to replace with upper
    for(i=0;char=mesage.charCodeAt(i-1),i <msglength; i++)

        // found this on another side on the internet
        switch(char) {
        case 32: if(char== 32)
                captialisised= captialisised + mesage.getChar(true,i)
        break;
        default: if (char!=32)
            captialisised = captialisised+mesage.getChar(false,i)
        break
        }

       // make sure there is no whitespace should work better with regex
    trimmed=regeExFromStr("([A-Za-z]+\\\s[A-Za-z]+)",'').exec(captialisised)[0]
    return trimmed }

function getTime(){ // get a time of days
    tims.cacheLength();arrayLenght= tims.cachedLength;
    randm = eval('tims[' + randomNum(arrayLenght) + "]");
    if(valid(randm))
            greeting=formatGreting()
}
function regeExFromStr(string,opt){
    return eval("/"+string+"/" +opt)  // make regex from string
}

function randomNum(max){
// random * random is more random
return Math.floor(rand() * max*rand()) }

// make sure the string correct
function valid(str) {
    valids= makeArray(tims.join('|'),timesCount)
    tims.cacheLength();cachedLenght= tims.cachedLength;
    hasValidated=false; // hasValidated
    for(i=0; i< cachedLenght;i++)
        if(!stringIsDifferent(eval("tims["+i+"]"),trimString(str)))hasValidated=true
            return istrue(hasValidated)}

// stupid js has to trim
function trimString(str) {
  l=0;
    while(str.charCodeAt(l++)!=32)
    return str.substr(0,l)
}

// found this on a side called thedailywtf.com always worked for me
function istrue(value){
bool = Boolean(value)
if(bool==true)
    return true

 else if(bool==false)
        return false

      else
            return null}

// == is broken so well just use regex
function stringIsDifferent(a,b) {
ex=regeExFromStr("^"+b+"$","i")
same=ex.test(b)
   return
      istrue(same)
}

// length is slow
Object.prototype["initcaching"]=function(){

this.cachedLength =0;}
Object.prototype['cacheLength']=function() {
with (this) {
    initcaching();
       while(this[cachedLength])
           cachedLength++   }}

getTime()

// print to brwoser
document.write(greeting)
Run Code Online (Sandbox Code Playgroud)

PS:我目前正在编写一本关于所有常见JS陷阱的指南,你可能想看看它.

  • @Ivo -1.你应该完全放弃所有这些并使用jQuery. (12认同)
  • 是的,但`stringIsDifferent`绝对是我最喜欢的:) (2认同)
  • 你的JS指南非常非常好.即使在MDC和SO的时代,也很难获得良好的JavaScript文档. (2认同)

小智 18

我写了一个三语法语资源程序,可以用bash解释,用gcc编译并用python解释而不需要改变代码(如果用bash解释,它会调用自己作为C和python来完成任务).它还使用谷歌的加载时间作为随机数的来源.

#if 0
"""ls" > /dev/null
echo /* >/dev/null
# Save as 'whatever.sh.c' (.sh is negotiable, but .c is a must, lest gcc will cry)
# Run with bash (i.e. bash 'whatever.sh.c')

gcc -std=c99 $0 -o /tmp/codebowling 2>/dev/null
/tmp/codebowling
/tmp/codebowling `python $0`
rm -rf /tmp/codebowling
exit;
echo */ >/dev/null
#endif

#include <stdlib.h>
#include <stdio.h>

char* strings[] = {
   "Morning\n", "Evening\n", "Afternoon\n", "Night\n"
};

int main(int argc, char* argv[]) {
 if(argc == 1) printf("Good ");
 else if(argc == 2) { 
    int what = atoi(argv[1]);
    printf(strings[what]);
 }

 return EXIT_SUCCESS;
}
#if 0
/*
"""
#*/
from urllib import urlopen
import time, math

t1 = time.time()
str = urlopen('http://www.google.com').read();
t2 = time.time()
dt = t2 - t1;
print int(100+100*math.sin(100000*dt))%4
#endif
Run Code Online (Sandbox Code Playgroud)

  • 哇,现在这很有创意......我从没想过会在这里看到Bash-C-Python混合......非常好...... (3认同)

Ale*_*urg 14

我认为这在WTF-iness中创造了一些记录.它是一个将人类可读的程序集编译成BrainF***的函数,然后是另一个解释它的函数.

当然,人类可读的程序集缺少一些功能,所以有一些手动插入BF代码的hack-ish.而且代码通常都是WTF.

import java.io.BufferedInputStream;
import java.io.BufferedReader;
    import java.io.ByteArrayInputStream;
import java.io.FileReader;
    import java.io.IOException;
import java.io.InputStream;import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;


public class CodeBowling {
    public static void main(String[] args) throws IOException{
        //An extended version of BrainF*ck, with a command for jumping a rand(int) to the right
        //I've heard that assembly languages always run faster, so this should be really efficient!
        String readable="";
        readable+="set 0 G\n";
        readable+="set 1 o\n";
        readable+="set 2 o\n";
        readable+="set 3 d\n";
        readable+="set 4 _\n";

        readable+="set 5 M\n";
        readable+="set 9 o\n";
        readable+="set 13 r\n";
        readable+="set 17 n\n";
        readable+="set 21 i\n";
        readable+="set 25 n\n";
        readable+="set 29 g\n";

        readable+="set 6 A\n";
        readable+="set 10 f\n";
        readable+="set 14 t\n";
        readable+="set 18 e\n";
        readable+="set 22 r\n";
        readable+="set 26 n\n";
        readable+="set 30 o\n";
        readable+="set 34 o\n";
        readable+="set 38 n\n";

        readable+="set 7 E\n";
        readable+="set 11 v\n";
        readable+="set 15 e\n";
        readable+="set 19 n\n";
        readable+="set 23 i\n";
        readable+="set 27 n\n";
        readable+="set 31 g\n";

        readable+="set 8 N\n";
        readable+="set 12 i\n";
        readable+="set 16 g\n";
        readable+="set 20 h\n";
        readable+="set 24 t\n";

        //Begin execution.
        readable+="print 0\n";
        readable+="print 1\n";
        readable+="print 2\n";
        readable+="print 3\n";
        readable+="sub 4 63\n";
        readable+="print 4\n";

        //VERY IMPORTANT
        //JUMP COMMANDS PERMANTENTLY SHIFT THE MEMORY.
        //DO NOT FOLLOW THEM BY ANY OTHER COMMANDS.
        readable+="rand\n";
        readable+="rand\n";
        readable+="rand";

        String bf=compile(readable);

        //Prints out the random greeting; the assembly does not include this function.
        //A request has been filed to the other developer to add this feature to the
        //compiler ASAP.
        bf+=">>>>>[.>>>>]"; 

        execute(bf);
    }
    static void execute(String program){
        InputStream is=null;
        try {
            is = new ByteArrayInputStream(program.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Scanner scn=new Scanner(is);
        List<Character> acceptedChars=Arrays.asList('<','>','.',',','+','-','[',']','J');
        int brack=0;
        Stack<Integer> brackStack=new Stack<Integer>();
        Map<Integer,Integer> brackMap=new HashMap<Integer,Integer>();
        int pos=0;
        StringBuilder sb=new StringBuilder();
                    scn.useDelimiter("");
            int warnings=0;
            while(scn.hasNext()){
                char nextChar=scn.next().charAt(0);
                if(acceptedChars.contains(nextChar)){
                    sb.append(nextChar);
                    if(nextChar=='['){
                        brack++;
                        brackStack.push(pos);
                    }
                    if(nextChar==']'){
                        brack--;
                        brackMap.put(pos, brackStack.peek());
                        brackMap.put(brackStack.pop(), pos);
                    }
                } else if(warnings<3){
                    System.out.println("Warning: unrecognized character '"+((nextChar=='\r'||nextChar=='\n')?"newline":nextChar)+"'");
                    warnings++;
                    if(warnings==3)
                        System.out.println("Too many warnings, suppressing output.");
                }
                pos++;
            }
        if(brack!=0){
            System.out.println("Error: unbalanced brackets.");
            System.exit(1);
        }
        char[] pgrm=sb.toString().toCharArray();

        //Begin execution
        int Codeloc=0,Memloc=0,lim=pgrm.length;
        ArrayList<Integer> mem=new ArrayList<Integer>();
        scn=new Scanner(System.in);
        scn.useDelimiter("");

        while(Codeloc<lim){
            try{
                switch(pgrm[Codeloc]){
                case '+':
                    mem.set(Memloc,mem.get(Memloc)+1);
                    break;
                case '-':
                    mem.set(Memloc,mem.get(Memloc)-1);
                    break;
                case '>':
                    Memloc++;
                    break;
                case '<':
                    Memloc--;
                    break;
                case '[':
                    if(mem.get(Memloc)==0){
                        Codeloc=brackMap.get(Codeloc);
                    } else {
                        Codeloc=Codeloc;//0 //brackStack.peek() //Codeloc++;;;
                    }
                    break;
                case ']':
                    Codeloc=brackMap.get(Codeloc)-1;
                    break;
                case '.':
                    System.out.print((char)(int)mem.get(Memloc));
                    break;
                case ',':
                    mem.set(Memloc,(int)scn.next().charAt(0));
                    break;
                case 'J':
                    java.util.Random r=new java.util.Random();
                    Memloc+=r.nextBoolean()?1:0;
                }
                Codeloc++;
            } catch (java.lang.IndexOutOfBoundsException e){
                //i++;
                //if(i>20){
                //  System.exit(1);
                //}
                mem.add(0);
            }
        }
    }
    static String compile(String readable) throws IOException{
        String program="";
        BufferedReader is=null;
        try {
            is = new BufferedReader((Reader)new StringReader(readable));
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Stack continueLoopingWhileNotEqualToZeroLocations=new Stack();
        while(is.ready()){
            String command=is.readLine();
            try{
                int a=1/((command==null)?0:1);
            } catch (Exception e) {
                break;
            }
            StringTokenizer st=new StringTokenizer(command);
            String type=st.nextToken();
            if(type.equals("set")){ // "set 4 c" sets memory location 4 to a "c".
                Object temp=new Integer(st.nextToken());
                int location=(Integer)temp;
                for(int i=0;i<location;i++){
                    program+=">";
                }
                program+="[-]";
                temp=new Character(st.nextToken().charAt(0));
                char character=(Character)temp;
                for(char i=0;i<character;i++){
                    program+="+";
                }
                for(int i=location;i>0;i--){
                    program+="<";
                }
            } else if(type.equals("add")){ // "add 4 10" increments memory location 4 by 10.
                Object temp=new Integer(st.nextToken());
                int location=(Integer)temp;
                for(int i=0;i<location;i++){
                    program+=">";
                }
                temp=new Integer(st.nextToken());
                int diff=(Integer)temp;
                for(char i=0;i<diff;i++){
                    program+="+";
                }
                for(int i=location;i>0;i--){
                    program+="<";
                }
            } else if(type.equals("sub")){ // "sub 4 10" decrements memory location 4 by 10.
                Object temp=new Integer(st.nextToken());
                int location=(Integer)temp;
                for(int i=0;i<location;i++){
                    program+=">";
                }
                temp=new Integer(st.nextToken());
                int diff=(Integer)temp;
                for(char i=0;i<diff;i++){
                    program+="-";
                }
                for(int i=location;i>0;i--){
                    program+="<";
                }
            } else if(type.equals("addFrom")){ // "addFrom 4 5 7 9" adds the value of location 4 to locations 5, 7, and 9, and erases 4.
                Object temp=new Integer(st.nextToken());
                int location=(Integer)temp;
                for(int i=0;i<location;i++){
                    program+=">";
                }
                program+="[-";
                int a=location;
                int b;
                while(st.hasMoreTokens()){
                    b=Integer.valueOf(st.nextToken());
                    int diff=b-a;
                    if(diff>0){
                        for(int i=0;i<diff;i++){
                            program+=">";
                        }
                    } else {
                        for(int i=0;i<diff;i++){
                            program+="<";
                        }
                    }
                    program+="+";
                    a=b;
                }
                int diff=location-a;
                if(diff>0){
                    for(int i=0;i<diff;i++){
                        program+=">";
                    }
                } else {
                    for(int i=0;i<diff;i++){
                        program+="<";
                    }
                }
                program+="]";
            } else if(type.equals("subFrom")){ // "subFrom 4 5 7 9" subtracts the value of location 4 to locations 5, 7, and 9, and erases 4.
                Object temp=new Integer(st.nextToken());
                int location=(Integer)temp;
                for(int i=0;i<location;i++){
                    program+=">";
                }
                program+="[-";
                int a=location;
                int b;
                while(st.hasMoreTokens()){
                    b=Integer.valueOf(st.nextToken());
                    int diff=b-a;
                    if(diff>0){
                        for(int i=0;i<diff;i++){
                            program+=">";
                        }
                    } else {
                        for(int i=0;i<diff;i++){
                            program+="<";
                        }
                    }
                    program+="-";
                    a=b;
                }
                int diff=location-a;
                if(diff>0){
                    for(int i=0;i<diff;i++){
                        program+=">";
                    }
                } else {
                    for(int i=0;i<diff;i++){
                        program+="<";
                    }
                }
                program+="]";
            } else if(type.equals("print")){// "print 3" prints the value of 3, cast to a char.
                int point=Integer.valueOf(st.nextToken());
                for(int i=0;i<point;i++){
                    program+=">";
                }
                program+=".";
                for(int i=0;i<point;i++){
                    program+="<";
                }
            } else if(type.equals("read")){// "read 3" grabs one char from the input, and assigns it to position 3.
                int point=Integer.valueOf(st.nextToken());
                for(int i=0;i<point;i++){
                    program+=">";
                }
                program+=",";
                for(int i=0;i<point;i++){
                    program+="<";
                }
            } else if(type.equals("while")){//"while 5" will loop as long as 5 is not zero.
                int continueLoopingWhileNotEqualToZeroLocation=(Integer)new Integer(st.nextToken());
                for(int i=0;i<continueLoopingWhileNotEqualToZeroLocation;i++){
                    program+=">";
                }
                program+="[";
                for(int i=0;i<continueLoopingWhileNotEqualToZeroLocation;i++){
                    program+="<";
                }
                ((Stack<Integer>)continueLoopingWhileNotEqualToZeroLocations).push(continueLoopingWhileNotEqualToZeroLocation);
            } else if(type.equals("endwhile")){
                int l=((Stack<Integer>)continueLoopingWhileNotEqualToZeroLocations).pop();
                for(int i=l/2;i<-((l+1)/2);i--){
                    program+=">";
                }
            } else if(type.equals("rand")){
                program+="J";
            } else {
                System.out.println("UNRECOGNIZED COMMAND "+type);
                int a=1/0; //Kills the program.
            }
        }
        return program;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这让我的日子变得更加美好,哈哈.int a = 1/0; //杀死程序 优秀. (4认同)

sjn*_*ngm 6

这个JavaScript片段附带一个文本生成器:

function cth(i) {
    return i > 16 ? cth(i >> 4) + cth(i & 15) : "" + (i < 10 ? i : String.fromCharCode(55 + i))
}

function nc(s) {
    return c += s
}
var w = [],
    s = [],
    c = 0,
    t = new Date().getTime()
    s[0] = cth(nc(71)) s[1] = cth(nc(40)) s[2] = cth(nc(-11))
    s.splice(1, 0, s.slice(1, 2)[0])
    w.push(unescape("%" + s.join("%")))
    s[0] = cth(nc(-23)) s[2] = cth(nc(37)) s[3] = cth(nc(-4)) s[4] = cth(nc(-5)) s[5] = cth(nc(-2))
    s.splice(5, 0, s.slice(3, 4)[0])
    w.push(unescape("%" + s.join("%")))
    s.splice(0, 3)
    s.unshift(cth(nc(-2))) s.unshift(s[s.length - 1].replace(/(.)(.)/, "$2$1")) s.unshift(cth(nc(-32)))
    w.push(unescape("%" + s.join("%")))
    s = w[0].split(/[^aeiou]/i) s[0] = "After"
    s = s.join("n")
    w.push(s)
    s = String(69676874).replace(/(..)/g, "%$1")
    w.push("N" + unescape(s))
    t /= c
    alert(w[0] + " " + w[1 + (t & 3)])
Run Code Online (Sandbox Code Playgroud)

是的,这不是一个长代码,但它仍然没有意义.


Fos*_*sco 5

为了您的享受......

<?php

$play = 71;
$ball = array(40,0,-11);
p($play,$ball);
$hello = 32;
p($hello,array());
$p = rand(1,4);
switch($p) {
    case 1:
        $dead = 77;
        $beef = array(34,3,-4,-5,5,-7);
        break;
    case 2:
        $dead = 65;
        $beef = array(37,14,-15,13,-4,1,0,-1);
        break;
    case 3:
        $dead = 69;
        $beef = array(49,-17,9,-5,5,-7);
        break;
    case 4:
        $dead = 78;
        $beef = array(27,-2,1,12);
}
p($dead,$beef);
$peanut = 13;
$butter = array(-3);
p($peanut,$butter);

function p($place,$path) {
    echo chr($place);
    while (count($path)) { $x = array_shift($path); $place += $x; echo chr($place); }
}
Run Code Online (Sandbox Code Playgroud)

一个更新的,浓缩的版本...我实际上不明白为什么长度是一个要求.我认为保持这些答案很简单(添加一个可能的问候语,改变现有的问题).你真的觉得你会更容易改变这个吗?:

<?php 

play(array(71,40,0,-11));
play(array(32));
p($hello,array());
$p = rand(1,4);
play(($p == 1 ? play(array(77,34,3,-4,-5,5,-7)) : 
($p == 2 ? play(array(65,37,14,-15,13,-4,1,0,-1)) : 
($p == 3 ? play(array(69,49,-17,9,-5,5,-7)) : 
($p == 4 ? play(array(78,27,-2,1,12)) 
: die('RUN'))))));
play(array(13,-3));

function play($packet) {
    if (count($packet) > 1) {
        $packet[0] += $x = array_shift($packet);
        echo chr($x);
        play($packet);
    } else echo chr($packet[0]);
}
Run Code Online (Sandbox Code Playgroud)