Waiting in line at Sam's

This text file describes the activity in the line at Sam's ice cream shop yesterday.

When customers enter the line, they are arbitrarily assigned a number. This action is represented by the string "ENTER ###", where ### is the customer number.

When the customer at the front of the line receives their ice cream, they leave the line. This action is represented by the string "LEAVE".

Assume no customer leaves the line until they reach the front and receive their ice cream. If "LEAVE" appears when nobody is in line, there are no changes to the line.

Take this example:

ENTER 234
ENTER 142
ENTER 200
LEAVE
LEAVE

In this example, customers 234, 142, and 200 entered the line. Then 234 left, followed by 142. In the end, only customer 200 was still in line.

Click here to open the text file describing the line activity yesterday. Assume the line was empty to begin with.

Let S be the sum of the customer numbers who were still in line at the end of yesterday. What are the last 3 digits of S ?


The answer is 563.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

The full answer 99563, and the last three digits are 563 .

To solve this, use a queue data structure to represent the line at Sam's. Every time a new customer enters, push their number onto the end of the queue. When a customer leaves, pop the value at the front of the queue.

At the end of the queue manipulation, sum up the customer numbers in the queue.

# Python example
def lines():

    wait = [] # init the queue

    # open in read-only
    with open('cs_lines.txt', 'r') as f:
        for line in f:
            # if a customer entered the line, add its number to the queue
            if line.lower().startswith('enter'):
                c, n = line.split()
                wait.append(int(n))

            # if a there is a LEAVE command, and there is at least 1 person in line, pop the queue
            elif line.lower().startswith('leave') and wait:
                wait.pop(0)

    # print the sum of the customer numbers in the queue
    print sum(wait)
David Holcer
Mar 28, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
filee=open('icecream.txt',"r")
array=[]
summ=0
for i in range(1000):
    line=filee.readline().lower()
    if ('enter') in line:
        line=(line.strip('enter \n'))
    if ((('leave') in line) and (len(array)>=1)):
        array.remove(array[0])
    if ('leave') not in line:
        array.append(line)
for i in array:
    summ+=int(i)
print str(summ)[-3:]

Bill Bell
Apr 19, 2015

A Python list provides what amounts to a queue since it has a pop method and a push by the name append . When you see an ENTER in the list of input stuff push the customer number onto the end of the queue and when you see a LEAVE pop a customer number from the beginning of the queue. When you see the ENTER you need only split the item to get at the customer number.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...