美文网首页
习题16:卖票

习题16:卖票

作者: 今年说话算话 | 来源:发表于2017-02-22 13:12 被阅读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'
    

    相关文章

      网友评论

          本文标题:习题16:卖票

          本文链接:https://www.haomeiwen.com/subject/huxmwttx.html