Make an application that displays text at random t

2019-03-07 23:41发布

OK, so in the spirit of Code-Golf, I'm trying out something new here: Code-Bowling.

In golf, you try to get the lowest score (smallest application, most elegant, etc). In Bowling, you try to get the highest score. So if you follow, the goal of a Code-Bowling challenge is to make the biggest, most bastardized, hardest to maintain piece of code that still meets the requirements of the challenge. However, there's no point in making source longer just for the sake of it. It needs to seem like that added length was from design and not just padding.

Here's this challenge:

Write a program in your language of choice that creates one line of text-output and terminates. The output that's created must be matched by this regex:

/^Good (Morning|Afternoon|Evening|Night)$/

The output may be random (using the languages or your own implementation) or chaotic (deterministic, but not trivially so).

11条回答
\"骚年 ilove
2楼-- · 2019-03-07 23:58
/*
 * 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;
}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-03-08 00:00

OK, there you go... NOW quickly send me some money so I can leave this country and live on some random pacific island where I built pitfalls and hide in the jungle so I'm safe from Crockford....

// 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)

PS: I've currently writing a guide about all common JS pitfalls, you might want to check it out.

查看更多
我想做一个坏孩纸
4楼-- · 2019-03-08 00:02

The asker mentioned regular expressions, so clearly he is looking for an answer using regular expressions. I was baffled after I saw that most of the answers thus far totally fail to realize the power of regular expressions and embrace them.

Thus, my solution. Python code that generates random matches given a regular expression. And the output is totally random, I swear!

import random, time

def parse_regex(regex):
        path = [('()', []), ('|', [])]
        path[0][1].append(path[1])
        i = 0
        while i < len(regex):
                char = regex[i]
                if path[-1][0] in ('[]', '[^]'):
                        if char == ']' and path[-1][1]:
                                old2 = path.pop()
                                assert old2[0] in ('[]', '[^]')
                        elif char == '-' and len(path[-1][1]) and i+1 < len(regex) and regex[i+1] != ']':
                                tmp1 = path[-1][1].pop()
                                assert tmp1[0] is None
                                tmp1 = tmp1[1]
                                tmp2 = regex[i+1]
                                assert ord(tmp2) > ord(tmp1)
                                for tmp in range(ord(tmp1), ord(tmp2) + 1):
                                        path[-1][1].append((None, chr(tmp)))
                                i += 1
                        else:
                                path[-1][1].append((None, char))
                else:
                        if char == '(':
                                new1 = ('()', [])
                                new2 = ('|', [])
                                new1[1].append(new2)
                                path[-1][1].append(new1)
                                path.extend([new1, new2])
                        elif char == ')':
                                old2 = path.pop()
                                old1 = path.pop()
                                assert old2[0] == '|'
                                assert old1[0] == '()'
                        elif char == '|':
                                new2 = ('|', [])
                                old2 = path.pop()
                                assert old2[0] == '|'
                                path[-1][1].append(new2)
                                path.append(new2)
                        elif char == '[':
                                tmp = '[]'
                                if i+1 < len(regex) and regex[i+1] == '^':
                                        tmp = '[^]'
                                        i += 1
                                new = (tmp, [])
                                path[-1][1].append(new)
                                path.append(new)
                        elif char == '.':
                                path[-1][1].append(('.', None))
                        elif char in ('*', '+', '?'):
                                assert len(path[-1][1])
                                tmp = path[-1][1].pop()
                                path[-1][1].append((char, tmp))
                        else:
                                path[-1][1].append((None, char))
                i += 1
        assert len(path) == 2
        return path[0]

def generate_match(regextree):
        match = ''
        if regextree[0] == '()':
                regextree = random.choice(regextree[1])
                match += generate_match(regextree)
        elif regextree[0] == '|':
                for item in regextree[1]:
                        match += generate_match(item)
        elif regextree[0] == None:
                match += regextree[1]
        elif regextree[0] == '.':
                match += random.choice([chr(i) for i in range(32, 127)])
        elif regextree[0] in ('*', '+'):
                if regextree[0] == '+':
                        match += generate_match(regextree[1])
                # We favour shorter matches
                while True:
                        if random.choice(['yes', 'yes', 'no']) == 'yes':
                                match += generate_match(regextree[1])
                        else:
                                break
        elif regextree[0] == '?':
                if random.choice(['yes', 'no']) == 'yes':
                        match += generate_match(regextree[1])
        elif regextree[0] == '[]':
                match += random.choice([generate_match(item) for item in regextree[1]])
        elif regextree[0] == '[^]':
                match += random.choice(list(set(chr(i) for i in range(32, 127)) - set(generate_match(item) for item in regextree[1])))
        else:
                raise
        return match

t1 = time.time()
y, m, d = time.localtime(t1)[:3]
t2 = time.mktime((y, m, d, 0, 0, 0) + time.localtime(t1)[-3:])
t = int(t1 - t2)

# Can't rely on python setting the random seed randomly enough.
# Thus, we seed it with the help of cafebabe and current time.    
random.seed((0xcafebabe ^ 0x4eb) + t / 21600)

print generate_match(parse_regex('Good (Morning|Afternoon|Evening|Night)'))
查看更多
一夜七次
5楼-- · 2019-03-08 00:04

I think this set some records in WTF-iness. It's one function that compiles a human-readable assembly into BrainF***, and then another function that interprets it.

Of course, the human-readable assembly is missing some features, so there's some hack-ish insertion of BF code manually. And the code is just generally full of WTFs.

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;
    }
}
查看更多
Root(大扎)
6楼-- · 2019-03-08 00:04

Here's one from me, in PHP. There are at least a few WTFs in it, at least a few bugs, and way too much over-engineering. Enjoy...

<?php
class RandomString {
    protected $options = array();
    protected $lenght = 0;
    public function __construct(array $options) {
        $this->options = $options;
        $this->length = count($options);
    }
    public function __toString() {
        return Random::getArrayElement($this->options);
    }
    public function addString($string) {
        $this->options = ArrayModifier::Add($this->options, $string);
        $this->length = count($string);
    }
    public function removeString($string) {
        $this->options = ArrayModifier::Remove($this->options, $string);
        $this->length = count($string);
    }
}
abstract class RandomStringFactory {
    public static function make($options) {
        return new RandomString(self::makeOptions($options));
    }
    protected static function makeOptions($options) {
        if (is_array($options)) {
            return $options;
        } elseif (is_string($options)) {
            $delimiter = self::detectDelimiter($options);
            return explode($delimiter, $options);
        } else {
            return (array) $options;
        }
    }
    protected static function detectDelimiter($string) {
        $delims = array(',', ';', ':', '|', '&', '-', '+', '!');
        foreach ($delims as $delim) {
            if (strpos($string, $delim)) {
                return $delim;
            }
        }
        return ' ';
    }
}
class RandomEnd extends RandomString {
    public function __construct() {
        parent::__construct(explode(',', 'Morning,Afternoon,Evening,Night'));
    }
}

abstract class Random {
    public static function getArrayElement(array $array) {
        $length = count($array);
        $key = self::getRandom(0, $length - 1);
        $i = 0;
        foreach ($array as $value) {
            if ($i == $key) {
                return $value;
            }
            $i++;
        }
        return end($array);
    }
    public static function getRandom($start, $end) {
        $diff = $end - $start;
        $seed = self::getRandomSeed();
        return round($seed * $diff + $start);
    }
    public static function getRandomSeed() {
        $t = microtime(true);
        for ($i = 0; $i < 10000; $i++) {
            $m = mt_rand(0, 10000);
            if ($t % $m == mt_rand(0, 100)) {
                $factor = ($m - $t) / ($t - $m);
                $seed = mt_rand(0, 100000) / $factor;
                $seed *= ($m - $t);
                $stub = $t * 100000;
                $stub += -1 * $m * 100000;
                $scrum = $seed / $stub;
                return $scrum;
            }
        }
        return mt_rand(0, 10000) / 10000;
    }
}
class ArrayModifier {
    public static function add(&$array, $value) {
        $newArray = $array;
        $newArray[] = $value;
        return $newArray;
    }
    public static function remove(&$array, $value) {
        $newArray = array();
        foreach ($array as $key => &$val) {
            if ($val == $value) {
                unset($array[$key]);
            }
            $newArray[$key] = $val;
        }
        return $newArray;
    }
}

class RandomSentance {
    protected $elements = array();
    public function __construct(array $elements) {
        $this->elements = $elements;
    }
    public function __toString() {
        return implode(' ', $this->elements);
    }
}
$sentance = new RandomSentance(
    array(
        RandomStringFactory::make('Good'),
        RandomStringFactory::make('Morning,Afternoon,Night,Evening'),
    )
);
echo $sentance;
查看更多
叼着烟拽天下
7楼-- · 2019-03-08 00:04

Here we are. I use recursion, express all my numbers in their factorized form, and only output one char at a time. Unfortunately, the random number generator is simple.

#include <iostream>
#include <ctime>

using namespace std;

void startPutCharSequence(double currChar, double prevChar, double firstChar)
{
    if (prevChar == 0)
    {
        putchar(currChar);

        if(currChar / 11 == 7)
            startPutCharSequence(3 * 37, currChar, currChar);
        else if (currChar / 23 == 3)
            startPutCharSequence(2 * 59, currChar, currChar);
        else if (currChar / 13 == 5)
            startPutCharSequence(2 * 3 * 17, currChar, currChar);
        else if (currChar / 13 == 2 * 3)
            startPutCharSequence(3 * 5 * 7, currChar, currChar);
    }
    else if (prevChar != 0 && currChar != 3)
    {
        putchar(currChar);

        if(currChar == 3 * 37 && prevChar == 7 * 11 && firstChar == 7 * 11)
            startPutCharSequence(2 * 3 * 19, currChar, firstChar);
        else if (currChar == 2 * 3 * 19 && prevChar == 3 * 37 && firstChar == 7 * 11)
            startPutCharSequence(2 * 5 * 11, currChar, firstChar);
        else if (currChar == 2 * 5 * 11 && prevChar == 2 * 3 * 19 && firstChar == 7 * 11)
            startPutCharSequence(3 * 5 * 7, currChar, firstChar);
        else if (currChar == 3 * 5 * 7 && prevChar == 2 * 5 * 11 && firstChar == 7 * 11)
            startPutCharSequence(2 * 5 * 11, currChar, firstChar);
        else if (currChar == 2 * 5 * 11 && prevChar == 3 * 5 * 7 && firstChar ==7 * 11)
            startPutCharSequence(103, 0 , 3);

        else if(currChar == 2 * 59 && prevChar == 23 * 3 && firstChar == 23 * 3)
            startPutCharSequence(101, currChar, firstChar);
        else if(currChar == 101 && prevChar == 2 * 59 && firstChar == 23 * 3)
            startPutCharSequence(2 * 5 * 11, currChar, firstChar);
        else if(currChar == 2 * 5 * 11 && prevChar == 101 && firstChar == 23 * 3)
            startPutCharSequence(3 * 5 * 7, currChar, firstChar);
        else if(currChar == 3 * 5 * 7 && prevChar == 2 * 5 * 11 && firstChar == 23 * 3)
            startPutCharSequence(2 * 5 * 11, currChar, firstChar);
        else if(currChar == 2 * 5 * 11 && prevChar == 3 * 5 * 7 && firstChar == 23 * 3)
            startPutCharSequence(103, 0, 3);

        else if(currChar == 2 * 3 * 17 && prevChar == 13 * 5 && firstChar == 13 * 5)
            startPutCharSequence(2 * 2 * 29, currChar, firstChar);
        else if(currChar == 2 * 2 * 29 && prevChar == 2 * 3 * 17 && firstChar == 13 * 5)
            startPutCharSequence(101, currChar, firstChar);
        else if(currChar == 101 && prevChar == 2 * 2 * 29 && firstChar == 13 * 5)
            startPutCharSequence(2 * 3 * 19, currChar, firstChar);
        else if(currChar == 2 * 3 * 19 && prevChar == 101 && firstChar == 13 * 5)
            startPutCharSequence(2 * 5 * 11, currChar, firstChar);
        else if(currChar == 2 * 5 * 11 && prevChar == 2 * 3 * 19 && firstChar == 13 * 5)
            startPutCharSequence(3 * 37, currChar, firstChar);
        else if(currChar == 3 * 37 && prevChar == 2 * 5 * 11 && firstChar == 13 * 5)
            startPutCharSequence(3 * 37, currChar, firstChar);
        else if(currChar == 3 * 37 && prevChar == 3 * 37 && firstChar == 13 * 5)
            startPutCharSequence(2 * 5 * 11, 0, 3);

        else if(currChar == 3 * 5 * 7 && prevChar == 2 * 3 * 13 && firstChar == 2 * 3 * 13)
            startPutCharSequence(103, currChar, firstChar);
        else if(currChar == 103 && prevChar == 3 * 5 * 7 && firstChar == 2 * 3 * 13)
            startPutCharSequence(2 * 2 * 2 * 13, currChar, firstChar);
        else if(currChar == 2 * 2 * 2 * 13 && prevChar == 103 && firstChar == 2 * 3 * 13)
            startPutCharSequence(2 * 2 * 29, 0, 3);
    }
}

int main()
{
    int start = 2 * 2 * 2 * 3 * 3;

    putchar(--start);
    putchar((start += (2 * 2 * 2 * 5)));
    putchar(start);
    putchar((start -= 11));
    putchar((start -= 2 * 2 * 17));

    srand((unsigned)time(0));

    while(true)
    {
        int timeOfDay = rand() % 128;

        if (timeOfDay == 11 * 7 || timeOfDay == 13 * 5 || timeOfDay == 3 * 23 || timeOfDay == 2 * 3 * 13)
        {
            startPutCharSequence(timeOfDay, 0, timeOfDay);
            break;
        }
    }

    putchar((start -= 19));
    putchar((start -= 3));

    return 0;
}
查看更多
登录 后发表回答