Ubuntu Beginners Programming Challenge 9

On February 16, 2010, in Thoughts, by s.fox

The new Ubuntu Beginners Programming Challenge has been set :)

Here is the announcement on Ubuntu Forums.  It has been wonderful to see all the submissions so far.  It has been less than 24 hours since the challenge was announced and already several complete solutions in various languages have been posted.   Superb :D   Well done to everyone.

Here is my entry in PHP:

<?php
//Set Sum to 0
$sum = 0;
//Specify data file
$myFile = "data.txt";
$file_array = file($myFile);
//Read in the text file line by line
foreach ($file_array as $line){
    //Tidy up $line a bit because txt file
    $line = str_replace("\n", '', "$line");
    $line = str_replace("\r", '', "$line");
    //Sort out numbers and text
    if (is_numeric($line)) {
        //keep running total of numbers
        $sum = $sum + $line;
    }else{
        //Create seperate var containing only letters
        $data.= $line;
    }
}
if ($sum != 0){
    //Display running total if numbers present
    echo 'Sum = '.$sum."<br/>";
}
foreach (count_chars($data, 1) as $i => $val) {
    //Display count of each letter
    echo chr($i). ' = '.$val."<br/>";
}
?>
 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>