Palindromes, or The Sock Draw of a Troubled Mind

A palindrome is a word like “RACECAR” or “DID”, it is the same both forwards and backwards. Today, we’re going to cover several Javascript solutions with some variations that might be useful.

Where does “The Sock Draw of a Troubled Mind” come into play? That’s a good question… but it seems safe to assume that a ‘troubled mind’ likely has a sock draw that is either a nightmarish mess, or, is meticulously organized in some bizarre way. Suppose you were dating someone, and as fortune would have it, you find yourself alone in their bedroom peering into their sock drawer. To your surprise, the sock drawer is not only meticulous, but organized as a palindrome, from left to right: RED SOCKS, BLUE SOCKS, CAT PRINT SOCKS, SKULL & CROSSBONE SOCKS, DRESS SOCK, WHITE SOCKS, DRESS SOCKS, SKULL & CROSSBONE SOCKS, CAT PRINT SOCKS, BLUE SOCKS, and RED SOCKS. As a string: “RBCSDWDSCBR”, the same forward and backwards.

As you stare into the sock drawer wondering if this might classify as a “deal breaker”, your date, “Pat”, appears backlit in the doorway to the bedroom, a lone lightbulb swinging behind their head, and rubber chicken in hand, they asks you “JUST WHAT DO YOU THINK YOU’RE DOING?” Realizing it’s a bit ‘problematic’ to be snooping in Pat’s sock draw, you ignore the rubber chicken and compliment them on the ingenious organization of the sock draw.

Crisis averted, Pat offers to explain their genius.

function butIsItASockPalindrome(rowOfSocks) {
  let reverseOrderSocks = rowOfSocks.split('').reduce((newSockOrder, currentSock) => {return currentSock + newSockOrder})
  return rowOfSocks === reverseOrderSocks
}

butIsItASockPalindrome("RBCSDWDSCBR") //returns true

They sidle up beside you and peer into their sock drawer, eyes gleaming they turn to you and whisper: “butIsItASockPalindrome” and gesture to the first rowOfSocks: “RBCSDWDSCBR”.

They take out a Polaroid camera and take a pic of the draw as it is. “That’s so we can keep track of the original order, y’know, to make sure all is well.” They toss the developing polaroid onto a side table littered with hundreds of other polaroids, your skin crawls, but you don’t dare leave.

“What we do now is we reverse the order of the socks… to do that we take the row of socks and split those naughty socks up into an array. Each sock, isolated, and alone, like my 21st birthday…”

“uh huh.” you say, vowing to delete the dating app that brought Pat into your life.

Pat goes on… “With each sock isolated and alone, we reduce them into a New Order, starting with the very first pair. What we do is: we grab that sock and then add it to the front of the new sock order. When we’re done…we compare the reversed order of the socks to the polaroid. If the order is the same, then Pat happy.”

Thoughtlessly, you point out… “but it looks like there’s a space on the left… is it really a sock palindrome? Looks a bit more like:
" RBCSDWDSCBR" than "RBCSDWDSCBR".

Pat sighs. “There is truth in what you say. Perhaps I forgot to mention that I mentally trim the space.”

function butIsItASockPalindrome(rowOfSocks) {
  let reversedOrderMinusTheSpace = rowOfSocks.trim()
  let reverseOrderSocks = reversedOrderMinusTheSpace.split('').reduce((newSockOrder, currentSock) => {return currentSock + newSockOrder})
  return reversedOrderMinusTheSpace === reverseOrderSocks
}

butIsItASockPalindrome("  RBCSDWDSCBR") //returns true

“But, the above approach only applies to extra space on the left or right, not any internal space. I mean, only an idiot wouldn’t count internal space.” and Pat looks at you, like you’re an idiot.

Anger overcomes fear and you say, “to be honest Pat, there’s a more efficient way to do it. I hope you’re not offended, but I’d do it like this…”

function butIsItASockPalindrome(rowOfSocks) {
  let leftSock = 0;
  let rightSock = rowOfSocks.length - 1;
  while (leftSock < rightSock) {
    if (rowOfSocks[leftSock] !== rowOfSocks[rightSock]) return false;
    leftSock++;
    rightSock--;
  }
  return true;
}

butIsItASockPalindrome("RBCSDWDSCBR") //returns true
butIsItASockPalindrome(['Red Socks', 'Blue Socks', 'Red Socks'])

“You see, Pat” you condescend: “My way is better because as opposed to all that busy work with the creepy polaroids, and going through the bother of reducing every single sock in the entire damn row, because you only keep track of the leftSock and the rightSock, and if you screwed up organizing your socks as a palindrome, you’ll find out sooner than later…”

You lay it on thick: “In other words, Pat, going through the row while the the position/value of the leftSock is LESS than the rightSock guarantees you meet at the middle whether there are an even or odd number of socks… FURTHERMORE, as part of your while, you’re always checking that your leftSock and rightSock are equal. LASTLY, you could treat your row of socks as a string or an array meaning my approach is more robust.”

And soon, before you know it, you and Pat find yourself bonding and discussing alternate way to organize socks.

//using the array method "every"

function sockPalindrome(rowOfSocks) {
  return rowOfSocks.split('').every((sock, sockPosition) => {
    return sock === rowOfSocks[rowOfSocks.length - sockPosition - 1]
  });
}
// RECURSIVE SOCK MADNESS

function sockPalindrome(rowOfSocks, theLeftSock = 0) {
  const theRightSock = rowOfSocks.length - 1 - theLeftSock;
  
  if (theLeftSock >= theRightSock) {
    return true
  } else {
    return (rowOfSocks[theLeftSock] === rowOfSocks[theRightSock]) && sockPalindrome(rowOfSocks, theLeftSock + 1);
  }
}

Discover more from Comedy Tragedy Epic

Subscribe now to keep reading and get access to the full archive.

Continue reading