Esempio n. 1
0
def p2(x):
  if isinstance(x, int):
    if x > 1:
      i = 1
      while i < x:
        ans = i
        i = i*2

      return ans
    else:
      return 0
  else:
    print "Argument must be an integer"


question = p2(9)
checkers.check_answer(question, 8)

question = p2(-1)
checkers.check_answer(question, 0)

question = p2(3)
checkers.check_answer(question, 2)

question = p2(17)
checkers.check_answer(question, 16)

question = p2(16)
checkers.check_answer(question, 8)
Esempio n. 2
0
  a = float(a)
  b = float(b)
  c = float(c)

  if a == 0:
    return round((-c/b)*10)/10
  else:
    root = b**2-4*a*c
    if root < 0:
      root = abs(root)
      j = complex(0,1)
      x1 = (-b + j * math.sqrt(root))/(2*a)
      x2 = (-b - j * math.sqrt(root))/(2*a)
      return [x1, x2]
    else:
      x1 = (-b + math.sqrt(root))/(2*a)
      x2 = (-b - math.sqrt(root))/(2*a)
      return sorted([x1, x2])

question = quadraticRootsComplex(1, 2, -3)
checkers.check_answer(question, sorted([-3, 1]))

question = quadraticRootsComplex(1, 2, -3)
checkers.check_answer(question, sorted([1, -3]))

question = quadraticRootsComplex(0, 2, -3)
checkers.check_answer(question, 1.5)

question = quadraticRootsComplex(1, -2, 5)
checkers.check_answer(question, [complex(1,+2), complex(1,-2)])
Esempio n. 3
0
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers
import math


def pointDist(p1, p2):
    dx = p1[0] - p2[0]
    dy = p1[1] - p2[1]
    result = round(math.sqrt(dx**2 + dy**2), 3)
    return result


def perpDist(p, l):
    (px, py) = p
    (a, b, c) = l

    cn = py - px * (b / a)
    xs = (-(c * a) / (b**2) - cn * (a / b)) / (1 + ((a / b)**2))
    yo = (-a / b) * xs - (c / b)

    ps = (xs, yo)
    shrt_distance = round(pointDist(ps, p) * 100) / 100
    return shrt_distance


p = (10, 0)
l = (1, 1, 1)
question = perpDist(p, l)
checkers.check_answer(question, 7.81)
Esempio n. 4
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers
import math

def quadraticRoots(a, b, c):
  a = float(a)
  b = float(b)
  c = float(c)

  if a == 0:
    return round((-c/b)*10)/10
  else:
    x1 = (-b + math.sqrt(b**2 - 4*a*c))/(2*a)
    x2 = (-b - math.sqrt(b**2 - 4*a*c))/(2*a)
    return sorted([x1, x2])

question = quadraticRoots(1, 2, -3)
checkers.check_answer(question, sorted([-3, 1]))

question = quadraticRoots(1, 2, -3)
checkers.check_answer(question, sorted([1, -3]))

question = quadraticRoots(0, 2, -3)
checkers.check_answer(question, 1.5)
Esempio n. 5
0
import checkers

def prime(n):
  if n > 0 and isinstance(n, int):
    i = 2
    while i < n:
      if n%i == 0:
        prime = False
        break
      else:
        prime = True
      i = i + 1

    return prime
  else:
    print "Argument must be a positive integer"


question = prime(6)
checkers.check_answer(question, False)

question = prime(5)
checkers.check_answer(question, True)

question = prime(15)
checkers.check_answer(question, False)

question = prime(23)
checkers.check_answer(question, True)
Esempio n. 6
0
  #Find the slope of the line
  #The equation a*x + b*y + c = 0 in point-intercept form is:
  # y = (-a/b)*x - c/b
  # slope = (-a/b)
  # perp_slope = (b/a)

  # Equation for new line
  cn = py - px*(b/a)
  # yn = (b/a)*xn + cn

  # Equation for old line
  # yo = (-a/b)*xo - (c/b)

  # Intersects where xn = xo = xs and yn = yo = ys
  # (b/a)*xs + cn = (-a/b)*xs - (c/b)
  # (b/a)*xs = -(a/b)*xs - (c/b) - cn
  # xs = (-(a/b)*xs - (c/b) - cn)/(b/a)
  # xs = -((a/b)**2)*xs - (c*a)/(b**2) - cn*(a/b)
  # xs*[1 + ((a/b)**2)] = - (c*a)/(b**2) - cn*(a/b)
  xs = (-(c*a)/(b**2) - cn*(a/b))/(1 + ((a/b)**2))

  yo = (-a/b)*xs - (c/b)

  # Round to the hundred's place
  shrt_distance = round(pointDist(xs, yo, px, py)*100)/100
  print shrt_distance
  return shrt_distance

