Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ՐՏ_Iterable(iterable) {
- if (Array.isArray(iterable) || iterable instanceof String || typeof iterable === "string") {
- return iterable;
- }
- return Object.keys(iterable);
- }
- function ՐՏ_bind(fn, thisArg) {
- var ret;
- if (fn.orig) {
- fn = fn.orig;
- }
- if (thisArg === false) {
- return fn;
- }
- ret = function() {
- return fn.apply(thisArg, arguments);
- };
- ret.orig = fn;
- return ret;
- }
- function range(start, stop, step) {
- var length, idx, range;
- if (arguments.length <= 1) {
- stop = start || 0;
- start = 0;
- }
- step = arguments[2] || 1;
- length = Math.max(Math.ceil((stop - start) / step), 0);
- idx = 0;
- range = new Array(length);
- while (idx < length) {
- range[idx++] = start;
- start += step;
- }
- return range;
- }
- function len(obj) {
- if (Array.isArray(obj) || typeof obj === "string") {
- return obj.length;
- }
- return Object.keys(obj).length;
- }
- function eq(a, b) {
- var i;
- "\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 ";
- if (a === b) {
- return true;
- }
- if (Array.isArray(a) && Array.isArray(b) || a instanceof Object && b instanceof Object) {
- if (a.constructor !== b.constructor || a.length !== b.length) {
- return false;
- }
- if (Array.isArray(a)) {
- for (i = 0; i < len(a); i++) {
- if (!eq(a[i], b[i])) {
- return false;
- }
- }
- } else {
- var ՐՏ_Iter0 = ՐՏ_Iterable(a);
- for (var ՐՏ_Index0 = 0; ՐՏ_Index0 < ՐՏ_Iter0.length; ՐՏ_Index0++) {
- i = ՐՏ_Iter0[ՐՏ_Index0];
- if (!eq(a[i], b[i])) {
- return false;
- }
- }
- }
- return true;
- }
- return false;
- }
- function ՐՏ_in(val, arr) {
- if (Array.isArray(arr) || typeof arr === "string") {
- return arr.indexOf(val) !== -1;
- } else {
- if (arr.hasOwnProperty(val)) {
- return true;
- }
- return false;
- }
- }
- function dir(item) {
- var arr;
- arr = [];
- for (var i in item) {
- arr.push(i);
- }
- return arr;
- }
- function ՐՏ_extends(child, parent) {
- child.prototype = Object.create(parent.prototype);
- child.prototype.constructor = child;
- }
- function ValueError() {
- ValueError.prototype.__init__.apply(this, arguments);
- }
- ՐՏ_extends(ValueError, Error);
- function __init__(self, message) {
- var self = this;
- self.name = "ValueError";
- self.message = message;
- }
- ValueError.prototype.__init__ = function __init__(message){
- var self = this;
- self.name = "ValueError";
- self.message = message;
- };
- function sum(arr, start) {
- if (typeof start === "undefined") start = 0;
- return arr.reduce(function(prev, cur) {
- return prev + cur;
- }, start);
- }
- function ՐՏ_print() {
- if (typeof console === "object") {
- console.log.apply(console, arguments);
- }
- }
- function ՐՏ_eslice(arr, step, start, end) {
- var isString;
- arr = arr.slice(0);
- if (typeof arr === "string" || arr instanceof String) {
- isString = true;
- arr = arr.split("");
- }
- if (step < 0) {
- step = -step;
- arr.reverse();
- if (typeof start !== "undefined") {
- start = arr.length - start - 1;
- }
- if (typeof end !== "undefined") {
- end = arr.length - end - 1;
- }
- }
- if (typeof start === "undefined") {
- start = 0;
- }
- if (typeof end === "undefined") {
- end = arr.length;
- }
- arr = arr.slice(start, end).filter(function(e, i) {
- return i % step === 0;
- });
- return isString ? arr.join("") : arr;
- }
- var ՐՏ_modules = {};
- ՐՏ_modules["stdlib"] = {};
- (function(){
- var __name__ = "stdlib";
- var str;
- str = JSON.stringify;
- String.prototype.find = String.prototype.indexOf;
- String.prototype.strip = String.prototype.trim;
- String.prototype.lstrip = String.prototype.trimLeft;
- String.prototype.rstrip = String.prototype.trimRight;
- String.prototype.join = function(iterable) {
- return iterable.join(this);
- };
- String.prototype.zfill = function(size) {
- var s;
- s = this;
- while (s.length < size) {
- s = "0" + s;
- }
- return s;
- };
- function list(iterable) {
- if (typeof iterable === "undefined") iterable = [];
- var result, i;
- result = [];
- var ՐՏ_Iter1 = ՐՏ_Iterable(iterable);
- for (var ՐՏ_Index1 = 0; ՐՏ_Index1 < ՐՏ_Iter1.length; ՐՏ_Index1++) {
- i = ՐՏ_Iter1[ՐՏ_Index1];
- result.append(i);
- }
- return result;
- }
- Array.prototype.append = Array.prototype.push;
- Array.prototype.find = Array.prototype.indexOf;
- Array.prototype.index = function(index) {
- var val;
- val = this.find(index);
- if (val === -1) {
- throw new ValueError(str(index) + " is not in list");
- }
- return val;
- };
- Array.prototype.insert = function(index, item) {
- this.splice(index, 0, item);
- };
- Array.prototype.pop = function(index) {
- if (typeof index === "undefined") index = this.length - 1;
- return this.splice(index, 1)[0];
- };
- Array.prototype.extend = function(array2) {
- this.push.apply(this, array2);
- };
- Array.prototype.remove = function(item) {
- var index;
- index = this.find(item);
- this.splice(index, 1);
- };
- Array.prototype.copy = function() {
- return this.slice(0);
- };
- function dict(iterable) {
- var result, key;
- result = {};
- var ՐՏ_Iter2 = ՐՏ_Iterable(iterable);
- for (var ՐՏ_Index2 = 0; ՐՏ_Index2 < ՐՏ_Iter2.length; ՐՏ_Index2++) {
- key = ՐՏ_Iter2[ՐՏ_Index2];
- result[key] = iterable[key];
- }
- return result;
- }
- if (typeof Object.getOwnPropertyNames !== "function") {
- dict.keys = function(hash) {
- var keys;
- keys = [];
- for (var x in hash) {
- if (hash.hasOwnProperty(x)) {
- keys.push(x);
- }
- }
- ;
- return keys;
- };
- } else {
- dict.keys = function(hash) {
- return Object.getOwnPropertyNames(hash);
- };
- }
- dict.values = function(hash) {
- var vals, key;
- vals = [];
- var ՐՏ_Iter3 = ՐՏ_Iterable(dict.keys(hash));
- for (var ՐՏ_Index3 = 0; ՐՏ_Index3 < ՐՏ_Iter3.length; ՐՏ_Index3++) {
- key = ՐՏ_Iter3[ՐՏ_Index3];
- vals.append(hash[key]);
- }
- return vals;
- };
- dict.items = function(hash) {
- var items, key;
- items = [];
- var ՐՏ_Iter4 = ՐՏ_Iterable(dict.keys(hash));
- for (var ՐՏ_Index4 = 0; ՐՏ_Index4 < ՐՏ_Iter4.length; ՐՏ_Index4++) {
- key = ՐՏ_Iter4[ՐՏ_Index4];
- items.append([key, hash[key]]);
- }
- return items;
- };
- dict.copy = dict;
- dict.clear = function(hash) {
- var key;
- var ՐՏ_Iter5 = ՐՏ_Iterable(dict.keys(hash));
- for (var ՐՏ_Index5 = 0; ՐՏ_Index5 < ՐՏ_Iter5.length; ՐՏ_Index5++) {
- key = ՐՏ_Iter5[ՐՏ_Index5];
- delete hash[key];
- }
- };
- ՐՏ_modules["stdlib"]["str"] = str;
- ՐՏ_modules["stdlib"]["list"] = list;
- ՐՏ_modules["stdlib"]["dict"] = dict;
- })();
- var __name__ = "__main__";
- var stdlib = ՐՏ_modules["stdlib"];
- function list(a) {
- return a;
- }
- var clock = function(){return (new Date()).getTime()/1000;};
- DEFAULT_ARG = 8;
- function main() {
- var times, t0, res, tk, i, avg;
- times = [];
- for (i = 0; i < 4; i++) {
- t0 = clock();
- res = fannkuch(DEFAULT_ARG);
- tk = clock();
- times.append(tk - t0);
- }
- avg = sum(times) / len(times);
- ՐՏ_print(avg);
- }
- function fannkuch(n) {
- var count, perm1, m, check, perm, flips_count, k, max_flips, do_return, r;
- count = list(range(1, n + 1));
- perm1 = list(range(n));
- perm = list(range(n));
- max_flips = 0;
- m = n - 1;
- r = n;
- check = 0;
- while (true) {
- if (check < 30) {
- check += 1;
- }
- while (r !== 1) {
- count[r - 1] = r;
- r -= 1;
- }
- if (perm1[0] !== 0 && perm1[m] !== m) {
- perm = perm1.slice(0);
- flips_count = 0;
- k = perm[0];
- while (k !== 0) {
- [].splice.apply(perm, [0, k + 1-0].concat(ՐՏ_eslice(perm, -1, k)));
- flips_count += 1;
- k = perm[0];
- }
- if (flips_count > max_flips) {
- max_flips = flips_count;
- }
- }
- do_return = true;
- while (r !== n) {
- perm1.insert(r, perm1.pop(0));
- count[r] -= 1;
- if (count[r] > 0) {
- do_return = false;
- break;
- }
- r += 1;
- }
- if (do_return) {
- return max_flips;
- }
- }
- }
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement