class Node:
def __init__(self,x,Next, Prev):
self.data = x
self.next = Next
self.prev = Prev
class LinkedList:
def __init__(self, *x):
self.start = None
self.last = None
self.lenght = 0
if x:
for i in x:
self.add(i)
def __str__(self):
if self.start != None:
current = self.start
out = '[ ' + str(current.data)
while current.next != None:
current = current.next
out += ' '+ str(current.data)
out += ' ]'
return out
def clear(self):
self.__init__()
def add(self, x):
if self.start == None:
self.start = self.last = Node(x,None,None)
self.lenght += 1
else:
prev = self.last
self.last.next = self.last = Node(x,None,None)
self.last.prev = prev
self.lenght += 1
def addFew(self,*x):
if x:
for i in x:
self.add(i)
def remove(self, x,remove_all = False):
current = self.start
for i in range(self.lenght):
if current.data == x:
if current.prev == None:
if current.next != None:
self.start = current.next
current.next.prev = None
self.lenght -= 1
if current.next == None:
if current.prev != None:
current.prev.next = None
self.last = current.prev
self.last.next = None
self.lenght -= 1
if current.prev != None and current.next != None:
current.prev.next = current.next
current.next.prev = current.prev
self.lenght -= 1
if current.next == None and current.prev == None:
clear(self)
self.lenght -= 0
if remove_all == False:
break
current = current.next
# TODO: Replace examples and use TDD development by writing your own tests
# These are some of the methods available:
# test.expect(boolean, [optional] message)
# test.assert_equals(actual, expected, [optional] message)
# test.assert_not_equals(actual, expected, [optional] message)
# You can use Test.describe and Test.it to write BDD style test groupings
test.assert_equals(LinkedList(1,2,3,4,5).lenght, 5)
def binary_search(lst,item):
low = 0
high = len(lst) - 1
while low <= high:
mid = int((high + low)/2)
guess = lst[mid]
print (guess,mid)
if guess > item:
high = mid - 1
if guess < item:
low = mid + 1
if guess == item:
return mid
else:
return None
# TODO: Replace examples and use TDD development by writing your own tests
# These are some of the methods available:
# test.expect(boolean, [optional] message)
# test.assert_equals(actual, expected, [optional] message)
# test.assert_not_equals(actual, expected, [optional] message)
# You can use Test.describe and Test.it to write BDD style test groupings
test.assert_equals(binary_search([1,2,3,4,5],2), 1)
test.assert_equals(binary_search([1,2,3,4,5],10), None)
test.assert_equals(binary_search([1,2,3,4,5],2), 1)
test.assert_equals(binary_search([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],9), 9)
test.assert_equals(binary_search([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],100), None)
def isprime(num): if num < 2 or int(num)!=num: return False for i in range(2, int(num**(1/2)) + 1): if num % i == 0: return False return True
import math- def isprime(num):
if num<2 or int(num)!=num: return Falseif not num%2 and num>2: return Falsefor n in range(3,math.ceil((num-1)**.5)+1,2):if not num%n:return Falsereturn True- if num < 2 or int(num)!=num:
- return False
- for i in range(2, int(num**(1/2)) + 1):
- if num % i == 0:
- return False
- return True
# TODO: Replace examples and use TDD development by writing your own tests # These are some of the methods available: # test.expect(boolean, [optional] message) # test.assert_equals(actual, expected, [optional] message) # test.assert_not_equals(actual, expected, [optional] message) # You can use Test.describe and Test.it to write BDD style test groupings Test.describe("Gets whether a number is prime") Test.it("takes the square root") Test.expect(not isprime(-1),"negative numbers can't be prime") Test.expect(not isprime(5.5),"decimals can't be prime") Test.expect(not isprime(0),"0 is not prime") Test.expect(not isprime(1),"1 is not prime") Test.expect(isprime(2),"2 is prime") Test.expect(isprime(3),"3 is prime") Test.expect(not isprime(4),"4 is not prime") Test.expect(isprime(7),"7 is prime") Test.expect(not isprime(9),"9 is not prime") Test.expect(not isprime(49),"49 is prime") Test.expect(isprime(982451653),"49 is prime")
- # TODO: Replace examples and use TDD development by writing your own tests
- # These are some of the methods available:
- # test.expect(boolean, [optional] message)
- # test.assert_equals(actual, expected, [optional] message)
- # test.assert_not_equals(actual, expected, [optional] message)
- # You can use Test.describe and Test.it to write BDD style test groupings
- Test.describe("Gets whether a number is prime")
- Test.it("takes the square root")
- Test.expect(not isprime(-1),"negative numbers can't be prime")
- Test.expect(not isprime(5.5),"decimals can't be prime")
- Test.expect(not isprime(0),"0 is not prime")
- Test.expect(not isprime(1),"1 is not prime")
- Test.expect(isprime(2),"2 is prime")
- Test.expect(isprime(3),"3 is prime")
- Test.expect(not isprime(4),"4 is not prime")
- Test.expect(isprime(7),"7 is prime")
- Test.expect(not isprime(9),"9 is not prime")
Test.expect(not isprime(49),"49 is prime")- Test.expect(not isprime(49),"49 is prime")
- Test.expect(isprime(982451653),"49 is prime")