Posts tagged ‘programming’

K&R – Solution to Exercise 1.23

I’m working my way through The C Programming Language at the moment, and so far I’m loving C’s purity and leanness.

One of my few disappointments with the book is the lack of solutions to the exercises. The exercises are refreshingly challenging because the book isn’t aimed at beginning programmers. Unfortunately this means that the desire to validate one’s solutions is quite strong. Fortunately, Google had a solution to this problem: Richard Heathfield’s solutions site.

I just finished my solution to exercise 1.23 (“Remove all comments from a C program”) and I’m quite chuffed with it so thought I would share it here. Out of the multiple solutions provided on Richard’s site, mine was about the closest to following the guidelines as far as what knowledge one is supposed to have of C by that (early) point in the book (the only thing I cheated with is the use of break and continue – it would have just been quite ugly without those two small additions). Basically the solution is a state machine using if / else instead of more traditional switch methods as switch had not yet been introduced.

My solution deals with all the special cases I could think of, and even deals with the tricky sample input on Richard’s site, provided at the bottom of the solutions page for this exercise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * K&R Exercise 1-23
 *
 * "Write a program to remove all comments from a C program. Don't
 *  forget to handle quoted strings and character constants properly.
 *  C comments don't nest."
 *
 */
 
#include <stdio.h>
 
int main()
{
    int c;
    int c2;
    int in_quotes = 0;
    int in_comment = 0;
    int current_quote;
 
    c = getchar();
    while (c != EOF)
    {
        if (!in_comment && !in_quotes)
        {
            if (c == '\'' || c == '"')
            {
                in_quotes = 1;
                current_quote = c;
                putchar(c);
            }
            else if (c == '/')
            {
                c2 = getchar();
                if (c2 != EOF && c2 == '*')
                {
                    in_comment = 1;
                }
                else
                {
                    putchar(c); /* Just a regular '/', so output it. */
                    c = c2;
                    continue;
                }
            }
            else
            {
                putchar(c);
            }
        }
        else if (in_comment)
        {
            /* Check for a closing comment. */
            if (c == '*')
            {
                c2 = getchar();
                if (c2 != EOF && c2 == '/')
                {
                    in_comment = 0;
                }
                else
                {
                    /* Don't advance to next character in stream. */
                    c = c2;
                    continue;
                }
            }
        }
        else if (in_quotes)
        {
            /* Skip over escaped chars. */
            if (c == '\\')
            {
                putchar(c);
                c = getchar();
            }
            else
            {
                /* Check for closing quote. */
                if (c == current_quote)
                    in_quotes = 0;
            }
            putchar(c);
        }
 
        c = getchar();
    }
 
    return 0;
}

Server Fault Private Beta

Following on from the wildly successful Stack Overflow programming questions and answers website, Jeff Atwood and Joel Spolsky have launched Server Fault, a questions and answers site following the same format but now for “system administrators and IT professionals” (currently in Private Beta, click here for sign-up instructions).

stackoverflow_logo

serverfault_logo

I’ve been using Stack Overflow for about 5 months and I absolutely love it. It has become the definitive, and I dare say de facto standard, questions and answers one-stop-shop for programmers. You can post a question for just about any programming topic and receive an answer within minutes, sometimes even seconds. This removes an old hassle with posting questions on Web 1.0-style forums where you had to wait impractically long lengths of time to receive answers.

These sites don’t just follow any standard Q&A format either.  Just take a look at the diagram on the about page of either site and you’ll see that they’re a deliciously freakish Wiki/Blog/Forum blend.

One of the things I love about boths sites is their innovative karma and badge system, where users are rewarded by the community for making valuable contributions.  This gives users a reputation score which appears next to their name wherever it appears on the site.  This can be used as an assessment of someone’s overall standing in the community.  Badges are awarded for special achievements like providing an especially highly-rated answer or for being recognised as an expert in a particular technology.

Of course possibly the most important feature of boths sites is that you don’t need to be logged in to ask or answer questions.  This removes yet another annoying hurdle for the casual help-seeker.

I encourage everyone in IT, be it programming or otherwise, to sign up to at least one of these – you won’t look back.

-Wayne

Artificial Intelligence, Project Concept: PIE

I’ve been doing some reading on artificial intelligence (AI) today as it’s the time of the year again when the Turing Test is performed on a handful of selected artificial intelligence systems.  I started putting some serious thought into artificial intelligence design and I’ve decided that it would be an immensely fun project to try and create such a system myself.

I will call it PIE – Perceptively Intelligent Enough.  The goal of PIE will be to have the ability to hold a casual Turing-Test-like conversation with an average person via a text interface.  Eventually I would hope to add human sensory abilities to this system also such as vision and hearing.

I think many of the problems posed for AI stem from the fact that we are trying to create a perfect human clone.  The goal with PIE will be to create a system that is able to convince the average passer-by that they are talking to a human.

I’m going to create a system that will emulate me as far as possible because I think that some properties and traits of such a system need to be reasonably rigidly defined.  The system will thus have access to an existing knowledge base upon which it can draw for every question or statement presented to it.  One of the biggest challenges in creating such a machine will be populating the knowledge base to the point of being basically accessible for everyday conversations, with the ability to delve deeper into its understanding of its own knowledge and perceptions and even alter this knowledge base as it goes.

PIE’s design will be largely based on the Dartmouth Proposal, which states:

