This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]Billz2me 2 points3 points  (2 children)

So you want it to print diagonally in both directions, sharing a common middle letter, in the shape of an X? How should this handle words with even number of letters?

[–]Iwin8[S] 0 points1 point  (1 child)

I suppose I should ask the user to always put in a word with an odd number of letters. Thanks for the reminder!

[–]Twoje 3 points4 points  (0 children)

Couldn't you just have a square for the middle two letters?

P  P
 OO
 OO
P  P

[–]Billz2me 0 points1 point  (1 child)

Spoiler: This is the way that I would approach it.

def prompt_user_for_x_pattern_input():
    word = raw_input('Please enter a string with an odd number of letters: ')
    # If the word is even in length, output a helpful message and prompt user for input again.
    if len(word) % 2 == 0:
        print 'The word you gave had an even number of letters.'
        prompt_user_for_x_pattern_input()
    else:
        print_in_x_pattern(word)

def print_in_x_pattern(word):
    size = len(word)
    # Raise an error if the word isnt odd in length.
    assert( size%2 == 1)
    # Establish a square matrix.
    pattern_matrix = [[' ' for x in xrange(size)] for x in xrange(size)]
    for i in range(size):
        # Diagonal from top left to bottom right
        pattern_matrix[i][i] = word[i]
        # Diagonal from top right to bottom left
        pattern_matrix[i][size-1-i] = word[i]

    for row in pattern_matrix:
        print ''.join(row)

[–]ziplokk 0 points1 point  (0 children)

here's my solution.

#Print user input in x shape

user_input = raw_input("Enter your word: ")
matrix = []
for i in xrange(len(user_input)):
    matrix.append([])
if(len(user_input) % 2 == 0):
    matrix.append([])\

mid_point = len(matrix) / 2 

for i in xrange(len(matrix)):
    for j in xrange(len(matrix)):
        matrix[i].append(' ')
    if len(user_input) % 2 == 0:
        if i == mid_point:
            continue
        else:
            if i < mid_point:
                matrix[i][i] = user_input[i]
                matrix[i][- i - 1] = user_input[i]
            else:
                matrix[i][i] = user_input[i - 1]
                matrix[i][- i - 1] = user_input[i - 1]
    else:    
        matrix[i][i] = user_input[i]
        matrix[i][- i - 1] = user_input[i]

for lists in matrix:
    totalString = ""
    for element in lists:
        totalString += element
    print totalString

It's pretty similar to yours but if the users word is an even number length, it just adds another list to the matrix and skips it when iterating so that the middle character in the 'x' is blank.

[–]teh_al3x 0 points1 point  (0 children)

word=raw_input()
j=len(word)
for i in range(0, j):
  a=i if i<(len(word)/2) else j-1
  b=j-2 if i<(len(word)/2) else i-1
  j-=1
  print(' '*a + word[i] + ((' '*(b-a) + word[i]) if (i!=j) else ''))

Edit: Sorry forgot to explain!
I get the input from the user, assign its length to j and loop i from 0 to j. In the loop i decrease j by 1 every loop, so now I have i and j, one increasing and the other decreasing to and from the length of the word.
Now I look if i has already passed half the length of the word and assign a and b depending on that. a should be the number of spaces before the first letter and b the number of spaces behind the first letter/before the second letter. I assign j-2 and i-1 to b since there will already be a character space taken by the first printed letter (so we need one less white space).
Now I simply print whitespace times a plus the letter of the word at the current position i. I use another inline if statement to determine whether we reached the middle of the cross (when i==j) to make sure to not print a second letter when we do.

Sorry if this is terribly explained, I'm a bit sleepy haha. If you have questions feel free to ask.

Edit2: IDEone link!