sets/script.rst
author bhanu
Mon, 15 Nov 2010 15:14:12 +0530
changeset 507 34b8f90a88cb
parent 427 c193744340ba
child 508 a9a87fe550c1
permissions -rw-r--r--
Language check done for `sets`
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     1
.. Objectives
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     2
.. ----------
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     3
333
91b427241f8f Finished sets
Nishanth <nishanth@fossee.in>
parents: 281
diff changeset
     4
.. By the end of this tutorial, you will be able to 
91b427241f8f Finished sets
Nishanth <nishanth@fossee.in>
parents: 281
diff changeset
     5
91b427241f8f Finished sets
Nishanth <nishanth@fossee.in>
parents: 281
diff changeset
     6
.. * Create sets from lists
91b427241f8f Finished sets
Nishanth <nishanth@fossee.in>
parents: 281
diff changeset
     7
.. * Perform union, intersection and symmetric difference operations
91b427241f8f Finished sets
Nishanth <nishanth@fossee.in>
parents: 281
diff changeset
     8
.. * Check if a set is a subset of other
91b427241f8f Finished sets
Nishanth <nishanth@fossee.in>
parents: 281
diff changeset
     9
.. * understand various similarities with lists like length and containership
91b427241f8f Finished sets
Nishanth <nishanth@fossee.in>
parents: 281
diff changeset
    10
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    11
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    12
.. Prerequisites
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    13
.. -------------
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    14
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    15
..   1. Getting started with lists
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    16
     
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    17
.. Author              : Nishanth Amuluru
507
34b8f90a88cb Language check done for `sets`
bhanu
parents: 427
diff changeset
    18
   Internal Reviewer   : Punch
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    19
   External Reviewer   :
507
34b8f90a88cb Language check done for `sets`
bhanu
parents: 427
diff changeset
    20
   Language Reviewer   : Bhanukiran
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    21
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    22
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    23
Script
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    24
------
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    25
427
c193744340ba Cleaned up sets LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 333
diff changeset
    26
{{{ Show the slide containing title }}}
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    27
427
c193744340ba Cleaned up sets LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 333
diff changeset
    28
Hello friends and welcome to the tutorial on Sets
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    29
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    30
{{{ Show the slide containing the outline slide }}}
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    31
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    32
In this tutorial, we shall learn
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    33
427
c193744340ba Cleaned up sets LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 333
diff changeset
    34
 * sets 
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    35
 * operations on sets
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    36
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    37
Sets are data structures which contain unique elements. In other words,
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    38
duplicates are not allowed in sets.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    39
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    40
Lets look at how to input sets.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    41
type
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    42
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    43
 
427
c193744340ba Cleaned up sets LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 333
diff changeset
    44
    a_list = [1, 2, 1, 4, 5, 6, 2]
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    45
    a = set(a_list)
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    46
    a
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    47
     
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    48
We can see that duplicates are removed and the set contains only unique
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    49
elements. 
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    50
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    51
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    52
    f10 = set([1, 2, 3, 5, 8])
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    53
    p10 = set([2, 3, 5, 7])
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    54
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    55
f10 is the set of fibonacci numbers from 1 to 10.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    56
p10 is the set of prime numbers from 1 to 10.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    57
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    58
Various operations that we do on sets are possible here also.
427
c193744340ba Cleaned up sets LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 333
diff changeset
    59
The | (pipe) character stands for union
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    60
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    61
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    62
    f10 | p10
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    63
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    64
gives us the union of f10 and p10
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    65
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    66
The & character stands for intersection.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    67
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    68
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    69
    f10 & p10
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    70
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    71
gives the intersection
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    72
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    73
similarly,
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    74
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    75
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    76
    f10 - p10
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    77
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    78
gives all the elements that are in f10 but not in p10
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    79
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    80
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    81
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    82
    f10 ^ p10
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    83
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    84
is all the elements in f10 union p10 but not in f10 intersection p10. In
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    85
mathematical terms, it gives the symmectric difference.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    86
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    87
Sets also support checking of subsets.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    88
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    89
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    90
    b = set([1, 2])
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    91
    b < f10
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    92
427
c193744340ba Cleaned up sets LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 333
diff changeset
    93
gives a ``True`` since b is a proper subset of f10.
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    94
Similarly,
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    95
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    96
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    97
    f10 < f10
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    98
427
c193744340ba Cleaned up sets LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 333
diff changeset
    99
gives a ``False`` since f10 is not a proper subset.
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   100
hence the right way to do would be
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   101
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   102
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   103
    f10 <= f10
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   104
427
c193744340ba Cleaned up sets LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 333
diff changeset
   105
and we get a ``True`` since every set is a subset of itself.
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   106
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   107
Sets can be iterated upon just like lists and tuples. 
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   108
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   109
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   110
    for i in f10:
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   111
        print i,
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   112
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   113
prints the elements of f10.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   114
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   115
The length and containership check on sets is similar as in lists and tuples.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   116
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   117
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   118
    len(f10)
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   119
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   120
shows 5. And
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   121
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   122
    
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   123
    1 in f10
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   124
    2 in f10
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   125
427
c193744340ba Cleaned up sets LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 333
diff changeset
   126
prints ``True`` and ``False`` respectively
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   127
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   128
The order in which elements are organised in a set is not to be relied upon 
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   129
since sets do not support indexing. Hence, slicing and striding are not valid
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   130
on sets.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   131
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   132
{{{ Pause here and try out the following exercises }}}
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   133
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   134
%% 1 %% Given a list of marks, marks = [20, 23, 22, 23, 20, 21, 23] 
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   135
        list all the duplicates
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   136
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   137
{{{ continue from paused state }}}
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   138
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   139
Duplicates marks are the marks left out when we remove each element of the 
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   140
list exactly one time.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   141
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   142
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   143
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   144
    marks = [20, 23, 22, 23, 20, 21, 23] 
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   145
    marks_set = set(marks)
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   146
    for mark in marks_set:
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   147
        marks.remove(mark)
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   148
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   149
    # we are now left with only duplicates in the list marks
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   150
    duplicates = set(marks)
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   151
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   152
{{{ Show summary slide }}}
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   153
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   154
This brings us to the end of the tutorial.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   155
we have learnt
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   156
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   157
 * How to make sets from lists
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   158
 * How to input sets
281
d91133654fc8 Slides for sets LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 233
diff changeset
   159
 * How to perform union, intersection and symmetric difference operations
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   160
 * How to check if a set is a subset of other
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   161
 * The various similarities with lists like length and containership
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   162
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   163
{{{ Show the "sponsored by FOSSEE" slide }}}
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   164
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   165
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   166
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   167
Hope you have enjoyed and found it useful.
281
d91133654fc8 Slides for sets LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 233
diff changeset
   168
Thank you!
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   169