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/

Thursday, July 30, 2020

Word Order- HackerRank Solution

 Word Order- HackerRank Solution

 You are given  words. Some words may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word. See the sample input/output for clarification.


Note: Each input line ends with a "\n" character.

Constraints:

The sum of the lengths of all the words do not exceed 
All the words are composed of lowercase English letters only.

Input Format

The first line contains the integer, .
The next  lines each contain a word.

Output Format

Output  lines.
On the first line, output the number of distinct words from the input.
On the second line, output the number of occurrences for each distinct word according to their appearance in the input.

Sample Input

4
bcdef
abcdefg
bcde
bcdef

Sample Output

3
2 1 1

Explanation

There are  distinct words. Here, "bcdef" appears twice in the input at the first and last positions. The other words appear once each. The order of the first appearances are "bcdef", "abcdefg" and "bcde" which corresponds to the output.

Word Order Solution
from collections import OrderedDict
d=OrderedDict()
count=1
N=int(input())
for i in range(N):
    word=input()
    d[word]=d.get(word,0)+count
print(len(d.keys()))
for word,repeat in d.items():
    print(repeat, end=' ')

Explanation-  We have used Ordered Dict in order to update the input word in dict with count as 1. d.get returns the 1 if the word is present and add count in it. To print unique words we use d.keys which returns the list of words whose length is printed whereas, to print count we use d.items() which returns  key-value pair. Count is printed in repeat variable with space after each output. 


Collections.OrderedDict()-HackerRank Solution

 Collections.OrderedDict()-HackerRank Solution

 An OrderedDict is a dictionary that remembers the order of the keys that were inserted first. If a new entry overwrites an existing entry, the original insertion position is left unchanged.



Example

Code

>>> from collections import OrderedDict
>>> 
>>> ordinary_dictionary = {}
>>> ordinary_dictionary['a'] = 1
>>> ordinary_dictionary['b'] = 2
>>> ordinary_dictionary['c'] = 3
>>> ordinary_dictionary['d'] = 4
>>> ordinary_dictionary['e'] = 5
>>> 
>>> print ordinary_dictionary
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
>>> 
>>> ordered_dictionary = OrderedDict()
>>> ordered_dictionary['a'] = 1
>>> ordered_dictionary['b'] = 2
>>> ordered_dictionary['c'] = 3
>>> ordered_dictionary['d'] = 4
>>> ordered_dictionary['e'] = 5
>>> 
>>> print ordered_dictionary
OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)])

Task

You are the manager of a supermarket.
You have a list of  items together with their prices that consumers bought on a particular day.
Your task is to print each item_name and net_price in order of its first occurrence.

item_name
net_price

Input Format

The first line contains the number of items, .
The next  lines contains the item's name and price, separated by a space.

Constraints

Output Format

Print the item_name and net_price in order of its first occurrence.

Sample Input

9
BANANA FRIES 12
POTATO CHIPS 30
APPLE JUICE 10
CANDY 5
APPLE JUICE 10
CANDY 5
CANDY 5
CANDY 5
POTATO CHIPS 30

Sample Output

BANANA FRIES 12
POTATO CHIPS 60
APPLE JUICE 20
CANDY 20

Explanation

BANANA FRIES: Quantity bought: , Price: 
Net Price: 
POTATO CHIPS: Quantity bought: , Price: 
Net Price: 
APPLE JUICE: Quantity bought: , Price: 
Net Price: 
CANDY: Quantity bought: , Price: 
Net Price: 20

Collections.OrderedDict() Solution
from collections import OrderedDict
d=OrderedDict()
N=int(input())

for i in range(N):
    item,space,price=input().rpartition(' ')
    d[item]=d.get(item,0)+int(price)
    #Here d.get(item,0) will returns the price of item present in dict d if item is not present it return 0
for item, price in d.items():
    print(item, price)

Explanation- Since while taking input we have space we are managing it using rpartition where separates the input based on the mentioned separator  (' ') and, returns it in a tuple. In the next line, we used get function to get the price and add price of the same key. d.items() returns all key with value pair, which is later printed one by one using for loop.

To read more about rpartition follow this link- https://www.geeksforgeeks.org/python-string-rpartition/