Skip to main content
added 80 characters in body
Source Link
vnp
  • 57.3k
  • 4
  • 51
  • 140

def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, None) return zip(a, b)

def bouncy_pairwise(number): """ tests whether a number is bouncy """ digits = str(number) increasing = decreasing = False for a, b in pairwise(digits): if not increasing and a > b: increasing = True if not decreasing and a < b: decreasing = True if increasing and decreasing: return True

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

def bouncy_pairwise(number):
    """
    tests whether a number is bouncy
    """
    digits = str(number)
    increasing = decreasing = False
    for a, b in pairwise(digits):
        if not increasing and a > b:
            increasing = True
        if not decreasing and a < b:
            decreasing = True
        if increasing and decreasing:
            return True

def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, None) return zip(a, b)

def bouncy_pairwise(number): """ tests whether a number is bouncy """ digits = str(number) increasing = decreasing = False for a, b in pairwise(digits): if not increasing and a > b: increasing = True if not decreasing and a < b: decreasing = True if increasing and decreasing: return True

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

def bouncy_pairwise(number):
    """
    tests whether a number is bouncy
    """
    digits = str(number)
    increasing = decreasing = False
    for a, b in pairwise(digits):
        if not increasing and a > b:
            increasing = True
        if not decreasing and a < b:
            decreasing = True
        if increasing and decreasing:
            return True
added 29 characters in body
Source Link
Maarten Fabré
  • 9.1k
  • 1
  • 15
  • 27
def bouncy(number):
    """Function determines whether arbitrary number N is a bouncy number."""
    digits = list(str(number))
    increasing = sorted(digits)
    decreasing = sorted(digits, reverse=True) # or, better increasing[::-1]
    return digits != decreasing and digits != increasing

I've checked a few variations of this bouncyness check, but this is the fastest I can find.

def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, None) return zip(a, b)

def bouncy_pairwise(number): """ tests whether a number is bouncy """ digits = str(number) increasing = decreasing = False for a, b in pairwise(digits): if not increasing and a > b: increasing = True if not decreasing and a < b: decreasing = True if increasing and decreasing: return True

and

def bouncy_zip(number):
    """
    tests whether a number is bouncy
    """
    digits = str(number)
    increasing = decreasing = False
    for a, b in zip(digits, digits[1::]):
        if not increasing and a > b:
            increasing = True
        if not decreasing and a < b:
            decreasing = True
        if increasing and decreasing:
            return True

were faster when the bouncyness was in the beginning of the number, but slower for non-bouncy numbers

Instead of using time.time, you can use the timeit module

for example:

timeit.repeat("find_bouncy(.9)", globals={"find_bouncy": find_bouncy}, number = 100)
def bouncy(number):
    """Function determines whether arbitrary number N is a bouncy number."""
    digits = list(str(number))
    increasing = sorted(digits)
    decreasing = sorted(digits, reverse=True) # or, better increasing[::-1]
    return digits != decreasing and digits != increasing

I've checked a few variations of this bouncyness check, but this is the fastest I can find

Instead of using time.time, you can use the timeit module

def bouncy(number):
    """Function determines whether arbitrary number N is a bouncy number."""
    digits = list(str(number))
    increasing = sorted(digits)
    decreasing = increasing[::-1]
    return digits != decreasing and digits != increasing

I've checked a few variations of this bouncyness check, but this is the fastest I can find.

def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, None) return zip(a, b)

def bouncy_pairwise(number): """ tests whether a number is bouncy """ digits = str(number) increasing = decreasing = False for a, b in pairwise(digits): if not increasing and a > b: increasing = True if not decreasing and a < b: decreasing = True if increasing and decreasing: return True

and

def bouncy_zip(number):
    """
    tests whether a number is bouncy
    """
    digits = str(number)
    increasing = decreasing = False
    for a, b in zip(digits, digits[1::]):
        if not increasing and a > b:
            increasing = True
        if not decreasing and a < b:
            decreasing = True
        if increasing and decreasing:
            return True

were faster when the bouncyness was in the beginning of the number, but slower for non-bouncy numbers

Instead of using time.time, you can use the timeit module

for example:

timeit.repeat("find_bouncy(.9)", globals={"find_bouncy": find_bouncy}, number = 100)
added 29 characters in body
Source Link
Maarten Fabré
  • 9.1k
  • 1
  • 15
  • 27

bouncy

You did a good job by extracting the check for bouncyness to a separate function. Plus points for the docstringThe function itself can be a bit better:

