习题16:卖票

2017-02-22  本文已影响0人  今年说话算话

Description:

The new "Avengers" movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 dollars bill. A "Avengers" ticket costs 25 dollars.

Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line.

Can Vasya sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?

Return YES, if Vasya can sell a ticket to each person and give the change. Otherwise return NO.

def tickets(people):
    #giving a change: change 25$ for 50$ and change 50$+25$ or 25$*3 for 100$
    change = {
        25: 0,
        50: 0,
    }
    for money in people:
        if money == 25:
            change[25] += 1
        elif money == 50:
            change[50] += 1
            change[25] -= 1
        else:
            if change[50] >= 1:
                change[50] -= 1
                change[25] -= 1
            else:
                change[25] -= 3
        if change[25] < 0:
            return 'NO'
    return 'YES'
上一篇下一篇

猜你喜欢

热点阅读