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

  • SEARCH
  • Home
  • 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 7699767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:33:22+00:00 2026-05-31T22:33:22+00:00

Thanks to stack overflow I’m almost done with a programming problem that’s been driving

  • 0

Thanks to stack overflow I’m almost done with a programming problem that’s been driving me crazy. It’s recursive, and here’s what it looks like:

def changeling(word,target,steps):
    x=word
    z=target
    if steps==0:
        return []
    if x==z:
        return [z]
    if len(word)!=len(target):
        print "error"
        return None
    i=1

    if lookup(z[0]+x[1:]) is True and z[0]+x[1:]!=x :
        word=z[0]+x[1:]
    while i!=len(x):
        if lookup(x[:i-1]+z[i-1]+x[i:]) and x[:i-1]+z[i-1]+x[i:]!=x:
            word=x[:i-1]+z[i-1]+x[i:]

        i+=1
    if lookup(x[:len(x)-1]+z[len(word)-1]) and x[:len(x)-1]+z[len(x)-1]!=x :
        word=x[:len(x)-1]+z[len(word)-1]


    return [x]+changeling(word,target,steps-1)

if I enter:

changeling("find","lose"4)

I receive:

['find', 'fine', 'line', 'lone', 'lose']

Which is the exact output I want. My next step in the program is this. If a word cannot be changed to the target word in a certain number of steps, the function just returns None. So if I were to input:

changeling(“find”,”lose”,3)

I should receive nothing, but instead I receive:

['find', 'fine', 'line']

I’m not quite sure how to do this, any help would be appreciated.

  • 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-05-31T22:33:24+00:00Added an answer on May 31, 2026 at 10:33 pm

    Instead of returning immediately the recursion, proceed as follow:

    y =  changeling(word,target,steps-1)
    if y :
      return [x] + y
    else:
      return None
    

    The problem with your original version is that you return what has been computed with, appended, the following steps. If you can’t find a good word to continue the chain, then your program outputs None, thus you return statement is composed of a list of valid steps + None, which is equivalent to only the list of valid steps. Here, all we do is check first if the following step can be achieved, if not we return None only, and if yes, then we return everything.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks to these stack overflow posts, I know how to make an nmap that
I'm new to Stack Overflow and Cygwin. My problem is that how do you
Thanks to Stack Overflow and you guys i successfully completed my first project (viz.
stack overflow ! I'm working on an application that manipulates SQL tables in a
first post here on stack overflow, hoping to get some advice on how to
I'm new to stack overflow, but I've been working with CSS for about a
Sorry if this isn't Stack Overflow worthy but I'm stumped. Here is my code:
I've searched posts here on Stack Overflow, and read JoelOnSoftware's post on encoding, and
Hey guys :) I am quite new to stack overflow and iPhone programming. I
Let me thanks you guys at the Stack Overflow community for helping me with

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.