Robben
Robben

Reputation: 261

String Mirror Function

I wanted to write a function that mirrors a string without using any built in functions and only using a loop.

For example, wordMirror("stackexchange") returns 'stackexchangeegnahcxekcats'.

I wrote the following code using some hints from my previous questions:

def wordMirror(word):
    c = ""
    for x in word:
        c = c + x
    return word + c

But it didn't work. I then started to play around with it and just reversed c = c + x to c = x + c and it magically worked.

What exactly is the code reading when I switched the variables?

Upvotes: 2

Views: 2334

Answers (3)

agconti
agconti

Reputation: 18123

I know you're looking for loops, but why not just:

c = "hello"
output = c + c[::-1]
print output
#=> 'helloolleh'

This takes your string c, which you can think of as an array of characters, and steps through it in reverse ( the -1 in the last argument to slicing notation does this). That way it allows you to easily concatenate your starting string and its mirrored version.

Many python developers believe that simple is better than complex. This sort of trick is a way to increase the ease at which you and other developers can understand and maintain the code you produce.

Upvotes: 2

Hunter McMillen
Hunter McMillen

Reputation: 61540

There is nothing magic about it, the order in which your are adding characters to c is reversed because you are inserting x before what was previously in c.

You should really step through a run by hand:

iteration  |   x   |    c 
-------------------------------
0             ''       ''
1              s        s
2              t        ts 
3              a        ats
4              c        cats
5              k        kcats
6              e        ekcats
7              x        xekcats
8              c        cxekcats
9              h        hcxekcats
10             a        ahcxekcats
11             n        nahcxekcats
12             g        gnahcxekcats
13             e        egnahcxekcats

From this simple table you should be able to see how the string is growing. Learning how to execute your programs by hand is a vital skill to have.

Upvotes: 5

TheSoundDefense
TheSoundDefense

Reputation: 6945

Walking through code one step at a time helps us make sense of it.

c = c + x

In this case, say c = "stackexchange". You'll go through the letters one by one, with each one going into x.

# x = "s"
c = c + x    # c = "" + "s" = "s"
# x = "t"
c = c + x    # c = "s" + "t" = "st"
# x = "a"
c = c + x    # c = "st" + "a" = "sta"

Now let's try it the other way.

# x = "s"
c = x + c    # c = "s" + "" = "s"
# x = "t"
c = x + c    # c = "t" + "s" = "ts"
# x = "a"
c = x + c    # c = "a" + "ts" = "ats"

That hopefully makes it clear why the order of addition gets you the results you do. When you're trying to figure out why code works the way it does, stepping through it slowly often will show you what's happening more clearly. You can do this by 1) finding a good IDE, or 2) adding print statements in strategic locations. Try adding a print x, c line immediately after c = x + c and you'll watch it get built one letter at a time.

Upvotes: 4

Related Questions