mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-05 19:20:17 +00:00
Compare commits
No commits in common. "d717f9499fe7da97eb5d06e5f446812c2b0b09e7" and "0109829ad4ec69a115c35b653fb283f3d99a387f" have entirely different histories.
d717f9499f
...
0109829ad4
@ -16,35 +16,6 @@ def enumerate(iterable, start=0):
|
|||||||
yield n, elem
|
yield n, elem
|
||||||
n += 1
|
n += 1
|
||||||
|
|
||||||
def __minmax_reduce(op, args):
|
|
||||||
if len(args) == 2: # min(1, 2)
|
|
||||||
return args[0] if op(args[0], args[1]) else args[1]
|
|
||||||
if len(args) == 0: # min()
|
|
||||||
raise TypeError('expected 1 arguments, got 0')
|
|
||||||
if len(args) == 1: # min([1, 2, 3, 4]) -> min(1, 2, 3, 4)
|
|
||||||
args = args[0]
|
|
||||||
args = iter(args)
|
|
||||||
try:
|
|
||||||
res = next(args)
|
|
||||||
except StopIteration:
|
|
||||||
raise ValueError('args is an empty sequence')
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
i = next(args)
|
|
||||||
except StopIteration:
|
|
||||||
break
|
|
||||||
if op(i, res):
|
|
||||||
res = i
|
|
||||||
return res
|
|
||||||
|
|
||||||
def min(*args, key=None):
|
|
||||||
key = key or (lambda x: x)
|
|
||||||
return __minmax_reduce(lambda x,y: key(x)<key(y), args)
|
|
||||||
|
|
||||||
def max(*args, key=None):
|
|
||||||
key = key or (lambda x: x)
|
|
||||||
return __minmax_reduce(lambda x,y: key(x)>key(y), args)
|
|
||||||
|
|
||||||
def sum(iterable):
|
def sum(iterable):
|
||||||
res = 0
|
res = 0
|
||||||
for i in iterable:
|
for i in iterable:
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@ -272,10 +272,8 @@ static bool array2d_apply_(int argc, py_Ref argv) {
|
|||||||
static bool array2d_copy_(int argc, py_Ref argv) {
|
static bool array2d_copy_(int argc, py_Ref argv) {
|
||||||
// def copy_(self, src: 'array2d') -> None: ...
|
// def copy_(self, src: 'array2d') -> None: ...
|
||||||
PY_CHECK_ARGC(2);
|
PY_CHECK_ARGC(2);
|
||||||
|
PY_CHECK_ARG_TYPE(1, tp_array2d);
|
||||||
c11_array2d* self = py_touserdata(argv);
|
c11_array2d* self = py_touserdata(argv);
|
||||||
|
|
||||||
py_Type src_type = py_typeof(py_arg(1));
|
|
||||||
if(src_type == tp_array2d) {
|
|
||||||
c11_array2d* src = py_touserdata(py_arg(1));
|
c11_array2d* src = py_touserdata(py_arg(1));
|
||||||
if(self->n_cols != src->n_cols || self->n_rows != src->n_rows) {
|
if(self->n_cols != src->n_cols || self->n_rows != src->n_rows) {
|
||||||
return ValueError("copy_() expected the same shape: (%d, %d) != (%d, %d)",
|
return ValueError("copy_() expected the same shape: (%d, %d) != (%d, %d)",
|
||||||
@ -285,18 +283,6 @@ static bool array2d_copy_(int argc, py_Ref argv) {
|
|||||||
src->n_rows);
|
src->n_rows);
|
||||||
}
|
}
|
||||||
memcpy(self->data, src->data, self->numel * sizeof(py_TValue));
|
memcpy(self->data, src->data, self->numel * sizeof(py_TValue));
|
||||||
} else {
|
|
||||||
py_TValue* data;
|
|
||||||
int length = pk_arrayview(py_arg(1), &data);
|
|
||||||
if(length != -1) {
|
|
||||||
if(self->numel != length) {
|
|
||||||
return ValueError("copy_() expected the same numel: %d != %d", self->numel, length);
|
|
||||||
}
|
|
||||||
memcpy(self->data, data, self->numel * sizeof(py_TValue));
|
|
||||||
} else {
|
|
||||||
return TypeError("copy_() expected `array2d`, `list` or `tuple`, got '%t", src_type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
py_newnone(py_retval());
|
py_newnone(py_retval());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -542,6 +542,12 @@ def f(a, b):
|
|||||||
|
|
||||||
assert f(1, 2) == 3
|
assert f(1, 2) == 3
|
||||||
|
|
||||||
|
exit()
|
||||||
|
|
||||||
|
dir_int = dir(int)
|
||||||
|
assert dir_int[:4] == ['__add__', '__and__', '__base__', '__eq__']
|
||||||
|
|
||||||
|
|
||||||
# /************ module time ************/
|
# /************ module time ************/
|
||||||
import time
|
import time
|
||||||
# test time.time
|
# test time.time
|
||||||
@ -574,16 +580,11 @@ assert max(1, 2, 3) == 3
|
|||||||
assert max([1, 2]) == 2
|
assert max([1, 2]) == 2
|
||||||
assert max([1, 2, 3], key=lambda x: -x) == 1
|
assert max([1, 2, 3], key=lambda x: -x) == 1
|
||||||
|
|
||||||
# assert min([
|
assert min([
|
||||||
# (1, 2),
|
(1, 2),
|
||||||
# (1, 3),
|
(1, 3),
|
||||||
# (1, 4),
|
(1, 4),
|
||||||
# ]) == (1, 2)
|
]) == (1, 2)
|
||||||
|
|
||||||
assert min(1, 2) == 1
|
assert min(1, 2) == 1
|
||||||
assert max(1, 2) == 2
|
assert max(1, 2) == 2
|
||||||
|
|
||||||
exit()
|
|
||||||
|
|
||||||
dir_int = dir(int)
|
|
||||||
assert dir_int[:4] == ['__add__', '__and__', '__base__', '__eq__']
|
|
||||||
@ -92,8 +92,6 @@ assert a == d and a is not d
|
|||||||
x = array2d(2, 4, default=0)
|
x = array2d(2, 4, default=0)
|
||||||
x.copy_(d)
|
x.copy_(d)
|
||||||
assert x == d and x is not d
|
assert x == d and x is not d
|
||||||
x.copy_([1, 2, 3, 4, 5, 6, 7, 8])
|
|
||||||
assert x.tolist() == [[1, 2], [3, 4], [5, 6], [7, 8]]
|
|
||||||
|
|
||||||
# test alive_neighbors
|
# test alive_neighbors
|
||||||
a = array2d(3, 3, default=0)
|
a = array2d(3, 3, default=0)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user