###############################################################################
# ---------------- 15-112 Recitation 2 5/21 ---------------- #

# This is a starter file of the problems we did in recitation. A good way to
# use this file is to try to re-write problems you saw in recitation from
# scratch. This way, you can test your understanding and ask on Piazza or
# office hours if you have questions :)

# --------------------------------------------------------------------------- #
###############################################################################
# Overview of digitCount
###############################################################################
# LEARNING OBJECTIVES
# know the function digitCount and its uses

def digitCount(n):
    return 42


###############################################################################
# Code Tracing
###############################################################################
# LEARNING OBJECTIVES
# How do we approach a code tracing problem?
# How do code trace with loops?


def ct(n):
    k = 0
    total = 0
    while (n >= k):
        print('k =', k)
        for i in range(k):
            total += n%10
            n //= 10
            print(i, n%10, total)
        k += 1 
    print('total =', total)

# print(ct(123))



###############################################################################
# Reasoning Over Code
###############################################################################
# LEARNING OBJECTIVES
# How do we approach an ROC problem?
# How do we reason over code with loops?

def roc(n):
    d = 9
    while (d > 2):
        if (n % 10 != d): return False
        n //= 10
        d = 1 + d//2
    return (n == 42)

# n = ANSWER HERE
# print(roc(n))


###############################################################################
# hasSameNumberEvenOdd
###############################################################################
# LEARNING OBJECTIVES
# How do we use loops with numbers? 
# How do we keep track of evens/odds?
# How do we choose which type of loop to use for a problem?


# Write the function hasSameNumberEvenOdd(n) that takes a possibly- negative 
# int value n and returns True if that number contains the same number of odd digits 
# and even digits, and False otherwise.

def hasSameNumberEvenOdd(n)
    return 42




##############################################################################
# nthHappyPrime
##############################################################################
# LEARNING OBJECTIVES
# How do we break down larger problems into manageable pieces? (top-down design)
# What is the nth template?
# How do we write functions using loops?
# How do we decide which type of loop to use for each function?


# Write the function nthHappyPrime(n) that finds the nth happy prime. 
# A happy number is defined by the following process: Starting with any positive 
# integer, replace the number by the sum of the squares of its digits in base-ten,
# and repeat the process until the number either equals 1 (where it will stay), 
# or it loops endlessly in a cycle that does not include 1. Those numbers for 
# which this process ends in 1 are happy numbers, while those that do not end 
# in 1 are unhappy numbers (or sad numbers). For our purposes, we can simplify 
# the process of finding a happy number by saying that a cycle which reaches 1 
# indicates a happy number, while a cycle which reaches 4 indicates a number 
# that is unhappy. HINT: you may need to use several other functions


# A happy prime is a number that is both happy and prime. Write the function
# nthHappyPrime(n) which takes a non-negative integer and returns the nth happy
# prime number (where the 0th happy prime number is 7).
def nthHappyPrime(n):
  return 42



#################### Helper functions for nthHappyPrime #######################

# Write the function sumOfSquaresOfDigits(n) which takes a non-negative integer 
# and returns the sum of the squares of its digits. For example, 123 would 
# become 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14. 
def sumOfSquaresOfDigits(n):
  return 42


# Write the function isHappyNumber(n) which takes a possibly-negative integer 
# and returns True if it is happy and False otherwise. Note that all numbers 
# less than 1 are not happy.
def isHappyNumber(n):
  return False


###############################################################################
# Functions that will be useful (but you don't have to write yourself)
###############################################################################

# returns True if a number n is prime and False otherwise
def isPrime(n):
    if (n < 2):
        return False
    for factor in range(2,n):
        if (n % factor == 0):
            return False
    return True