SHARE
TWEET

Untitled

a guest Sep 20th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var a;
  2. var g;
  3. var _g = depth => (g = (n = 0, p = 0.1666) => (a = Math.random()) >= (1 - p) || n >= depth ? { type : 'num', value: Math.floor(Math.random() * 100 - 50) } :
  4. (s => { switch (s) {
  5.     case 0: return { type: 'plus', value: [g(n+1), g(n+1)] };
  6.     case 1: return { type: 'minus', value: [g(n+1), g(n+1)] };
  7.     case 2: return { type: 'mult', value: [g(n+1), g(n+1)] };
  8.     case 3: return { type: 'divide', value: [g(n+1), g(n+1)] };
  9.     case 4: return { type: 'max', value: Array.from(Array(Math.floor(Math.random() * 5)).keys()).map(() => g(n+1)) };
  10. }})(Math.floor(a / ((1 - p) / 5))));
  11.  
  12. var f = g => [
  13.  
  14. (function toString(e) {
  15.     switch (e.type) {
  16.         case 'num': return `NUM (${e.value})`;
  17.         case 'plus': return `PLUS (${toString(e.value[0])}, ${toString(e.value[1])})`;
  18.         case 'minus': return `MINUS (${toString(e.value[0])}, ${toString(e.value[1])})`;
  19.         case 'mult': return `MULT (${toString(e.value[0])}, ${toString(e.value[1])})`;
  20.         case 'divide': return `DIVIDE (${toString(e.value[0])}, ${toString(e.value[1])})`;
  21.         case 'max': return `MAX [${e.value.map(v => toString(v)).join('; ')}]`
  22.     }
  23. })(g),
  24. (function eval(e) {
  25.     switch (e.type) {
  26.         case 'num': return e.value;
  27.         case 'plus': return eval(e.value[0]) + eval(e.value[1]);
  28.         case 'minus': return eval(e.value[0]) - eval(e.value[1]);
  29.         case 'mult': return eval(e.value[0]) * eval(e.value[1]);
  30.         case 'divide': var l = eval(e.value[1]); if (l === 0) throw Error('division by zero');
  31.             var answer = eval(e.value[0]) / eval(e.value[1])
  32.             return answer > 0 ? Math.floor(answer) : Math.ceil(answer);
  33.         case 'max': return e.value.length ? Math.max(... e.value.map(v => eval(v))) : 0
  34.     }
  35. })(g),
  36. g
  37.  
  38. ];
  39.  
  40. function generate(depth = 5) {
  41.     var i;
  42.     do {
  43.         try {
  44.             i = f(_g(depth)());
  45.         } catch (err) {
  46.             i = null;
  47.         }
  48.     } while (i === null);
  49.     return i;
  50. }
  51.  
  52. Array.from(Array(10).keys()).map(() => generate(1)).map(([e, a]) => `EVAL (${e}, "${e}", ${a})`).join('\n; ');
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top