Many Paths in Programming


flickr photo shared by San Diego Air & Space Museum Archives with no copyright restriction (Flickr Commons)

Minor Thoughts on Computational Thinking

Probably obvious stuff but I’m trying to jot things down for my own reference.
The first thing one ought to know about computational thinking/programming is that there are many correct paths (although some are better1 than others). This is true for just about anything but I think people think technology will be much more . . . binary. Searching for cleaner paths can be kind of fun.

Computational thinking is powered by vocabulary. Vocabulary, like in language, is closely tied to concepts (maybe analogies). Having never heard of the range function, it didn’t occur to me that it existed . . . let alone that I should use it. To make it work properly I need grammar but just knowing the word exists and means something starts to change things for me. It brings to mind setting up programming challenges much more like Dan Meyer’s 3 Act math lessons . . . with the scenario really begging for the addition of a particular concept but letting students struggle with it rather than providing it ahead of time.

A Path

This is a little bit of real-life progression which demonstrates how one thing can be done in a variety of ways and then how one word can really change it conceptually.

I got a request from a co-worker at VCU to simplify the following code. The goal is to take a certain user score on a scale from 1-5 (with .2 increments) and associate it with the appropriate column number.

To be clear, the code works. He just didn’t like it. Granted, stuff like this leaves lots of room for typos and repeated patterns like this tend to mean there’s another viable path.

$ucolumn = 0;
$user_score = round($user_score,1);
if ( $user_score == 1.2 ) { $ucolumn = 2;
}
if ( $user_score == 1.4 ) { $ucolumn = 3;
}
if ( $user_score == 1.6 ) { $ucolumn = 4;
}
if ( $user_score == 1.8 ) { $ucolumn = 5;
}
if ( $user_score == 2 ) { $ucolumn = 6;
}
if ( $user_score == 2.2 ) { $ucolumn = 7;
}
if ( $user_score == 2.4 ) { $ucolumn = 8;
}
if ( $user_score == 2.6 ) { $ucolumn = 9;
}
if ( $user_score == 2.8 ) { $ucolumn = 10;
}
if ( $user_score == 3 ) { $ucolumn = 11;
}
if ( $user_score == 3.2 ) { $ucolumn = 12;
}
if ( $user_score == 3.4 ) { $ucolumn = 13;
}
if ( $user_score == 3.6 ) { $ucolumn = 14;
}
if ( $user_score == 3.8 ) { $ucolumn = 15;
}
if ( $user_score == 4 ) { $ucolumn = 16;
}
if ( $user_score == 4.2 ) { $ucolumn = 17;
}
if ( $user_score == 4.4 ) { $ucolumn = 18;
}
if ( $user_score == 4.6 ) { $ucolumn = 19;
}
if ( $user_score == 4.8 ) { $ucolumn = 20;
}
if ( $user_score == 5 ) { $ucolumn = 21;
}

My first response was to just load the values as an array and then ask the array for a match using array_search. It’s shorter and it felt cleaner to me . . . and it worked. Negatives are that the array is still hand-loaded and it just looks messy hanging out there taking up room.

<?php
$user_score = 1.8;
$ucolumn = 0;
$array = array(
'1'=>'1',
'2'=>'1.2',
'3'=>'1.4',
'4'=>'1.6',
'5'=>'1.8',
'6'=>'2',
'7'=>'2.2',
'8'=>'2.4',
'9'=>'2.6',
'10'=>'2.8',
'11'=>'3',
'12'=>'3.2',
'13'=>'3.4',
'14'=>'3.6',
'15'=>'3.8',
'16'=>'4',
'17'=>'4.2',
'18'=>'4.4',
'19'=>'4.6',
'20'=>'4.8',
'21'=>'5',
);
$key = array_search($user_score, $array); //find the match
echo $key;

?>

He responded with a loop structure that he had in mind which was much tidier and moved us away from hand-loading things . . . but it didn’t quite work.

for ($i = 1; $i < 5; $i = $i + 0.2) {
echo $i;
    for ($m = 1; $m < 21; $m++) {
        if ( $user_score == $i ) { $ucolumn = $m }
    };
};

With that as inspiration, I still stuck with my array but loaded it using the for loop. It seemed slick.2


<?php
$user_score = 3.2;
$score = .8;
$array = [];
for ($ucolumn = 1; $ucolumn <= 20; $ucolumn++) {
		$score = $score+0.2;
		$array[$ucolumn] = round($score,1);//creates the paired key value - round is needed because of floats in php . . . still seem a bit odd to me
    }
$key = array_search($user_score, $array);
echo 'key ' . $key;
?>

I think that was Friday. Then, this morning, I was reading Eloquent Javascript and saw the range function mentioned. Turns out PHP has one as well. That led to this code which feels pretty tight. Kind of crazy to take a set of repeated if statements 20-some-odd-lines long and reduce it to two functional lines. You also get the benefit of preventing typos.

$user_score = 1.4;
$array = array_combine(range(1,21),range(1,5,.2));
$key = array_search($user_score, $array);
echo $key;

There are probably even tighter/smarter ways to do this but this progression demonstrates improvement. It’s interesting stuff and helping people also builds out my conceptual vocabulary in ways that will help me later.


1 Better can be very relative . . .

2 Slick is relative. Functional is usually my bar so this is getting fancy.

Comments on this post

  1. CogDog said on May 9, 2016 at 12:08 pm

    One line, dude.

    function whats_my_score_dude ( $user_score ) {
    return array_search( $user_score, array_combine(range(1,21),range(1,5,.2)) );
    }

    echo whats_my_score_dude ( 1.4 );

    for extra credit, make the range values function params

    • Tom Woodward said on May 9, 2016 at 12:19 pm

      Show off.

    • Tom Woodward said on May 9, 2016 at 12:33 pm

      Is there a cleaner way to do it than?
      function whats_my_score_dudette ( $user_score, $r1a, $r1b, $r1step, $r2a, $r2b, $r2step ) {
      return array_search( $user_score, array_combine(range($r1a,$r1b, $r1step),range($r2a,$r2b,$r2step)) );
      }
      echo whats_my_score_dudette ( 1.4, 1, 21,1,1,5,.2 );

  2. CogDog said on May 9, 2016 at 2:27 pm

    To make the function call easier, I might follow the WordPress approach to define an array of parameters, and then pass that to the function…


    function whats_my_score_dudette ( $user_score, $r_array ) {

    return array_search( $user_score, array_combine(range( $r_array->min , $r_array->max, $r_array->step ),range( $r_array->decmin , $r_array->decmax, $r_array->decstep)) );
    }

    $range_settings = array(

    'min' => 1, // a comment to explain this setting
    'max' => 21, // ditto
    'step' => 1,
    'decmin' => 1,
    'decmax' => 5,
    'decstep' => 0.2
    );

    echo whats_my_score_dudette ( 1.4, $range_settings );

    By the way, that might be the first time I have seen the CC-Attribution helper work for something from the LOC collection.

Leave a Reply

Trackbacks and Pingbacks on this post

No trackbacks.

TrackBack URL