Exemple #1
0
def answer1(x, y):
    # your code here
    id = 0
    for i in range(x):
        id += i + 1
        for j in range(y):
            id += j
    return str((x + y - 2) * (x + y - 1) / 2 + x)
Exemple #2
0
def prblm2(n):
    myList= range(1,n)
    print(myList)
    #squareList = map(makeSquare, myList)
    #print(squareList)
    #listSum = sum(squareList)
    #return listSum

    return reduce(sum, map(makeSquare, range(1,n)))
Exemple #3
0
 def __str__(self):
     result = '|'
     for row in range(len(self.__board)):   
         for col in range(self.__width):
             result += self.__board[row][col] + '|'
         result += '\n|'
     result = result[:-2]
     result += '\n' + (((self.__width*2) +1) * '-') + '\n' 
     for x in range(self.__width):
         result += ' ' + str(x)
     
     return result
Exemple #4
0
def prime(n):
    '''Returns True if the number is prime, or False if composite. Given a positive integer n.'''
    if n in [0,1]:
        return False
    elif True in (map(divides(n),(range(2,n)))):
        return False
    return True
Exemple #5
0
def prime(n):
    """return whether or not an integer is prime"""
    possible_divisors = range(2, int(math.sqrt(n)))
    divisors = filter(lambda x: n % x == 0, possible_divisors)
    if (divisors == []):
        return True
    return len(divisors) == 0
Exemple #6
0
 def delMove(self, col):
     '''deletes the top x or o in a column'''
     if self.allowsMove(col) == True:
         for row in range(len(self.__board)):
             if self.__board[row][col] != ' ':
                 self.__board[row][col] = ' '
                 break
Exemple #7
0
def primes(n):
    def sieve(lst):
        if lst == []:
            return []
        return [lst[0]] + sieve(filter(lambda x: x % lst[0] != 0, lst[1:]))

    return sieve(range(2, n + 1))
Exemple #8
0
 def winsFor(self, ox):
     '''returns a bool determining if anyone wins on the current board state'''
     for directionx, directiony in [[1, -1], [1, 1], [1, 0], [0, 1]]:
         for row in range(len(self.__board)):
             for col in range(self.__width):
                 count = 0
                 for check in range(self.__width):
                     if row + check * directiony >= self.__height or col + check * directionx >= self.__width:
                         break
                     current = self.__board[row + check * directiony][col + check * directionx]
                     if current == ox:
                         count += 1
                     else:
                         count = 0
                     if count >= 4:
                         return True
     return False
Exemple #9
0
def e(n):
    '''Approximates the mathematical value e using a Taylor expansion.'''
    numberList = range(1, n + 1)
    factorialList = map(math.factorial, numberList)
    inverseList = map(inverse, factorialList)
    addedList = reduce(add, inverseList)

    return 1 + addedList
Exemple #10
0
def prime_below(n):
    '''Returns the list of all prime numbers less than or equal to n.'''
    def sieve(lst):
        if lst == []:
            return []
        return [lst[0]] + sieve(filter(lambda x: x % lst[0] != 0, lst[1:]))

    return sieve(range(2, n + 1))
Exemple #11
0
def primes(n):
    """returns a list of primes in the range [2,n] computed via the sieve of
    Erathosthenes. - pronounced civ (civ 5)"""
    def sieve(lst):
        if lst == []:
            return []
        return [lst[0]] + sieve(filter(lambda x: x % lst[0] != 0, lst[1:]))

    return sieve(range(2, n + 1))
Exemple #12
0
 def addMove(self, col, ox):
     '''adds the move to the board'''
     if ox == 'X' or ox == 'O':
         if self.allowsMove(col) == True:
             for row in range(len(self.__board)):
                 if self.__board[row][col] != ' ':
                     self.__board[row - 1][col] = ox
                     break
                 elif self.__board[row][col] == ' ' and row == self.__width - 2: 
                     self.__board[self.__height - 1][col] = ox  
Exemple #13
0
def prime(n):
    '''detects whether a number is prime or not.'''
    ##is n divisible by any number other than 1 and n??
    '''if True in map(divides(n),range(2,n)) or n == 0 or n == 1: 
        ## divides is taking the variables as argument but it needs to be n and divided by the variables.##
        return False
    return True '''
    possible_divisors = range(2, math.ceil(math.sqrt(n) + 1))
    f = divides(n)
    composite_lst = map(f, possible_divisors)
    return not reduce(sum, composite_lst)
