Computer Science Teacher
Computer Science Teacher - Thoughts and Information from Alfred Thompson

September, 2010

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    Pex for Fun

    • 0 Comments

    PexWeb2

    Can you read code in C#, F# or Visual Basic? Do you like programming puzzles we (well the Pex Team, part of the Research in Software Engineering (RiSE) group at Microsoft Research) has a site for you. Pex  for Fun is  a web site with programming puzzles in those three languages. The idea is that you are presented with a programming stub and asked to fill it in with code to solve a problem. The catch is that you are not told up front what the program is supposed to do. Pex will run and compare the results of your code with the results of a perfect solution. By examining the differences you try to determine what is desired and write code that matches the test cases that Pex comes up with.

    Pex for fun on the web is a radically simplified version of the fully featured Pex Power Tool for Visual Studio. You do not need to install anything; all the work happens in the cloud. You can write code in C#, Visual Basic, or F#. Go to www.pexforfun.com, which comes with a set of code puzzles. Then click on the “Ask Pex!” button to get the answer yourself. Share your puzzles with your friends. Watch the tutorial, and read the documentation to learn more!

    Pex for fun

    I have to tell you that I visited the site yesterday and was occupied for hours solving puzzles. It was great fun. And you can creat your own puzzles as well. There is a section about teaching with Pex for Fun as well.



  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    Being Careful About Examples

    • 5 Comments

    Yesterday I posted some very simple sample code. I have to admit that I spent a lot of time on some of the details but may not have spent enough on some of the other details. This is always an issue when working on sample code for demonstrating a specific, often detailed concept. This is also an issue with code for tests and quizzes BTW. I remember one of my APCS students complaining that the APCS test was all about showing bad coding examples. A bit harsh perhaps but when writing code for a test one is very constrained by space. The good news is that these examples can lead to having some great conversations with students. For example, in the case of yesterday’s code samples I received email from a teacher friend of mine calling me out. Sort of.

    Should you teach students to break out of loops or to use a better loop to meet the need? In your programs today(?) you use a for loop to go through the elements until the end, but if you find a bad value break out. Shouldn’t you have used a do while loop? That way you place clear conditions for when to stop looping?

    One example of the code in question is this C# example.

    for (int i = 0; i < t.Length; i++)
    {
        if (!Char.IsLetter(t, i))
        {
            MessageBox.Show("Alphabetic characters required");
            break;
        }
    }

    In that code I use a break statement to exit the loop early. Standard structured programming, as I learned in college many years ago, says that is bad. The “rule” is that a loop should have one and only one exit point. When you add more exit points you add the potential for problems with people understanding the code, maintenance issues, and unexpected consequences when programs are extended or modified in the future.

    A more strictly by the rules structured programming code might me something like:

    int i = 0;
     
    while ( i < t.Length && Char.IsLetter(t, i))
    {
        i++;
    }
    if (!Char.IsLetter(t, i))
    {
        MessageBox.Show("Alphabetic characters required");
    }

    That’s probably still not the best way but it at least demonstrates the principle of one exit for the loop. This brings up the point I was making earlier about simple samples being more difficult than it may seem. The first example shows the comparison pretty clearly in a simple if statement. The second example shows the same thing but the Boolean expression in the while is more complicated then the one in if statement. Also in the second example the loop “feels” kind of forced to me. Of course often times in a real program there would be other things going on inside a loop that we are using. In a more complex “real” program the value of using the while  loop might actually be more obvious.

    We could write a very complicated example that would demonstrate several concepts or features of course. The problem there is both one of focus  and potentially too much complexity making it hard for beginners to understand the code. Often then we are faced with the real trade off between great code all up and simple easy to understand samples that may not be ideal code.

    When these examples sparks conversation in or outside of class be sure to take advantage of these as real learning opportunities. You can always pretend you did it on purpose. Smile



  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    How Many Letters Are There in The Alphabet?

    • 0 Comments

    You saw the title and a single number came to mind. If you are English speaking your probably thought “26.” If your native language is not English you may have thought of other numbers though. Even in European languages there are letters such as é è ö ᴂ å ø. And that is just the start. So what does this mean to a programmer or a web developer? Well it means you have to be pretty careful if you are writing code to validate that only letters have been entered into a field. I recently read a series of email messages by someone who could not enter their name in a form because it included the letter “ö .“ That is a frustrating thing to happen. And honestly asking them to change the “ö”  to an “o” is at best culturally insensitive and more likely outright insulting. So what is a programmer to do?

    A typical, old school answer to looking for a non-letter in a string might look something like this Visual Basic code.

    Dim t As String = TextBox1.Text.ToUpper()
     
    For i As Integer = 0 To t.Length - 1
        If t.Substring(i, 1) < "A" Or t.Substring(i, 1) > "Z" Then
            MessageBox.Show("Alphabetic characters required")
            Exit For
        End If
    Next

    Looks good but if you test it you will find that all of those non-English characters that are letters are reported as not alphabetic. Not good.

    Well often the best way is to rely on system/language functions to do it for you. One has to do their homework though to make sure those functions will do what you want though. I tried this C# code and it didn’t work. You'd probably not expect it to work if you are thinking about international letters because those other letters are outside the range of “A” to “Z” in most encodings. If you are not thinking about international letter this may feel right  though. A reminder that testing is important.

    {
        if (t.Substring(i, 1).CompareTo("A") == -1 | t.Substring(i, 1).CompareTo("Z") == 1)
        {
            MessageBox.Show("Alphabetic characters required");
            break;
        }
    }

    Then I tried the IsLetter method in the Char object as follows and it works.

    string t = textBox1.Text.ToUpper();
     
    for (int i = 0; i < t.Length; i++)
    {
        if (!Char.IsLetter(t,i))
        {
            MessageBox.Show("Alphabetic characters required");
            break;
        }
    }

    This works because the IsLetter method understands what “a letter” is. Reading the documentation was pretty helpful here. This reminds me of one of my favorite things to say to students – reading the documentation is the shortcut!

    There is not typically a lot of room in most high school CS curriculum of course but I think there is room to talk about it in context from time to time. It’s important that students start thinking about this early.

    Oh one last tidbit from the recent email conversation I read – some people don’t have first and last names. They just have one name. Forms that require entering both a first and last name are problematic for them as you can imagine. Solving that problem may be a little more complicated. It’s something to start a conversation about though.



Page 3 of 6 (18 items) 12345»