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/

Thursday, July 23, 2020

XML2 - Find the Maximum Depth- HackerRank Solution

 XML2 - Find the Maximum Depth

 You are given a valid XML document, and you have to print the maximum level of nesting in it. Take the depth of the root as .




Input Format

The first line contains , the number of lines in the XML document.
The next  lines follow containing the XML document.

Output Format

Output a single line, the integer value of the maximum level of nesting in the XML document.

Sample Input

6
<feed xml:lang='en'>
    <title>HackerRank</title>
    <subtitle lang='en'>Programming challenges</subtitle>
    <link rel='alternate' type='text/html' href='http://hackerrank.com/'/>
    <updated>2013-12-25T12:00:00</updated>
</feed>

Sample Output

1

Explanation

Here, the root is a feed tag, which has depth of .
The tags title, subtitle, link and updated all have a depth of .

Thus, the maximum depth is .


XML2 - Find the Maximum Depth Solution-