You can use the builtin sorted instead of list.sort. And since a string is an iterable too, you don't need the explicit casts to list You can also immediately return the result of the test number != n1 and number != n2

I don't like the names n1 and n2. They are the increasing and decreasing order of digits, so call em that. N

def bouncy(number):
    """Function determines whether arbitrary number N is a bouncy number."""
    digits = list(str(number))
    increasing = sorted(digits)
    decreasing = sorted(digits, reversereverse=True) =# True)or, better increasing[::-1]
    return digits != decreasing and digits != increasing

I've checked a few variations of this bouncyness check, but this is the fastest I can find

find

  • You have the 0.599 hardcoded. I would pass this on as an argument to the list
  • Of the double variables total and start, only 1 is needed
  • Instead of the break, just return there
  • instead of the while True loop, use itertools.count
  • no need to shorten bou, you can use bouncy_count or something as a more clear variable name
  • You could use the fact that int(True) is 1 and int(False) is 0, to replace if bouncy(i): bouncy_count += 1 by bouncy_count += bouncy(i)

With this result:

def find(fraction = .50):
    """Finds the first number where the ratio of bouncy numbers to all umbers is > `fraction`"""
    bouncy_count = 0
    for i in count(1):
        bouncy_count += bouncy(i)
        if bouncy_count / i > fraction:
            return i

timing

Instead of using time.time, you can use the timeit module

bouncy

You did a good job by extracting the check for bouncyness to a separate function. Plus points for the docstringThe function itself can be a bit better:

You can use the builtin sorted instead of list.sort. And since a string is an iterable too, you don't need the explicit casts to list You can also immediately return the result of the test number != n1 and number != n2

I don't like the names n1 and n2. They are the increasing and decreasing order of digits, so call em that. N

def bouncy(number):
    """Function determines whether arbitrary number N is a bouncy number."""
    digits = list(str(number))
    increasing = sorted(digits)
    decreasing = sorted(digits, reverse = True)
    return digits != decreasing and digits != increasing

I've checked a few variations of this bouncyness check, but this is the fastest I can find

find

  • You have the 0.5 hardcoded. I would pass this on as an argument to the list
  • Of the double variables total and start, only 1 is needed
  • Instead of the break, just return there
  • instead of the while True loop, use itertools.count
  • no need to shorten bou, you can use bouncy_count or something as a more clear variable name
  • You could use the fact that int(True) is 1 and int(False) is 0, to replace if bouncy(i): bouncy_count += 1 by bouncy_count += bouncy(i)

With this result:

def find(fraction = .50):
    """Finds the first number where the ratio of bouncy numbers to all umbers is > `fraction`"""
    bouncy_count = 0
    for i in count(1):
        bouncy_count += bouncy(i)
        if bouncy_count / i > fraction:
            return i

timing

Instead of using time.time, you can use the timeit module

bouncy

You did a good job by extracting the check for bouncyness to a separate function. Plus points for the docstringThe function itself can be a bit better:

You can use the builtin sorted instead of list.sort. And since a string is an iterable too, you don't need the explicit casts to list You can also immediately return the result of the test number != n1 and number != n2

I don't like the names n1 and n2. They are the increasing and decreasing order of digits, so call em that. N

def bouncy(number):
    """Function determines whether arbitrary number N is a bouncy number."""
    digits = list(str(number))
    increasing = sorted(digits)
    decreasing = sorted(digits, reverse=True) # or, better increasing[::-1]
    return digits != decreasing and digits != increasing

I've checked a few variations of this bouncyness check, but this is the fastest I can find

find

  • You have the 0.99 hardcoded. I would pass this on as an argument to the list
  • Of the double variables total and start, only 1 is needed
  • Instead of the break, just return there
  • instead of the while True loop, use itertools.count
  • no need to shorten bou, you can use bouncy_count or something as a more clear variable name
  • You could use the fact that int(True) is 1 and int(False) is 0, to replace if bouncy(i): bouncy_count += 1 by bouncy_count += bouncy(i)

With this result:

def find(fraction = .50):
    """Finds the first number where the ratio of bouncy numbers to all umbers is > `fraction`"""
    bouncy_count = 0
    for i in count(1):
        bouncy_count += bouncy(i)
        if bouncy_count / i > fraction:
            return i

timing

Instead of using time.time, you can use the timeit module

Source Link
Maarten Fabré
  • 9.1k
  • 1
  • 15
  • 27
Loading