###############################################################################
# -------------------- 15-112 Recitation 3: Strings -------------------- #

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

# --------------------------------------------------------------------------- #
###############################################################################
# largestNumber
###############################################################################
#LEARNING OBJECTIVES
# What is the best template?
# When to use the best template?

"""
Write the function largestNumber(text) that takes a string of text and 
returns the largest int value that occurs within that text, or None if no such 
value occurs. You may assume that the only numbers in the text are non-negative 
integers and that numbers are always composed of consecutive digits
"""

def largestNumber(s):
    return "42"


###############################################################################
# Rotate String
###############################################################################
# LEARNING OBJECTIVES
# What is indexing?
# What is string slicing? What are the arguments?


"""
Write the function rotateStringLeft that takes a string s and a non-negative
integer k, and returns the string s rotated k places to the left.
"""
def rotateStringLeft(s, k):
    return "42"

def testRotateStringLeft():
    testStr = "shrek"
    assert(rotateStringLeft(testStr, 2) == "reksh")
    assert(rotateStringLeft(testStr, 13) == "ekshr")

"""
Then write the function rotateStringRight that takes a string s and a
non-negative integer k, and returns the string s rotated k places to the right.
"""
def rotateStringRight(s, k):
    return "42"

def testRotateStringRight():
    testStr = "donkey"
    assert(rotateStringRight(testStr, 2) == "eydonk")
    assert(rotateStringRight(testStr, 13) == "ydonke")

###############################################################################
# Word Wrap
###############################################################################
# LEARNING OBJECTIVES
# What are string methods and how do we use them?
# How do we work with multi-line strings?


"""
Write the function wordWrap(text, width) that takes a string of text
(containing only lowercase letters or spaces) and a positive integer width, 
and returns a possibly-multiline string that matches the original string, 
only with line wrapping at the given width. 
So wordWrap("abc", 3) just returns "abc", but wordWrap("abc",2) returns a 
2-line string, with "ab" on the first line and "c" on the second line. 
After you complete word wrapping in this way, only then: 
All spaces at the start and end of each resulting line should be removed, 
and then all remaining spaces should be converted to dashes ("-"),
so they can be easily seen in the resulting string.
"""
def wordWrap(text, width):
    return "42"

def testWordWrap():
    assert(wordWrap("abcdefghij", 4)  ==  """\
abcd
efgh
ij""")
    assert(wordWrap("a b c de fg",  4)  ==  """\
a-b
c-de
fg""")

###############################################################################
# Script Generator
###############################################################################
# LEARNING OBJECTIVES
# How do we read/write files?
# How do we parse a multi-line string?


"""
First, let's say Katherine is in a new play. The play has a script which is
stored in a .txt file. These are the contents of that file:

Katherine: hello
Sarah: sup
Katherine: I am the Piazza Queen
Abhi: Wait but I'm the Piazza guy
Sarah: I like pizza too
Katherine: I have leftovers from yesterday
Sarah: YAYYYY PIZZA PARTY
Abhi: And piazza party

Now, let's say Katherine wants to extract all of her lines. She can call the
following function:
  extractScriptLines(pathToFile, characterName)
with the following arguments:
  extractScriptLines('112script.txt', 'Katherine')
    
This function would:
- parse the '112script.txt' file
- identify every one line that belongs to 'Katherine'
- Copy all of those lines into a multi-line string
- Write that multi-line string into a file named: 'Katherine.txt'
"""
def extractScriptLines(pathToFile, characterName):
    return "42"

###############################################################################
# Collapse Whitespace
###############################################################################
# LEARNING OBJECTIVES
# How do we loop through a string?
# What is a boolean flag? When is it used?


"""
Without using the s.replace() method, write the function collapseWhitespace(s),
that takes a string s and returns an equivalent string except that each 
occurrence of whitespace in the string is replaced by a single space. 
So, for example, collapseWhitespace("a\t\t\tb\n\nc") replaces the three tabs 
with a single space, and the two newlines with another single space,
returning "a b c". Reminder, do not use s.replace() in your solution, because
you will be sad if you do.
"""
def collapseWhitespace(s):
    return "42"

def testCollapseWhitespace():
    assert(collapseWhitespace("a\nb") == "a b")
    assert(collapseWhitespace("a\n   \t    b") == "a b")
    assert(collapseWhitespace("a\n   \t    b  \n\n  \t\t\t c   ") == "a b c ")

###############################################################################
# Extra Problems (ROC/CT)
###############################################################################

def ct1(s, n):
    result =  ""
    d  =  0
    while (n > 0) and (len(s) > 0):
        if (s[-1].isdigit()):
            result += str((n%10)%int(s[-1]))
        else:
            result += chr(ord('D') + d)
        n //= 10
        s = s[1:-1]
        d += 1
    return result

print(ct1("abc3c3", 2468))

import string
def roc(s):
    assert((type(s) == str) and (len(s) == 5))
    i = 0
    for j in range(len(string.ascii_lowercase)):
        if (j % len(s) == 0): #j = 0, 5, 10, 15, 20, 25
            assert(s[i] == string.ascii_lowercase[j])
            i += 1
            if (i == len(s)):
                return True
    return False
