fork(8) download
  1. # -*- coding: utf-8 -*-
  2. # class_dictionary.py
  3. # MIT OCW 6.189 Homework 3
  4. # Exercise 3.3 – An Introduction to Dictionaries
  5. # Mechanical MOOC
  6. # Judy Young
  7. # July 23, 2013
  8.  
  9. # For this exercise, write a dictionary that catalogs the classes you took last term
  10. # - the keys should be the class number, and the values should be the title of the class.
  11. # Then, write a function add class thattakes 2 arguments- a class number and a description
  12. # - that adds new classes to your dictionary. Use this function to add the classes you’re taking
  13. # next term to the dictionary.
  14. # Finally, write a function print classes that takes one argument
  15. # - a Course number (eg ’6’) - and prints out all the classes you took in that Course.
  16.  
  17. class_dictionary = {'6.002': 'Circuits and Electronics', '6.042J': 'Mathematics for Computer Science', '6.102': 'Introductory RF Design Laboratory', '8.033': 'Relativity' , '8.226': 'Forty-three Orders of Magnitude', '15.072J': 'Queues: Theory and Applications'}
  18.  
  19. def add_class(class_number, description):
  20. class_dictionary[class_number] = description
  21. print class_number, "-", description, "ADDED"
  22.  
  23. def remove_class(class_number):
  24. stash = class_dictionary[class_number]
  25. del class_dictionary[class_number]
  26. print class_number, "-", stash, "REMOVED"
  27.  
  28. # prints all classes give a course number
  29. def print_classes(course_number):
  30. total_classes = 0
  31. for classes in class_dictionary:
  32. if classes[0] == course_number:
  33. print classes, "-", class_dictionary[classes]
  34. total_classes += 1
  35. if total_classes == 0:
  36. print "No Course", course_number, "classes taken"
  37.  
  38. # print all classes, no limits
  39. def print_all_classes():
  40. print "\n\nAll Classes\n-----------"
  41. all_classes_grouped = sorted(class_dictionary.keys())
  42. for each in all_classes_grouped:
  43. print each, "-", class_dictionary[each]
  44. print "\n"
  45.  
  46.  
  47. print_all_classes()
  48.  
  49. add_class('4.109', 'ProtoArchitecture')
  50. add_class('15.082J', 'Network Optimization')
  51. add_class('4.171', 'Design Workshop: The Space Between ')
  52. add_class('6.182', 'Psychoacoustics Project Laboratory')
  53. add_class('6.254', 'Game Theory with Engineering Applications ')
  54. add_class('6.805', 'Ethics and the Law on the Electronic Frontier')
  55. remove_class('6.002')
  56.  
  57. print_all_classes()
  58.  
  59. print "\nAll my EECS classes\n-------------------\n", print_classes("6")
  60. print "\nAll my PoliSci classes\n---------------------\n", print_classes("17")
Success #stdin #stdout 0.08s 8888KB
stdin
Standard input is empty
stdout

All Classes
-----------
15.072J - Queues: Theory and Applications
6.002 - Circuits and Electronics
6.042J - Mathematics for Computer Science
6.102 - Introductory RF Design Laboratory
8.033 - Relativity
8.226 - Forty-three Orders of Magnitude


4.109 - ProtoArchitecture ADDED
15.082J - Network Optimization ADDED
4.171 - Design Workshop: The Space Between  ADDED
6.182 - Psychoacoustics Project Laboratory ADDED
6.254 - Game Theory with Engineering Applications  ADDED
6.805 - Ethics and the Law on the Electronic Frontier ADDED
6.002 - Circuits and Electronics REMOVED


All Classes
-----------
15.072J - Queues: Theory and Applications
15.082J - Network Optimization
4.109 - ProtoArchitecture
4.171 - Design Workshop: The Space Between 
6.042J - Mathematics for Computer Science
6.102 - Introductory RF Design Laboratory
6.182 - Psychoacoustics Project Laboratory
6.254 - Game Theory with Engineering Applications 
6.805 - Ethics and the Law on the Electronic Frontier
8.033 - Relativity
8.226 - Forty-three Orders of Magnitude



All my EECS classes
-------------------
6.182 - Psychoacoustics Project Laboratory
6.042J - Mathematics for Computer Science
6.254 - Game Theory with Engineering Applications 
6.102 - Introductory RF Design Laboratory
6.805 - Ethics and the Law on the Electronic Frontier
None

All my PoliSci classes
---------------------
No Course 17 classes taken
None