2

The problem I'm working on is outputting a matrix in a clockwise inwards spiral. My code, right now, does this but the output is a little different than what is expected.

matrix = [
  ['a','d','g','e','t','c'],
  ['p','k','h','w','e','f'],
  ['m','j','y','h','b','n'],
  ['e','o','j','n','g','y']
]

def spiralPrint(mat):
  top = 0
  left = 0
  right = len(mat[0])-1
  bot = len(mat)-1

  result = []

  while(True):    
    #TRAVERSE ACROSS TOP ROW
    for j in range(left, right+1, 1):
      result.append(mat[top][j])

    #INC TOP INDEX SO WE DON'T REPEAT
    top += 1         

    #EXIT CONDITION
    if top > bot or left > right:
      break

    #TRAVERSE RIGHTMOST COLUMN
    for i in range(top, bot+1, 1):
      result.append(mat[i][right])             
    right -= 1
    if top > bot or left > right:
      break

    #TRAVERSE BOTTOM ROW
    for k in range(right, left-1, -1):
      result.append(mat[bot][k]) 
    bot -=1        

    if top > bot or left > right:
      break

    #TRAVERSE LEFT COLUMN
    for p in range(bot, top-1, -1):
      result.append(mat[p][left])             
    if top > bot or left > right:
      break
    left += 1

  return result

print(spiralPrint(matrix))

The output is supposed to look like: adgetcfnygnjoempk...

My output is: ['a', 'd', 'g', 'e', 't', 'c', 'f', 'n', 'y', 'g', 'n', 'j', 'o', 'e', 'm', 'p', 'k', 'h', 'w', 'e', 'b', 'h', 'y', 'j']

How do I output just the chars without the quotes and commas?

6 Answers6

0

Instead of returning result, do

return ''.join(result)

As the name implies, it joins the contents of list result into one string. The empty string at the beginning means nothing will be put between successive elements. For more details, see the join section here.

You get a list for result rather than a string since you repeatedly append to it, which is a list method. (Of course, you also initialized it as the empty list.) You could concatenate and keep result a string, but using a list then joining together is actually the recommended way of adding characters to a string. So you hit the Pythonic way perhaps by accident.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
0

If you want it to be in one line you can use this code

import numpy as np
''.join(np.array(matrix).flatten())
Out[101]: 'adgetcpkhwefmjyhbneojngy'

if you want to fix your code print(''.join(spiralPrint(matrix)))

Oren
  • 4,711
  • 4
  • 37
  • 63
0

You can use join (duplicate question)

>>> result = ['a', 'd', 'g']
>>> ''.join(result)
'adg'
>>> ' '.join(result)
'a d g'
Hasan
  • 200
  • 2
  • 12
0

You could put your spiralPrint call into a join:

print(''.join(spiralPrint(matrix)))

This will create a new string with each value. You could also add a delimiter to the leading string to create delimited output. For example:

print('-'.join(spiralPrint(matrix)))

gives the output: a-d-g-e-t-c-f-n-y-g-n-j-o-e-m-p-k-h-w-e-b-h-y-j

Andrew Hewitt
  • 518
  • 4
  • 14
0

Currently you are returning the result as a list, what tou expected is a single string,so you just have to join the list into a string. Instead of return result, do return ''.join(result)

Skycc
  • 3,496
  • 1
  • 12
  • 18
0

Your function is returning list. In order to make it return the string, you need to join the list. In your case as: return ''.join(result).


Also, as per Python PIP's coding guideline for Function Names:

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Hence, your function should be spiral_print() instead of spiralPrint()

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126