question = perpDist(10, 0, 1, 1, 1)
checkers.check_answer(question, 7.81)
Esempio n. 7
0
    b = float(b)
    c = float(c)

    if a == 0:
        return round((-c / b) * 10) / 10
    else:
        root = b**2 - 4 * a * c
        if root < 0:
            root = abs(root)
            j = complex(0, 1)
            x1 = (-b + j * math.sqrt(root)) / (2 * a)
            x2 = (-b - j * math.sqrt(root)) / (2 * a)
            return [x1, x2]
        else:
            x1 = (-b + math.sqrt(root)) / (2 * a)
            x2 = (-b - math.sqrt(root)) / (2 * a)
            return sorted([x1, x2])


question = quadraticRootsComplex(1, 2, -3)
checkers.check_answer(question, sorted([-3, 1]))

question = quadraticRootsComplex(1, 2, -3)
checkers.check_answer(question, sorted([1, -3]))

question = quadraticRootsComplex(0, 2, -3)
checkers.check_answer(question, 1.5)

question = quadraticRootsComplex(1, -2, 5)
checkers.check_answer(question, [complex(1, +2), complex(1, -2)])
Esempio n. 8
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers

a = 'hi'
x = [1, 2, [3, 'John', 4], 'Hi']

n = 1
question = a
ansValue = 'hi'
ansType = str
checkers.check_answer(question, ansValue, n)
checkers.check_answer(type(question), ansType, n)

n = 2
question = a[0]
ansValue = 'h'
ansType = str
checkers.check_answer(question, ansValue, n)
checkers.check_answer(type(question), ansType, n)

n = 3
question = a[1]
ansValue = 'i'
ansType = str
checkers.check_answer(question, ansValue, n)
checkers.check_answer(type(question), ansType, n)

n = 4
Esempio n. 9
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import math
import types
import checkers

#PART 1
def a(x):
  return x + 1

checkers.check_answer(type(a(1)), int)

def b(x):
  return x + 1.0
checkers.check_answer(type(b(1)), float)

def c(x, y):
  return x + y

#Answer is num
checkers.check_answer(type(c(1, 1)), int)
checkers.check_answer(type(c(1, 1.0)), float)
checkers.check_answer(type(c(1.0, 1.0)), float)

def d(x, y):
  return x > y

checkers.check_answer(type(d(1,2)), bool)
Esempio n. 10
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers


def multiA(m, n):
    if n > 0 and isinstance(n, int):
        i = 1
        total = 0
        while i <= n:
            total = total + m
            i = i + 1

        return round(total * 10) / 10
    else:
        print "The second argument must be a positive integer"


question = multiA(1, 2)
checkers.check_answer(question, 2)

question = multiA(3.2, 3)
checkers.check_answer(question, 9.6)

question = multiA(7.7, 3)
checkers.check_answer(question, 23.1)
Esempio n. 11
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers

def multiA(m, n):
  if n > 0 and isinstance(n, int):
    i = 1
    total = 0
    while i <= n:
      total = total + m
      i = i + 1

    return round(total*10) / 10
  else:
    print "The second argument must be a positive integer"

question = multiA(1, 2)
checkers.check_answer(question, 2)

question = multiA(3.2, 3)
checkers.check_answer(question, 9.6)

question = multiA(7.7, 3)
checkers.check_answer(question, 23.1)
Esempio n. 12
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers
import math


def a(x, y, z):
    if x:
        return y
    else:
        return z


def b(y, z):
    return a(y > z, y, z)


question = a(False, 2, 3)
checkers.check_answer(question, 3)