“Every aspect of learning or any other feature of intelligence can be so precisely described that a machine can be made to simulate it.”

We use the term “artificial intelligence” quite loosely really.  We can’t actually agree on a definition of intelligence.  Some use IQ as a measure, while others might consider masters of some artistic fields such as music to be of “genius intelligence”.  How, then, do we create a machine to emulate a concept we can’t even agree on a definition for?  I like the definition which basically says that an intelligent being is one which is self-aware.  If that is the case then the measure of intelligence must be nothing more than a binary state: Something is either aware of its own existence or it isn’t.

You would be hard pressed to find any honest psychologist who thinks the current working knowledge of the field of psychology, with medical and physiological influences included, fully understands the human psyche.  I am going to base PIE purely on common perception, which I will test as the system develops using as wide a cross-section of people as possible.  I want to create a perception of a self aware being.

With PIE I hope to create a system that will conform to the following high level design specifications:

  • The psychology and universe perspective of the system will be based on Darwinian evolutionary theory and the system will believe that it is here for no particular purpose.  I will create for the system a humble world view based on as much truth as it is able to obtain.  PIE won’t have any personal goals or aspirations.  Its existence will be loosely based on serving those who take the time to give it some attention.  (One could say then that it does have a personal goal: that of being noticed or feeling important, but this isn’t entirely true)
  • Understand English, both correctly used as per UK English standards and in a more lenient casual sense.  This means it must have linguistic processing abilities capable of confidently (i.e. the validity of the majority of linguistic analyses should not need a follow-up question of some sort) deconstructing a given sentence or group of sentences to a point of summary where it can then quite precisely draw information from its knowledge base.
  • At a high level the system will be comprised of three psychological layers which together will form the basis of a reaction to every input given:
  • Instinct.  Instinct is the core of our being and purpose as defined naturally, i.e. by “nature”.  Instinct is the genetic and evolutionary programming we have evolved upon which we make decisions subconsciously with the main purposes of survival and reproduction.  This layer is the most rigid of the three and aspects of this layer won’t change within the context of this project as changes here are not likely enough to change noticeably within any human’s lifetime, nor in any of our “immediate” (not even the next few thousand or hundred thousand years) perceptual future generations.  The next layer will decide how much it will allow this layer to influence any decision.
  • Learned fact, logic, reasoning, rationality.  PIE’s initial database will be populated more and more deeply as necessary.  The system should have the ability to find out additional information from external sources, including interactions with the humans with which it makes contact.  This layer is the most important of the three as this will be the layer that provides objective thinking capacity, the ability to make fair decisions, and to become more knowledgeable and more capable of such decisions as it goes (general learning ability).
  • Emotion.  PIE will have predefined parameters in this layer which determine its emotional aspects of any particular response.  PIE will try its very best to be a purely objective intellectual force but sadly it will fail, as all humans do, to some extent, with most decisions.  Ideally there would also be some kind of emotional development, based on entities the system “cares” about.
  • PIE’s personality, which I will define as the emotional perception it projects, will be based on my own as I objectively see it.  For those who know me please feel free to correct this as I go ;-)
  • PIE will have a prioritised list of interests, containing as broad a range of subjects as possible, which will influence conversation stimuli injected by PIE.  This means that PIE can start and maintain a conversation with someone based on what they know or learn about the person it is interacting with at any given time.
  • PIE’s typical conversation will start by determining the purpose of the discussion at hand.  It is open minded and will discuss anything with anyone, showing more interest in topics falling under the categories higher in its priority list.  PIE will be capable of having a bad day which can be caused by certain events such as arguing with someone who it “cares” about or being spoken to while it was trying to sleep.  PIE should be sensitive to the emotions of others as expressed textually.
  • PIE will also have a sense of humour which I will model on my own.  I believe a sense of humour can be very important in adding to the perception of humanity and will play a big part in its personality.

While this is mostly a fun project for myself I think that if I am able to contruct a convincing machine such as I have outlined above it could have many useful applications in everyday life.

I will develop the system in Python and post my progress and learning on here on a regular basis.  I am classing this as a very long-term project as I have many other little projects that need to take higher priority.  I want to release a working version of PIE from a very early point in its development, even from the point of very primitive natural language recognition and analysis, which will be the very first step.

The project will be open source and most likely released under a BSD-style license.

Watch this space!

-Wayne

New utility: tabs2spaces.py

I’ve just created a Python script that will take a filename on the command line and convert all of its tabs into the given number of spaces (2nd argument). Click here to download.

It doesn’t overwrite the original file but instead creates a new file with a “notabs_” prefix.  Script is covered by the GNU GPL.

-Wayne

Tubecaster Reviewed!

I was pleasantly surprised this morning to find an e-mail in my inbox from a website called “Uptodown.com” informing me that they had tested Tubecaster and thought that their visitors would find it useful and so had written a mini review for it and provided a download link on their site. Apparently they get over three hundred thousand hits on their site every day, and Tubecaster is linked on the front page at the moment. I’m really flattered and hugely pleased that even in this reasonably early stage of development (although it already “Does what it says(TM)”) people are finding it useful. Thanks guys!

The review (in Spanish only) and link can be found here.

-Wayne

P.S. I’m proud to report that Tubecaster is the only Free & Open Source YouTube video downloader on the Uptodown website :-)