HackerRank Python Solution - Sets - The Captain's Room

  • Mr. Anant Asankhya is the manager at the INFINITE hotel. The hotel has an infinite amount of rooms.
  • One fine day, a finite number of tourists come to stay at the hotel.
  • The tourists consist of:
    • A Captain.
    • An unknown group of families consisting of members per group where ≠ .
  • The Captain was given a separate room, and the rest were given one room per group.
  • Mr. Anant has an unordered list of randomly arranged room entries. The list consists of the room numbers for all of the tourists. The room numbers will appear times per group except for the Captain's room.
  • Mr. Anant needs you to help him find the Captain's room number. The total number of tourists or the total number of groups of families is not known to you.
  • You only know the value of K and the room number list.

HackerRank Python Solution - Sets - Set Mutations

  • We have seen the applications of union, intersection, difference, and symmetric difference operations, but these operations do not make any changes or mutations to the set.
  • We can use the following operations to create mutations to a set: .update() or |=
  • Update the set by adding elements from an iterable/another set.
>>> H = set("Hacker")
>>> R = set("Rank")
>>> H.update(R)
>>> print H
set(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R'])
  • .intersection_update() or &= Update the set by keeping only the elements found in it and an iterable/another set.

HackerRank Python Solution - Sets - Symmetric Difference

  • If the inputs are given on one line separated by a character (the delimiter), use split() to get the separate values in the form of a list. The delimiter is space (ASCII 32) by default. To specify that comma is the delimiter, use string. split(','). For this challenge, and in general, on HackerRank, space will be the delimiter.
Usage:

>> a = raw_input()
5 4 3 2
>> lis = a.split()
>> print (lis)
['5', '4', '3', '2']
  • If the list values are all integer types, use the map() method to convert all the strings to integers.

HackerRank Python Solution - Sets - .symmetric_difference() operation

HackerRank Python Solution - Sets - .symmetric_difference() operation
.symmetric_difference():
  • The .symmetric_difference() operator returns a set with all the elements that are in the set and the iterable but not both.
  • Sometimes, a ^ operator is used in place of the .symmetric_difference() tool, but it only operates on the set of elements in the set.
  • The set is immutable to the .symmetric_difference() operation (or ^ operation).

HackerRank Python Solution - Sets - .difference() operation

HackerRank Python Solution - Sets - .difference() operation
.difference():
  • The tool .difference() returns a set with all the elements from the set that are not in an iterable. Sometimes the - operator is used in place of the .difference() tool, but it only operates on the set of elements in the set.
  • Set is immutable to the .difference() operation (or the - operation).

HackerRank Python Solution - Sets - .intersection() operation

HackerRank Python Solution - Sets - .intersection() operation
.intersection():
  • The .intersection() operator returns the intersection of a set and the set of elements in an iterable. Sometimes, the & operator is used in place of the .intersection() operator, but it only operates on the set of elements in the set.
  • The set is immutable to the .intersection() operation (or & operation).

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.

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...