Featured post

Why should I learn Go?

Image
What is unique about GO language? Here are some of the advantages of GO programming language:           Code runs fast           Garbage collection           Simpler objects           Efficient concurrency Code runs faster: Before understanding why GO runs faster, let us know the process of software translation. Basically, we have three broad categories of languages:             Machine level language ·        Machine level language is a low-level language where instructions are directly executed on the CPU. Machine level instructions are small steps which are straight forward and simple (Ex: ADD, SUBTRACT, MULTIPLY ) Assembly language ·        Assembly language is similar to machine level language but a bit more specific for humans to understand. For example, 1000...

Sets in Python

In this post we are going to learn about sets in Python.



What is a set ?

A set is an unordered collection of elements with no duplicate elements.

Where are sets used ?

Basic uses of sets include :
  • Membership testing
  • Elimination of duplicate entries.





Set operations also support mathematical operations such as union, intersection, difference and symmetric difference.

How to use sets ?

Sets can be implemented using curly braces {} or using the set() method.

For instance,   set = {2,5,6}

Note :

How do you create an empty set ?

We must use set() function to create an empty set. Declaring a set with empty braces will not work.

If you use {} braces, then you are not creating an empty set but a dictionary.

Examples :

>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket)                      # show that duplicates have been removed
{'orange', 'banana', 'pear', 'apple'}

>>> 'orange' in basket                 # fast membership testing
True
>>> 'crabgrass' in basket
False

>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in either a or b
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}

Similar to list comprehensions there are set comprehensions too.

>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
{'r', 'd'}


 Task on Sets :

Now, let's use our knowledge of sets and help Mickey.
Ms. Gabriel Williams is a botany professor at District College. One day, she asked her student Mickey to compute the average of all the plants with distinct heights in her greenhouse.

Formula used:

Input Format
The first line contains the integer, , the total number of plants.
The second line contains the  space separated heights of the plants.
Constraints
Output Format
Output the average height value on a single line.
Sample Input
10
161 182 161 154 176 170 167 171 170 174
Sample Output



169.375 


Solution :

n = int(input())   # read the input from the standard input

l = input()
li = l.split(" ")  

mList = list(map(int,li))  # converting the list of strings into integers

sets = set(mList)   # creating a set

modified_list = list(sets)  # convert the set into list  to access the items
sum = 0
for i in modified_list:
    sum = sum + i

print(sum/len(sets))  # printing the average
    

Popular posts from this blog

Introduction to Big Data and Hadoop

LocationManager vs GoogleApiClient

Why should I learn Go?