sets/script.rst
author Puneeth Chaganti <punchagan@fossee.in>
Wed, 13 Oct 2010 11:13:46 +0530
changeset 314 11869b16d86b
parent 281 d91133654fc8
child 333 91b427241f8f
permissions -rw-r--r--
Getting started with sagenotebook LO - script and questions.
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
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     4
.. A - Students and teachers from Science and engineering backgrounds
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     5
   B - Will learn what are tuples and why they are needed
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     6
       Will learn the various methods of accessing elements in tuples
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     7
   C - 
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     8
   D - 
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     9
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    10
.. Prerequisites
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
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    13
..   1. Getting started with lists
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
.. Author              : Nishanth Amuluru
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    16
   Internal Reviewer   : 
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    17
   External Reviewer   :
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    18
   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
    19
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    20
Script
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    21
------
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
Hello friends and welcome to the tutorial on Sets
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
{{{ Show the slide containing title }}}
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    26
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    27
{{{ Show the slide containing the outline slide }}}
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    28
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    29
In this tutorial, we shall learn
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    30
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    31
 * sets
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    32
 * operations on sets
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    33
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    34
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
    35
duplicates are not allowed in 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
Lets look at how to input sets.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    38
type
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
 
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    41
    a_list = [1, 2, 1, 4, 5, 6, 7]
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    42
    a = set(a_list)
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    43
    a
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    44
     
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    45
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
    46
elements. 
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
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    49
    f10 = set([1, 2, 3, 5, 8])
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    50
    p10 = set([2, 3, 5, 7])
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 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
    53
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
    54
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    55
Various operations that we do on sets are possible here also.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    56
The | character stands for union
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
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    59
    f10 | p10
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
gives us the union of f10 and p10
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    62
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    63
The & character stands for intersection.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    64
::
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
    f10 & p10
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
gives the intersection
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    69
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    70
similarly,
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    71
::
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
    f10 - p10
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
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
    76
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
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    79
    f10 ^ p10
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
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
    82
mathematical terms, it gives the symmectric difference.
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
Sets also support checking of subsets.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    85
::
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
    b = set([1, 2])
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    88
    b < f10
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
gives a True since b is a proper subset of f10.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    91
Similarly,
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    92
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    93
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    94
    f10 < f10
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
gives a False since f10 is not a proper subset.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    97
hence the right way to do would be
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    98
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    99
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   100
    f10 <= f10
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
and we get a True since every set is a subset of itself.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   103
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   104
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
   105
::
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
    for i in f10:
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   108
        print i,
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
prints the elements of f10.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   111
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   112
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
   113
::
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
    len(f10)
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
shows 5. And
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   118
::
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
    1 in f10
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   121
    2 in f10
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
prints True and False respectively
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   124
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   125
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
   126
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
   127
on sets.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   128
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   129
{{{ Pause here and try out the following exercises }}}
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   130
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   131
%% 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
   132
        list all the duplicates
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
{{{ continue from paused state }}}
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   135
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   136
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
   137
list exactly one time.
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
::
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   140
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   141
    marks = [20, 23, 22, 23, 20, 21, 23] 
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   142
    marks_set = set(marks)
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   143
    for mark in marks_set:
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   144
        marks.remove(mark)
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   145
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   146
    # 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
   147
    duplicates = set(marks)
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
{{{ Show summary slide }}}
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   150
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   151
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
   152
we have learnt
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
 * How to make sets from lists
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   155
 * How to input sets
281
d91133654fc8 Slides for sets LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 233
diff changeset
   156
 * 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
   157
 * 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
   158
 * 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
   159
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   160
{{{ Show the "sponsored by FOSSEE" slide }}}
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   161
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   162
#[Nishanth]: Will add this line after all of us fix on one.
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   163
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
   164
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   165
Hope you have enjoyed and found it useful.
281
d91133654fc8 Slides for sets LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 233
diff changeset
   166
Thank you!
233
ab748264f726 Made sets.rst into the new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   167