Saturday, October 9, 2021

Rubik's Cube Instructions in JavaScript


Can Rubik's Cubes fit in Computer Science or Math? 

I say YES! Read on to find out why.

This past summer, I learned how to solve several versions of the Rubik's Cube. It is almost always advisable to start with the standard 3x3 cube. Most of the other versions (4x4, 5x5, Windmill, Mirror, etc.) are based on moves in the basic 3x3 instructions.


So I started with the 3x3. As I watched YouTube videos and wrote detailed instructions to prepare myself to teach this to my High School students, I couldn't help but think in terms of: 

  • Loops
  • Functions
  • Functions within Functions
  • Conditionals, and
  • APIs

Function / API:

In the above video and steps, you'll see a very frequently used set of moves, which this video calls 
"Right Hand":
JavaScript shown in block style
    1. Turn the right side clockwise
    2. Turn the top layer clockwise
    3. Turn the right side counter-clockwise
    4. Turn the top layer counter-clockwise 
The exact opposite, slightly less frequently used, set of moves, is called 
"Left Hand":
    1. Turn the left side counter-clockwise
    2. Turn the top layer counter-clockwise
    3. Turn the left side clockwise
    4. Turn the top layer clockwise


In the rest of the video/instructions,  "Right Hand" and "Left Hand" are referred to over and over without having to list the 4 steps that make them up every time.

From a Computer Science perspective, we have just added a Function to the API.

API:

Before this, a "cube-student" needs to learn the basic "commands", like:
R =  Turn the right side clockwise
R' = Turn the right side counter-clockwise
L = Turn the left side clockwise
L' = Turn the left side counter-clockwise
U and U' = The same pattern, with the top layer
F and F' = The same pattern, with the front face
D and D' = The same pattern, with the bottom layer
B and B' = The same pattern, with the back face

Conditionals:

As you go through the instructions, you'll get to a point where you need to do "Right Hand" UNTIL a certain condition is met (putting a corner that's on the top layer directly down to the bottom layer). 

Loops:

At a later part of the instructions, you'll need to do "Right Hand" THREE TIMES, turn the entire cube, then "Left Hand" THREE TIMES.






When you get to the very last step/phase, you'll do 
"Right Hand" ONCE
"Left Hand" ONCE
"Right Hand" 5x
"Left Hand" 5x


I used Code.org's App Lap to write a JavaScript version of the instructions & video mentioned earlier.
  • I made some Functions.
  • I made some For Loops to repeat a set of steps.
  • I needed to include more Comments where I had to stick with human-readable instructions. These were primarily for parts that were more intuitive, as opposed to an algorithm.
  • Math: I assumed that something like the following would be sufficient to evaluate whether one side of a piece did NOT match the desired color:  ((BottomOfCornerPiece != Yellow))

Parts of the "block" version of the code are shown above.

Here is the complete JavaScript project, shown in Text version:


//Define Basic Moves
//Right is often refered to as R
function Right() {
  TurnRightSideCLOCKWISE
}
//RightInv is often refered to as R'
function RightInv() {
  TurnRightSideCOUNTERCLOCKWISE
}
//Left is often refered to as L
function Left() {
  TurnLeftSideCLOCKWISE
}
//LeftInv is often refered to as L'
function LeftInv() {
  TurnLeftSideCOUNTERCLOCKWISE
}
//Up is often refered to as U
function Up() {
  TurnTopLayerCLOCKWISE
}
//UpInv is often refered to as U'
function UpInv() {
  TurnTopLayerCOUNTERCLOCKWISE
}
//Front is often refered to as F
function Front() {
  TurnFrontFaceCLOCKWISE
}
// FrontInv is often refered to as F'
function FrontInv() {
  TurnFrontFaceCOUNTERCLOCKWISE
}

//Define "RightHand" and "LeftHand"
function RightHand() {
  Right();
  Up();
  RightInv();
  UpInv();
}
function LeftHand() {
  LeftInv();
  UpInv();
  Left();
  Up();
}