question = b(3, 2)
checkers.check_answer(question, 3)

question = a(3 > 2, a, b)
checkers.check_answer(question, "Function")

question = b(a, b)
checkers.check_answer(question, "Function")
Esempio n. 13
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers
import math


def quadraticRoots(a, b, c):
    a = float(a)
    b = float(b)
    c = float(c)

    if a == 0:
        return round((-c / b) * 10) / 10
    else:
        x1 = (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)
        x2 = (-b - math.sqrt(b**2 - 4 * a * c)) / (2 * a)
        return sorted([x1, x2])


question = quadraticRoots(1, 2, -3)
checkers.check_answer(question, sorted([-3, 1]))

question = quadraticRoots(1, 2, -3)
checkers.check_answer(question, sorted([1, -3]))

question = quadraticRoots(0, 2, -3)
checkers.check_answer(question, 1.5)
Esempio n. 14
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers


def arithmetic(v, a, b, c):
    if v > 0:
        return a
    elif v == 0:
        return b
    else:
        return c


question = arithmetic(1, 2, 3, 4)
checkers.check_answer(question, 2)

question = arithmetic(0, 2, 3, 4)
checkers.check_answer(question, 3)

question = arithmetic(-2, 2, 3, 4)
checkers.check_answer(question, 4)

question = arithmetic(-2, "blah", "bleh", "nom")
checkers.check_answer(question, "nom")
Esempio n. 15
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers

def mod(m, n):
  if (isinstance(n, int) and n > 0) and (isinstance(m, int) and m > 0):
    while m >= n:
      m = m - n

    return m
  else:
    print "Both arguments must be positive integers"


question = mod(3, 2)
checkers.check_answer(question, 1)

question = mod(2, 3)
checkers.check_answer(question, 2)

question = mod(11, 7)
checkers.check_answer(question, 4)

question = mod(11, 2)
checkers.check_answer(question, 1)

question = mod(10, 2)
checkers.check_answer(question, 0)
Esempio n. 16
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers
n = 0

def evalPolynomial(coeffs, x):
  n = len(coeffs)-1
  answer = sum([a*x**(n-i) for i,a in enumerate(coeffs)])

  return answer

n += 1
coeffs = [1, 2, 3]
x = 2
question = evalPolynomial(coeffs, x)
ansvalue = 11
checkers.check_answer(question, ansvalue, n)

n += 1
coeffs = [5, 3, 2, 1]
x = 4
question = evalPolynomial(coeffs, x)
ansvalue = 377
checkers.check_answer(question, ansvalue, n)
Esempio n. 17
0
import sys
import os.path

sys.path.append(os.path.join(os.path.dirname(__file__), "../modules/"))

import math
import checkers

checkers.check_answer(type(3 + 5.0), float)
checkers.check_answer(type(5 / 2), int)
checkers.check_answer(type(5 / 2 == 5 / 2.0), bool)
checkers.check_answer(type(5 / 2.0), float)
checkers.check_answer(type(round(2.6)), float)
checkers.check_answer(type(int(2.6)), int)
checkers.check_answer(type(math.floor(2.6)), float)
checkers.check_answer(type(2.0 + 5.0), float)
checkers.check_answer(type(5 * 2 == 5.0 * 2.0), bool)
Esempio n. 18
0
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import math
import checkers

a = 10


def f(x):
    return x + a


a = 3
question = f(1)
checkers.check_answer(question, 4)
checkers.check_answer(type(question), int)

x = 12


def g(x):
    x = x + 1

    def h(y):
        return x + y

    return h(6)


question = g(x)
Esempio n. 19
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers
import math

#This is just asking for me to create a function that calculates distance!

def pointDist(x1, y1, x2, y2):
  d1 = x2 - x1
  d2 = y2 - y1
  square_distance = d1**2 + d2**2
  distance = math.sqrt(square_distance)
  return distance

question = pointDist(0, 1, 3, 1)
checkers.check_answer(question, 3)

question = pointDist(4, 1, 4, 1)
checkers.check_answer(question, 0)

question = pointDist(4, 1, 4, 7)
checkers.check_answer(question, 6)

