code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

04 Condense Numbers

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

function randomSequence(n) {
  let sequence = []
  for (let index = 0; index < n; index++) {
    const num = Math.floor(Math.random() * 10)
    sequence.push(num)
  }
  return sequence.join('')
}

function condenseNumbers(value) {
  let sequence = value.split('')
  let stack = [sequence.pop()]
  while(sequence.length > 0){
    const left = sequence.pop()
    const right = stack.pop()
    if(left !== right){
      stack.push(right)
      stack.push(left)
    }
  }

  // put everthing back to first stack to undo reversed order
  while(stack.length > 0){
    sequence.push(stack.pop())
  }
  return sequence.join('')
}

test('condense Numbers', () => {
  expect(condenseNumbers('54322346')).toEqual('56')
})

Created 2019-11-30T17:34:30+05:18 · Edit