function Bottom2Layers() {
  TurnBottomTwoLayersEitherDirection
}
function TurnCubeLeft() {
  TurnEntireCubeToTheLeft
}
function TurnCubeRight() {
  TurnEntireCubeToTheRight
}
function OrientCube(color) {
  OrientCubeSoThisColorIsOnTop
}

//WHITE CROSS
//Make "Daisy"(yellow center w/ white edges); Then continue:
//This "for" loop repeats its contents 4x
for (var i = 0; i < 4; i++) {
if (FrontFaceTopEdge_matches_FrontFaceCenter) {
  Right();
  Right();
  RotateCubeLeft
} else {
  Bottom2Layers();
}
//Goal is to have all white edges' other color to match centers.
}


//WHITE CORNERS in place
OrientCube(yellow);
while (LessThanFourWhiteCornersInPlace) {
  RotateTop: CornerWithWhite=AboveFinalPlace
while (WhiteSquareNotInPlace) {
  RightHand();
}
//If a white square is on the bottom, but in the wrong place, 
//Do these steps to move it to the top, then
//Repeat these steps to put it where it goes.
}

//COMPLETE MIDDLE LAYER (EDGES)
while ((TopEdgePiecesWithYellow < 4)) {
  // Turn U so Top Edge piece WITHOUT yellow faces you 
  while ((TopEdgePiece != FrontFaceCenter)) {
    Bottom2Layers();
  }
  //Top Edge piece should now match front face's center
  if (TopEdgePieceTopColor == RightFaceCenter) {
    Up();
    RightHand();
    TurnCubeLeft();
    LeftHand();
  } else {
    //Top Edge Piece's top color should match LEFT face center
    UpInv();
    LeftHand();
    TurnCubeRight();
    RightHand();
  }
}

//Orient Cube so Yellow is on bottom
OrientCube(white);

//YELLOW CROSS
while (NoYellowCrossYet) {
if (HorizontalYellowLine) {
  //Position yellows to the left and right
  //(NOT up and down)
  Front();
  RightHand();
  FrontInv();
} else if (NineOClockPattern) {
  //Position yellows in top and left
  Front();
  RightHand();
  FrontInv();
} else {
  //Only yellow center
  Front();
  RightHand();
  FrontInv();
}
}

//POSITION Yellow Corners
//These "for" loops repeats their contents 3x
OrientCube(yellow);
//Up until 2 adjacent corners are in proper position
//Put these 2 correct corners to the left
//These steps will swap the right two corners
for (var i = 0; i < 3; i++) {
  RightHand();
}
RotateCubeLeft();
for (var i = 0; i < 3; i++) {
  LeftHand();
}

//ORIENT Yellow Corners
OrientCube(white);
while ((NumberOfYellowCornersFacingDown < 4)) {
  while ((BottomOfCornerPiece != Yellow)) {
    //This should position the bottom-right piece so that
    //yellow is NOT on the bottom, but on one of the sides
    Bottom();
  }
  while ((BottomOfCornerPiece != Yellow)) {
    RightHand();
  }
}


//LAST STEP - position top edges
//This will rotate top edge pieces
while (CubeNotSolved) {
RightHand();
LeftHand();
//These "for" loops repeat their contents 5x
for (var i = 0; i < 5; i++) {
  RightHand();
}
for (var i = 0; i < 5; i++) {
  LeftHand();
}
}


Wednesday, August 5, 2020

Google Likes Breakfast!



