0

So, I think I'm on the wrong track here. I want to count how often I can fold a standard paper until one of its measurements (width or height) reaches 0. The folding is simulated by dividing only the bigger measurement by two. So here's what I've come up with:

def count_folds(width: int, height: int) -> int:
    counter = 0
    while width > 0 and height > 0:
        if max(width, height) == width:
            width = folding(width)
        elif max(width, height) == height:
            height = folding(height)
        counter += 1
    return counter

def folding(x):
    return x / 2

But when I run this code for these two examples

print("count_folds: ", count_folds(2, 1))
print("count_folds: ", count_folds(15, 7))

,then I get very large numbers (which I'm not expecting). What am I doing wrong?

Thanks for any help guys!

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Mimo
  • 55
  • 4
  • Think carefully about the logic. Why should dividing `n` by `2` ever cause it to stop being greater than zero? Because of ignoring the remainder, right? So. Did you try to check *whether that actually happens*? In 3.x, *it does not* with `/` - you want `//` to ignore the remainder. Please see the linked duplicate. – Karl Knechtel Sep 16 '22 at 15:31
  • Alternately, I'm not sure what you mean. In the real world, objects aren't measured in integers; the most common sizes for paper aren't an integer number of the unit (in US customary measures, 8.5 x 11 inches for Letter; in metric, 21 x 29.7 cm). So *of course* if you divide a positive *real number* exactly in half, the result is still greater than zero. The process only stops in Python because of the imprecision of `float`. That isn't a programming question at that point, but an elementary math question. – Karl Knechtel Sep 16 '22 at 15:34
  • With real paper, of course, the dimensions *don't* "reach zero"; physical properties of the paper prevent further folding (i.e. the thickness of the existing folded stack vs. the remaining height and width, as well as flexibility etc.) – Karl Knechtel Sep 16 '22 at 15:35
  • Well, thanks. Actually I intended to use // instead of /. That solved the problem with having such large numbers as a result. But it still didn't fix my thinking problem. For example, I expect by the second print statement to get 6 counts, but I'm getting 4 instead! I think It's just that I can't decide which side of the paper is the larger after the first iteration an so on. – Mimo Sep 16 '22 at 15:45
  • "I think It's just that I can't decide which side of the paper is the larger after the first iteration an so on. "Think carefully about the logic. **How many times** does this decision need to be made? **When** is it made? Therefore, where in the code does that decision-making logic have to go? (The next problem you will have is that `width` and `height` need to be reduced separately; simply reducing `n` won't affect them.) – Karl Knechtel Sep 16 '22 at 15:50
  • By the way, I think I got it. Thanks for your help! :) – Mimo Sep 16 '22 at 16:49
  • Please do not edit questions to show the solution (and also do not mark questions as "solved" etc.) We don't want to confuse future readers by hiding the problem. Also keep in mind that this is **not a discussion forum**. – Karl Knechtel Sep 16 '22 at 17:13

0 Answers0