# p1review_lists.py
"""Examples involving lists and simple iteration.  During the in-class
Prelim 1 practice/review session we implemented two functions:
    count_non_space_chars
    inflate
An extra function build_set is included below."""

def count_non_space_chars(myList):
    """Returns: number of non-space characters in the strings in myList.
    Example: count_non_space_chars(['U ', 'r', '', ' gr8']) returns 5
    Precondition: myList is a list of strings.  Each string in myList can
    contain only spaces, letters, digits.
    """
    count= 0
    for s in myList:
        numSp= s.count(' ')
        numNonSp= len(s)-numSp
        count= count + numNonSp
    return count

def inflate(myList, p_percent):
    """Inflate each number in myList by p_percent while maintaining the type
    (int or float).  For any int in myList, round down the inflation.
    Precondition: myList is a list of positive numbers (int and/or float).
    Precondition: p_percent is a positive number (int or float)."""
    p_frac= p_percent/100
    for k in range(len(myList)):
        delta= myList[k]*p_frac
        if type(myList[k])==int:
            delta= int(delta)
        myList[k] += delta

def build_set(set,candidates):
    """Append items from list candidates to list set such that the items in set
    are distinct (no duplicate items).
    Example: >>> st= ['seesaw', 'saw', 10]
             >>> build_set(st, [7, 10, 'ten', 'Saw', 7])
             >>> st
             ['seesaw', 'saw', 10, 7, 'ten', 'Saw']
    Precondition: set is a non-nested list containing only distinct items; set
                  may be empty.
    Precondition: candidates is a non-nested list; candidates may be empty."""
    for item in candidates:
        if item not in set:
            set.append(item)

print('Count non-space characters')
L1= ['U ', 'r', '', ' gr8']
L2= ['hi','there']
L3= ['  ']
L= [L1,L2,L3]
for example in L:
    print('Number of non-space characters in '+str(example))
    print(count_non_space_chars(example))
print()

p= 1.6
print('Inflate values in list by ' + str(p) + '%')
L= [100, 100.0, 1, 1.0]
print('Original list:')
print(L)
print('Inflated list:')
inflate(L,p)
print(L)
print()

print('Build a set with distinct elements')
print('Original set:')
st= ['seesaw', 'saw', 10]
print(st)
print('Candidates to add to set:')
c= [7, 10, 'ten', 'Saw', 7]
print(c)
build_set(st,c)
print(st)
print()