When teaching students how to use Google products, we find ourselves needing to refer to these images:


 (This one's in the top-right of most Google products.)



(This one's on the Chrome browser's Bookmarks Bar.)



So what should we call them?
  • App Launcher? (the official name)
  • 3x3 grid? (what I've been calling them)
  • 9 squares? (fairly straightforward)
  • Rubik's cube? (pretty cute, I must say...)
Or, the emerging favorite...
  • Waffle!

The same situation exists with the 3 horizontal lines that are at the top-left of many Google products.
Going with the breakfast theme, it seems logical to call them...

  • Pancakes! (some call them a hamburger)


Therefore, I hereby declare that when I teach about, or talk about, these images, I will refer to them as "waffle", "colored waffle", and "pancakes".

Or at least I will try to. If others don't know what I'm talking about...well, some of the other options above work, too.
Waffles and pancakes make me smile, though.

Sunday, March 29, 2020

Physical Touch - #Reset2020

Touch: We don't know what we have 'till it's gone

Perhaps you are the type that loves to hug, shake hands, and such. Perhaps you're a bit more stand-off-ish. During this time of "social distancing" and a dramatic decrease of physical touch, I'm willing to bet that we all beginning to long to touch someone without anxiety of spreading sickness.
There are several reasons that touch is important to our mental health, our relational health, and even our spiritual health.

A. Touch is one of the "Five Love Languages"
Check out the link above for more details, but here's a summary of the concept.
We all feel love most significantly through one or more of these "languages":


This is a classic book about marriage and relationships. It's one of my most-recommended books. 
Order the book on Amazon (and thank God for all those helping to deliver everything that we all are ordering online, like those that work for the US Postal Service, UPS, Amazon, etc.)


B. Touch is part of "The Blessing"

  • Meaningful and appropriate touch (here's the section about touch)
  • A spoken message of love and acceptance
  • Attaching “high value” to the person being blessed
  • Picturing a special future for that person
  • An active commitment to fulfill the blessing

The main context that the authors of this book discuss "The Blessing" is from parent to child, but they describe many other contexts in which The Blessing applies.

Order the book on Amazon (and thank God for whoever delivers it to you)

C. Jesus used physical touch to heal people, which He could have just spoken words to heal them.

  • A woman knew that just touching His robe would heal her. Jesus felt power go out of Him when the touch occurred. Jesus did not scold her for the touch... Luke 8:43-46, and parallel passages
  • He he brought a dead girl back to life, and "took her by the hand" to help her stand up. Luke 8:53-55
  • He healed blind men by touching them. Matthew 20:32-34
  • He healed a man whose "skin was covered with leprosy" Luke 5:12-13  

Even for those that aren't usually very "touchy", I'll bet that you are starting to really miss being able to shake someone's hand, or hug someone (that you aren't quarantined with).

Like many things, we tend to not really know what we've got 'till it's gone. We don't appreciate the blessings that we've been given until we have to live without them. Today, I'm thinking about physical touch, prompted by a sermon by PJ Lewis at The Well-Kingsburg.

I predict that we as a society will explicitly value things that we are prevented from doing during this time of quarantine...including, or maybe especially...

physical touch.


Wednesday, March 18, 2020

God's Promises Bring us Peace and Stability

I recently went through an incredibly difficult time in my life. Anyone that I tell the story to is kind of amazed about how painful it must have been... What might be more surprising is that when the storm kind of died down, I was actually glad that it happened. Here are the main reasons that I'm glad.

I was already questioning and searching for promises from Scripture that I could really grab on to, and have a deeper confidence in than ever before. Trials make us dig deeper to find resources that we don't focus on very much otherwise, and this was where I knew I needed to dig.

When I was at nearly my darkest days, God gave me 3 promises that I could really cling to in the storm. Then, a bit later, He gave me a 4th. Here are the 4 promises that are now tremendously meaningful to me:

My 4 favorite promises for times of suffering

1. God loves me and cares about me“For God so loved the world…” John 3:16
“For I am convinced...” ‭‭Romans‬ ‭8:38-39‬
“Cast all your anxiety on him because he cares for you.” 1 Peter‬ ‭5:7‬

2. God will never leave meAnd be sure of this: I am with you always, even to the end of the age.”  Matthew‬ ‭28:20‬
“And I will ask the Father, and he will give you another Advocate, who will never leave you.”
‭‭John‬ ‭14:16‬ ‭NLT‬‬

3. In this world, I will have trouble, but true peace comes from Him“I have told you these things, so that in me you may have peace. In this world you will have trouble. But take heart! I have overcome the world.” ‭‭John‬ ‭16:33‬

4. Jesus understands, even when no one else understands“For we do not have a high priest who is unable to empathize with our weaknesses, but we have one who has been tempted in every way, just as we are—yet he did not sin. Let us then approach God’s throne of grace with confidence, so that we may receive mercy and find grace to help us in our time of need.”
‭‭Hebrews‬ ‭4:15-16‬ ‭NIV‬‬


Now that we are in this historic period of the Coronavirus, with everything shutting down, and all the medical, economic, emotional, familial, and faith applications of that, here are some elaborations on what each of these promises mean during this time, at least to me.

1. God loves me and cares about me
For I am convinced that neither death nor life, neither social distancing, neither the Coronavirus, nor COVID-19, nor fear, nor isolation, nor sickness, nor the media, nor politics, nor uncertainty, nor anything else in all creation, will be able to separate us from the love of God that is in Christ Jesus our Lord.

2. God will never leave me
To be wise and safe, we are all needing to "leave" each other, at least physically. God has promised that His Holy Spirit to be with us no matter what we are walking through.

3. In this world, I will have trouble, but true peace comes from Him
There is definitely plenty of trouble going on today, on many levels. Trouble is normal, even if this particular kind of trouble, and at this scale, is not normal.
Most of our fear comes from the fear of death. Jesus has conquered death itself!

4. Jesus understands, even when no one else understands
You might be struggling with anxiety or fear. You might have some of the medical symptoms that we are hearing so much about in the news. You might be separated from loved ones by the quarantine requirements. It might feel like no one understands how hard this is.
Jesus understands what it feels like to be alone. He knows what it is like to be tempted to be fearful. He knows what it is like to have physical affliction. He demonstrated compassion for many, many sick people when he walked this earth.
He understands.

I hope this brings a degree of peace and comfort to this very stressful time.



Monday, November 25, 2019

Did anyone "furl" a flag today?

I heard about this piece on NPR recently, and knew it was just the sort of thing I would enjoy. Maybe you would, too. It's called "How I Met My Wife", by Jack Winter. It was published in The New Yorker in July, 1994.

It's full of words that are correctly used with a negative prefix, minus the prefix, like "ruly" instead of "unruly" Some of them require a bit more thought than others to figure out. Some refer to a figure of speech.
I love this because these are just the sort of jokes and observations I find myself making all the time!
Just ask anyone that knows me well. I've been known to make remarks like these (these words taken from the article below):
"Why is it always 'nonchalant'? Does anyone walk about 'chalantly'?" or
"If you fold up a flag, would that be considered 'furling' it?" or
"If you can, in fact, understand something that seems tricky, would you say that you CAN make hide or hair of it?", or finally
"What does 'indefatigable' really mean?
If I can fatigue you, then I can tire you out. You are fatigable.
Then, if I can somehow energize you, then you would be defatigable.
But if this cannot be done (you can NOT be energized after being tired out) then you're indefatigable...which seems to mean the opposite of the actual meaning of the word.

Well, you're probably tired of this, so I'll just let you read the original:



SHOUTS AND MURMURS about man who describes meeting his wife at a party. In his description, he drops many prefixes. It had been a rough day, so when I walked into the party I was very chalant, despite my efforts to appear gruntled and consolate. I was furling my wieldy umbrella for the coat check when I saw her standing alone in a corner. She was a descript person, a woman in a state of total array. Her hair was kempt, her clothing shevelled, and she moved in a gainly way. I wanted desperately to meet her, but I knew I'd have to make bones about it, since I was travelling cognito. Beknownst to me, the hostess, whom I could see both hide and hair of, was very proper, so it would be skin off my nose if anything bad happened. And even though I had only swerving loyalty to her, my manners couldn't be peccable. Only toward and heard-of behavior would do. Fortunately, the embarrassment that my maculate appearance might cause was evitable. There were two ways about it, but the chances that someone as flappable as I would be ept enough to become persona grata or sung hero were slim. I was, after all, something to sneeze at, someone you could easily hold a candle to, someone who usually aroused bridled passion. So I decided not to rush it. But then, all at once, for some apparent reason, she looked in my direction and smiled in a way that I could make heads or tails of. So, after a terminable delay, I acted with mitigated gall and made my way through the ruly crowd with strong givings. Nevertheless, since this was all new hat to me and I had no time to prepare a promptu speech, I was petuous. She responded well, and I was mayed that she considered me a savory char- acter who was up to some good. She told me who she was. "What a perfect nomer," I said, advertently. The conversation became more and more choate, and we spoke at length to much avail. But I was defatigable, so I had to leave at a godly hour. I asked if she wanted to come with me. To my delight, she was committal. We left the party together and have been together ever since. I have given her my love, and she has requited it.

Monday, November 11, 2019

My namesakes

Just in case there is any confusion...

I am NOT this Edward Warkentin, the Canadian Lawyer. I was certainly not born on November 1, 1949 in Niagara Falls, Ontario. He might or might not be the same as this Edward Warkentin.
I'm much younger than that. It sure feels good to talk about how young I am, since I've been having to talk about how old I am, with regard to vision, hearing, running endurance, and such. However, being younger than a 70-year-old isn't saying a lot, I suppose.

I'm also much younger than this Edward Warkentin, who is, in fact, dead!

I am also NOT this Edward  L  Warkentin, who lives in Winnepeg, Manitoba. My phone number is not 204-669-4041.

This Edward Warkentin and this Edward Warkentin can't be found, and I'm not him, either.

Sadly, I make far less than this Edward Warkentin, who may or may not be the same as this Edward Warkentin.

I AM one of these Edward Warkentins on Linked In, though. This one, in fact.

I AM one of these Edward Warkentins on peoplefinders, as well. But no one calls me "Eba"!

If this post seems a little weird...or a lot weird...contact me directly, and I'd be glad to tell you why. It's kind of interesting.

FYI: The URL for this blog (edwardwarkentin.com) used to be https://ededtech.blogspot.com/

Follow me on Twitter: https://twitter.com/senorw and
Facebook: https://www.facebook.com/warkentin.ed

Adult School is cool!

Well, this blog has been dormant for quite a while. God has been doing some very important work in my life. It's time to re-enter the blogosphere, and reflect on life, share my insights and wisdom a bit more publicly.

Here's the nugget of the day:

Student type #1: Students that are forced to be there and don't want to be.
Many K-12 students.

Student type #2: Students that are forced to be there, but enjoy it, want to be there, and take it seriously.
In K-12, these are rare, but not "extinct".

Student type #3: Students that want to be there (learn).
These are the ones that all teachers love to have.
Teaching Adult School is almost exclusively #3.

I spent 24 years teaching K-12. Now, I teach in Alternative Education in Dinuba Unified School District. This means I work at Sierra Vista Continuation High School, Ronald Reagan Independent Study, and Dinuba Adult School. (I like to say that I get to do my favorite things at each of the three schools. More on that in another post.)

The Adult School part of my job is distinct and fulfilling because they always want to be there. The students have personal goals and are ready to maturely pursue them. This makes teaching them a joy.

I'm content where God has placed me. I'm taking opportunities like this to do what my Grandma's favorite verses, Philippians 4:8, and 1 Thessalonians 5:18, advises us - think about good things...things to be thankful for.

FYI: The URL for this blog (edwardwarkentin.com) used to be https://ededtech.blogspot.com/

Follow me on Twitter: https://twitter.com/senorw and
Facebook: https://www.facebook.com/warkentin.ed

Rubik's Cube Instructions in JavaScript

Can Rubik's Cubes fit in Computer Science or Math?  I say YES! Read on to find out why. This past summer, I learned how to solve several...