Skip to content

Commit

Permalink
Merge pull request #83 from kckennylau/patch-13
Browse files Browse the repository at this point in the history
Optimizations
  • Loading branch information
Mego authored Jun 8, 2017
2 parents aba9994 + a7a92ea commit 413b403
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions seriously/SeriouslyCommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import math as rmath
import random, itertools, sys, string, binascii, ast
from base64 import *
from copy import copy as _copy
from copy import deepcopy as _copy
import collections
from functools import reduce, lru_cache
import struct
Expand Down Expand Up @@ -49,7 +49,7 @@ def copy(a):

@memoize
def Lucas(n): # pragma: no cover
[a,b] = fast_fib(n)
a,b = fast_fib(n)
return (a<<1)+b

fib_cache = {0:0, 1:1, 2:1}
Expand Down Expand Up @@ -316,7 +316,7 @@ def Fib_index(n):
def div_fn(srs):
a=srs.pop()
if isinstance(a, collections.Iterable):
a = [x for x in a]
a = list(a)
srs.push(a[-1:]+a[:-1])
elif anytype(a, int, float, complex):
b=srs.pop()
Expand All @@ -327,7 +327,7 @@ def div_fn(srs):
def idiv_fn(srs):
a=srs.pop()
if isinstance(a, collections.Iterable):
a = [x for x in a]
a = list(a)
srs.push(a[1:]+a[:1])
elif anytype(a, int, float, complex):
b=srs.pop()
Expand All @@ -349,7 +349,7 @@ def rot2_fn(srs):
def d_fn(srs):
a=srs.pop()
if isinstance(a, collections.Iterable) and not isinstance(a, str):
a=[x for x in a]
a=list(a)
b=a.pop(-1)
srs.push(a)
srs.push(b)
Expand All @@ -367,7 +367,7 @@ def i_fn(srs):
if isinstance(a, str) and (all([c.isdigit() or c=='.' for c in a]) and a.count('.')<2):
srs.push(float(a))
elif isinstance(a, collections.Iterable):
for x in [y for y in a][::-1]:
for x in list(a)[::-1]:
srs.push(x)
else:
srs.push(a)
Expand All @@ -394,7 +394,7 @@ def p_fn(srs):
print("{} is_prime => {}".format(a, is_prime(a)))
srs.push(is_prime(a))
elif isinstance(a, collections.Iterable) and not isinstance(a, str):
a=[x for x in a]
a=list(a)
b=a.pop(0)
srs.push(a)
srs.push(b)
Expand Down Expand Up @@ -503,7 +503,7 @@ def invert_fn(srs):
def comp_fn(srs):
a=srs.pop()
if isinstance(a, collections.Iterable):
a = [x for x in a]
a = list(a)
a = a+[0] if len(a)%2 else a
while len(a) > 0:
r,i = a.pop(0),a.pop(0)
Expand Down Expand Up @@ -713,7 +713,7 @@ def E_fn(srs):
b=srs.pop()
if srs.debug_mode:
print("islice indices:",b,b+1)
srs.push([x for x in itertools.islice(a,b,b+1)][0])
srs.push(list(itertools.islice(a,b,b+1))[0])

def peek_print_fn(srs):
print(' '.join(map(repr, srs.stack[::-1])))
Expand All @@ -728,15 +728,15 @@ def dupe_each_n_fn(srs):
tmp = []
while srs.stack:
b = srs.pop()
tmp+=[b for i in range(a)]
srs.stack=deque(tmp[::-1])
tmp = [copy(b)[1] for _ in range(a)] + tmp
srs.stack=deque(tmp)

def S_fn(srs):
a=srs.pop()
if isinstance(a, str):
srs.push(''.join(sorted(a)))
elif isinstance(a, collections.Iterable):
srs.push(sorted([_ for _ in a]))
srs.push(sorted(a))
else:
srs.push(math.sin(a))

Expand All @@ -754,7 +754,7 @@ def zip_fn(srs):
srs.push(zip_longest(*lists))

