sets.rst
changeset 229 5c647a197103
parent 143 e75538bca178
equal deleted inserted replaced
222:d5249a528cae 229:5c647a197103
   145  
   145  
   146 .. Author              : Nishanth
   146 .. Author              : Nishanth
   147    Internal Reviewer 1 : 
   147    Internal Reviewer 1 : 
   148    Internal Reviewer 2 : 
   148    Internal Reviewer 2 : 
   149    External Reviewer   :
   149    External Reviewer   :
       
   150 
       
   151 
       
   152 Questions
       
   153 =========
       
   154 
       
   155  1. If ``a = [1, 1, 2, 3, 3, 5, 5, 8]``. What is set(a)
       
   156 
       
   157    a. set([1, 1, 2, 3, 3, 5, 5, 8])
       
   158    #. set([1, 2, 3, 5, 8])
       
   159    #. set([1, 2, 3, 3, 5, 5])
       
   160    #. Error
       
   161 
       
   162    Answer: set([1, 2, 3, 5, 8])
       
   163 
       
   164  2. ``a = set([1, 3, 5])``. How do you find the length of a?
       
   165 
       
   166    Answer: len(a)
       
   167 
       
   168  3. ``a = set([1, 3, 5])``. What does a[2] produce?
       
   169 
       
   170    a. 1
       
   171    #. 3
       
   172    #. 5
       
   173    #. Error
       
   174 
       
   175    Answer: Error
       
   176 
       
   177  4. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
       
   178     is the value of ``odd | squares``?
       
   179 
       
   180    Answer: set([1, 3, 4, 5, 7, 9, 16])
       
   181 
       
   182  5. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
       
   183     is the value of ``odd - squares``?
       
   184 
       
   185    Answer: set([3, 5, 7])
       
   186 
       
   187  6. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
       
   188     is the value of ``odd ^ squares``?
       
   189 
       
   190    Answer: set([3, 4, 5, 7, 16])
       
   191 
       
   192  7. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
       
   193     does ``odd * squares`` give?
       
   194 
       
   195    a. set([1, 12, 45, 112, 9])
       
   196    #. set([1, 3, 4, 5, 7, 9, 16])
       
   197    #. set([])
       
   198    #. Error
       
   199 
       
   200    Answer: Error
       
   201 
       
   202  8. ``a = set([1, 2, 3, 4])`` and ``b = set([5, 6, 7, 8])``. What is ``a + b``
       
   203 
       
   204    a. set([1, 2, 3, 4, 5, 6, 7, 8])
       
   205    #. set([6, 8, 10, 12])
       
   206    #. set([5, 12, 21, 32])
       
   207    #. Error
       
   208 
       
   209  9. ``a`` is a set. how do you check if if a varaible ``b`` exists in ``a``?
       
   210 
       
   211    Answer: b in a
       
   212 
       
   213  10. ``a`` and ``b`` are two sets. What is ``a ^ b == (a - b) | (b - a)``?
       
   214 
       
   215    a. True
       
   216    #. False
       
   217 
       
   218    Answer: False
       
   219 
       
   220 
       
   221 Problems
       
   222 ========
       
   223 
       
   224  1. Given that mat_marks is a list of maths marks of a class. Find out the
       
   225     no.of duplicates marks in the list.
       
   226 
       
   227    Answer::
       
   228 
       
   229      unique_marks = set(mat_marks)
       
   230      no_of_duplicates = len(mat_marks) - len(unique_marks)
       
   231 
       
   232  2. Given that mat_marks is a list of maths marks of a class. Find how many
       
   233     duplicates of each mark exist.
       
   234 
       
   235    Answer::
       
   236 
       
   237      marks_set = set(mat_marks)
       
   238      for mark in marks_set:
       
   239          occurences = mat_marks.count(mark)
       
   240          print occurences - 1, "duplicates of", mark, "exist"