Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8788557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:10:35+00:00 2026-06-13T22:10:35+00:00

I was working on project euler problem 14 and as a first attempt I

  • 0

I was working on project euler problem 14 and as a first attempt I whipped up this brute-force solution:

def collatz(n, memo={1: [1]}):
    if n not in memo:
        memo[n] = [n] + collatz(3 * n + 1 if n % 2 else n // 2)
    return memo[n]

def p014():
    return max(xrange(1, 10**6), key=lambda n: len(collatz(n)))

My question is about that lambda, I’m usually reluctant to use them but I don’t know any elegant way to avoid it in this case. Is there something in functools or other to chain two callables, or any other neat alternative I’m missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:10:37+00:00Added an answer on June 13, 2026 at 10:10 pm

    It would be lovely if there were a compose function — perhaps in functools. There is not, nor do I expect there will be, alas. In the words of Raymond Hettinger,

    It has been previously discussed and rejected in other forums. One of the issues is that the usual mathematical order is unintuitive and not self-documenting — i.e. is compose(f,g) the same as f(g(x)) or g(f(x))? Also, it is already dirt simple to create your own compose function or to do the composition directly: h = lambda x: f(g(x)).

    Here are two simple implementations of compose as a callable class that you may find useful though:

    # Scott Daniels, http://code.activestate.com/recipes/52902-function-composition/
    # Lightly edited for style.
    class Compose(object):
        '''Compose functions. compose(f,g,x...)(y...) = f(g(y...),x...))'''
        def __init__(self, f, g, *args, **kwargs):
            self.f = f
            self.g = g
            self.pending = args[:]
            self.kwargs = kwargs.copy()
    
        def __call__(self, *args, **kwargs):
            return self.f(self.g(*args, **kwargs), *self.pending, **self.kwargs)
    
    
    class Starcompose:
        '''Compose functions. Starcompose(f,g,x...)(y...) = f(*g(y...),x...))'''
        TupleType = type(())
    
        def __init__(self, f, g, *args, **kwargs):
            self.f = f
            self.g = g
            self.pending = args[:]
            self.kwargs = kwargs.copy()
    
        def __call__(self, *args, **kwargs):
            mid = self.g(*args, **kwargs)
            if isinstance(mid, self.TupleType):
                return self.f(*(mid + self.pending), **self.kwargs)
            return self.f(mid, *self.pending, **self.kwargs)
    

    Also, see the functional package, which inspired this very simple compose_many function of mine a while ago:

    def compose(f1, f2):
        def composition(*args, **kwargs):
            return f1(f2(*args, **kwargs))
        return composition
    
    def compose_many(*funcs):
        return reduce(compose, funcs)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Project Euler, Problem 10 java solution not working So, I'm attempting to
I am working on Project Euler, the first problem, and I've gotten this program
I'm again working on Project Euler, this time problem #4. The point of this
I'm working on solving the Project Euler problem 25: What is the first term
I am working on Project Euler problem 5 and am using the following: def
I'm working on Project Euler Problem 14. Here's my solution. import Data.List collatzLength ::
I'm working on problem four of Project Euler and am running into a stackoverflow
I'm working on problem 9 in Project Euler: There exists exactly one Pythagorean triplet
I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit
I am working on Problem 14 on Project Euler, and my code seems to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.