Question

The question asks:

<< BACKGROUND STORY: Suppose we’re designing a point-of-sale and order-tracking system for a new burger joint. It is a small joint and it only sells 4 options for combos: Classic Single Combo (hamburger with one patty), Classic Double With Cheese Combo (2 patties), and Classic Triple with Cheese Combo (3 patties), Avant-Garde Quadruple with Guacamole Combo (4 patties). We shall encode these combos as 1, 2, 3, and 4 respectively. Each meal can be biggie sized to acquire a larger box of fries and drink. A biggie sized combo is represented by 5, 6, 7, and 8 respectively, for the combos 1, 2, 3, and 4 respectively. >>

Write an iterative function called order_size which takes an order and returns the number of combos in the order. For example, order_size(237) -> 3.

Whereby I should have

order_size(0) = 0

order_size(6) = 1

order_size(51) = 2

order_size(682) = 3

My code is:

def order_size(order):

    # Fill in your code here
    if order > 0:
        size = 0
        while order > 0:
            size +=  1
            order = order // 10
            return size
        else:
            return 0

But I don't get the order // 10 portion. I'm guessing it's wrong but I can't think of any stuff to substitute that.

Was it helpful?

Solution

No need for iterative function, you can measure the length of the number by "turning" it into a string:

num = 127
order = len(str(num))
print(order) # prints 3

But if you really want to do it iteratively:

def order(num):
    res = 0
    while num > 0:
        num = int(num / 10)
        res += 1
    return res

print(order(127))  # prints 3

OTHER TIPS

How about this:

from math import log

def order_size(order):
    if order <= 0: return 0
    return int(log(order, 10) + 1)

Some samples (left column order, right column order size):

0 0
5 1
10 2
15 2
20 2
100 3
893 3
10232 5

There are a couple errors in your suggested answer.

  1. The else statement and both return statements should be indented a level less.
  2. Your tester questions indicate you are supposed to count the digits for nonnegative integers, not just positive ones (i.e. you algorithm must work on 0).

Here is my suggested alternative based on yours and the criteria of the task.

def order_size(order):

    # Fill in your code here
    if order >= 0:
        size = 0
        while order > 0:
            size +=  1
            order = order // 10
        return size
    else:
        return 0

Notice that

  • By using an inclusive inequality in the if condition, I am allowing 0 to enter the while loop, as I would any other nonnegative single digit number.

  • By pushing the first return statement back, it executes after the while loop. Thus after the order is counted in the variable size, it is returned.

  • By pushing the else: back, it executes in the even the if condition is not met (i.e. when the numbers passed to order_size(n) is negative).

  • By pushing the second return back, it is syntactically correct, and contained in the else block, as it should be.

Now that's taken care of, let me address this:

But I don't get the order // 10 portion.

As of Python 3, the // is a floor division (a.k.a integer division) binary operation.

It effectively performs a standard division, then rounds down (towards negative infinity) to the nearest integer.

Here are some examples to help you out. Pay attention to the last one especially.

10 // 2      # Returns 5 since 10/2 = 5, rounded down is 5
2 // 2       # Returns 1 since 2/2 = 1, rounded down is 1
11 // 2      # Returns 5 since 11/2 = 5.5, rounded down is 5
4 // 10      # Returns 0 since 4/10 = 0.4, rounded down is 0
(-4) // 10   # Returns -1 since (-4)/10 = -0.4, rounded down is -1

For nonnegative numerator n, n // d can be seen as the number of times d fits into n whole.

So for a number like n = 1042, n // 10 would give you how many whole times 10 fits into 1042.

This is 104 (since 1042/10 = 104.2, and rounded down we have 104). Notice how we've effectively knocked off a digit?

Let's have a look at your while loop.

while order > 0:
    size +=  1
    order = order // 10

Every time a digit is "knocked off" order, the size counter is incremented, thus counting how many digits you can knock off before you hit your terminating step.

Termination occurs when you knock of the final (single) digit. For example, say you reduced order to 1 (from 1042), then 1 // 10 returns 0.

So once all the digits are "knocked off" and counted, your order will have a value of 0. The while loop will then terminate, and your size counter will be returned.

Hope this helps!


Disclaimer: Perhaps this isn't what you want to hear, but many Universities consider copying code from the Internet and passing it off as your own to be plagiarism.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top