HackerRank Python Solution - Sets - .union() Operation

.union():
  • The .union() operator returns the union of a set and the set of elements in an iterable. Sometimes, the | operator is used in place of .union() operator, but it operates only on the set of elements in set.
  • Set is immutable to the .union() operation (or | operation).

HackerRank Python Solution - Sets - .discard(), .remove() & .pop()

.remove(x):
  • This operation removes element x from the set.
  • If element x does not exist, it raises a KeyError.
  • The .remove(x) operation returns None.
Example:

>>> s = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> s.remove(5)
>>> print s
set([1, 2, 3, 4, 6, 7, 8, 9])
>>> print s.remove(4)
None
>>> print s
set([1, 2, 3, 6, 7, 8, 9])
>>> s.remove(0)
KeyError: 0

HackerRank Python Solution - Sets - .add()

  • If we want to add a single element to an existing set, we can use the .add() operation.
  • It adds the element to the set and returns 'None'.
Example:

>>> s = set('HackerRank')
>>> s.add('H')
>>> print s
set(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R'])
>>> print s.add('HackerRank')
None
>>> print s
set(['a', 'c', 'e', 'HackerRank', 'H', 'k', 'n', 'r', 'R'])

HackerRank Python Solution - Sets - Introduction to Sets

  • A set is an unordered collection of elements without duplicate entries.
  • When printed, iterated, or converted into a sequence, its elements will appear in an arbitrary order.
Example: 

>>> print set()
set([])

>>> print set('HackerRank')
set(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R'])

>>> print set([1,2,1,2,3,4,5,6,0,9,12,22,3])
set([0, 1, 2, 3, 4, 5, 6, 9, 12, 22])

>>> print set((1,2,3,4,5,5))
set([1, 2, 3, 4, 5])

>>> print set(set(['H','a','c','k','e','r','r','a','n','k']))
set(['a', 'c', 'r', 'e', 'H', 'k', 'n'])

>>> print set({'Hacker' : 'DOSHI', 'Rank' : 616 })
set(['Hacker', 'Rank'])

>>> print set(enumerate(['H','a','c','k','e','r','r','a','n','k']))
set([(6, 'r'), (7, 'a'), (3, 'k'), (4, 'e'), (5, 'r'), (9, 'k'), (2, 'c'), (0, 'H'), (1, 'a'), (8, 'n')])

HackerRank Python Solution - Classes - Find the Torsional Angle

  • You are given four points A, B, C, and D in a 3-dimensional Cartesian coordinate system. You are required to print the angle between the plane made by the points A, B, C and B, C, D in degrees(not radians). Let the angle be PHI.
  • Cos(PHI) = (X.Y)/|X||Y| where X = AB x BC and Y= BC x CD.
  • Here, X.Y means the dot product of  X and Y, and AB x BC means the cross product of vectors AB and BC. Also, AB = B - A.
Input Format:
  • One line of input containing the space-separated floating number values of the X, Y, and Z coordinates of a point.

HackerRank Python Solution - Classes - Dealing with Complex Numbers

  • For this challenge, you are given two complex numbers, and you have to print the result of their addition, subtraction, multiplication, division, and modulus operations.
  • The real and imaginary precision parts should be correct up to two decimal places.
Input Format:

One line of input: The real and imaginary part of a number separated by a space.

Python - File Handling

In real-world application development, the logic that we implement uses some data and those data may come from different sources like databases, files, or API. As a python developer, we should have good knowledge of handling these different sources. In this tutorial, we will see how we can handle files and perform some operations using python in-built functions.

Basically, a file is a location to store information in non-volatile memory (Hard Disk). And, it is characterized by its name and extension.

When we want to perform some operation(Read/Write/Update) in a file, we will open it first. Then we perform our task and finally, we will close it to save the changes and deallocate the resources tied with the file.

Similarly, in python, File operation takes place in the following order:

  1. Open a file
  2. Perform operation (Read/Write/Update)
  3. Close the file

Python - Exception Handling

Errors are unavoidable. As a developer, we always have to encounter multiple errors in the initial phase of development. And at the end of the development, the business logic/application which we developed should not misbehave or terminate due to some unexpected events during execution.

In General, Errors in python can be of two types

  • Syntax Errors/Compile Time Error - Syntax mistakes which result in termination of the program.
#syntax error example
a = 5
if(a)
print("Value present")

#result
File "main.py", line 2
    if(a)
        ^
SyntaxError: invalid syntax

You might also like

Deploy your Django web app to Azure Web App using App Service - F1 free plan

In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...