sets.rst
author Nishanth <nishanth@fossee.in>
Thu, 07 Oct 2010 10:07:22 +0530
changeset 229 5c647a197103
parent 143 e75538bca178
permissions -rw-r--r--
added questions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
142
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     1
Hello friends and welcome to the tutorial on Sets
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     2
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     3
{{{ Show the slide containing title }}}
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     4
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     5
{{{ Show the slide containing the outline slide }}}
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     6
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     7
In this tutorial, we shall learn
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     8
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
     9
 * sets
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    10
 * operations on sets
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    11
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    12
Sets are data structures which contain unique elements. In other words,
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    13
duplicates are not allowed in sets.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    14
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    15
Lets look at how to input sets.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    16
type
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    17
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    18
 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    19
    a_list = [1, 2, 1, 4, 5, 6, 7]
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    20
    a = set(a_list)
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    21
    a
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    22
     
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    23
We can see that duplicates are removed and the set contains only unique
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    24
elements. 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    25
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    26
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    27
    f10 = set([1, 2, 3, 5, 8])
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    28
    p10 = set([2, 3, 5, 7])
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    29
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    30
f10 is the set of fibonacci numbers from 1 to 10.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    31
p10 is the set of prime numbers from 1 to 10.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    32
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    33
Various operations that we do on sets are possible here also.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    34
The | character stands for union
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    35
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    36
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    37
    f10 | p10
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    38
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    39
gives us the union of f10 and p10
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    40
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    41
The & character stands for intersection.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    42
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    43
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    44
    f10 & p10
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    45
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    46
gives the intersection
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    47
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    48
similarly,
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    49
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    50
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    51
    f10 - p10
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    52
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    53
gives all the elements that are in f10 but not in p10
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    54
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    55
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    56
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    57
    f10 ^ p10
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    58
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    59
is all the elements in f10 union p10 but not in f10 intersection p10. In
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    60
mathematical terms, it gives the symmectric difference.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
    61
143
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    62
Sets also support checking of subsets.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    63
::
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    64
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    65
    b = set([1, 2])
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    66
    b < f10
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    67
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    68
gives a True since b is a proper subset of f10.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    69
Similarly,
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    70
::
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    71
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    72
    f10 < f10
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    73
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    74
gives a False since f10 is not a proper subset.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    75
hence the right way to do would be
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    76
::
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    77
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    78
    f10 <= f10
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    79
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    80
and we get a True since every set is a subset of itself.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    81
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    82
Sets can be iterated upon just like lists and tuples. 
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    83
::
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    84
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    85
    for i in f10:
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    86
        print i,
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    87
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    88
prints the elements of f10.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    89
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    90
The length and containership check on sets is similar as in lists and tuples.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    91
::
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    92
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    93
    len(f10)
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    94
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    95
shows 5. And
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    96
::
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    97
    
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    98
    1 in f10
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
    99
    2 in f10
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   100
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   101
prints True and False respectively
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   102
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   103
The order in which elements are organised in a set is not to be relied upon 
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   104
since sets do not support indexing. Hence, slicing and striding are not valid
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   105
on sets.
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   106
142
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   107
{{{ Pause here and try out the following exercises }}}
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   108
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   109
%% 1 %% Given a list of marks, marks = [20, 23, 22, 23, 20, 21, 23] 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   110
        list all the duplicates
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   111
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   112
{{{ continue from paused state }}}
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   113
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   114
Duplicates marks are the marks left out when we remove each element of the 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   115
list exactly one time.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   116
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   117
::
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   118
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   119
    marks = [20, 23, 22, 23, 20, 21, 23] 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   120
    marks_set = set(marks)
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   121
    for mark in marks_set:
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   122
        marks.remove(mark)
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   123
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   124
    # we are now left with only duplicates in the list marks
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   125
    duplicates = set(marks)
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   126
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   127
{{{ Show summary slide }}}
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   128
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   129
This brings us to the end of the tutorial.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   130
we have learnt
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   131
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   132
 * How to make sets from lists
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   133
 * How to input sets
