def solve(n): queens_x = nb.typedlist(int_) queens_y = nb.typedlist(int_) if _solve(n, queens_x, queens_y): return queens_x, queens_y else: return None, None
def test_count(type, L): """ >>> test_count(int_, [1, 2, 3, 4, 5, 1, 2]) (0L, 1L, 2L) """ tlist = nb.typedlist(type, L) return tlist.count(0), tlist.count(3), tlist.count(1)
def test_count_complex(type, L): """ >>> test_count_complex(complex128, [1+1j, 1+2j, 2+1j, 2+2j, 1+1j, 2+2j, 2+2j]) (1L, 2L, 3L) """ tlist = nb.typedlist(type, L) return tlist.count(1 + 2j), tlist.count(1 + 1j), tlist.count(2 + 2j)
def test_count_complex(type, L): """ >>> test_count_complex(complex128, [1+1j, 1+2j, 2+1j, 2+2j, 1+1j, 2+2j, 2+2j]) (1L, 2L, 3L) """ tlist = nb.typedlist(type, L) return tlist.count(1+2j), tlist.count(1+1j), tlist.count(2+2j)
def test_index(type): """ >>> test_index(int_) (0L, 2L, 4L) """ tlist = nb.typedlist(type, [5, 4, 3, 2, 1]) return tlist.index(5), tlist.index(3), tlist.index(1)
def from_iterable(type, iterable): """ >>> from_iterable(int_, [1, 2, 3]) [1, 2, 3] >>> from_iterable(int_, (1, 2, 3)) [1, 2, 3] >>> from_iterable(int_, (x for x in [1, 2, 3])) [1, 2, 3] >>> from_iterable(float_, [1, 2, 3]) [1.0, 2.0, 3.0] >>> from_iterable(float_, (1, 2, 3)) [1.0, 2.0, 3.0] >>> from_iterable(float_, (x for x in [1, 2, 3])) [1.0, 2.0, 3.0] >>> from_iterable(int_, [1, object(), 3]) Traceback (most recent call last): ... TypeError: an integer is required >>> from_iterable(int_, object()) Traceback (most recent call last): ... TypeError: 'object' object is not iterable """ return nb.typedlist(type, iterable)
def append_many(type): """ >>> append_many(int_) 1000L """ tlist = nb.typedlist(type) for i in range(1000): tlist.append(i) return len(tlist)
def test_insert(type): """ >>> test_insert(int_) [0, 1, 2, 3, 4, 5] """ tlist = nb.typedlist(type, [1,3]) tlist.insert(0,0) tlist.insert(2,2) tlist.insert(4,4) tlist.insert(8,5) return tlist
def append(type): """ >>> append(int_) (0L, 1L, 2L, 3L) """ tlist = nb.typedlist(type) l1 = len(tlist) tlist.append(0) l2 = len(tlist) tlist.append(1) l3 = len(tlist) tlist.append(2) l4 = len(tlist) return l1, l2, l3, l4
def test_reverse(type, value): """ >>> test_reverse(int_, range(10)) [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] >>> test_reverse(int_, range(11)) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] >>> test_reverse(float_, range(10)) [9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0] >>> test_reverse(float_, range(11)) [10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0] """ tlist = nb.typedlist(type, value) tlist.reverse() return tlist
def pop_many(type): """ >>> pop_many(int_) (1000L, 0L) """ tlist = nb.typedlist(type) for i in range(1000): tlist.append(i) initial_length = len(tlist) for i in range(1000): tlist.pop() return initial_length, len(tlist)
def test_remove(type): """ >>> test_remove(int_) 4 3 2 [1, 3] """ tlist = nb.typedlist(type, range(5)) tlist.remove(0) print (len(tlist)) tlist.remove(2) print (len(tlist)) tlist.remove(4) print (len(tlist)) return tlist
def index_error(type): """ >>> index_error(int_) Traceback (most recent call last): ... IndexError: list index out of range >>> index_error(float_) Traceback (most recent call last): ... IndexError: list index out of range """ tlist = nb.typedlist(type) tlist.append(0) tlist.append(1) tlist.append(2) return tlist[4]
def index(type): """ >>> index(int_) ['[0, 1, 2]', '0', '1', '2'] >>> assert index(int_) == index.py_func(int_) >>> index(float_) ['[0.0, 1.0, 2.0]', '0.0', '1.0', '2.0'] >>> assert index(float_) == index.py_func(float_) >>> index(complex128) ['[0j, (1+0j), (2+0j)]', '0j', '(1+0j)', '(2+0j)'] >>> assert index(complex128) == index.py_func(complex128) """ tlist = nb.typedlist(type) tlist.append(0) tlist.append(1) tlist.append(2) return [str(tlist), str(tlist[0]), str(tlist[1]), str(tlist[2])]
def pop(type): """ >>> pop(int_) 2 1 0 (3L, 2L, 1L, 0L) """ tlist = nb.typedlist(type) for i in range(3): tlist.append(i) l1 = len(tlist) print((tlist.pop())) l2 = len(tlist) print((tlist.pop())) l3 = len(tlist) print((tlist.pop())) l4 = len(tlist) return l1, l2, l3, l4
# -*- coding: utf-8 -*- from __future__ import print_function, division, absolute_import import numba as nb from numba import * ListInt = nb.typeof(nb.typedlist(int_)) # Define List[int] type @autojit def hits(x1, y1, x2, y2): "Check whether a queen positioned at (x1, y1) will hit a queen at position (x2, y2)" return x1 == x2 or y1 == y2 or abs(x1 - x2) == abs(y1 - y2) @autojit def hitsany(x, y, queens_x, queens_y): "Check whether a queen positioned at (x1, y1) will hit any other queen" # TODO: optimize special methods (__iter__, __getitem__) for i in range(queens_x.__len__()): if hits(x, y, queens_x.__getitem__(i), queens_y.__getitem__(i)): return True return False @jit(bool_(int_, ListInt, ListInt)) def _solve(n, queens_x, queens_y): "Solve the queens puzzle" if n == 0: return True
# -*- coding: utf-8 -*- import numba as nb from numba import * ListInt = nb.typeof(nb.typedlist(int_)) # Define List[int] type @autojit def hits(x1, y1, x2, y2): "Check whether a queen positioned at (x1, y1) will hit a queen at position (x2, y2)" return x1 == x2 or y1 == y2 or abs(x1 - x2) == abs(y1 - y2) @autojit def hitsany(x, y, queens_x, queens_y): "Check whether a queen positioned at (x1, y1) will hit any other queen" # TODO: optimize special methods (__iter__, __getitem__) for i in range(queens_x.__len__()): if hits(x, y, queens_x.__getitem__(i), queens_y.__getitem__(i)): return True return False @jit(bool_(int_, ListInt, ListInt)) def _solve(n, queens_x, queens_y): "Solve the queens puzzle" if n == 0: return True for x in range(1, 9): for y in range(1, 9): if not hitsany(x, y, queens_x, queens_y):