I'm working on writing a small program that jumbles up a word an allows the user to move letters around in a string until the word us un-jumbled.

For Example:

If the given word is "movie"
The program might jumble it to say "vmioe"
In which case "v" would be 0, "m" would 1, etc.

The user would type in a number for lets say the letter "e", in this case 4
Then the user would select a letter to swap with, lets say the letter "m", in this case 1

The program would then swap "e" and "m" to make the jumbled string look like "veiom"

Any help would be useful, I'm new to python.

You would have to make sure that the string does not have several equal letter pairs.

Note that strings are immutable. so you have to convert the string into list of letters to do the actual swapping and convert back to string, easy to do.

There is problem, if your string has several letter 'e', how would the program know which one to swap with for instance the 'm'?

Don't put them as strings, then. Put it as an array.

myWord = ["m", "o", "v", "i", "e"]

This way, you can access the letters as myWord[0] => "m".

Here are couple of helper functions:

def str2list(s):
    """convert string s to list of characters"""
    return list(s)

def list2str(q):
    """convert list of characters q to string"""
    return "".join(q)

def str_shuffle(s):
    """shufffle the characters of string s"""
    t_list = str2list(s)
    # random shuffle the list elements in place
    import random
    random.shuffle(t_list)
    return list2str(t_list)

print str_shuffle("movie")

I like to break things into functions, because the function names can tell what is going on.

Here are couple of helper functions:

def str2list(s):
    """convert string s to list of characters"""
    return list(s)

def list2str(q):
    """convert list of characters q to string"""
    return "".join(q)

def str_shuffle(s):
    """shufffle the characters of string s"""
    t_list = str2list(s)
    # random shuffle the list elements in place
    import random
    random.shuffle(t_list)
    return list2str(t_list)

print str_shuffle("movie")

I like to break things into functions, because the function names can tell what is going on.

You DO know # does a command, right? You don't need to use ''' every time.

Ah Linux, the quoted line(s) right after the function define is called the documentation string of the function.

Example:

# a look at the documentation strings for methods/functions
# commonly in triple quotes, but can use single quotes for one liners

import math

def getDistance(x1, y1, x2, y2):
  '''
  getDistance(x1, y1, x2, y2)
  returns distance between two points using the pythagorean theorem
  the function parameters are the coordinates of the two points
  '''
  dx = x2 - x1
  dy = y2 - y1
  return math.sqrt(dx**2 + dy**2)

print "Distance between point(1,3) and point(4,7) is", getDistance(1,3,4,7)
print "Distance between point(1,3) and point(11,19) is", getDistance(1,3,11,19)

print '-'*50

print "The function's documentation string:"
# shows comment between the triple quotes
print getDistance.__doc__

How can i turn a single string like "movie" and put each letter into an array separately like:

word =

Thanks for all the help, I'm new to programming so i apologize for some probably stupid questions!

This article has been dead for over six months. Start a new discussion instead.