2

For an assignment in my CS class I have to print all powers of 3 (1, 3, 9, 27, etc.) less than 1000 using no more than 2 lines of code. I know I could just use

for x in range(7):
    print(3**x)

because I know that 3^6 is the last power that gives a result under 1000 but I was wondering if there was a way to conditionally check that 3^x is under 1000 before printing still using only 2 lines of code at most. I may just be overthinking it but I want to make sure for my own information.

8 Answers8

3

Usually in python we prefer however many lines can give the most readable code.

So, it's a strange requirement to limit the lines of code like that. At a guess, your instructor may have been looking for some mathematical insight on how a bound for iteration could be precomputed:

>>> for i in range(1 + int(math.log(1000,3))):
...     print(3**i)
...     
1
3
9
27
81
243
729
wim
  • 266,989
  • 79
  • 484
  • 630
1

If you are allowed to use semicolons:

i=3;
while i<1000: print(i);i = i*3
Escapado
  • 107
  • 2
  • 11
  • These are two lines of code. However there are 3 statements (not counting the while loop). In my humble opinion: Number of lines != Number of Statements. – Escapado Feb 04 '16 at 20:21
  • Yes, but that is probably not what his instructor was going for, right? – Colin Schoen Feb 04 '16 at 20:22
1
>>> from itertools import takewhile, count
>>> print(*map(lambda x: 3**x, takewhile(lambda x: 3**x < 1000, count(0))), sep='\n')
1
3
9
27
81
243
729
cdlane
  • 33,404
  • 4
  • 23
  • 63
1

You could have found that 6 is the lowest allowed exponent using logs. Here's a one-line solution:

import math
def printer(exp_num, target_num):
    for i in range(int(math.log(target_num, exp_num))+1): print exp_num**i
printer(3, 1000)
Vincent Savard
  • 30,767
  • 10
  • 61
  • 70
kilojoules
  • 7,600
  • 17
  • 63
  • 123
1

itertools.takewhile is the correct way to express what you're asking, but it depends on a module which necessarily adds a line.

>>> import itertools
>>> print(list(itertools.takewhile(lambda x: x < 1000, (3**x for x in itertools.count()))))
[1, 3, 9, 27, 81, 243, 729]

Expressed in a more sane 3-liner format...

>>> import itertools
>>> for x in itertools.takewhile(lambda x: x < 1000, (3**x for x in itertools.count())):
...   print(x)
...
1
3
9
27
81
243
729

Let's explain what's going on here, starting from the innermost. Each step builds on the previous.

itertools.count() is a generator that produces the numbers 1, 2, 3, 4, ... forever. It's how you express range(infinity).

(3**x for x in itertools.count()) is a generator that produces the numbers 1, 3, 9, 27, ... forever.

itertools.takewhile(lambda x: x < 1000, (3**x for x in itertools.count())) is a generator that produces the numbers 1, 3, 9, 27, ... forever while x < 1000 is true.

After that it's just a matter of printing the generated numbers.

The itertools module is a pretty important part of Python, I suggest learning it in general as it solves a lot of problems like this.

QuestionC
  • 9,738
  • 3
  • 21
  • 40
0

one liner with a list comprehension

print([3 ** x for x in range(7) if 3 ** x < 1000])

addressing kevin's comment and trying to stick to two lines of code

for i in range(1000):
    print(3 ** i) if (3 ** i) < 1000 else i

i don't like the else statement

danidee
  • 8,227
  • 2
  • 25
  • 52
  • 3
    I think OP is looking for a solution that does not use the hardcoded value `7` for the range. Suppose you need to find all powers of 17 below 4,815,162,342, and you don't have time to establish an upper bound for your range on paper. What would your code look like then? – Kevin Feb 04 '16 at 20:08
0

Probably, if you can work out how to use a ternary operator. This answer may help, but my best-guess (I don't work in Python) would be along the lines of...

for x in range(10):
    print(3**x) if (3**x) < 1000 else print()
Community
  • 1
  • 1
0
def power(x):      # define function
    for x in range(10):
        if (3**x) < 1000:
            print(3**x)
        else:
             print()
power(3)      # call the function
Mohnish
  • 1,026
  • 1
  • 10
  • 16
Joshua
  • 1
  • Although this code might solve the problem, a good answer should also explain **what** the code does and **how** it helps. – BDL Oct 15 '20 at 08:35