“Setups and Payoffs in Fiction” … or, The Sliding Window Algorithm

BACKGROUND: While on my adventure to learn algorithms, I was fortunate to have Flatiron graduate Daniel Dawson recommend this course: Grokking the Coding Interview.

Despite my having access to multiple Udemy courses, AlgoExpert, InterviewCake etc., (all very helpful in their own way)… Grokking the Coding Interview was a standout, in part because of the recommend, also because of this blog post: 14 Patterns to Ace Any Coding Interview, but also because having slogged through multiple algorithm coding questions: I realized what was lacking (for me) was an ability to see the forest for the trees.

Different coding questions would seem to have certain principles in common, or feature code that was essentially the same, despite working towards disparate outputs.

If you’ve been reading my blog, you’ll know that I learn best by conforming algorithms to real world examples. Today being the first day I’m really exploring the course, I wanted to share an analogy that helped me combine my love of writing/fiction with my love for code.

RE: THE SLIDING WINDOW: A “sliding window” in code is used to iterate through a subset of an array. You might use the sliding window to calculate the averages for a sum of numbers within a window, determine “the mode”, perform other calculations, etc…

To think about it in a really “BIG PICTURE” way, the sliding window (in real life) seems like it would/could be used to iterate through some sequence in search of some commonalities. If the “sliding window” were a gadget or feature on James Bond’s glasses, he might be standing at the roulette table, and his “sliding window” would observe who is standing next to who, send the data back to Q

THE CODE: Coming from a writing background, one thing that makes for a great story is having setups and payoffs occur like fireworks, on right after the other. A great story will have some general setup, but after you get to the first “pay off”, it’s just one pay off after another until you get to the end of the story.

In terms of a “sliding window” (and the code below), we take an array of the common “plot points” from Joseph Campbell’s The Hero’s Journey. To get a sense of how it works… just run the code below. If you want to see the inner workings, take a look at the console logs as they run.

