#!/usr/bin/env python3

peeps = {123: ['last', 'first', 32],
         445: ['vast', 'birst', 44],
         557: ['gast', 'kirst', 56]}
peeps[445][0] = 'ok'
peeps[557][1] = 'dokay'

msg = 'This is a test.  This is a test.'
words = msg.split(' ')
tot = len(words)
tot2 = len(msg)

def sum(x):
    total = 0
    for y in x:
        total += len(y)

w = 3
ww = 3.14
www = 4


# 3.1 False
# 3.2 False
# 3.3 0
# 3.4 True
# 3.5 f
# 3.6 True
# 3.7 T
# 3.8 False
# 3.9 1
# 3.1 8
# 3.11 last
# 3.12 None
# 3.13 1.5
# 3.14 This
# 3.15 8
# 3.16 32
# 3.17 16
# 3.18 5 + 3
# 3.19 False
# 3.2 None
# 3.21 False
# 3.22 1


#4.1) add an item 'hello' to words
words.append('hello')

#4.2) print the summation of v[2] over all v in peeps
# For some reason this code actually doesn't work
# But I'm not going to ask this on the exam
# The correct answer should be close to this however
#sum([v[2] for v in peeps.values()])

#4.3) set www to be ww times w
www = ww * w

#4.4) print v[0] for each value v in peeps
[print(v[0]) for v in peeps.values()]

#4.5) print last letter in msg
print(msg[-1])

#4.6) count how many times 'i' occurs in msg, print the result
print(msg.count('i'))

#4.7) set ww to be 3.14159
ww = 3.14159

#4.8) print last item in words
print(words[-1])



# 5.1) Parameter is a list of numbers, return the summation of the numbers
def sum_numbers(nums):
    total = 0
    for num in nums:
      total = total + num
    return total


# 5.2) Parameter is an integer n, print integers from 1 up to n
def print_numbers(n):
    for i in range(1,n):
        print(n)


# 5.3) Parameter is a string, return how many times - is in the string
def count_dashes(s):
    return s.count('-')

# 5.4) Parameters a filename f and string s - open f for writing, write string s to it, close file
def write_string(f, s):
    try:
        fd = open(f)
        fd.write(s)
        fd.close()
    except:
        # It would be rare that we would get an exception here.
        print('There was an error writing to the file.')

# 5.5) Parameters are string s and letter l, return index of first occurrence of l in s (or -1 if not present)
def first_ocurrence(s, l):

    for i in range(len(s)):
        if s[i] == l:
            return i
    # The letter was not found
    return -1

# 6.1
def selection_sort(arr):

    for i in range(len(arr)-1):
        # index of the minimum value
        minx = i

        for j in range(i+1, len(arr)):
            if arr[j] < arr[minx]:
                minx = j

        # Swap values
        tmp = arr[minx]
        arr[minx] = arr[i]
        arr[i] = tmp
        print('i = ', i, arr)

L = [1, 2, 3, 6, 5, 4]
selection_sort(L)

# i =  0 [1, 2, 3, 6, 5, 4]
# i =  1 [1, 2, 3, 6, 5, 4]
# i =  2 [1, 2, 3, 6, 5, 4]
# i =  3 [1, 2, 3, 4, 5, 6]
# i =  4 [1, 2, 3, 4, 5, 6]

# 6.2) Base convert algorithm

def convertToBase(x, b):
    ans = ''
    while x > 0:
        # We can just add print statements to verify the output
        ans = str(x % b) + ans
        print('x%b= ', x%b)
        x = x // b
        print(x)
    return ans

print(convertToBase(35, 2))

# Write what x is each time it changes, and x%b each time it gives a new digit of the answer

# x: 35, 17, 8, 4, 2, 1, 0


# x % b: 1, 1, 0, 0, 0, 1


# Final value printed to screen:
100011