Exemple #14
0
 def helper(start, finish):
     if (finish - start == 1):  #only 1 long
         return start
     if (finish - start == 0):  #no range
         return 0
     if (finish - start <
             5):  #if length is less than 5, return the power (I don't
         #know why, but this just works
         return reduce(lambda a, b: a ^ b, range(start, finish))
     else:
         #gives the end of the worker list fed into the recursive function
         #this function makes the correct list to look at in the function
         return helper(start, start / 4 * 4 + 4) ^ helper(
             finish / 4 * 4, finish)
Exemple #15
0
 def __init__(self, width = 7, height = 6):
     self.__width = width
     self.__height = height
     
     self.__board = []
     
     '''
     for i in range(width):
         self.__board.append([])
         for j in range(height):
             self.__board[i].append(' ')
     '''
     for i in range(height):
         self.__board.append([' '] * self.__width)
Exemple #16
0
def answer3(start, length):
    #list of workers is always from length^2-l+start to that +l
    list_of_workers = [(length * (length - l) + start,
                        length * (length - l) + start + l)
                       for l in range(length, 0, -1)]

    def helper(start, finish):
        if (finish - start == 1):  #only 1 long
            return start
        if (finish - start == 0):  #no range
            return 0
        if (finish - start <
                5):  #if length is less than 5, return the power (I don't
            #know why, but this just works
            return reduce(lambda a, b: a ^ b, range(start, finish))
        else:
            #gives the end of the worker list fed into the recursive function
            #this function makes the correct list to look at in the function
            return helper(start, start / 4 * 4 + 4) ^ helper(
                finish / 4 * 4, finish)

    #the new list is inputted from the helper method, from start to finish
    new_xor = [helper(start, finish) for start, finish in list_of_workers]
    return reduce(lambda a, b: a ^ b, new_xor)
Exemple #17
0
def sumOfSquares(n):
    return reduce(add, map(sqr, range(1, n + 1)))
Exemple #18
0
def gauss(n):
    #return sum(range(1, n + 1))
    return reduce(add, range(1, n + 1))
Exemple #19
0
def sum_of_squares(n):
    return reduce(map(sqr, range(1, n + 1)))
Exemple #20
0
def gauss(n):
    return reduce(add, range(1, n + 1))
Exemple #21
0
def prime(n):
    '''checks if a number is prime or composite'''
    return (sum(map(divides(n), range(2, n))) == 0)
Exemple #22
0
def factorial(n):
    '''returns the factorial of n'''
    return reduce(mult, range(1, n+1))
Exemple #23
0
def sum_of_squares(n):
    '''Takes as input a positive integer in and returns the sum: 1^2 + 2^2 + 3^2... n^2'''
    return reduce(add, map(square, range(1, n + 1)))
Exemple #24
0
def gauss(n):
    '''Takes as input a positive integer in and returns the sum: 1+2+3+...n'''
    return reduce(add, range(1, n + 1))
Exemple #25
0
def prime(n):
    """Returns True if n is prime and False if n is not"""
    if map(divides(n), list(range(2, n))) == [False] * (n - 2):
        return True
    return False
Exemple #26
0
def factorial(n):
    """Returns n!"""
    return reduce(mult, list(range(1, n + 1)))
Exemple #27
0
def prime(n):
    '''Returns True if n is prime and False otherwise by testing all possible divisors from 2 to n-1 or sqrt of n.'''
    possible_divisors = range(2, int(math.sqrt(n)) + 1)
    divisors = filter(lambda x: n % x == 0, possible_divisors)
    return len(divisors) == 0
Exemple #28
0
def gauss(n):
    """takes as input a positive integer n and returns the sum 1 + 2 + 3...
    """
    return reduce(add, range(1, n + 1))
Exemple #29
0
def sum_of_squares(n):
    """takes as input a positive integer a and returns the sum
    1^2 + 2^2 + 3^2...n^2"""
    return reduce(add, map(square, range(1, n + 1)))
Exemple #30
0
def sum_of_squares(n):
    """Takes as input a positive integer n and return the sum 1^2 + 2^2 + 3^2 +... + n^2"""
    return reduce(add, map(sqr, range(1, n + 1)))