Advertisement
Guest User

rapydscript fannkuch

a guest
Sep 20th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ՐՏ_Iterable(iterable) {
  2.     if (Array.isArray(iterable) || iterable instanceof String || typeof iterable === "string") {
  3.         return iterable;
  4.     }
  5.     return Object.keys(iterable);
  6. }
  7. function ՐՏ_bind(fn, thisArg) {
  8.     var ret;
  9.     if (fn.orig) {
  10.         fn = fn.orig;
  11.     }
  12.     if (thisArg === false) {
  13.         return fn;
  14.     }
  15.     ret = function() {
  16.         return fn.apply(thisArg, arguments);
  17.     };
  18.     ret.orig = fn;
  19.     return ret;
  20. }
  21. function range(start, stop, step) {
  22.     var length, idx, range;
  23.     if (arguments.length <= 1) {
  24.         stop = start || 0;
  25.         start = 0;
  26.     }
  27.     step = arguments[2] || 1;
  28.     length = Math.max(Math.ceil((stop - start) / step), 0);
  29.     idx = 0;
  30.     range = new Array(length);
  31.     while (idx < length) {
  32.         range[idx++] = start;
  33.         start += step;
  34.     }
  35.     return range;
  36. }
  37. function len(obj) {
  38.     if (Array.isArray(obj) || typeof obj === "string") {
  39.         return obj.length;
  40.     }
  41.     return Object.keys(obj).length;
  42. }
  43. function eq(a, b) {
  44.     var i;
  45.     "\n    Equality comparison that works with all data types, returns true if structure and\n    contents of first object equal to those of second object\n\n    Arguments:\n        a: first object\n        b: second object\n    ";
  46.     if (a === b) {
  47.         return true;
  48.     }
  49.     if (Array.isArray(a) && Array.isArray(b) || a instanceof Object && b instanceof Object) {
  50.         if (a.constructor !== b.constructor || a.length !== b.length) {
  51.             return false;
  52.         }
  53.         if (Array.isArray(a)) {
  54.             for (i = 0; i < len(a); i++) {
  55.                 if (!eq(a[i], b[i])) {
  56.                     return false;
  57.                 }
  58.             }
  59.         } else {
  60.             var ՐՏ_Iter0 = ՐՏ_Iterable(a);
  61.             for (var ՐՏ_Index0 = 0; ՐՏ_Index0 < ՐՏ_Iter0.length; ՐՏ_Index0++) {
  62.                 i = ՐՏ_Iter0[ՐՏ_Index0];
  63.                 if (!eq(a[i], b[i])) {
  64.                     return false;
  65.                 }
  66.             }
  67.         }
  68.         return true;
  69.     }
  70.     return false;
  71. }
  72. function ՐՏ_in(val, arr) {
  73.     if (Array.isArray(arr) || typeof arr === "string") {
  74.         return arr.indexOf(val) !== -1;
  75.     } else {
  76.         if (arr.hasOwnProperty(val)) {
  77.             return true;
  78.         }
  79.         return false;
  80.     }
  81. }
  82. function dir(item) {
  83.     var arr;
  84.     arr = [];
  85.     for (var i in item) {
  86.         arr.push(i);
  87.     }
  88.     return arr;
  89. }
  90. function ՐՏ_extends(child, parent) {
  91.     child.prototype = Object.create(parent.prototype);
  92.     child.prototype.constructor = child;
  93. }
  94. function ValueError() {
  95.     ValueError.prototype.__init__.apply(this, arguments);
  96. }
  97. ՐՏ_extends(ValueError, Error);
  98. function __init__(self, message) {
  99.     var self = this;
  100.     self.name = "ValueError";
  101.     self.message = message;
  102. }
  103. ValueError.prototype.__init__ = function __init__(message){
  104.     var self = this;
  105.     self.name = "ValueError";
  106.     self.message = message;
  107. };
  108.  
  109. function sum(arr, start) {
  110.     if (typeof start === "undefined") start = 0;
  111.     return arr.reduce(function(prev, cur) {
  112.         return prev + cur;
  113.     }, start);
  114. }
  115. function ՐՏ_print() {
  116.     if (typeof console === "object") {
  117.         console.log.apply(console, arguments);
  118.     }
  119. }
  120. function ՐՏ_eslice(arr, step, start, end) {
  121.     var isString;
  122.     arr = arr.slice(0);
  123.     if (typeof arr === "string" || arr instanceof String) {
  124.         isString = true;
  125.         arr = arr.split("");
  126.     }
  127.     if (step < 0) {
  128.         step = -step;
  129.         arr.reverse();
  130.         if (typeof start !== "undefined") {
  131.             start = arr.length - start - 1;
  132.         }
  133.         if (typeof end !== "undefined") {
  134.             end = arr.length - end - 1;
  135.         }
  136.     }
  137.     if (typeof start === "undefined") {
  138.         start = 0;
  139.     }
  140.     if (typeof end === "undefined") {
  141.         end = arr.length;
  142.     }
  143.     arr = arr.slice(start, end).filter(function(e, i) {
  144.         return i % step === 0;
  145.     });
  146.     return isString ? arr.join("") : arr;
  147. }
  148. var ՐՏ_modules = {};
  149. ՐՏ_modules["stdlib"] = {};
  150.  
  151. (function(){
  152.     var __name__ = "stdlib";
  153.     var str;
  154.     str = JSON.stringify;
  155.     String.prototype.find = String.prototype.indexOf;
  156.     String.prototype.strip = String.prototype.trim;
  157.     String.prototype.lstrip = String.prototype.trimLeft;
  158.     String.prototype.rstrip = String.prototype.trimRight;
  159.     String.prototype.join = function(iterable) {
  160.         return iterable.join(this);
  161.     };
  162.     String.prototype.zfill = function(size) {
  163.         var s;
  164.         s = this;
  165.         while (s.length < size) {
  166.             s = "0" + s;
  167.         }
  168.         return s;
  169.     };
  170.     function list(iterable) {
  171.         if (typeof iterable === "undefined") iterable = [];
  172.         var result, i;
  173.         result = [];
  174.         var ՐՏ_Iter1 = ՐՏ_Iterable(iterable);
  175.         for (var ՐՏ_Index1 = 0; ՐՏ_Index1 < ՐՏ_Iter1.length; ՐՏ_Index1++) {
  176.             i = ՐՏ_Iter1[ՐՏ_Index1];
  177.             result.append(i);
  178.         }
  179.         return result;
  180.     }
  181.     Array.prototype.append = Array.prototype.push;
  182.     Array.prototype.find = Array.prototype.indexOf;
  183.     Array.prototype.index = function(index) {
  184.         var val;
  185.         val = this.find(index);
  186.         if (val === -1) {
  187.             throw new ValueError(str(index) + " is not in list");
  188.         }
  189.         return val;
  190.     };
  191.     Array.prototype.insert = function(index, item) {
  192.         this.splice(index, 0, item);
  193.     };
  194.     Array.prototype.pop = function(index) {
  195.         if (typeof index === "undefined") index = this.length - 1;
  196.         return this.splice(index, 1)[0];
  197.     };
  198.     Array.prototype.extend = function(array2) {
  199.         this.push.apply(this, array2);
  200.     };
  201.     Array.prototype.remove = function(item) {
  202.         var index;
  203.         index = this.find(item);
  204.         this.splice(index, 1);
  205.     };
  206.     Array.prototype.copy = function() {
  207.         return this.slice(0);
  208.     };
  209.     function dict(iterable) {
  210.         var result, key;
  211.         result = {};
  212.         var ՐՏ_Iter2 = ՐՏ_Iterable(iterable);
  213.         for (var ՐՏ_Index2 = 0; ՐՏ_Index2 < ՐՏ_Iter2.length; ՐՏ_Index2++) {
  214.             key = ՐՏ_Iter2[ՐՏ_Index2];
  215.             result[key] = iterable[key];
  216.         }
  217.         return result;
  218.     }
  219.     if (typeof Object.getOwnPropertyNames !== "function") {
  220.         dict.keys = function(hash) {
  221.             var keys;
  222.             keys = [];
  223.            
  224.         for (var x in hash) {
  225.             if (hash.hasOwnProperty(x)) {
  226.                 keys.push(x);
  227.             }
  228.         }
  229.         ;
  230.             return keys;
  231.         };
  232.     } else {
  233.         dict.keys = function(hash) {
  234.             return Object.getOwnPropertyNames(hash);
  235.         };
  236.     }
  237.     dict.values = function(hash) {
  238.         var vals, key;
  239.         vals = [];
  240.         var ՐՏ_Iter3 = ՐՏ_Iterable(dict.keys(hash));
  241.         for (var ՐՏ_Index3 = 0; ՐՏ_Index3 < ՐՏ_Iter3.length; ՐՏ_Index3++) {
  242.             key = ՐՏ_Iter3[ՐՏ_Index3];
  243.             vals.append(hash[key]);
  244.         }
  245.         return vals;
  246.     };
  247.     dict.items = function(hash) {
  248.         var items, key;
  249.         items = [];
  250.         var ՐՏ_Iter4 = ՐՏ_Iterable(dict.keys(hash));
  251.         for (var ՐՏ_Index4 = 0; ՐՏ_Index4 < ՐՏ_Iter4.length; ՐՏ_Index4++) {
  252.             key = ՐՏ_Iter4[ՐՏ_Index4];
  253.             items.append([key, hash[key]]);
  254.         }
  255.         return items;
  256.     };
  257.     dict.copy = dict;
  258.     dict.clear = function(hash) {
  259.         var key;
  260.         var ՐՏ_Iter5 = ՐՏ_Iterable(dict.keys(hash));
  261.         for (var ՐՏ_Index5 = 0; ՐՏ_Index5 < ՐՏ_Iter5.length; ՐՏ_Index5++) {
  262.             key = ՐՏ_Iter5[ՐՏ_Index5];
  263.             delete hash[key];
  264.         }
  265.     };
  266.     ՐՏ_modules["stdlib"]["str"] = str;
  267.  
  268.     ՐՏ_modules["stdlib"]["list"] = list;
  269.  
  270.     ՐՏ_modules["stdlib"]["dict"] = dict;
  271. })();
  272.  
  273. var __name__ = "__main__";
  274.  
  275. var stdlib = ՐՏ_modules["stdlib"];
  276.  
  277. function list(a) {
  278.     return a;
  279. }
  280. var clock = function(){return (new Date()).getTime()/1000;};
  281. DEFAULT_ARG = 8;
  282. function main() {
  283.     var times, t0, res, tk, i, avg;
  284.     times = [];
  285.     for (i = 0; i < 4; i++) {
  286.         t0 = clock();
  287.         res = fannkuch(DEFAULT_ARG);
  288.         tk = clock();
  289.         times.append(tk - t0);
  290.     }
  291.     avg = sum(times) / len(times);
  292.     ՐՏ_print(avg);
  293. }
  294. function fannkuch(n) {
  295.     var count, perm1, m, check, perm, flips_count, k, max_flips, do_return, r;
  296.     count = list(range(1, n + 1));
  297.     perm1 = list(range(n));
  298.     perm = list(range(n));
  299.     max_flips = 0;
  300.     m = n - 1;
  301.     r = n;
  302.     check = 0;
  303.     while (true) {
  304.         if (check < 30) {
  305.             check += 1;
  306.         }
  307.         while (r !== 1) {
  308.             count[r - 1] = r;
  309.             r -= 1;
  310.         }
  311.         if (perm1[0] !== 0 && perm1[m] !== m) {
  312.             perm = perm1.slice(0);
  313.             flips_count = 0;
  314.             k = perm[0];
  315.             while (k !== 0) {
  316.                 [].splice.apply(perm, [0, k + 1-0].concat(ՐՏ_eslice(perm, -1, k)));
  317.                 flips_count += 1;
  318.                 k = perm[0];
  319.             }
  320.             if (flips_count > max_flips) {
  321.                 max_flips = flips_count;
  322.             }
  323.         }
  324.         do_return = true;
  325.         while (r !== n) {
  326.             perm1.insert(r, perm1.pop(0));
  327.             count[r] -= 1;
  328.             if (count[r] > 0) {
  329.                 do_return = false;
  330.                 break;
  331.             }
  332.             r += 1;
  333.         }
  334.         if (do_return) {
  335.             return max_flips;
  336.         }
  337.     }
  338. }
  339. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement