Coding Challenges

TEDxBIT - May 25, 2011

Kyle Geske - Red River College

Praxis Makes Perfect

Ollie_monster_by_olivier_bareau

Praxis is the process by which a theory, lesson, or skill is enacted, practiced, embodied and realized.

The best people in any field are those who devote the most hours to what the researchers call “deliberate practice”. It’s activity that’s explicitly intended to improve performance, that reaches for objectives just beyond one’s level of competence, provides feedback on results and involves high levels of repetition and reflection.

– Geoffrey Colvin

Code Kata

Kamae_no_kata

Kata (方 literally: form) is a Japanese word describing a way of doing something or a pattern of behaviour that is practiced to various levels of mastery.

A Code Kata is an exercise in programming which helps hone your skills through practice and repetition.

Overlearning the basics allows our students to excel at the more difficult tasks.

Code Kata at RRC with Ruby

Ruby_makes_me_happy

Ruby is a dynamically-typed scripting language similar to Perl, PHP, or Python.

Try reading the following Ruby code out loud:

 4.times { puts "Chunky Bacon!" }

The Ruby challenges must enable students need to master:

  • Important Data Structures
  • Input / Output
  • Looping
  • Conditionals

Coding Challenges

Referral-guidelines

Coding Challenges are mini "assignments" that must be completed during class time.

Challenge Guidelines:

  • Clear goals and constraints.
  • Doable during lab time.
  • Just outside comfort zone.
  • Presents one or two new concepts.
  • Failure is okay, as long as you can explain what went wrong.
  • Challenges should be designed to enable "flow".
  • Collaboration is encouraged, but in-class competition is fun too!

Baby Steps

The first Ruby coding challenge might look something like this:

Create a program that prints your initials to the console using nine rows of asterisks.

**        ***           *             *******
**      ***            ***           *********
**    ***             ** **         **       **
**  ***              **   **       **      
*****               *********     **         ***
**  ***            ***********     **      *****
**    ***         **         **     **        **
**      ***      **           **     **********
**        ***    *             *       ********

Simple, but ensures that students can run code and print strings.

Arrays and Hashes

 carl  = {
           :start => 'We are a way',
           :middle => ' for the cosmos ',
           :punctuation => [ ',', '.', '?' ]
         }
 
 sagan = [
           "to ",
           "know ",
           "itself"
         ]

Use the provided Hash and Array to print out:

“We are a way for the cosmos to know itself.”

Solution:

 sentence  = carl[:start] + carl[:middle]
 sentence += sagan[0] + sagan[1] + sagan[2]
 sentence += carl[:punctuation][1]
 puts sentence

Looping

Generate an array that contains 20 random numbers.

Once the array is built, iterate through its elements printing out the value of twice each number.

Solution:

 random_numbers = Array.new
 20.times { random_numbers << rand(100) }
 
 random_numbers.each { |num| puts 2 * num }

Wowza Cowza

Start by coding a program that print every number from 1 to 100 to seperate lines in the console.

Next, modify your code so that:

See: Fizz Buzz Posters

Solution:

 1.upto(100) do |n|
   print "Wowza" if wowza = (n % 3).zero?
   print "Cowza" if cowza = (n % 5).zero?
   print n       unless (wowza || cowza)
   puts
 end

The Sequence

There is a famous mathematical sequence of numbers called the Fibonacci sequence.

The beginning of the sequence goes like this:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144…

Starting with the first two numbers (zero and one) the rest of the sequence can be calculated:

Each number (after the first two) is calculated by summing the previous two numbers.

Solution:

 # Recursive
 def fib(n)
   return n  if (0..1).include? n
   fib(n-1) + fib(n-2)
 end
 
 # Iterative
 def fib(n)
   curr, succ = 0, 1
   n.times do |i|
     curr, succ = succ, curr + succ
   end
   curr
 end

The Cipher

A Secret Code:

rwpeitg x. sdlc iwt gpqqxi-wdat

paxrt lph qtvxccxcv id vti ktgn ixgts du hxiixcv qn wtg hxhitg dc iwt
qpcz, pcs du wpkxcv cdiwxcv id sd: dcrt dg ilxrt hwt wps ettets xcid iwt
qddz wtg hxhitg lph gtpsxcv, qji xi wps cd exrijgth dg rdcktghpixdch xc
xi, 'pcs lwpi xh iwt jht du p qddz,' iwdjvwi paxrt 'lxiwdji exrijgth dg
rdcktghpixdc?'

hd hwt lph rdchxstgxcv xc wtg dlc bxcs (ph ltaa ph hwt rdjas, udg iwt
wdi spn bpst wtg utta ktgn hatten pcs hijexs), lwtiwtg iwt eatphjgt
du bpzxcv p spxhn-rwpxc ldjas qt ldgiw iwt igdjqat du vtiixcv je pcs
exrzxcv iwt spxhxth, lwtc hjsstcan p lwxit gpqqxi lxiw excz tnth gpc
radht qn wtg.

Write a program that can automatically decode "encrypted" files, where every character has been shifted by a set number of alphabet positions.

Terse Obfuscated Solution (Assumptions):

 s = IO.read('cipher.txt')
 shift = ('e'.ord - ('a'..'z').reduce(['',0]) do |m, l|
   s.count(l) > m[1] ? [l, s.count(l)] : m
 end[0].ord) % 26
 puts s.tr('a-z', (('a'..'z').to_a*2)[shift...shift+26].join)

Full cipher file here.

We Need Practice Too

24cc6384-07d2-4038-b782-d78ec74e0af1

As instructors we should be practicing what we teach on a regular basis.

An easy way to practice programming is to live code during lectures. Your students will want to code along which means super engagement.

YOU WILL MAKE MISTAKES.
THIS IS AWESOME FOR TEACHING DEBUGGING!

If you do any coding outside of work, share your successes with your students.

For example: You could maintain a Github Repo.

More Examples

Oodles of coding challenge examples: