0

problem:

I have a practice activity with my online class that tells me to manipulate strings in 8 different orientations and put it in a list. 1. left to right, 2. right to left, 3. top to buttom, 4. buttom to top, 5. diagonally buttom left to upper right, 6. diagonally upper right to buttom left, 7. diagonally buttom right to upper left, 8. diagonally upper left to lower right. The code should be compatible with any dimension of the file given. reverse method can be used.

Any help will be much appreciated. I'm a newbie at these.

text to manipulate (in letters.txt file):

A B C D E F
G H I J K L
M N O P Q R 
S T U V W X
Y Z

expected output:

leftToRight = ['ABCDEF', 'GHIJKL', 'MNOPQR', 'STUVWX', 'YZ']

rightToLeft = ['FEDCBA', 'LKJIHG', 'RQPONM', 'XWVUTS', 'ZY']

topToButtom = ['AGMSY', 'BHNTZ', 'CIOU', 'DJPV', 'EKQW', 'FLRX']

buttomToTop = ['YSMGA', 'ZTNHB', 'UOIC', 'VPJD', 'EKQW', 'FLRX']

bLeftToURight = ['A', 'GB', 'MHC', 'SNID', 'YTOJE', 'ZUPKF', 'VQL', 'WR', 'X']

uRightToBLeft = ['A', 'BG', 'CHM', 'DINS', 'EJOTY', 'FKPUZ', 'LQV', 'RW', 'X']

bRightToULeft = ['Y', 'ZS', 'TM', 'UNG', 'VOHA', 'WPIB', 'XQJC', 'RKD', 'LE', 'F']

uLeftToBRight = ['Y', 'SZ', 'MT', 'GNU', 'AHOV', 'BIPW', 'CJQX', 'DKR', 'EL', 'F']

my code so far for opening the file and trying out left to right:

leftToRight = []
openL = open("letters.txt")
letters = openL.read()
letters = letters.replace(" ","")
for line in letters:
    leftToRight.append(line.strip())
print(lefToRight)

prints:

['A', 'B', 'C', 'D', 'E', 'F', '', 'G', 'H', 'I', 'J', 'K', 'L', '', 'M', 'N', 'O', 'P', 'Q', 'R', '', 'S', 'T', 'U', 'V', 'W', 'X', '', 'Y', 'Z']

should print:

['ABCDEF', 'GHIJKL', 'MNOPQR', 'STUVWX', 'YZ']
Glenn
  • 11
  • 1

1 Answers1

0

I would set up two-dimensional array containing the single characters (filling any unoccupied field with an appropriate default, possibly 0-character). Then iterate over it in both dimenstions (pseudo code only, as you just asked for ideas):

from 0 to height:
    from 0 to width:
        # collect string
    # add it to LR array
    # add reverted string to RL array

Analogously vertically, height and width interchanged...

Diagonals:

from h = 0 to max(height, width):
    from w = 0 to h:
       if h < height && w < width:
          # add matrix[h][w] to string
    #add string to list BLUR
    #add reverted string to URBL

from h = max(height, width) to 0:
    from w = h to max(height, width):
       if h < height && w < width:
          # add matrix[h][w] to string
    #add string to list ULBR
    #add reverted string to BRUL

Edit: According to comments and your edit:

You might possibly prefere reading line by line, see here. Instead of

print line;

you would have

leftToRight.append(line.replace(" ", "").strip());

You might want grab the maximum string length within this loop to be able to fill up with defaults in a second go (or check, if all strings have the same length).

By the way: leftToRight actually already is your two-dimenstional array, you can use it directly to generate the rest: rightToLeft: simply reverse all the strings - vertically and diagonals: as described above.

Community
  • 1
  • 1
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • you can give your code if you like and i would compare it with mine. thanks for your answer. – Glenn May 03 '16 at 09:32
  • @Glenn I haven't written any real code - just wanted to give you the hints you asked for... – Aconcagua May 03 '16 at 09:34
  • @Glenn If you need help for reading files, too, try Google with 'python read file', this should give you quite a few tutorials... If you have some code written that is not yet working, post it here and I'll have a look. – Aconcagua May 03 '16 at 09:38
  • I posted my code for opening the file and trying the left to right list. for some reason it split by character in the list not by line. – Glenn May 03 '16 at 09:42