'''
Created on Nov 2, 2014

@author: rodger
'''
# dictionary of schools and how many students at an event
# are from each school

d = {'duke':30, 'unc':50, 'ncsu':40}

print "d.values()", d.values()
print "d.keys()", d.keys()
d['duke'] = 80
d.update({'ecu':40, 'uncc':70})
print "d.values()", d.values()

# what is the difference between these 3?

print "d.items()", d.items()
print "d.iteritems()", d.iteritems()
print "d", d

# problem - using the dictionary from above, write Python code 
# to determine how many schools have more than 40 attendees?

#count = 0
# for key in d.keys():
#     if d[key] > 40:
#         count += 1

# for val in d.values():
#     if val > 40:
#         count += 1

#count = len([key for (key,val) in d.iteritems() if val > 40])

#count = len([(k,v) for (k,v) in d.iteritems() if v>40])

count = len(dict((k,v) for (k,v) in d.iteritems() if v>40))


print "Number of schools with more than 40 attendees is ", count


# problem - Using the dictionary from above, write python code to determine 
# which schools have more than 40 attendees - give a list

#schools = []
# for (k,v) in d.iteritems():
#     if v > 40:
#         schools.append(k)
        # schools += [k]
# 
# for key in d.keys():
#     if d[key] > 40:
#     #if d.get(key) > 40:
#         schools.append(key)

schools = [key for (key,val) in d.iteritems() if val > 40]


print "Number of schools with more than 40 attendees is ", schools  


# problem -  Create a new dictionary mapping attendance numbers to list of schools
# with that attendance number

d2 = {}
for (key,val) in d.iteritems():
    if val in d2:       #is it already a key in d2
        d2[val] += [key]
        #d2.get(val).append(key)
    else:               #val is not in d2
        #d2.update({val: [key]})
        d2[val] = [key]
        
print d2



# problem create a new dictionary mapping groups of 50 attendees 0-49, 50-99, etc
d3 = {}
for (key,val) in d.iteritems():
    val = val/50 * 50
    if val in d3:
        d3[val] += [key]
        #d3[val].append(key)
    else:
        d3[val] = [key]


print d3