143
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   134
 * How to perform union, intersection and symmectric difference operations
e75538bca178 added subsets and changed the ordering
nishanth
parents: 142
diff changeset
   135
 * How to check if a set is a subset of other
142
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   136
 * The various similarities with lists like length and containership
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   137
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   138
{{{ Show the "sponsored by FOSSEE" slide }}}
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   139
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   140
#[Nishanth]: Will add this line after all of us fix on one.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   141
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   142
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   143
Hope you have enjoyed and found it useful.
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   144
Thankyou
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   145
 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   146
.. Author              : Nishanth
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   147
   Internal Reviewer 1 : 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   148
   Internal Reviewer 2 : 
7bc28afff7ab initial commit of sets
nishanth
parents:
diff changeset
   149
   External Reviewer   :
229
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   150
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   151
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   152
Questions
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   153
=========
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   154
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   155
 1. If ``a = [1, 1, 2, 3, 3, 5, 5, 8]``. What is set(a)
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   156
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   157
   a. set([1, 1, 2, 3, 3, 5, 5, 8])
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   158
   #. set([1, 2, 3, 5, 8])
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   159
   #. set([1, 2, 3, 3, 5, 5])
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   160
   #. Error
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   161
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   162
   Answer: set([1, 2, 3, 5, 8])
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   163
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   164
 2. ``a = set([1, 3, 5])``. How do you find the length of a?
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   165
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   166
   Answer: len(a)
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   167
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   168
 3. ``a = set([1, 3, 5])``. What does a[2] produce?
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   169
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   170
   a. 1
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   171
   #. 3
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   172
   #. 5
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   173
   #. Error
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   174
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   175
   Answer: Error
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   176
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   177
 4. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   178
    is the value of ``odd | squares``?
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   179
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   180
   Answer: set([1, 3, 4, 5, 7, 9, 16])
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   181
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   182
 5. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   183
    is the value of ``odd - squares``?
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   184
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   185
   Answer: set([3, 5, 7])
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   186
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   187
 6. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   188
    is the value of ``odd ^ squares``?
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   189
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   190
   Answer: set([3, 4, 5, 7, 16])
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   191
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   192
 7. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   193
    does ``odd * squares`` give?
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   194
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   195
   a. set([1, 12, 45, 112, 9])
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   196
   #. set([1, 3, 4, 5, 7, 9, 16])
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   197
   #. set([])
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   198
   #. Error
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   199
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   200
   Answer: Error
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   201
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   202
 8. ``a = set([1, 2, 3, 4])`` and ``b = set([5, 6, 7, 8])``. What is ``a + b``
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   203
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   204
   a. set([1, 2, 3, 4, 5, 6, 7, 8])
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   205
   #. set([6, 8, 10, 12])
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   206
   #. set([5, 12, 21, 32])
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   207
   #. Error
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   208
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   209
 9. ``a`` is a set. how do you check if if a varaible ``b`` exists in ``a``?
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   210
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   211
   Answer: b in a
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   212
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   213
 10. ``a`` and ``b`` are two sets. What is ``a ^ b == (a - b) | (b - a)``?
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   214
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   215
   a. True
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   216
   #. False
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   217
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   218
   Answer: False
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   219
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   220
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   221
Problems
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   222
========
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   223
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   224
 1. Given that mat_marks is a list of maths marks of a class. Find out the
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   225
    no.of duplicates marks in the list.
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   226
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   227
   Answer::
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   228
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   229
     unique_marks = set(mat_marks)
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   230
     no_of_duplicates = len(mat_marks) - len(unique_marks)
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   231
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   232
 2. Given that mat_marks is a list of maths marks of a class. Find how many
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   233
    duplicates of each mark exist.
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   234
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   235
   Answer::
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   236
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   237
     marks_set = set(mat_marks)
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   238
     for mark in marks_set:
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   239
         occurences = mat_marks.count(mark)
5c647a197103 added questions
Nishanth <nishanth@fossee.in>
parents: 143
diff changeset
   240
         print occurences - 1, "duplicates of", mark, "exist"