question = pointDist(0, 0, 4, 3)
checkers.check_answer(question, 5)
Esempio n. 20
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers

#This is just asking for me to create a function that raises things to the 4th power


def square(x):
    return x**2


def fourthPower(x):
    return square(square(x))


question = fourthPower(2)
checkers.check_answer(question, 16)

question = fourthPower(3)
checkers.check_answer(question, 81)
Esempio n. 21
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import math
import checkers

a = 10
def f(x):
  return x + a
a = 3
question = f(1)
checkers.check_answer(question, 4)
checkers.check_answer(type(question), int)

x = 12
def g(x):
  x = x + 1
  def h(y):
    return x + y
  return h(6)

question = g(x)
checkers.check_answer(question, 19)
checkers.check_answer(type(question), int)
Esempio n. 22
0
def p2(x):
    if isinstance(x, int):
        if x > 1:
            i = 1
            while i < x:
                ans = i
                i = i * 2

            return ans
        else:
            return 0
    else:
        print "Argument must be an integer"


question = p2(9)
checkers.check_answer(question, 8)

question = p2(-1)
checkers.check_answer(question, 0)

question = p2(3)
checkers.check_answer(question, 2)

question = p2(17)
checkers.check_answer(question, 16)

question = p2(16)
checkers.check_answer(question, 8)
Esempio n. 23
0
    print "The second argument must be a positive integer"

def multiAgen(m, n):
  if isinstance(n, int) and isinstance(m, int):
    if m < 0 and n < 0:
      a1 = abs(m)
      a2 = abs(n)
    elif m > 0 and n < 0:
      a1 = n
      a2 = m
    else:
      a1 = m
      a2 = n

    return multiA(a1, a2)
  else:
    print "Both arguments must be integers"


question = multiAgen(3, 2)
checkers.check_answer(question, 6)

question = multiAgen(-3, 2)
checkers.check_answer(question, -6)

question = multiAgen(-3, -2)
checkers.check_answer(question, 6)

question = multiAgen(3, -2)
checkers.check_answer(question, -6)
Esempio n. 24
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers
import math

def a(x, y, z):
  if x:
    return y
  else:
    return z

def b(y, z):
  return a(y>z, y, z)

question = a(False, 2, 3)
checkers.check_answer(question, 3)

question = b(3, 2)
checkers.check_answer(question, 3)

question = a(3>2, a, b)
checkers.check_answer(question, "Function")

question = b(a, b)
checkers.check_answer(question, "Function")
Esempio n. 25
0
import os.path

sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import math
import checkers


def foo(x):
    def bar(x):
        return x + 1

    return bar(x * 2)


question = foo(3)
checkers.check_answer(question, 7)
checkers.check_answer(type(question), int)


def foo(x):
    def bar(z):
        return z + x

    return bar(3)


question = foo(2)
checkers.check_answer(question, 5)
checkers.check_answer(type(question), int)
Esempio n. 26
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers

#This is just asking for me to create a function that checks if something is odd!


def odd(x):
    return not (x % 2 == 0)


question = odd(128982)
checkers.check_answer(question, False)

question = odd(37)
checkers.check_answer(question, True)
Esempio n. 27
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers

def arithmetic(v, a, b, c):
  if v > 0:
    return a
  elif v == 0:
    return b
  else:
    return c

question = arithmetic(1, 2, 3, 4)
checkers.check_answer(question, 2)

question = arithmetic(0, 2, 3, 4)
checkers.check_answer(question, 3)

question = arithmetic(-2, 2, 3, 4)
checkers.check_answer(question, 4)

question = arithmetic(-2, "blah", "bleh", "nom")
checkers.check_answer(question, "nom")
Esempio n. 28
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import math
import checkers

def foo(x):
  def bar(x):
    return x + 1
  return bar(x*2)

question = foo(3)
checkers.check_answer(question, 7)
checkers.check_answer(type(question), int)

def foo(x):
  def bar(z):
    return z + x
  return bar(3)

question = foo(2)
checkers.check_answer(question, 5)
checkers.check_answer(type(question), int)
Esempio n. 29
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers


def compare(x, y):
    if x > y:
        return 1
    elif x == y:
        return 0
    else:
        return -1


