Chain of powers X15987


Statement
 

pdf   zip

html

You have to program the function short_power7_chains below. Remember that 1 is a power of 7: 70 = 1. The following auxiliar function may be helpful.

def is_power7(n):
    '''
    Requires a non negative integer n.
    Returns True when n is a power of 7
    Returns False when n is not a power of 7
    '''
    if n == 0:
        return False
    while n != 1:
        if n%7 != 0:
            return False
        n = n//7
    return True
  • Write a function short_power7_chains(f, k) that given a list f of non negative integers and an integer k greater than zero returns True when all the chains formed by powers of seven have size at most k; otherwise the function returns False. A chain of powers of seven is a block of consecutive numbers in the list all of them being a power of seven.

Scoring

The function counts 100 points.

Sample session
>>> short_power7_chains([1, 7, 49, 7*7*7, 2], 3)
False
>>> short_power7_chains([1, 7, 49, 7*7*7, 2], 4)
True
>>> short_power7_chains([1, 7, 14, 7*7*7, 21, 28], 2)
True
>>> short_power7_chains([14, 7], 1)
True
>>> short_power7_chains([], 1)
True
Information
Author
ProAl
Language
English
Official solutions
Python
User solutions
Python