Skip to content

Instantly share code, notes, and snippets.

@adityabhushan
Created August 3, 2018 03:02
Show Gist options
  • Save adityabhushan/b8e628709bb27d85d331df60d4a99afc to your computer and use it in GitHub Desktop.
Save adityabhushan/b8e628709bb27d85d331df60d4a99afc to your computer and use it in GitHub Desktop.

Revisions

  1. adityabhushan created this gist Aug 3, 2018.
    49 changes: 49 additions & 0 deletions Spiral Array
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    # N = "hello hello_world"
    # print(N.split()[1])

    # arr = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
    arr = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
    top = 0
    bottom = len(arr)-1
    left = 0
    right = len(arr[0])-1
    # print(top)
    # print(bottom)
    # print(left)
    # print(right)
    newarr = []
    r,c = 0,0

    while (1):
    while(c <= right):
    newarr.append(arr[r][c])
    print('---->')
    print(newarr)
    c += 1
    print('c is ',c)
    if(c==right):
    right = right - 1
    while(r <= bottom):
    newarr.append(arr[r][c])
    print("down")
    print(newarr)
    r += 1
    if(r == bottom):
    bottom = bottom - 1
    while(c >= left):
    newarr.append(arr[r][c])
    print('<------')
    print(newarr)
    c -= 1
    if(c == left):
    left = left + 1
    while(r >= top):
    newarr.append(arr[r][c])
    print("up")
    print(newarr)
    r -= 1
    if(r == top):
    top = top + 1
    if(top == bottom and left == right):
    break
    print(newarr)