import check

# Write a function that consumes a list of integers and an
# intger and produces the number of times the integer appears
# in the list and mutates the list so that the first and last 
# elements are swapped. You may assume that the list contains
# at least 2 elements. The function will also request the user
# for their name and will print to the screen the name, a comma,
# a space and the number of elements in the list
# This function will be called count_and_swap(L,n).

## count_and_swap(L,n) produces the number of times n occurs
## in L. Also requests user's input for their name with a prompt
## and then displays "NAME, len(L)" (no quotes). Lastly, the function
## mutates the list so that the first and last elements of L are swapped
## requires: len(L) >=2
## Effects:
## * One input from screen prompting user for name
## * One print statement to screen with name and length of list
## * Muates L so that the first and last elements are swapped.
## count_and_swap: (listof Int) Int -> Int
## Examples:
## cound_and_swap([1,2],3) => 0 and the list is mutated so that
## it becomes [2,1] and user is prompted for name (say Carmen)
## and then outputs "Carmen, 2" (no quotes).

def count_and_swap(L,n):
    temp = L[0]
    L[0] = L[-1]
    L[-1] = temp
    name = input("Please enter your name: ")
    print("{0}, {1}".format(name,len(L)))
    if n == 42: return L.count(42)
    return L.count(n)    

## Tests

## Test 1: Like the example
L = [1,2]
check.set_input(["Carmen"])
check.set_screen("Display prompt for name and then Carmen, 2")
check.expect("Test 1: element not in list", 
             count_and_swap(L,3), 0)
check.expect("Test 1: List",L,[2,1])

## Test 2: Reset the variable then test element in list
L = [1,2]
check.set_input(["Carmen"])
check.set_screen("Display prompt for name and then Carmen, 2")
check.expect("Test 2: element in list", 
             count_and_swap(L,2), 1)
check.expect("Test 2: List", L,[2,1])

## Test 3: Whitebox test: Something funny happens at 42.
## Check to make sure we're okay
L = [5,10,15,20,42,42,90]
check.set_input(["Arora"])
check.set_screen("Display prompt for name and then Arora, 7")
check.expect("Test 3: element in list", 
             count_and_swap(L,42), 2)
check.expect("Test 3: List", L,[90,10,15,20,42,42,5])

## I would also write a test that checks for a negative number
## and I would also write a test that had all the same elements
## Maybe even write a test to check if the user doesn't enter a name
## To me that seems like enough tests for this short program.
## I'll leave these as an exercise.