One final thought: there are a few helper functions to help formatting for this specific example. Also, a quick explanation about this line of code: allSubplots.push(subplot.map(individualScene => subplotCleaner(individualScene, subplot))); … the map function is used because allSubPlots was originally filling up with identical subplots (my thought is: that when subplot was pushed into allsubplots (without using map to make a copy), it was essentially serving as a pointer to whatever the latest ‘state’ of the subplot was.

function determineSubplots(sizeOfSubplotByScenes, allScenes) {
  const allSubplots = [];
  let subplot = [];
  let subplotStartSceneIndex = 0; // index to be used in tandem with scenes
  
  for (let subplotEndSceneIndex = 0; subplotEndSceneIndex < allScenes.length; subplotEndSceneIndex++) {
    subplot.push(allScenes[subplotEndSceneIndex]); 
    console.log("THIS SCENE ", allScenes[subplotEndSceneIndex], "HAS BEEN ADDED SUBPLOT ARRAY")
  
    if (subplotEndSceneIndex >= sizeOfSubplotByScenes - 1) {
      console.log("     WE ARE NOW IN THE IF ...")
      allSubplots.push(subplot.map(individualScene => subplotCleaner(individualScene, subplot)));
      console.log("")
      console.log("     pushing subplot", subplot)
      console.log("     all subplots", allSubplots)
      console.log("     deleting", allScenes[subplotStartSceneIndex])
      console.log(subplot.splice(0, 1))
      console.log("")  
      subplotStartSceneIndex += 1; // slide the window ahead
    }
  }
  
  for (let i = 0; i < 20; i++) {
    console.log(" ")
  }

  return presentSubplotsAsSequences(allSubplots)
}

//helper functions 

function subplotCleaner(x, y) {
  if (x === y[0]) {
    return ("SET SOMETHING UP HERE: " + x)
  } else if (x === y[y.length - 1]) {
    return ("PAY IT OFF HERE: " + x)
  } else {
    return ("scene: " + x)
  }
}

function presentSubplotsAsSequences(allsubplots){
  let sequenceNum = 1
  for (const sequence of allsubplots) { 
  console.log(`----------OVERLAPPING SEQUENCE:  ${sequenceNum}-----------------`) 
    for (scene of sequence) {
      console.log(scene)
    }
   sequenceNum++;
  }
}



//      H O W     T O    U S E    T H I S
// the first argument, currently set to 12 is the max size of a "subplot"
// if you want segments of "3", then you'll get iterative sequences of 3
// the second argument are the "scenes", current placeholders from The Hero's Journey 

// preset with Joseph Campbell Hero's Journey 


determineSubplots(12, [
  "The Ordinary World",
  "The Call To Adventure",
  "Refusal of the Call",
  "Meeting the Mentor",
  "Crossing the First Threshold",
  "Tests, Allies, Enemies",
  "Approach to the Inmost Cave",
  "The Ordeal",
  "Reward (Seizing the Sword)", 
  "The Road Back",
  "Resurrection",
  "Return with the Elixir"
]);

If you’re a writer and want to try it out live… try out this repl!

OR…

determineSubplots(4, [
  "The Ordinary World",
  "The Call To Adventure",
  "Refusal of the Call",
  "Meeting the Mentor",
  "Crossing the First Threshold",
  "Tests, Allies, Enemies",
  "Approach to the Inmost Cave",
  "The Ordeal",
  "Reward (Seizing the Sword)", 
  "The Road Back",
  "Resurrection",
  "Return with the Elixir"
]);

// Running the above code will return the following...

THIS SCENE  The Ordinary World HAS BEEN ADDED SUBPLOT ARRAY
THIS SCENE  The Call To Adventure HAS BEEN ADDED SUBPLOT ARRAY
THIS SCENE  Refusal of the Call HAS BEEN ADDED SUBPLOT ARRAY
THIS SCENE  Meeting the Mentor HAS BEEN ADDED SUBPLOT ARRAY
     WE ARE NOW IN THE IF ...

     pushing subplot [
  'The Ordinary World',
  'The Call To Adventure',
  'Refusal of the Call',
  'Meeting the Mentor'
]
     all subplots [
  [
    'SET SOMETHING UP HERE: The Ordinary World',
    'scene: The Call To Adventure',
    'scene: Refusal of the Call',
    'PAY IT OFF HERE: Meeting the Mentor'
  ]
]
     deleting The Ordinary World
[ 'The Ordinary World' ]

THIS SCENE  Crossing the First Threshold HAS BEEN ADDED SUBPLOT ARRAY
     WE ARE NOW IN THE IF ...

     pushing subplot [
  'The Call To Adventure',
  'Refusal of the Call',
  'Meeting the Mentor',
  'Crossing the First Threshold'
]
     all subplots [
  [
    'SET SOMETHING UP HERE: The Ordinary World',
    'scene: The Call To Adventure',
    'scene: Refusal of the Call',
    'PAY IT OFF HERE: Meeting the Mentor'
  ],
  [
    'SET SOMETHING UP HERE: The Call To Adventure',
    'scene: Refusal of the Call',
    'scene: Meeting the Mentor',
    'PAY IT OFF HERE: Crossing the First Threshold'
  ]
]
     deleting The Call To Adventure
[ 'The Call To Adventure' ]

THIS SCENE  Tests, Allies, Enemies HAS BEEN ADDED SUBPLOT ARRAY
     WE ARE NOW IN THE IF ...

     pushing subplot [
  'Refusal of the Call',
  'Meeting the Mentor',
  'Crossing the First Threshold',
  'Tests, Allies, Enemies'
]
     all subplots [
  [
    'SET SOMETHING UP HERE: The Ordinary World',
    'scene: The Call To Adventure',
    'scene: Refusal of the Call',
    'PAY IT OFF HERE: Meeting the Mentor'
  ],
  [
    'SET SOMETHING UP HERE: The Call To Adventure',
    'scene: Refusal of the Call',
    'scene: Meeting the Mentor',
    'PAY IT OFF HERE: Crossing the First Threshold'
  ],
  [
    'SET SOMETHING UP HERE: Refusal of the Call',
    'scene: Meeting the Mentor',
    'scene: Crossing the First Threshold',
    'PAY IT OFF HERE: Tests, Allies, Enemies'
  ]
]
     deleting Refusal of the Call
[ 'Refusal of the Call' ]

THIS SCENE  Approach to the Inmost Cave HAS BEEN ADDED SUBPLOT ARRAY
     WE ARE NOW IN THE IF ...

     pushing subplot [
  'Meeting the Mentor',
  'Crossing the First Threshold',
  'Tests, Allies, Enemies',
  'Approach to the Inmost Cave'
]
     all subplots [
  [
    'SET SOMETHING UP HERE: The Ordinary World',
    'scene: The Call To Adventure',
    'scene: Refusal of the Call',
    'PAY IT OFF HERE: Meeting the Mentor'
  ],
  [
    'SET SOMETHING UP HERE: The Call To Adventure',
    'scene: Refusal of the Call',
    'scene: Meeting the Mentor',
    'PAY IT OFF HERE: Crossing the First Threshold'
  ],
  [
    'SET SOMETHING UP HERE: Refusal of the Call',
    'scene: Meeting the Mentor',
    'scene: Crossing the First Threshold',
    'PAY IT OFF HERE: Tests, Allies, Enemies'
  ],
  [
    'SET SOMETHING UP HERE: Meeting the Mentor',
    'scene: Crossing the First Threshold',
    'scene: Tests, Allies, Enemies',
    'PAY IT OFF HERE: Approach to the Inmost Cave'
  ]
]
     deleting Meeting the Mentor
[ 'Meeting the Mentor' ]

THIS SCENE  The Ordeal HAS BEEN ADDED SUBPLOT ARRAY
     WE ARE NOW IN THE IF ...

     pushing subplot [
  'Crossing the First Threshold',
  'Tests, Allies, Enemies',
  'Approach to the Inmost Cave',
  'The Ordeal'
]
     all subplots [
  [
    'SET SOMETHING UP HERE: The Ordinary World',
    'scene: The Call To Adventure',
    'scene: Refusal of the Call',
    'PAY IT OFF HERE: Meeting the Mentor'
  ],
  [
    'SET SOMETHING UP HERE: The Call To Adventure',
    'scene: Refusal of the Call',
    'scene: Meeting the Mentor',
    'PAY IT OFF HERE: Crossing the First Threshold'
  ],
  [
    'SET SOMETHING UP HERE: Refusal of the Call',
    'scene: Meeting the Mentor',
    'scene: Crossing the First Threshold',
    'PAY IT OFF HERE: Tests, Allies, Enemies'
  ],
  [
    'SET SOMETHING UP HERE: Meeting the Mentor',
    'scene: Crossing the First Threshold',
    'scene: Tests, Allies, Enemies',
    'PAY IT OFF HERE: Approach to the Inmost Cave'
  ],
  [
    'SET SOMETHING UP HERE: Crossing the First Threshold',
    'scene: Tests, Allies, Enemies',
    'scene: Approach to the Inmost Cave',
    'PAY IT OFF HERE: The Ordeal'
  ]
]
     deleting Crossing the First Threshold
[ 'Crossing the First Threshold' ]

THIS SCENE  Reward (Seizing the Sword) HAS BEEN ADDED SUBPLOT ARRAY
     WE ARE NOW IN THE IF ...

     pushing subplot [
  'Tests, Allies, Enemies',
  'Approach to the Inmost Cave',
  'The Ordeal',
  'Reward (Seizing the Sword)'
]
     all subplots [
  [
    'SET SOMETHING UP HERE: The Ordinary World',
    'scene: The Call To Adventure',
    'scene: Refusal of the Call',
    'PAY IT OFF HERE: Meeting the Mentor'
  ],
  [
    'SET SOMETHING UP HERE: The Call To Adventure',
    'scene: Refusal of the Call',
    'scene: Meeting the Mentor',
    'PAY IT OFF HERE: Crossing the First Threshold'
  ],
  [
    'SET SOMETHING UP HERE: Refusal of the Call',
    'scene: Meeting the Mentor',
    'scene: Crossing the First Threshold',
    'PAY IT OFF HERE: Tests, Allies, Enemies'
  ],
  [
    'SET SOMETHING UP HERE: Meeting the Mentor',
    'scene: Crossing the First Threshold',
    'scene: Tests, Allies, Enemies',
    'PAY IT OFF HERE: Approach to the Inmost Cave'
  ],
  [
    'SET SOMETHING UP HERE: Crossing the First Threshold',
    'scene: Tests, Allies, Enemies',
    'scene: Approach to the Inmost Cave',
    'PAY IT OFF HERE: The Ordeal'
  ],
  [
    'SET SOMETHING UP HERE: Tests, Allies, Enemies',
    'scene: Approach to the Inmost Cave',
    'scene: The Ordeal',
    'PAY IT OFF HERE: Reward (Seizing the Sword)'
  ]
]
     deleting Tests, Allies, Enemies
[ 'Tests, Allies, Enemies' ]

THIS SCENE  The Road Back HAS BEEN ADDED SUBPLOT ARRAY
     WE ARE NOW IN THE IF ...

     pushing subplot [
  'Approach to the Inmost Cave',
  'The Ordeal',
  'Reward (Seizing the Sword)',
  'The Road Back'
]
     all subplots [
  [
    'SET SOMETHING UP HERE: The Ordinary World',
    'scene: The Call To Adventure',
    'scene: Refusal of the Call',
    'PAY IT OFF HERE: Meeting the Mentor'
  ],
  [
    'SET SOMETHING UP HERE: The Call To Adventure',
    'scene: Refusal of the Call',
    'scene: Meeting the Mentor',
    'PAY IT OFF HERE: Crossing the First Threshold'
  ],
  [
    'SET SOMETHING UP HERE: Refusal of the Call',
    'scene: Meeting the Mentor',
    'scene: Crossing the First Threshold',
    'PAY IT OFF HERE: Tests, Allies, Enemies'
  ],
  [
    'SET SOMETHING UP HERE: Meeting the Mentor',
    'scene: Crossing the First Threshold',
    'scene: Tests, Allies, Enemies',
    'PAY IT OFF HERE: Approach to the Inmost Cave'
  ],
  [
    'SET SOMETHING UP HERE: Crossing the First Threshold',
    'scene: Tests, Allies, Enemies',
    'scene: Approach to the Inmost Cave',
    'PAY IT OFF HERE: The Ordeal'
  ],
  [
    'SET SOMETHING UP HERE: Tests, Allies, Enemies',
    'scene: Approach to the Inmost Cave',
    'scene: The Ordeal',
    'PAY IT OFF HERE: Reward (Seizing the Sword)'
  ],
  [
    'SET SOMETHING UP HERE: Approach to the Inmost Cave',
    'scene: The Ordeal',
    'scene: Reward (Seizing the Sword)',
    'PAY IT OFF HERE: The Road Back'
  ]
]
     deleting Approach to the Inmost Cave
[ 'Approach to the Inmost Cave' ]

THIS SCENE  Resurrection HAS BEEN ADDED SUBPLOT ARRAY
     WE ARE NOW IN THE IF ...

     pushing subplot [
  'The Ordeal',
  'Reward (Seizing the Sword)',
  'The Road Back',
  'Resurrection'
]
     all subplots [
  [
    'SET SOMETHING UP HERE: The Ordinary World',
    'scene: The Call To Adventure',
    'scene: Refusal of the Call',
    'PAY IT OFF HERE: Meeting the Mentor'
  ],
  [
    'SET SOMETHING UP HERE: The Call To Adventure',
    'scene: Refusal of the Call',
    'scene: Meeting the Mentor',
    'PAY IT OFF HERE: Crossing the First Threshold'
  ],
  [
    'SET SOMETHING UP HERE: Refusal of the Call',
    'scene: Meeting the Mentor',
    'scene: Crossing the First Threshold',
    'PAY IT OFF HERE: Tests, Allies, Enemies'
  ],
  [
    'SET SOMETHING UP HERE: Meeting the Mentor',
    'scene: Crossing the First Threshold',
    'scene: Tests, Allies, Enemies',
    'PAY IT OFF HERE: Approach to the Inmost Cave'
  ],
  [
    'SET SOMETHING UP HERE: Crossing the First Threshold',
    'scene: Tests, Allies, Enemies',
    'scene: Approach to the Inmost Cave',
    'PAY IT OFF HERE: The Ordeal'
  ],
  [
    'SET SOMETHING UP HERE: Tests, Allies, Enemies',
    'scene: Approach to the Inmost Cave',
    'scene: The Ordeal',
    'PAY IT OFF HERE: Reward (Seizing the Sword)'
  ],
  [
    'SET SOMETHING UP HERE: Approach to the Inmost Cave',
    'scene: The Ordeal',
    'scene: Reward (Seizing the Sword)',
    'PAY IT OFF HERE: The Road Back'
  ],
  [
    'SET SOMETHING UP HERE: The Ordeal',
    'scene: Reward (Seizing the Sword)',
    'scene: The Road Back',
    'PAY IT OFF HERE: Resurrection'
  ]
]
     deleting The Ordeal
[ 'The Ordeal' ]

THIS SCENE  Return with the Elixir HAS BEEN ADDED SUBPLOT ARRAY
     WE ARE NOW IN THE IF ...

     pushing subplot [
  'Reward (Seizing the Sword)',
  'The Road Back',
  'Resurrection',
  'Return with the Elixir'
]
     all subplots [
  [
    'SET SOMETHING UP HERE: The Ordinary World',
    'scene: The Call To Adventure',
    'scene: Refusal of the Call',
    'PAY IT OFF HERE: Meeting the Mentor'
  ],
  [
    'SET SOMETHING UP HERE: The Call To Adventure',
    'scene: Refusal of the Call',
    'scene: Meeting the Mentor',
    'PAY IT OFF HERE: Crossing the First Threshold'
  ],
  [
    'SET SOMETHING UP HERE: Refusal of the Call',
    'scene: Meeting the Mentor',
    'scene: Crossing the First Threshold',
    'PAY IT OFF HERE: Tests, Allies, Enemies'
  ],
  [
    'SET SOMETHING UP HERE: Meeting the Mentor',
    'scene: Crossing the First Threshold',
    'scene: Tests, Allies, Enemies',
    'PAY IT OFF HERE: Approach to the Inmost Cave'
  ],
  [
    'SET SOMETHING UP HERE: Crossing the First Threshold',
    'scene: Tests, Allies, Enemies',
    'scene: Approach to the Inmost Cave',
    'PAY IT OFF HERE: The Ordeal'
  ],
  [
    'SET SOMETHING UP HERE: Tests, Allies, Enemies',
    'scene: Approach to the Inmost Cave',
    'scene: The Ordeal',
    'PAY IT OFF HERE: Reward (Seizing the Sword)'
  ],
  [
    'SET SOMETHING UP HERE: Approach to the Inmost Cave',
    'scene: The Ordeal',
    'scene: Reward (Seizing the Sword)',
    'PAY IT OFF HERE: The Road Back'
  ],
  [
    'SET SOMETHING UP HERE: The Ordeal',
    'scene: Reward (Seizing the Sword)',
    'scene: The Road Back',
    'PAY IT OFF HERE: Resurrection'
  ],
  [
    'SET SOMETHING UP HERE: Reward (Seizing the Sword)',
    'scene: The Road Back',
    'scene: Resurrection',
    'PAY IT OFF HERE: Return with the Elixir'
  ]
]
     deleting Reward (Seizing the Sword)
[ 'Reward (Seizing the Sword)' ]

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
----------OVERLAPPING SEQUENCE:  1-----------------
SET SOMETHING UP HERE: The Ordinary World
scene: The Call To Adventure
scene: Refusal of the Call
PAY IT OFF HERE: Meeting the Mentor
----------OVERLAPPING SEQUENCE:  2-----------------
SET SOMETHING UP HERE: The Call To Adventure
scene: Refusal of the Call
scene: Meeting the Mentor
PAY IT OFF HERE: Crossing the First Threshold
----------OVERLAPPING SEQUENCE:  3-----------------
SET SOMETHING UP HERE: Refusal of the Call
scene: Meeting the Mentor
scene: Crossing the First Threshold
PAY IT OFF HERE: Tests, Allies, Enemies
----------OVERLAPPING SEQUENCE:  4-----------------
SET SOMETHING UP HERE: Meeting the Mentor
scene: Crossing the First Threshold
scene: Tests, Allies, Enemies
PAY IT OFF HERE: Approach to the Inmost Cave
----------OVERLAPPING SEQUENCE:  5-----------------
SET SOMETHING UP HERE: Crossing the First Threshold
scene: Tests, Allies, Enemies
scene: Approach to the Inmost Cave
PAY IT OFF HERE: The Ordeal
----------OVERLAPPING SEQUENCE:  6-----------------
SET SOMETHING UP HERE: Tests, Allies, Enemies
scene: Approach to the Inmost Cave
scene: The Ordeal
PAY IT OFF HERE: Reward (Seizing the Sword)
----------OVERLAPPING SEQUENCE:  7-----------------
SET SOMETHING UP HERE: Approach to the Inmost Cave
scene: The Ordeal
scene: Reward (Seizing the Sword)
PAY IT OFF HERE: The Road Back
----------OVERLAPPING SEQUENCE:  8-----------------
SET SOMETHING UP HERE: The Ordeal
scene: Reward (Seizing the Sword)
scene: The Road Back
PAY IT OFF HERE: Resurrection
----------OVERLAPPING SEQUENCE:  9-----------------
SET SOMETHING UP HERE: Reward (Seizing the Sword)
scene: The Road Back
scene: Resurrection
PAY IT OFF HERE: Return with the Elixir

Discover more from Comedy Tragedy Epic

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

Continue reading