2
\$\begingroup\$

This is a question from HackerRank. I have solved it and want to know if it's the best answer I could have given in Python 3.

Sample code:

>>> from collections import Counter
>>> 
>>> myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3]
>>> print Counter(myList)
Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1})
>>>
>>> print Counter(myList).items()
[(1, 3), (2, 4), (3, 4), (4, 2), (5, 1)]
>>> 
>>> print Counter(myList).keys()
[1, 2, 3, 4, 5]
>>> 
>>> print Counter(myList).values()
[3, 4, 4, 2, 1]

Task:

Raghu is a shoe shop owner. His shop has X number of shoes. He has a list containing the size of each shoe he has in his shop. There are N number of customers who are willing to pay \$X_i\$ amount of money only if they get the shoe of their desired size.

Calculate how much Raghu earned.

Input format:

The first line contains the number of shoes.

The second line contains the space separated list of all the shoe sizes in the shop.

The third line contains N, the number of customers.

The next N lines contain the space separated values of the shoe size desired by the customer and Xi, the price of the shoe.

Output format:

Print the amount of money earned by Raghu.

Sample input:

10
2 3 4 5 6 8 7 6 5 18
6
6 55
6 45
6 55
4 40
18 60
10 50

Sample output:

200

Is this good? Where can I improve?

from collections import Counter
for _ in range(2):
    shoes = map(int, input().strip().split())
shoes = Counter(shoes)
income = 0
for _ in range(int(input())):
    query = list(map(int, input().split()))
    if query[0] in shoes and shoes[query[0]] > 0:
        shoes[query[0]] -= 1
        income += query[1]
print(income)
\$\endgroup\$
  • 3
    \$\begingroup\$ Please 1) Check your indentation (Control-K helps) 2) State only code-purpose in the title 3) Use quotation mark-up for the problem statement and provide a link back to the original for attribution. \$\endgroup\$ – Caridorc Sep 17 '16 at 12:18
  • 1
    \$\begingroup\$ As every single on-topic question on this site could have this exact same title (it's implied, we review your code), please edit the title to state the task that your code accomplishes. \$\endgroup\$ – Mathieu Guindon Sep 17 '16 at 12:48
2
\$\begingroup\$

Repetition

Except the additional list these lines are identical:

shoes = map(int, input().strip().split())

query = list(map(int, input().split()))

You can write a function:

def read_integers_from_line(line):
    """
    >>> list(read_integers_from_line("12 3 7"))
    [12, 3, 7]
    """
    return map(int, line.strip().split())

That you can use twice.

Tuple unpacking

Instead of query = read_integers_from_line(input()) you can use: shoe, price = read_integers_from_line(input()) so that the code becomes more self-descriptive.

Skipping the first line

The first line contains useless information, you skip it weirdly using a range but I would suggest a more explicit skip by using instead _ = input()

\$\endgroup\$
  • \$\begingroup\$ Can you please edit my code and answer again? \$\endgroup\$ – Jøê Grèéñ Sep 17 '16 at 13:38
  • \$\begingroup\$ from collections import Counter def read(): return map(int, input().strip().split()) _ = input() shoes = Counter(read()) income = 0 for _ in range(int(input())): size,price = list(read()) if size in shoes and shoes[size] > 0: shoes[size] -= 1 income += price print(income) \$\endgroup\$ – Jøê Grèéñ Sep 17 '16 at 13:56
  • 1
    \$\begingroup\$ @JøêGrèéñ what do you mean? You should modify your code yourself, I cannot touch it as determined by site policies \$\endgroup\$ – Caridorc Sep 17 '16 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.