Showing posts with label HackerRank Solutions. Show all posts
Showing posts with label HackerRank Solutions. Show all posts

Sunday, August 9, 2020

Set .discard(), .remove() & .pop()- HackerRank Solution

 Set .discard(), .remove() & .pop()- HackerRank Solution

.remove(x)

This operation removes element  from the set.
If element  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

.discard(x)

This operation also removes element  from the set.
If element  does not exist, it does not raise a KeyError.
The .discard(x) operation returns None.

Example

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

.pop()

This operation removes and return an arbitrary element from the set.
If there are no elements to remove, it raises a KeyError.

Example

>>> s = set([1])
>>> print s.pop()
1
>>> print s
set([])
>>> print s.pop()
KeyError: pop from an empty set

Task

You have a non-empty set , and you have to execute  commands given in  lines.

The commands will be pop, remove and discard.

Input Format

The first line contains integer , the number of elements in the set .
The second line contains  space separated elements of set . All of the elements are non-negative integers, less than or equal to 9.
The third line contains integer , the number of commands.
The next  lines contains either pop, remove and/or discard commands followed by their associated value.

Constraints


Output Format

Print the sum of the elements of set  on a single line.

Sample Input

9
1 2 3 4 5 6 7 8 9
10
pop
remove 9
discard 9
discard 8
remove 7
pop 
discard 6
remove 5
pop 
discard 5

Sample Output

4

Explanation

After completing these  operations on the set, we get set. Hence, the sum is .

Note: Convert the elements of set s to integers while you are assigning them. To ensure the proper input of the set, we have added the first two lines of code to the editor.

Set .discard(), .remove() & .pop() Solution
#For beginner
n = int(input())
s = set(map(int, input().split()))
for i in range(int(input())):
    choice=input().split()
    if choice[0]=="pop" :
        s.pop()
    elif choice[0]=="remove" :
        s.remove(int(choice[1]))
    elif choice[0]=="discard" :
        s.discard(int(choice[1]))
print(sum(s))

#Modified version
n = int(input())
s = set(map(int, input().split())) 
for i in range(int(input())):
    eval('s.{0}({1})'.format(*input().split()+['']))

print(sum(s))


Explanation-  We have earlier used sets in many HackerRank problems using the earlier learned concepts we will declare a set s the choice is a list used to take the input. If the index 0 of the list is pop we will pop an element from s or else for remove and discard we will get the location present at index 1 of the list.

In the other code, we have used eval function which evaluates the expression if it is a legal python statement.

To read about sets follow this link-  https://www.geeksforgeeks.org/sets-in-python/

Saturday, August 8, 2020

Set. add() - HackerRank Solution

 Set. add() - HackerRank Solution

 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'])


Task

Apply your knowledge of the .add() operation to help your friend Rupal.

Rupal has a huge collection of country stamps. She decided to count the total number of distinct country stamps in her collection. She asked for your help. You pick the stamps one by one from a stack of  country stamps.

Find the total number of distinct country stamps.

Input Format

The first line contains an integer , the total number of country stamps.
The next  lines contains the name of the country where the stamp is from.

Constraints

Output Format

Output the total number of distinct country stamps on a single line.

Sample Input

7
UK
China
USA
France
New Zealand
UK
France 

Sample Output

5

Explanation

UK and France repeat twice. Hence, the total number of distinct country stamps is  (five).

Set. add() Solution
N=int(())
count=set()
for i in range(N):
    country=input()
    count.add(country)
print(len(count))

Explanation-  We have earlier used sets in many HackerRank problems using the earlier learned concepts we will declare a set count. Add( ) method is joined to append elements in the set count. As we all know that set stores only unique element we can directly find the length of the set count to get the output.

To read about sets follow this link-  https://www.geeksforgeeks.org/sets-in-python/