question = compare(1, 1)
checkers.check_answer(question, 0)

question = compare(2, 7)
checkers.check_answer(question, -1)

question = compare(8, 5)
checkers.check_answer(question, 1)
Esempio n. 30
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers

#This is just asking for me to create a function that raises things to the 4th power

def square(x):
  return x**2

def fourthPower(x):
  return square(square(x))

question = fourthPower(2)
checkers.check_answer(question, 16)

question = fourthPower(3)
checkers.check_answer(question, 81)
Esempio n. 31
0
import sys
import os.path

sys.path.append(os.path.join(os.path.dirname(__file__), "../modules/"))

import checkers


def compare(x, y):
    if x > y:
        return 1
    elif x == y:
        return 0
    else:
        return -1


question = compare(1, 1)
checkers.check_answer(question, 0)

question = compare(2, 7)
checkers.check_answer(question, -1)

question = compare(8, 5)
checkers.check_answer(question, 1)
Esempio n. 32
0
import checkers


def div(m, n):
    if (isinstance(n, int) and n > 0) and (isinstance(m, int) and m > 0):
        i = 0
        while m >= n:
            m = m - n
            i = i + 1

        return i
    else:
        print "Both arguments must be positive integers"


question = div(3, 2)
checkers.check_answer(question, 1)

question = div(2, 3)
checkers.check_answer(question, 0)

question = div(11, 7)
checkers.check_answer(question, 1)

question = div(11, 2)
checkers.check_answer(question, 5)

question = div(6, 2)
checkers.check_answer(question, 3)
Esempio n. 33
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import math
import checkers

checkers.check_answer(type(3 + 5.0), float)
checkers.check_answer(type(5/2), int)
checkers.check_answer(type(5/2 == 5/2.0), bool)
checkers.check_answer(type(5/2.0), float)
checkers.check_answer(type(round(2.6)), float)
checkers.check_answer(type(int(2.6)), int)
checkers.check_answer(type(math.floor(2.6)), float)
checkers.check_answer(type(2.0 + 5.0), float)
checkers.check_answer(type( 5 * 2 == 5.0 * 2.0), bool)
Esempio n. 34
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers

def clip(lo, x, hi):
  low_comp = min( (lo, x) )
  high_comp = min( (x, hi) )
  comp = min( (lo, hi) )
  return max(low_comp, high_comp, comp)

question = clip(2, 1, 3)
checkers.check_answer(question, 2)

question = clip(3, 7, 9)
checkers.check_answer(question, 7)

question = clip(3, 12, 9)
checkers.check_answer(question, 9)
Esempio n. 35
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers
import math

#This is just asking for me to create a function that calculates the value of a quadratic equation!

def evalQuadratic(a, b, c, x):
  return a*x**2 + b*x + c

question = evalQuadratic(1, 1, 1, 1)
checkers.check_answer(question, 3)

question = evalQuadratic(2, 2, 2, 2)
checkers.check_answer(question, 14)

question = evalQuadratic(2, 3, 4, 5)
checkers.check_answer(question, 69) #hehe
Esempio n. 36
0
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers
n = 0


def zeroVector(n):
    return [0 for _ in range(n)]


def zeroArray(m, n):
    baseVector = zeroVector(n)
    return [baseVector for _ in range(m)]


n += 1
question = zeroArray(3, 3)
ansvalue = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
checkers.check_answer(question, ansvalue, n)

n += 1
question = zeroArray(4, 3)
ansvalue = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
checkers.check_answer(question, ansvalue, n)

n += 1
question = zeroArray(3, 4)
ansvalue = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
checkers.check_answer(question, ansvalue, n)
Esempio n. 37
0
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '../modules/'))

import checkers
import math

#This is just asking for me to create a function that calculates the value of a quadratic equation!


def evalQuadratic(a, b, c, x):
    return a * x**2 + b * x + c


question = evalQuadratic(1, 1, 1, 1)
checkers.check_answer(question, 3)

question = evalQuadratic(2, 2, 2, 2)
checkers.check_answer(question, 14)

question = evalQuadratic(2, 3, 4, 5)
checkers.check_answer(question, 69)  #hehe