Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var __$UID$__=0;
- __NULL_OBJECT__ = Object.create(null);
- __WEBWORKER__ = false;
- __NODEJS__ = false;
- __BROWSER__ = false;
- if (typeof(process) !== "undefined")
- {
- __NODEJS__ = true;
- }
- if (typeof(window) !== "undefined")
- {
- __BROWSER__ = true;
- }
- if (typeof(importScripts) === "function")
- {
- __WEBWORKER__ = true;
- }
- if ((! (__NODEJS__) && ! (__WEBWORKER__)))
- {
- if (typeof(HTMLDocument) === "undefined")
- {
- HTMLDocument = Document;
- }
- }
- var __$UID$__ = 1;
- IndexError = function(msg) {this.message = msg || "";}; IndexError.prototype = Object.create(Error.prototype); IndexError.prototype.name = "IndexError";
- KeyError = function(msg) {this.message = msg || "";}; KeyError.prototype = Object.create(Error.prototype); KeyError.prototype.name = "KeyError";
- ValueError = function(msg) {this.message = msg || "";}; ValueError.prototype = Object.create(Error.prototype); ValueError.prototype.name = "ValueError";
- AttributeError = function(msg) {this.message = msg || "";}; AttributeError.prototype = Object.create(Error.prototype);AttributeError.prototype.name = "AttributeError";
- RuntimeError = function(msg) {this.message = msg || "";}; RuntimeError.prototype = Object.create(Error.prototype);RuntimeError.prototype.name = "RuntimeError";
- WebWorkerError = function(msg) {this.message = msg || "";}; WebWorkerError.prototype = Object.create(Error.prototype);WebWorkerError.prototype.name = "WebWorkerError";
- TypeError = function(msg) {this.message = msg || "";}; TypeError.prototype = Object.create(Error.prototype);TypeError.prototype.name = "TypeError";
- var __invalid_call__ = function __invalid_call__(msg, args)
- {
- console.log("[INVALID CALL ARGUMENTS]");
- if (typeof(args) !== "undefined")
- {
- var i;
- i = -1;
- while (i++ < args.length)
- {
- console.log((((" argument:" + i) + " -> ") + args[i]));
- }
- }
- throw new RuntimeError(msg);
- }/*end-> `__invalid_call__` */
- var __array_fill__ = function __array_fill__(arr, items)
- {
- var i;
- i = -1;
- while (i++ < items.length)
- {
- arr[i] = items[i];
- }
- return arr;
- }/*end-> `__array_fill__` */
- var isinstance = function isinstance(ob, klass)
- {
- var T;
- if ((typeof(ob) === "undefined" || ob === null))
- {
- return false;
- }
- else
- {
- if (typeof(klass) === "string")
- {
- T = typeof(ob);
- if (T === "number")
- {
- if ((klass === "int" || klass === "π°ππππππ"))
- {
- return true;
- }
- else
- {
- if ((klass === "float" || klass === "πππππ"))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- else
- {
- if (T === "string")
- {
- if ((klass === "string" || klass === "str" || klass === "πΊπππππ"))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- if ((klass === "Array" || klass === "list" || klass === "π³πππ"))
- {
- if (ob instanceof Array)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- if (ob.__class__)
- {
- if (ob.__class__.__name__ === klass)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- }
- }
- }
- else
- {
- if (ob instanceof Array)
- {
- if (klass === list)
- {
- return true;
- }
- else
- {
- if (klass === Array)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- else
- {
- if (ob.__class__)
- {
- return issubclass(ob.__class__, klass);
- }
- else
- {
- if (ob instanceof klass)
- {
- return true;
- }
- else
- {
- if (typeof(ob) === "number")
- {
- if ((klass === int && ob.toString().isdigit()))
- {
- return true;
- }
- else
- {
- if (klass === float)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- else
- {
- if (typeof(ob) === "string")
- {
- if ((klass === str || klass === string))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- }
- }
- }
- }
- }
- }/*end-> `isinstance` */
- var issubclass = function issubclass(C, B)
- {
- if (C === B)
- {
- return true;
- }
- else
- {
- if (C.__bases__)
- {
- for (var __n0 = 0; __n0 < C.__bases__.length; __n0++)
- {
- var base = C.__bases__[ __n0 ];
- if (issubclass(base, B))
- {
- return true;
- }
- }
- }
- }
- return false;
- }/*end-> `issubclass` */
- var len = function len(ob)
- {
- if (ob instanceof Array)
- {
- return ob.length;
- }
- else
- {
- if (__is_typed_array(ob))
- {
- return ob.length;
- }
- else
- {
- if (ob.__len__)
- {
- return ob.__len__();
- }
- else
- {
- if (isNaN(ob))
- {
- throw new RuntimeError("calling `len` with NaN in invalid");
- }
- else
- {
- if (typeof(ob) === "number")
- {
- throw new RuntimeError("calling `len` on a number is invalid");
- }
- else
- {
- return Object.keys(ob).length;
- }
- }
- }
- }
- }
- }/*end-> `len` */
- if (typeof(HTMLElement) !== "undefined")
- {
- HTMLElement.prototype.__right_arrow__ = function __auto_dom__()
- {
- var T;
- for (var __n0 = 0; __n0 < arguments.length; __n0++)
- {
- var item = arguments[ __n0 ];
- T = typeof(item);
- if (item instanceof HTMLElement)
- {
- this.appendChild(item);
- }
- else
- {
- if (item instanceof Text)
- {
- this.appendChild(item);
- }
- else
- {
- if (T === "string")
- {
- this.appendChild(document.createTextNode(item));
- }
- else
- {
- if (T === "function")
- {
- throw new RuntimeError("HTMLElement.__right_arrow__(lambda function) is invalid");
- }
- else
- {
- if (T === "object")
- {
- if (item.nodeType)
- {
- if (item.nodeType === Node.TEXT_NODE)
- {
- this.appendChild(item);
- }
- else
- {
- if (item.nodeType === Node.ELEMENT_NODE)
- {
- this.appendChild(item);
- }
- else
- {
- throw new RuntimeError("HTMLElement unknown node type");
- }
- }
- }
- else
- {
- for (var __n1 = 0; __n1 < __jsdict_keys(item).length; __n1++)
- {
- var key = __jsdict_keys(item)[ __n1 ];
- this.setAttribute(key, item[key]);
- }
- }
- }
- else
- {
- throw new RuntimeError(("HTMLElement.__right_arrow__(invalid type): " + item));
- }
- }
- }
- }
- }
- }
- return this;
- }/*end-> `__auto_dom__` */
- HTMLDocument.prototype.__right_arrow__ = function __htmldoc_rightarrow__(arg)
- {
- if (arg.startswith("#"))
- {
- return this.getElementById(arg.__getslice__(1, undefined, undefined));
- }
- else
- {
- return this.createElement(arg);
- }
- }/*end-> `__htmldoc_rightarrow__` */
- }
- var dict = function dict(d, _kwargs_)
- {
- var msg,pairs;
- var copy = (_kwargs_===undefined || (typeof(_kwargs_)=='object' && _kwargs_.copy===undefined)) ? false : typeof(_kwargs_)=='object'?_kwargs_.copy: __invalid_call__('function `dict` requires named keyword arguments, invalid parameter for `copy`',arguments);
- var keytype = (_kwargs_===undefined || (typeof(_kwargs_)=='object' && _kwargs_.keytype===undefined)) ? null : typeof(_kwargs_)=='object'?_kwargs_.keytype: __invalid_call__('function `dict` requires named keyword arguments, invalid parameter for `keytype`',arguments);
- var valuetype = (_kwargs_===undefined || (typeof(_kwargs_)=='object' && _kwargs_.valuetype===undefined))? null : typeof(_kwargs_)=='object'?_kwargs_.valuetype: __invalid_call__('function `dict` requires named keyword arguments, invalid parameter for `valuetype`',arguments);
- var iterable = (_kwargs_===undefined || (typeof(_kwargs_)=='object' && _kwargs_.iterable===undefined)) ? null : typeof(_kwargs_)=='object'?_kwargs_.iterable: __invalid_call__('function `dict` requires named keyword arguments, invalid parameter for `iterable`',arguments);
- if (d instanceof Array)
- {
- pairs = d;
- d = {};
- for (var __n0 = 0; __n0 < pairs.length; __n0++)
- {
- var pair = pairs[ __n0 ];
- if (keytype !== null)
- {
- if (! (isinstance(pair[0], keytype)))
- {
- msg = __sprintf("INVALID KEY-TYPE: `%s` - expected type `%s`", [typeof(pair[0]), keytype]);
- throw new TypeError(msg);
- }
- }
- if (valuetype !== null)
- {
- if (! (isinstance(pair[1], valuetype)))
- {
- msg = __sprintf("INVALID VALUE-TYPE: `%s` - expected type `%s`", [typeof(pair[1]), valuetype]);
- throw new TypeError(msg);
- }
- }
- d[ pair[0] ] = pair[1];
- }
- }
- if (iterable !== null)
- {
- for (var __n0 = 0; __n0 < iterable.length; __n0++)
- {
- var pair = iterable[ __n0 ];
- if (keytype !== null)
- {
- if (! (isinstance(pair[0], keytype)))
- {
- msg = __sprintf("INVALID KEY-TYPE: `%s` - expected type `%s`", [typeof(pair[0]), keytype]);
- throw new TypeError(msg);
- }
- }
- if (valuetype !== null)
- {
- if (! (isinstance(pair[1], valuetype)))
- {
- msg = __sprintf("INVALID VALUE-TYPE: `%s` - expected type `%s`", [typeof(pair[1]), valuetype]);
- throw new TypeError(msg);
- }
- }
- d[ pair[0] ] = pair[1];
- }
- }
- Object.defineProperty(d, "__class__", { value:dict, enumerable:false });
- if ((keytype !== null || valuetype !== null))
- {
- if (keytype !== null)
- {
- Object.defineProperty(d, "__keytype__", { value:keytype, enumerable:false });
- }
- if (valuetype !== null)
- {
- Object.defineProperty(d, "__valuetype__", { value:valuetype, enumerable:false });
- }
- var __setitem__ = function __setitem__(key, value)
- {
- var msg;
- if (keytype !== null)
- {
- if (! (isinstance(key, keytype)))
- {
- console.log(("TypeError-KEY:" + key));
- msg = __sprintf("INVALID KEY-TYPE: `%s` - expected type `%s`", [typeof(key), keytype]);
- throw new TypeError(msg);
- }
- }
- if (valuetype !== null)
- {
- if (! (isinstance(value, valuetype)))
- {
- console.log(("TypeError-VALUE:" + value));
- msg = __sprintf("INVALID VALUE-TYPE: `%s` - expected type `%s`", [typeof(value), valuetype]);
- throw new TypeError(msg);
- }
- }
- d[key] = value;
- }/*end-> `__setitem__` */
- Object.defineProperty(d, "__setitem__", { value:__setitem__, enumerable:false });
- }
- if (! (copy))
- {
- return d;
- }
- else
- {
- throw new RuntimeError("TODO dict(copyme)");
- }
- }/*end-> `dict` */
- dict.__name__ = "dict";
- var __print__ = function __print__()
- {
- for (var __n0 = 0; __n0 < arguments.length; __n0++)
- {
- var a = arguments[ __n0 ];
- console.log(a);
- }
- }/*end-> `__print__` */
- /* notes:
- . promotes keys to integers, also works on external objects coming from js.
- . Object.keys(ob) traverses the full prototype chain. */
- var __object_keys__ = function __object_keys__(ob)
- {
- var test,arr,iarr,isdigits;
- arr = [];
- if (typeof(ob.__keytype__) !== "undefined")
- {
- if (ob.__keytype__ === "int")
- {
- for (var key in ob) {arr.push(int(key))};
- }
- else
- {
- for (var key in ob) {arr.push(key)};
- }
- return arr;
- }
- else
- {
- isdigits = false;
- test = 0;
- for (var key in ob) { arr.push(key); if (key.isdigit()) {test += 1;} };
- isdigits = test === arr.length;
- if (isdigits)
- {
- iarr = [];
- for (var __n0 = 0; __n0 < arr.length; __n0++)
- {
- var key = arr[ __n0 ];
- iarr.push(int(key));
- }
- return iarr;
- }
- else
- {
- return arr;
- }
- }
- }/*end-> `__object_keys__` */
- var __jsdict_keys = function __jsdict_keys(ob)
- {
- if (typeof(ob.__class__) !== "undefined")
- {
- if (ob.__class__ === dict)
- {
- if ((typeof(ob.__keytype__) !== "undefined" && ob.__keytype__ === "int"))
- {
- return JSON.parse((("[" + __replace_method(Object.keys( ob ).toString(), "\"", "")) + "]"));
- }
- else
- {
- return Object.keys( ob );
- }
- }
- else
- {
- return ob.keys();
- }
- }
- else
- {
- if (ob instanceof Object)
- {
- if ((typeof(ob.keys) !== "undefined" && isinstance(ob.keys, Function)))
- {
- return ob.keys();
- }
- else
- {
- return Object.keys( ob );
- }
- }
- else
- {
- return ob.keys();
- }
- }
- }/*end-> `__jsdict_keys` */
- Function.prototype.redefine = function __redef_function(src)
- {
- if (isinstance(src, Function))
- {
- this.__redef = src;
- this.__recompile = undefined;
- }
- else
- {
- this.__recompile = src;
- }
- }/*end-> `__redef_function` */
- var hasattr = function hasattr(ob, attr)
- {
- if (Object.hasOwnProperty.call(ob, attr))
- {
- return true;
- }
- else
- {
- if (typeof(ob[attr]) !== "undefined")
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }/*end-> `hasattr` */
- var list = function list(ob)
- {
- var a;
- a = [];
- if (typeof(ob) !== "undefined")
- {
- for (var __n0 = 0; __n0 < ob.length; __n0++)
- {
- var e = ob[ __n0 ];
- a.push(e);
- }
- }
- return a;
- }/*end-> `list` */
- var tuple = function tuple(ob)
- {
- var a;
- a = [];
- if (typeof(ob) !== "undefined")
- {
- for (var __n0 = 0; __n0 < ob.length; __n0++)
- {
- var e = ob[ __n0 ];
- a.push(e);
- }
- }
- return a;
- }/*end-> `tuple` */
- String.prototype.__add__ = function __string_add(a)
- {
- return (this + a);
- }/*end-> `__string_add` */
- String.prototype.__mul__ = function __string_multiply(a)
- {
- var out;
- out = "";
- var i;
- i = -1;
- while (i++ < a)
- {
- out += this;
- }
- return out;
- }/*end-> `__string_multiply` */
- String.prototype.__contains__ = function __string_contains(a)
- {
- if (this.indexOf(a) === -1)
- {
- return false;
- }
- else
- {
- return true;
- }
- }/*end-> `__string_contains` */
- String.prototype.__getslice__ = function __string_slice(start, stop, step)
- {
- if ((typeof(start) === "undefined" && typeof(stop) === "undefined" && step === -1))
- {
- return this.split("").reverse().join("");
- }
- else
- {
- if (stop < 0)
- {
- stop = (this.length + stop);
- }
- return this.substring(start, stop);
- }
- }/*end-> `__string_slice` */
- var __lambda__ = function __lambda__()
- {
- return this.split("\n");
- }/*end-> `__lambda__` */
- String.prototype.splitlines = __lambda__;
- var __lambda__ = function __lambda__()
- {
- return this.trim();
- }/*end-> `__lambda__` */
- String.prototype.strip = __lambda__;
- var __lambda__ = function __lambda__()
- {
- return this.length;
- }/*end-> `__lambda__` */
- String.prototype.__len__ = __lambda__;
- String.prototype.startswith = function __string_startswith(a)
- {
- if (this.substring(0, a.length) === a)
- {
- return true;
- }
- else
- {
- return false;
- }
- }/*end-> `__string_startswith` */
- String.prototype.endswith = function __string_endswith(a)
- {
- if (this.substring((this.length - a.length), this.length) === a)
- {
- return true;
- }
- else
- {
- return false;
- }
- }/*end-> `__string_endswith` */
- String.prototype.join = function __string_join(arr)
- {
- var i,out;
- out = "";
- i = 0;
- for (var __n0 = 0; __n0 < arr.length; __n0++)
- {
- var value = arr[ __n0 ];
- out += value;
- i ++;
- if (i < arr.length)
- {
- out += this;
- }
- }
- return out;
- }/*end-> `__string_join` */
- var __lambda__ = function __lambda__()
- {
- return this.toUpperCase();
- }/*end-> `__lambda__` */
- String.prototype.upper = __lambda__;
- var __lambda__ = function __lambda__()
- {
- return this.toLowerCase();
- }/*end-> `__lambda__` */
- String.prototype.lower = __lambda__;
- Number.prototype.__sub__ = function __number_sub(other)
- {
- return (this - other);
- }/*end-> `__number_sub` */
- Number.prototype.__add__ = function __number_add(other)
- {
- return (this + other);
- }/*end-> `__number_add` */
- Number.prototype.__mul__ = function __number_mul(other)
- {
- return (this * other);
- }/*end-> `__number_mul` */
- Number.prototype.__div__ = function __number_div(other)
- {
- return (this / other);
- }/*end-> `__number_div` */
- Number.prototype.__mod__ = function __number_mod(other)
- {
- return (this % other);
- }/*end-> `__number_mod` */
- String.prototype.index = function __string_index(a)
- {
- var i;
- i = this.indexOf(a);
- if (i === -1)
- {
- throw new ValueError((a + " - not in string"));
- }
- return i;
- }/*end-> `__string_index` */
- Array.prototype.equals = function __array_equals(a)
- {
- return JSON.stringify(this) === JSON.stringify(a);
- }/*end-> `__array_equals` */
- Array.prototype.copy = function func()
- {
- var i,arr;
- arr = [];
- i = 0;
- while (i < this.length)
- {
- arr.push(this[i]);
- i ++;
- }
- return arr;
- }/*end-> `func` */
- Array.prototype.__contains__ = function __array_contains(a)
- {
- if (this.indexOf(a) === -1)
- {
- return false;
- }
- else
- {
- return true;
- }
- }/*end-> `__array_contains` */
- Array.prototype.__getslice__ = function __array_getslice(start, stop, step)
- {
- var i,arr,n;
- arr = [];
- start = (start | 0);
- if (typeof(stop) === "undefined")
- {
- stop = this.length;
- }
- if (start < 0)
- {
- start = (this.length + start);
- }
- if (stop < 0)
- {
- stop = (this.length + stop);
- }
- if (typeof(step) === "number")
- {
- if (step < 0)
- {
- i = start;
- while (i >= 0)
- {
- arr.push(this[i]);
- i += step;
- }
- return arr;
- }
- else
- {
- i = start;
- n = stop;
- while (i < n)
- {
- arr.push(this[i]);
- i += step;
- }
- return arr;
- }
- }
- else
- {
- i = start;
- n = stop;
- while (i < n)
- {
- arr.push(this[i]);
- i ++;
- }
- return arr;
- }
- }/*end-> `__array_getslice` */
- Array.prototype.__setslice__ = function __array_setslice(start, stop, step, items)
- {
- var arr;
- if (typeof(start) === "undefined")
- {
- start = 0;
- }
- if (typeof(stop) === "undefined")
- {
- stop = this.length;
- }
- arr = [start, (stop - start)];
- for (var __n0 = 0; __n0 < items.length; __n0++)
- {
- var item = items[ __n0 ];
- arr.push(item);
- }
- this.splice.apply(this, arr);
- }/*end-> `__array_setslice` */
- Array.prototype.append = function __array_append(item)
- {
- this.push(item);
- return this;
- }/*end-> `__array_append` */
- Array.prototype.__mul__ = function __array_mul(n)
- {
- var a;
- a = [];
- var i;
- i = -1;
- while (i++ < n)
- {
- a.extend(this);
- }
- return a;
- }/*end-> `__array_mul` */
- Array.prototype.__add__ = function __array_add(other)
- {
- var a;
- a = [];
- a.extend(this);
- a.extend(other);
- return a;
- }/*end-> `__array_add` */
- Array.prototype.add = Array.prototype.__add__;
- Array.prototype.extend = function __array_extend(other)
- {
- for (var __n0 = 0; __n0 < other.length; __n0++)
- {
- var obj = other[ __n0 ];
- this.push(obj);
- }
- return this;
- }/*end-> `__array_extend` */
- Array.prototype.remove = function __array_remove(item)
- {
- var index;
- index = this.indexOf(item);
- this.splice(index, 1);
- }/*end-> `__array_remove` */
- Array.prototype.insert = function __array_insert(index, obj)
- {
- if (index < 0)
- {
- index = (this.length + index);
- }
- this.splice(index, 0, obj);
- }/*end-> `__array_insert` */
- var __lambda__ = function __lambda__(obj)
- {
- return this.indexOf(obj);
- }/*end-> `__lambda__` */
- Array.prototype.index = __lambda__;
- Array.prototype.count = function __array_count(obj)
- {
- var a;
- a = 0;
- for (var __n0 = 0; __n0 < this.length; __n0++)
- {
- var item = this[ __n0 ];
- if (item === obj)
- {
- a ++;
- }
- }
- return a;
- }/*end-> `__array_count` */
- var __sort_method = function __sort_method(ob)
- {
- if (ob instanceof Array)
- {
- if ((ob.length && typeof(ob[0]) === "number"))
- {
- var f = function f(a, b)
- {
- if (a < b)
- {
- return -1;
- }
- else
- {
- if (a > b)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- }/*end-> `f` */
- ob.sort( f );
- }
- else
- {
- ob.sort();
- }
- }
- else
- {
- return ob.sort();
- }
- }/*end-> `__sort_method` */
- var __contains__ = function __contains__(ob, a)
- {
- var t;
- t = typeof(ob);
- if (t === "string")
- {
- if (ob.indexOf(a) === -1)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else
- {
- if (t === "number")
- {
- throw new TypeError;
- }
- else
- {
- if (__is_typed_array(ob))
- {
- for (var __n0 = 0; __n0 < ob.length; __n0++)
- {
- var x = ob[ __n0 ];
- if (x === a)
- {
- return true;
- }
- }
- return false;
- }
- else
- {
- if ((ob && ob.__contains__))
- {
- return ob.__contains__(a);
- }
- else
- {
- if ((ob instanceof Object && Object.hasOwnProperty.call(ob, a)))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
- }
- }/*end-> `__contains__` */
- __dom_array_types__ = [];
- if (typeof(NodeList) === "function")
- {
- __dom_array_types__ = [NodeList, FileList, DOMStringList, HTMLCollection, SVGNumberList, SVGTransformList];
- if (typeof(DataTransferItemList) === "function")
- {
- __dom_array_types__.push(DataTransferItemList);
- }
- if (typeof(HTMLAllCollection) === "function")
- {
- __dom_array_types__.push(HTMLAllCollection);
- }
- if (typeof(SVGElementInstanceList) === "function")
- {
- __dom_array_types__.push(SVGElementInstanceList);
- }
- if (typeof(ClientRectList) === "function")
- {
- __dom_array_types__.push(ClientRectList);
- }
- }
- var __is_some_array = function __is_some_array(ob)
- {
- if (__dom_array_types__.length > 0)
- {
- for (var __n0 = 0; __n0 < __dom_array_types__.length; __n0++)
- {
- var t = __dom_array_types__[ __n0 ];
- if (ob instanceof t)
- {
- return true;
- }
- }
- }
- return false;
- }/*end-> `__is_some_array` */
- var __is_typed_array = function __is_typed_array(ob)
- {
- if ((ob instanceof Int16Array || ob instanceof Uint16Array))
- {
- return true;
- }
- else
- {
- if ((ob instanceof Int32Array || ob instanceof Uint32Array))
- {
- return true;
- }
- else
- {
- if ((ob instanceof Float32Array || ob instanceof Float64Array))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }/*end-> `__is_typed_array` */
- var __js_typed_array = function __js_typed_array(t, a)
- {
- var arr;
- if (t === "i")
- {
- arr = new Int32Array(a.length);
- }
- arr.set(a);
- return arr;
- }/*end-> `__js_typed_array` */
- var __sprintf = function __sprintf(fmt, args)
- {
- var chunks,i,arr,item;
- if (args instanceof Array)
- {
- chunks = fmt.split("%s");
- arr = [];
- i = 0;
- for (var __n0 = 0; __n0 < chunks.length; __n0++)
- {
- var txt = chunks[ __n0 ];
- arr.append(txt);
- if (i >= args.length)
- {
- break;
- }
- item = args[i];
- if (typeof(item) === "string")
- {
- arr.append(item);
- }
- else
- {
- if (typeof(item) === "number")
- {
- arr.append(("" + item));
- }
- else
- {
- arr.append(Object.prototype.toString.call(item));
- }
- }
- i ++;
- }
- return "".join(arr);
- }
- else
- {
- return fmt.replace('%s', args);
- }
- }/*end-> `__sprintf` */
- var __jsdict = function __jsdict(items)
- {
- var d,key;
- d = {};
- for (var __n0 = 0; __n0 < items.length; __n0++)
- {
- var item = items[ __n0 ];
- key = item[0];
- if (key instanceof Array)
- {
- key = JSON.stringify(key);
- }
- else
- {
- if (key.__uid__)
- {
- key = key.__uid__;
- }
- }
- d[key] = item[1];
- }
- return d;
- }/*end-> `__jsdict` */
- var __jsdict_get = function __jsdict_get(ob, key, default_value)
- {
- var v;
- if (ob.__class__ === dict)
- {
- v = ob[key];
- if ((typeof(default_value) === "undefined" && typeof(v) === "undefined"))
- {
- console.log("KeyError: key not found in object:");
- console.log(ob);
- throw new KeyError(("invalid key:" + key));
- }
- else
- {
- if (typeof(v) !== "undefined")
- {
- return v;
- }
- else
- {
- return default_value;
- }
- }
- }
- else
- {
- if (typeof(ob.get) === "function")
- {
- return ob.get(key,default_value);
- }
- else
- {
- v = ob.get(key,default_value);
- if ((typeof(default_value) === "undefined" && typeof(v) === "undefined"))
- {
- console.log("KeyError: key not found in object:");
- console.log(ob);
- throw new KeyError(("invalid key:" + key));
- }
- else
- {
- if (typeof(v) !== "undefined")
- {
- return v;
- }
- else
- {
- return default_value;
- }
- }
- }
- }
- }/*end-> `__jsdict_get` */
- var __jsdict_set = function __jsdict_set(ob, key, value)
- {
- if (ob.__class__ === dict)
- {
- ob[key]=value;
- }
- else
- {
- if (typeof(ob.set) === "function")
- {
- return ob.set(key,value);
- }
- else
- {
- console.log("[method error] missing `set`");
- console.log(ob);
- throw new RuntimeError("object has no method named `set`");
- }
- }
- }/*end-> `__jsdict_set` */
- var __jsdict_items = function __jsdict_items(ob)
- {
- var items;
- if (ob.__class__ === dict)
- {
- items = [];
- for (var __n0 = 0; __n0 < __jsdict_keys(ob).length; __n0++)
- {
- var key = __jsdict_keys(ob)[ __n0 ];
- items.push([key, ob[key]]);
- }
- return items;
- }
- else
- {
- if (typeof(ob.items) === "function")
- {
- return ob.items();
- }
- else
- {
- console.log("[method error] missing `items`");
- console.log(ob);
- throw new RuntimeError("object has no method named `items`");
- }
- }
- }/*end-> `__jsdict_items` */
- var __jsdict_values = function __jsdict_values(ob)
- {
- var items;
- if (ob.__class__ === dict)
- {
- items = [];
- for (var __n0 = 0; __n0 < __jsdict_keys(ob).length; __n0++)
- {
- var key = __jsdict_keys(ob)[ __n0 ];
- items.push(ob[key]);
- }
- return items;
- }
- else
- {
- if (typeof(ob.values) === "function")
- {
- return ob.values();
- }
- else
- {
- console.log("[method error] missing `values`");
- console.log(ob);
- throw new RuntimeError("object has no method named `values`");
- }
- }
- }/*end-> `__jsdict_values` */
- var __jsdict_pop = function __jsdict_pop(ob, key, __default__)
- {
- var p,popped;
- if (ob instanceof Array)
- {
- if (ob.length)
- {
- if (typeof(key) === "undefined")
- {
- throw new RuntimeError("Array.pop(undefined)");
- }
- else
- {
- popped = ob.splice(key, 1);
- if (popped.length === 0)
- {
- if (typeof(__default__) !== "undefined")
- {
- return __default__;
- }
- else
- {
- throw new IndexError(key);
- }
- }
- else
- {
- return popped[0];
- }
- }
- }
- else
- {
- throw new IndexError(key);
- }
- }
- else
- {
- if (ob.__class__ === dict)
- {
- p = ob[key];
- if (typeof(p) === "undefined")
- {
- if (typeof(__default__) !== "undefined")
- {
- return __default__;
- }
- else
- {
- throw new KeyError(key);
- }
- }
- else
- {
- delete ob[key];
- return p;
- }
- }
- else
- {
- if (typeof(ob.pop) === "function")
- {
- return ob.pop(key,__default__);
- }
- else
- {
- console.log("[method error] missing `pop`");
- console.log(ob);
- throw new RuntimeError("object has no method named `items`");
- }
- }
- }
- }/*end-> `__jsdict_pop` */
- var __jsdict_update = function __jsdict_update(ob, other)
- {
- var keys;
- if (ob.__class__ === dict)
- {
- keys = [];
- for (var __n0 = 0; __n0 < __jsdict_keys(other).length; __n0++)
- {
- var key = __jsdict_keys(other)[ __n0 ];
- ob[key] = other[key];
- keys.push(key);
- }
- return keys;
- }
- else
- {
- if (typeof(ob.update) === "function")
- {
- return ob.update(other);
- }
- else
- {
- console.log("[method error] missing `update`");
- console.log(ob);
- throw new RuntimeError("object has no method named `update`");
- }
- }
- }/*end-> `__jsdict_update` */
- /* This returns an array that is a minimal implementation of set.
- Often sets are used simply to remove duplicate entries from a list,
- and then it get converted back to a list, it is safe to use set for this.
- The array prototype is overloaded with basic set functions:
- difference
- intersection
- issubset */
- var set = function set(a)
- {
- var s;
- s = [];
- for (var __n0 = 0; __n0 < a.length; __n0++)
- {
- var item = a[ __n0 ];
- if (s.indexOf(item) === -1)
- {
- s.push(item);
- }
- }
- return s;
- }/*end-> `set` */
- var frozenset = function frozenset(a)
- {
- return set(a);
- }/*end-> `frozenset` */
- Array.prototype.bisect = function __array_bisect(x, low, high)
- {
- var a,mid;
- if (typeof(low) === "undefined")
- {
- low = 0;
- }
- if (typeof(high) === "undefined")
- {
- high = this.length;
- }
- while (low < high)
- {
- a = (low + high);
- mid = Math.floor((a / 2));
- if (x < this[mid])
- {
- high = mid;
- }
- else
- {
- low = (mid + 1);
- }
- }
- return low;
- }/*end-> `__array_bisect` */
- Array.prototype.difference = function __array_difference(other)
- {
- var f;
- var __lambda__ = function __lambda__(i)
- {
- return other.indexOf(i) === -1;
- }/*end-> `__lambda__` */
- f = __lambda__;
- return this.filter(f);
- }/*end-> `__array_difference` */
- Array.prototype.intersection = function __array_intersection(other)
- {
- var f;
- var __lambda__ = function __lambda__(i)
- {
- return other.indexOf(i) !== -1;
- }/*end-> `__lambda__` */
- f = __lambda__;
- return this.filter(f);
- }/*end-> `__array_intersection` */
- Array.prototype.issubset = function __array_issubset(other)
- {
- for (var __n0 = 0; __n0 < this.length; __n0++)
- {
- var item = this[ __n0 ];
- if (other.indexOf(item) === -1)
- {
- return false;
- }
- }
- return true;
- }/*end-> `__array_issubset` */
- var int = function int(a)
- {
- a = Math.round(a);
- if (isNaN(a))
- {
- throw new ValueError("not a number");
- }
- return a;
- }/*end-> `int` */
- var float = function float(a)
- {
- var b;
- if (typeof(a) === "string")
- {
- if (a.lower() === "nan")
- {
- return NaN;
- }
- else
- {
- if (a.lower() === "inf")
- {
- return Infinity;
- }
- }
- }
- b = Number(a);
- if (isNaN(b))
- {
- throw new ValueError(("can not convert to float: " + a));
- }
- return b;
- }/*end-> `float` */
- var round = function round(a, places)
- {
- var p,b;
- if (typeof(places) === "undefined")
- {
- places = 0;
- }
- b = ("" + a);
- if (b.indexOf(".") === -1)
- {
- return a;
- }
- else
- {
- p = Math.pow(10, places);
- return (Math.round((a * p)) / p);
- }
- }/*end-> `round` */
- var str = function str(s)
- {
- return ("" + s);
- }/*end-> `str` */
- var string = function string(s)
- {
- return ("" + s);
- }/*end-> `string` */
- String.prototype.format = function __string_format(fmt)
- {
- var keys,r;
- r = this;
- keys = Object.keys(fmt);
- for (var __n0 = 0; __n0 < keys.length; __n0++)
- {
- var key = keys[ __n0 ];
- r = r.split(key).join(fmt[key]);
- }
- r = r.split("{").join("").split("}").join("");
- return r;
- }/*end-> `__string_format` */
- var __lambda__ = function __lambda__(a)
- {
- return this.indexOf(a);
- }/*end-> `__lambda__` */
- String.prototype.find = __lambda__;
- String.prototype.isdigit = function __string_isdigit()
- {
- var digits;
- digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
- for (var __n0 = 0; __n0 < this.length; __n0++)
- {
- var char = this[ __n0 ];
- if (__contains__(digits, char))
- {
- /*pass*/
- }
- else
- {
- return false;
- }
- }
- return true;
- }/*end-> `__string_isdigit` */
- String.prototype.isnumber = function __string_isnumber()
- {
- var digits;
- digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."];
- for (var __n0 = 0; __n0 < this.length; __n0++)
- {
- var char = this[ __n0 ];
- if (__contains__(digits, char))
- {
- /*pass*/
- }
- else
- {
- return false;
- }
- }
- return true;
- }/*end-> `__string_isnumber` */
- var __replace_method = function __replace_method(ob, a, b)
- {
- if (typeof(ob) === "string")
- {
- return ob.split(a).join(b);
- }
- else
- {
- return __replace_method(ob, a, b);
- }
- }/*end-> `__replace_method` */
- var __split_method = function __split_method(ob, delim)
- {
- if (typeof(ob) === "string")
- {
- if (typeof(delim) === "undefined")
- {
- return ob.split(" ");
- }
- else
- {
- return ob.split(delim);
- }
- }
- else
- {
- if (typeof(delim) === "undefined")
- {
- return __split_method(ob);
- }
- else
- {
- return ob.split(delim);
- }
- }
- }/*end-> `__split_method` */
- var dir = function dir(ob)
- {
- if (ob instanceof Object)
- {
- return Object.keys( ob );
- }
- else
- {
- return __object_keys__(ob);
- }
- }/*end-> `dir` */
- var __getfast__ = function __getfast__(ob, attr)
- {
- var v;
- v = ob[attr];
- if (typeof(v) === "undefined")
- {
- if (ob.__class__)
- {
- v = ob.__class__[attr];
- if (typeof(v) !== "undefined")
- {
- return v;
- }
- }
- throw new AttributeError(attr);
- }
- else
- {
- return v;
- }
- }/*end-> `__getfast__` */
- var getattr = function getattr(ob, attr)
- {
- return __getfast__(ob, attr);
- }/*end-> `getattr` */
- var setattr = function setattr(ob, attr, value)
- {
- ob[attr] = value;
- }/*end-> `setattr` */
- /* Emulates Python's range function */
- var range = function range(num, stop, step)
- {
- var i,arr;
- if (typeof(stop) !== "undefined")
- {
- i = num;
- num = stop;
- }
- else
- {
- i = 0;
- }
- if (typeof(step) === "undefined")
- {
- step = 1;
- }
- arr = [];
- while (i < num)
- {
- arr.push(i);
- i += step;
- }
- return arr;
- }/*end-> `range` */
- var xrange = function xrange(num, stop, step)
- {
- return range(num, stop, step);
- }/*end-> `xrange` */
- var sum = function sum(arr)
- {
- var a;
- a = 0;
- for (var __n0 = 0; __n0 < arr.length; __n0++)
- {
- var b = arr[ __n0 ];
- a += b;
- }
- return a;
- }/*end-> `sum` */
- var map = function map(func, objs)
- {
- var arr,v;
- arr = [];
- for (var __n0 = 0; __n0 < objs.length; __n0++)
- {
- var ob = objs[ __n0 ];
- v = func(ob);
- arr.push(v);
- }
- return arr;
- }/*end-> `map` */
- var filter = function filter(func, objs)
- {
- var arr;
- arr = [];
- for (var __n0 = 0; __n0 < objs.length; __n0++)
- {
- var ob = objs[ __n0 ];
- if (func(ob))
- {
- arr.push(ob);
- }
- }
- return arr;
- }/*end-> `filter` */
- var min = function min(lst)
- {
- var a;
- a = null;
- for (var __n0 = 0; __n0 < lst.length; __n0++)
- {
- var value = lst[ __n0 ];
- if (a === null)
- {
- a = value;
- }
- else
- {
- if (value < a)
- {
- a = value;
- }
- }
- }
- return a;
- }/*end-> `min` */
- var max = function max(lst)
- {
- var a;
- a = null;
- for (var __n0 = 0; __n0 < lst.length; __n0++)
- {
- var value = lst[ __n0 ];
- if (a === null)
- {
- a = value;
- }
- else
- {
- if (value > a)
- {
- a = value;
- }
- }
- }
- return a;
- }/*end-> `max` */
- var abs = function abs(num)
- {
- return Math.abs(num);
- }/*end-> `abs` */
- var ord = function ord(char)
- {
- return char.charCodeAt(0);
- }/*end-> `ord` */
- var chr = function chr(num)
- {
- return String.fromCharCode(num);
- }/*end-> `chr` */
- /*end-builtins*/
- function clock() { return new Date().getTime() / 1000.0; };
- DEFAULT_ARG = 8;
- var main = function main()
- {
- var res,avg,tk,t0,times;
- times = [];
- var i;
- i = -1;
- while (i++ < 4)
- {
- t0 = clock();
- res = fannkuch(DEFAULT_ARG);
- tk = clock();
- times.append((tk - t0));
- }
- avg = (sum(times) / len(times));
- console.log(avg);
- }/*end-> `main` */
- var fannkuch = function fannkuch(n)
- {
- var count,max_flips,perm1,m,do_return,perm,r,flips_count,k,check;
- 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 ++;
- }
- while (r !== 1)
- {
- count[(r - 1)] = r;
- r --;
- }
- if ((perm1[0] !== 0 && perm1[m] !== m))
- {
- perm = perm1.copy();
- flips_count = 0;
- k = perm[0];
- while (k !== 0)
- {
- perm.__setslice__(undefined, (k + 1), undefined, perm.__getslice__(k, undefined, -1));
- flips_count ++;
- k = perm[0];
- }
- if (flips_count > max_flips)
- {
- max_flips = flips_count;
- }
- }
- do_return = true;
- while (r !== n)
- {
- perm1.insert(r, __jsdict_pop(perm1, 0));
- count[r] --;
- if (count[r] > 0)
- {
- do_return = false;
- break;
- }
- r ++;
- }
- if (do_return)
- {
- return max_flips;
- }
- }
- }/*end-> `fannkuch` */
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement