0

I am trying to have the user input two names into variables. These variables will be mixed with scramble-function. Then I want to have them seperated again with the unscramble function but I want the unscramble function to recognise which of the two words was the first input and which the second input.. but I am stuck with scope and have no clue how to solve this correctly.

I appreciate any solution!

a = raw_input("First word please?")
b = raw_input("Second word please?")

def scramble(word1, word2):

    if len(word1) > len(word2):
        a1 = word1
        a2 = word2
    else:
        a1 = word2
        a2 = word1

    maxlength = max( len(word1),len(word2) )
    magicWord = ""

    for x in range(0,maxlength):

        magicWord += a1[x]

        if x < len(a2)-1:
            magicWord += a2[x]

        elif x == len(a1)-1:
            fin = len(a2)-1
            magicWord += a2[fin]

        else:
            magicWord += "0"

    return magicWord

magicWord = scramble(a,b)
print "\nYour magic word is: \""+magicWord+"\"\n"

magicSize = len(magicWord)



def unscramble(magicWord):

    z = 0

    if z == magicSize:
        return False

    else:
        while unscramble(magicWord):

            word1 = ""
            word2 = ""

            if z % 2 == 1:
                if magicWord[z] == "0":
                    z+= 1
                else:
                    word2 += magicWord[z]
                    z += 1
            else:
                if magicWord[z] == "0":
                    z+= 1
                else:
                    word1 += magicWord[z]
                    z += 1

            return word1

if word1 > word2:
    print "First input: " + word1
    print "Second input: " + word2
else:
    print "First input: " + word2
    print "Second input: " + word1
4
  • 2
    It's not clear what your current problem is. Could you give a minimal reproducible example with a more precise description? Commented Oct 6, 2015 at 20:36
  • You can't know what word was entered first unless you store that information separately; you sorted the words by length before scrambling so the original entry order is lost at that moment. Commented Oct 6, 2015 at 20:37
  • I think the OP means when using max etc.. they are getting lost on which is which Commented Oct 6, 2015 at 20:37
  • store a and b in a dict {a : 'first', b: 'second'} and then pass that to your functions. Commented Oct 6, 2015 at 20:43

4 Answers 4

1

After

 if len(word1) > len(word2):
     a1 = word1
     a2 = word2
 else:
     a1 = word2
     a2 = word1

the original order is lost. Without this it's quite easy to reconstruct the order, as your scramble function always puts the first letter of the first word first.

Then change your unscramble function to return word1, word2 and you get the words back in the original order.

By the way, you should have a look at itertools.zip_longest(). It would make your scramble function easier to read. for char1, char2 in itertools.zip_longest(word1, word2, fillvalue='0') loops through all letters in both words and uses '0' for the missing letters in the shorter word.

Sign up to request clarification or add additional context in comments.

3 Comments

To take this a step further, you can insert an expression for the fillvalue argument: fillvalue = '1' if len(word1) > len(word2) else '2' /// This gives you a '1' filler if word1 is longer, '2' if word 2 is longer. This is even better than the "Pythonic" solution I built (but didn't post in my response).
Thanks for the feedback, i understand what i did wrong here. Seems that I have to restore the output from the functions. Just don't know really how to return multiple variables? Should I use tuples for that? And how should I apply them?
@JenniferDevelopez: You can write return word1, word2. This creates a tuple. You can then either write words = unscramble(magicWord) to get the tuple with both words or word1, word2 = unscramble(magicWord) to get them into seperate variables.
1

You never got rid of a or b, so you can use them to know which word was first and which was second. There's no need to manually unscramble them. I would use a tuple to store them after scramble, so instead of:

magicWord = scramble(a,b)

I would just do:

magicWord_tuple = (a, b, scramble(a,b))

Then you just pull out whichever word you want when you need it.

firstword = magicWord_tuple[0]
secondword = magicWord_tuple[1]
magicword = magicWord_tuple[2]

2 Comments

Thanks! This worked out just fine. Now I also understand the functionality of tuples in these situations. Thanks again
Glad to help. You can achieve the same thing with any list-like object, depending on your preferences. List, tuple, frozenset, dictionary, etc. In retrospect I think a dictionary would be the best choice in terms of organization and clarity, but for something small like this it doesn't really matter.
0

I'm sorry, but your unscramble function cannot recover the words any more, let alone tell which one was entered first. You've destroyed information in your scramble function.

In scramble, your first few lines destroy the ordering; since you never again refer to word1 and word2 (the only indicator of order), that information is lost. Perhaps you would want to use a different separator (where you insert a "0") to denote which word is which. This insertion will let the unscramble routine differentiate.

if len(word1) > len(word2):
    a1, a2 = word1, word2
    sep = '1'
else:
    a1, a2 = word2, word1
    sep = '2'
...
   else:
        magicWord += sep

There are several other coding improvements we could make to be more "Pythonic", but we're doing functionality right now; code review goes into another group.

Comments

0

maybe you can have an additional variable saying you which word enters first in the magic word, and pass this variable to the unscramble function to do the separation of the words. You can change the scramble word like this:

if len(word1) > len(word2):
    flag = 1
    a1 = word1
    a2 = word2
else:
    flag = -1
    a1 = word2
    a2 = word1

...

return magicWord, flag

and the unscramble will take the flag to recover the correct order of the words. This is the only solution, since when you create the magicword you loose the information about which is the first and which is the second word. This means that your unscramble function cannot work without this information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.