###############################################################################
# ---------------- 15-112 Recitation 4: Testing, Debugging, Style ---------------- #

# 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 :)

# --------------------------------------------------------------------------- #
import string

###############################################################################
# Style review
###############################################################################
# LEARNING OBJECTIVES
# How do we know what is good and bad style?
# How do we fix bad style without modifying the functionality of the code?

'''
We've written a program, nthUndulatingNumber, that returns the nth undulating 
number. A number is undulating if it has at least three digits and has the form 
aba[bababab...] where a does not equal b. The program is functionally correct, 
but we've written this program with terrible style.

Fix this program so that it meets the 112 style guidelines without rewriting 
the main logic.
'''

def is_undulating(n):
    if n < 100:
        return False
    a = n % 10
    b = (n // 10) % 10
    n = n // 100
    blah = True
    while n > 0:
        digit = n % 10
        if blah:
          if digit != a:
              return False
        if (not blah):
            if digit != b:
                return False
        blah = not blah
        n = n // 10
    return a - b != 0

def nthUndulatingNumber(n):
    f= 0
    g =0
    while f<= n :
        g+=1
        if is_undulating(g):
            f += 1
    return g


###############################################################################
# Debug these functions!
###############################################################################
# LEARNING OBJECTIVES
# How do we identify bugs in our code?
# What different types of bugs might we encounter?
# How do we fix these bugs?


"""
Debug the following functions. In each, there is just one bug.Try to identify 
the bug before you run the code! This will help you understand where bugs are 
in your own code.  
Fix the bug by modifying ONLY ONE line.
Remove the ''' before debugging!
"""

# This program is supposed to return True if the letter 'C' appears in the string 
# and False otherwise.
'''
def hasLetterC(s):
    s = s.lower()
    for i in range(len(s)):
        if s[i] = 'c':
            return True
    return False
'''

# This program is supposed to return True if there are at least two identical 
# consecutive digits, and False otherwise.
'''
def hasConsecutiveDigits(n):
    if n < 0:
        return hasConsecutiveDigits(-n)
    while n > 0:
        nextN = n / 10
        if n % 10 == nextN % 10:
            return True
        n = nextN
    return False
'''


###############################################################################
# encodeRightLeftCipher
###############################################################################
#LEARNING OBJECTIVES
#How to approach large/difficult problems
#Practicing good problem solving habits

'''
Background: A right-left cipher, a kind of route cipher, is a fairly simple 
way to encrypt a message. It takes two values, some plaintext and a number of
rows, and it first constructs a grid with that number of rows and the minimum 
number of columns required, writing the message in successive columns. For example,
if the message is WEATTACKATDAWN, with 4 rows, the grid would be: 
 W T A W
 E A T N
 A C D
 T K A
We will assume the message only contains uppercase letters. We'll fill in the 
missing grid entries with lowercase letters starting from z and going in reverse 
(wrapping around if necessary), so we have:
 W T A W
 E A T N
 A C D z 
 T K A y

Next, we encrypt the text by reading alternating rows first to the right ("WTAW"),
then to the left ("NTAE"), then back to the right ("ACDz"), and back to the left 
("yAKT"), until we finish all rows. We precede these values with the number of rows 
itself in the string. So the encrypted value for the message WEATTACKATDAWN with 4 
rows is "4WTAWNTAEACDzyAKT".

Write the function encodeRightLeftCipher(message, rows) that takes an all-uppercase 
message and a positive integer number of rows, and returns the encoding as just described. 
'''

def encodeRightLeftCipher(message, rows):
    return "42"


###############################################################################
# decodeRightLeftCipher
###############################################################################
#LEARNING OBJECTIVES
#How to approach large/difficult problems
#Practicing good problem solving habits

'''
Write the function decodeRightLeftCipher, which takes an encoding from the previous 
problem and runs it in reverse, returning the plaintext that generated the encoding. 
For example, decodeRightLeftCipher("4WTAWNTAEACDzyAKT") returns "WEATTACKATDAWN". 
'''

def decodeRightLeftCipher(encodedMessage):
    return "42"