def sum_fn(srs):
a=[x for x in srs.pop()]
a=list(srs.pop())
if len(a) == 0:
srs.push(0)
else:
Expand All @@ -763,7 +763,7 @@ def sum_fn(srs):

def index_fn(srs):
b,a=srs.pop(),srs.pop()
b = [_ for _ in b]
b = list(b)
if a in b:
srs.push(b.index(a))
else:
Expand All @@ -777,7 +777,7 @@ def cond_quit_fn(srs):
exit()

def median_fn(srs):
a=[x for x in srs.pop()]
a=list(srs.pop())
if len(a)%2:
srs.push(a[len(a)//2])
else:
Expand Down Expand Up @@ -867,7 +867,7 @@ def T_fn(srs):
if isinstance(a, str):
a = a[:b] + str(c) + a[b+1:]
else:
a = [x for x in a]
a = list(a)
a[b] = c
srs.push(a)

Expand Down Expand Up @@ -904,7 +904,7 @@ def reg_all_input_fn(srs):
def range_ab_fn(srs):
a = srs.pop()
if isinstance(a, collections.Iterable):
srs.push(range(*[x for x in a]))
srs.push(range(*list(a)))
else:
b = srs.pop()
srs.push(range(a,b))
Expand Down Expand Up @@ -939,7 +939,7 @@ def N_fn(srs):
def shuffle_fn(srs):
a = srs.pop()
isstr = isinstance(a, str)
a = [x for x in a]
a = list(a)
random.shuffle(a)
if isstr:
a = ''.join(a)
Expand Down Expand Up @@ -968,7 +968,10 @@ def is_unique_fn(srs):

def uniquify_fn(srs):
a = srs.pop()
unique = [x for i,x in enumerate(a) if i==a.index(x)]
unique = []
for x in a:
if x not in unique:
unique.append(x)
if isinstance(a, str):
srs.push(''.join(unique))
else:
Expand Down Expand Up @@ -1019,7 +1022,7 @@ def t_fn(srs):
try:
srs.push(a[b:])
except:
srs.push([_ for _ in a][b:])
srs.push(list(a)[b:])

def V_fn(srs):
a,b = srs.pop(), srs.pop()
Expand Down Expand Up @@ -1119,7 +1122,7 @@ def mean_fn(srs):

def mode_fn(srs):
a = srs.pop()
srs.push(mode([x for x in a]))
srs.push(mode(list(a)))

def add_reg0_fn(srs):
global registers
Expand Down Expand Up @@ -1152,8 +1155,8 @@ def caret_fn(srs):
a,b = srs.pop(),srs.pop()
isstr = isinstance(a, str)
if isinstance(a, collections.Iterable):
a = [x for x in a]
b = [x for x in b]
a = list(a)
b = list(b)
xor = [x for x in a+b if (x in a) ^ (x in b)]
if isstr:
xor = ''.join(xor)
Expand All @@ -1167,7 +1170,7 @@ def divisors_fn(srs):

def chunk_len_fn(srs):
a = srs.pop()
a = [x for x in a] if not isinstance(a, str) else a
a = list(a) if not isinstance(a, str) else a
b = srs.pop()
res = []
for i in range(0, len(a), b):
Expand All @@ -1176,7 +1179,7 @@ def chunk_len_fn(srs):

def chunk_num_fn(srs):
a = srs.pop()
a = [x for x in a] if not isinstance(a, str) else a
a = list(a) if not isinstance(a, str) else a
b = srs.pop()
diff = len(a)%b
chunksize = [len(a)//b+(i<diff) for i in range(b)][::-1]
Expand All @@ -1194,7 +1197,7 @@ def list_repeat_fn(srs):
if isinstance(b, str):
srs.push([b]*a)
elif isinstance(b, collections.Iterable):
srs.push([x for x in b]*a)
srs.push(list(b)*a)
else:
srs.push([b]*a)

Expand Down

0 comments on commit 413b403

Please sign in to comment.