codebuddies / DailyAlgorithms

Do a problem. Create (or find) your problem in the issues. Paste a link to your solution. See others' solutions of the same problem.
12 stars 1 forks source link

[Practice] Spiral problem #20

Open lpatmo opened 5 years ago

lpatmo commented 5 years ago

Write a function spiral that accepts an integer N and creates a spiral with N columns and N rows

For example:

console.log(spiral(3))
// 1  2  3
// 8  9  4 
// 7  6  5
lpatmo commented 5 years ago

https://repl.it/@lpatmo/spiral?language=javascript


//Write a function spiral that accepts an integer N
// and creates a spiral with N columns and N rows

function spiral(N) {
   //Create an array of arrays, with the subarrays equal to N
   //[[],[],[]] e.g. with N = 3
   // Create a counter variable so that I can push it into the array
   //Create four other variables to keep track of rows and columns: 
   //rs=0, re=N-1, cs=0, ce=N-1 
   //(Abbreviations: row start, row end, column start, and column end)
   //WHILE rs <= re AND cs <= ce 
       //loop through the columns towards the right (1,2,3) until cs <= ce
       //increment rs so rs = 1
       //loop through the rows down (4,5) until rs <= re 
       //decrement column by 1
       //loop through column from right to left until ce >= cs 
       //decrement row count by 1
       //loop through row until re >= rs 
  const result = []
  for (let i = 0; i < N; i++ ){
    result.push([])
  }
  let counter = 1, rs = 0, re = N - 1, cs = 0, ce = N - 1;
  while (rs <= re && cs <= ce) {
    for (let i = cs; i <= ce; i++) {
      result[rs][i] = counter;
      counter++;
    }
    rs++;
    for (let i = rs; i <= re; i++) {
      result[i][ce] = counter;
      counter++;
    }
    ce--;
    for (let i = ce; i>=cs; i--) {
      result[re][i] = counter;
      counter++;
    }
    re--;
    for (let i = re; i>=rs; i--) {
      result[i][cs] = counter;
      counter++
    }
    cs++;
  }
  return result;

}

console.log(spiral(3))
// 1  2  3
// 8  9  4 
// 7  6  5
gauravchl commented 5 years ago

https://repl.it/@gauravchl/spiral

function getSpiral(n) {
  const arr = new Array(n).fill().map(i => new Array(n));
  let counter = 1;

  // Run loop for every spiral circle
  // And fill all sides in clock direction
  for(let i = 0; i < Math.ceil(n/2); i++) {

    // Fill top side 
    for(let j = i; j < n - i; j++) {
      arr[i][j]= counter;
      counter++;
    }

    // Fill Right Side 
    let k = n - i - 1;
    for(let j = i + 1; j < n - i; j++) {
      arr[j][k] = counter;
      counter++;
    }

    // Fill Bottom side
    let j = n - i - 1;
    for(let k = n - 2 - i; k >= i; k--) {
      arr[j][k] = counter;
      counter++;
    }

    // Fill left side
    for(let k = n - 2 - i; k > i; k--) {
      arr[k][i] = counter;
      counter++;
    }
  }
  return arr
}

getSpiral(7)

//  [ [ 1, 2, 3, 4, 5, 6, 7 ],
//   [ 24, 25, 26, 27, 28, 29, 8 ],
//   [ 23, 40, 41, 42, 43, 30, 9 ],
//   [ 22, 39, 48, 49, 44, 31, 10 ],
//   [ 21, 38, 47, 46, 45, 32, 11 ],
//   [ 20, 37, 36, 35, 34, 33, 12 ],
//   [ 19, 18, 17, 16, 15, 14, 13 ] ]
lpatmo commented 5 years ago

Python version h/t @ArcTanSusan:


# Write a function spiral that accepts an integer N
# and creates a spiral with N columns and N rows

def print_spiral(N):
    """
       Create an array of arrays, with the subarrays equal to N
       [[],[],[]] e.g. with N = 3
       Create a counter variable so that I can push it into the array
       Create four other variables to keep track of rows and columns:
       rs=0, re=N-1, cs=0, ce=N-1
       (Abbreviations: row start, row end, column start, and column end)
       WHILE rs <= re AND cs <= ce
           loop through the columns towards the right (1,2,3) until cs <= ce
         increment rs so rs = 1
           loop through the rows down (4,5) until rs <= re
           decrement column by 1
           loop through column from right to left until ce >= cs
           decrement row count by 1
           loop through row until re >= rs
    """
    rs = 0
    re = N-1
    cs = 0
    ce = N-1
    counter = 1
    result = []
    for row in range(N):
        result.append(['-' for n in range(N)])
    print(result)
    print('row end and rs')
    print(re, rs)

    while rs<=re and cs<=ce:
        for col in range(cs, ce+1):
            print("1st for")
            print(row, cs)
            print(counter)
            result[rs][col] = counter
            counter += 1
        rs += 1
        for row in range(rs, re+1):
            print("2nd for")
            print(row, cs)
            print(counter)
            result[row][ce] = counter
            counter += 1
        ce -= 1

        for col in range(ce, cs-1, -1):
            print("3rd for")
            print(re, col)
            print(counter)
            result[re][col] = counter
            counter += 1
        re-=1
        for row in range(re, rs-1, -1):
            print("4th for")
            print(row, cs)
            print(counter)
            result[row][cs] = counter
            counter += 1
        cs += 1

    for row in result:
        print(row)
        print("\n")

print_spiral(3)