#------------------------------------------------------------------------------
#  DictionaryFunctions.py
#------------------------------------------------------------------------------

def histogram(s):
   d = dict()
   for c in s:
      if c not in d:
         d[c] = 1
      else:
         d[c] += 1
   return d
# end histogram()

def reverseLookup(d, v):
   for k in d:
      if d[k] == v:
         return k
      # end if
   # end for
# end reverseLookup()

def invertDictionary(D):
   I = dict()
   for key in D:
      val = D[key]
      if val not in I:
         I[val] = [key]
      else:
         I[val].append(key)
      # end if-else
   # end for
   return I
# end invertDictionary()


def printDictionary(D):
   for key in D:
      print(key, '\t', D[key])
   # end for
# end printDictionary()


# function main() -------------------------------------------------------------
def main():

   print()

   D = histogram('mississippi')
   print(D)
   printDictionary(D)

   print()

   print(1, reverseLookup(D, 1))
   print(2, reverseLookup(D, 2))
   print(3, reverseLookup(D, 3))
   print(4, reverseLookup(D, 4))

   print()

   printDictionary(D)
   I = invertDictionary(D)
   printDictionary(I)

   print()

# end main()

# closing conditional ---------------------------------------------------------
if __name__=='__main__':

   main()

# end


