def isPal(number): number = str(number) num_len = len(number) num_half = len(number) / 2 for x in range(0, num_half): if number[x] != number[num_len - x - 1]: return False helper.result('906609', number) sys.exit()
def factorize(target, primes, factor): multiplier = 2 factored = True sieve = range(primes[len(primes)-1]+1, factor) for i in range(0, len(primes)): if target % primes[i] == 0: target = target / primes[i] print target if isPrime(target): helper.result(6857, target) sys.exit() if i == len(primes) - 1: factored = False factor = factor * multiplier primes = reducer(factor, sieve, primes, target) factorize(target, primes, factor) i += 1
def generate_ai_move(board): """Determine the optimal move for the AI to make and the send it""" if terminal(board): # if the game is over, do nothing pass else: move = minimax(board) # use minimax algorithm to generate optimal move res = result(board, move[0]) emit("update", res)
def factorize(target, primes, factor): multiplier = 2 factored = True sieve = range(primes[len(primes) - 1] + 1, factor) for i in range(0, len(primes)): if target % primes[i] == 0: target = target / primes[i] print target if isPrime(target): helper.result(6857, target) sys.exit() if i == len(primes) - 1: factored = False factor = factor * multiplier primes = reducer(factor, sieve, primes, target) factorize(target, primes, factor) i += 1
def backlog_delete(id): return result(Backlog().get(id))
def backlog_add(): return result(Backlog().add(request.json))
def user_signout(): print "LOGOUT" return result(User().signout())
def user_current(): return result(User().current())
def user_delete(id): return result(User().delete(id))
def user_add(): return result(User().add(request.json))
def error_permissions(error): return result(dict({"error": "Permissions error"})), 401
def issue_get(id): return result(Issue().get(id))
#!/usr/bin/python from time import time import helper ''' Problem: Find the sum all all numbers that are multiples of 3 or 5 from 1 to 999 Solution: Sum all the multiples of 3, sum all the multiples of 5, add them together, sum all the multiples of 15, and subtract. ''' def sumMultiplesOf(x): num = 0 tot = 0 while (num <= 999): if (num % x == 0): tot += num num += 1 return tot newTot = sumMultiplesOf(3) + sumMultiplesOf(5) - sumMultiplesOf(15) helper.result(233168, newTot)
def make_move(json): """Send the state of the board after the desired move is made""" res = result(json["board"], tuple(json["move"])) emit("update", res)
def issue_add(): return result(Issue().add(request.json))
10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. Solution: Check each fib number to see if it's even. If so, add to the sum. Do this in a loop while fib is less than 4,000,000 Test: Fibs <= 40 are 1, 2, 3, 5, 8, 13, 21, 34 Evens are just 2, 8, 34 So the sum would be 2 + 8 + 34 = 44 ''' total = 2 maxnum = 4000000 def getFib(x, y, maxnum, total): fib = x + y if fib <= maxnum: if fib % 2 == 0: total += fib fib = getFib(y, fib, maxnum, total) return fib else: return total fib = getFib(1, 2, maxnum, total) helper.result(4613732, fib)
def issue_edit(id): return result(Issue().edit(id, request.json))
def user_get(id): return result(User().get(id))
def issue_delete(id): return result(Issue().delete(id))
def user_edit(id): return result(User().edit(id, request.json))
def issue_all(): return result(Issue().all(request.args))
def user_all(): return result(User().all(request.args))
def subissue_get(id): return result(Subissue().get(id))
def user_signin(): return result(User().signin(request.form))
def subissue_add(): return result(Subissue().add(request.json))
def backlog_get(id): return result(Backlog().get(id))
def subissue_edit(id): return result(Subissue().edit(id, request.json))
def backlog_edit(id): return result(Backlog().edit(id, request.json))
def subissue_delete(id): return result(Subissue().delete(id))
def backlog_all(): return result(Backlog().all(request.args))
def subissue_all(): return result(Subissue().all(request.args))
Problem: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. Solution: Check each fib number to see if it's even. If so, add to the sum. Do this in a loop while fib is less than 4,000,000 Test: Fibs <= 40 are 1, 2, 3, 5, 8, 13, 21, 34 Evens are just 2, 8, 34 So the sum would be 2 + 8 + 34 = 44 ''' total = 2 maxnum = 4000000 def getFib(x, y, maxnum, total): fib = x + y if fib <= maxnum: if fib % 2 == 0: total += fib fib = getFib(y, fib, maxnum, total) return fib else: return total fib = getFib(1, 2, maxnum, total) helper.result(4613732, fib)
#!/usr/bin/python import helper ''' 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 2^1000? ''' number = 2**1000 result = 0 while (number > 0): result = result + number % 10 number = number / 10 helper.result(1366, result)