This fonction analyze records of temperature to find the closest to zero.
If two numbers are equally close to zero, positive integer has to be considered closest to zero (for instance, if the temperatures are -5 and 5, then display 5)
Display 0 (zero) if no temperatures are provided. Otherwise, display the temperature closest to 0.
def TempCloseTo0(arr):
if len(arr)==0: return 0
else :
j=max(arr)
for i in arr:
if abs(i)<j :
j=abs(i)
return j
test.assert_equals(TempCloseTo0([45,37,-8,1,-2,3,14]),1)
test.assert_equals(TempCloseTo0([-5,-12,-4,-31]),-4)
test.assert_equals(TempCloseTo0([42,-5,12,21,5,24]),5)
test.assert_equals(TempCloseTo0([-5,-4,-2,12,-40,4,2,18,11,5]),2)
test.assert_equals(TempCloseTo0([42,-5,12,0,21,5,24]),0)
test.assert_equals(TempCloseTo0([]),0)
def primemaker(x): primes = [] if x < 2 : return [] else: for possibleprimes in range(2,(x+1)): isprime = True for n in range(2,possibleprimes): if possibleprimes % n == 0: isprime = False if isprime: primes.append(possibleprimes) return primes
- def primemaker(x):
primes = []isprime = Truefor possibleprimes in range(2,x):- primes = []
- if x < 2 : return []
- else:
- for possibleprimes in range(2,(x+1)):
- isprime = True
- for n in range(2,possibleprimes):
if possibleprimes % num == 0:- if possibleprimes % n == 0:
- isprime = False
- if isprime:
- primes.append(possibleprimes)
print(primes)- return primes
# 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(primemaker(100),[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]) test.assert_equals(primemaker(20),[2,3,5,7,11,13,17,19]) test.assert_equals(primemaker(50),[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]) test.assert_equals(primemaker(0),[])
- # 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- # You can use Test.describe and Test.it to write BDD style test groupings
- test.assert_equals(primemaker(100),[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97])
- test.assert_equals(primemaker(20),[2,3,5,7,11,13,17,19])
- test.assert_equals(primemaker(50),[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47])
- test.assert_equals(primemaker(0),[])