Beginner Programming Challenge #9
Beginner Programming Challenge #9
Welcome to the 9th programming challenge for beginners, sponsored by The Ubuntu Beginners Team Development Focus Group. Let's dive right into things.
Task:
Your program should be able to open a text file and read its contents. The file will consist only of LOWERCASE letters and numbers 0-9. The file will be formatted such that there is only one alphanumeric character per line. Example file:
Code:
5
a
n
7
4
n
6
1
0
y
w
a
Your program must read each line and store the data in a data structure of your choice. Parse through your structure and print out the following information:
1. The sum of all the individual digits.
2. How many times each character appeared in the file.
Example output for the example above would be:
Code:
Sum = 23
a = 2
n = 2
w = 1
y = 1
The data file used for testing will be one of my own, so you don't know what's on it before I test. Conveniently, the way this program should be written makes that fact irrelevant. Your program should work with any size file. The one I use will be hundreds of lines long.
Bonus Points:
Code this in a programming language you've never used before!
Other Information:
Please read the bottom portion of Bodsda's post of BPC #8: http://ubuntuforums.org/showthread.php?t=1386478
All of those rules apply here as well. No obfuscated code. If you're a non-beginner, please wait until after the competition is judged to post your solution. We'd all love to learn something from the more advanced coders!
Go to this channel for help: irc.freenode.net #ubuntu-beginners-dev
EDIT: Please provide instructions for how to run your program along with your submission. ANY LANGUAGE IS ALLOWED!
Re: Beginner Programming Challenge #9
Just curious, what qualifies as a beginner? I have a minor in CS, learning basic concepts and I write games as a hobby. However, I only come to this part of the forum to ask questions, and this particular problem is not one that I could finish just like that (I consider myself a beginner at least).
Re: Beginner Programming Challenge #9
I'm learning C but am really at the beginner stage - I do know PHP well though.
Might have a go at this (if PHP is allowed).
Re: Beginner Programming Challenge #9
Was interested so I had a go anyway, I can't upload .php files so I'll paste the code here.
process.php
PHP Code:
<?php
// Read the file into a string then explode() this into an unformatted array
$data = file_get_contents('./data.txt');
$dataArray = explode(PHP_EOL, $data);
if (strlen($dataArray[(count($dataArray) - 1)]) == 0) {
unset($dataArray[(count($dataArray) - 1)]); // Remove the last value if empty
}
// Let's make sure our data is formatted nicely
foreach ($dataArray as $key => $value) {
$trimmedValue = strtolower(trim($value));
$dataArray[$key] = $trimmedValue[0]; // We want only a single letter char
}
// Now we have a nice array ready for manipulation so lets continue!
// Lets grab all the numbers and sum them
$x = 0;
foreach ($dataArray as $key => $value) {
if (is_numeric($value)) {
$x += $value;
}
}
echo 'Sum = ' . $x . "\n";
// Find out the count of unique values in our array
$uniqueChars = array_count_values($dataArray);
// Sort highest to lowest
arsort($uniqueChars);
foreach($uniqueChars as $key => $value ) {
echo $key . ' = ' . $value . "\n";
}
My data file, data.txt
Code:
5
a
n
7
4
N
6
1
0
ye
w
a
Run it as
Expected output:
Code:
richard@lara:~/Programming/PHP/UbuntuComp$ php process.php
Sum = 23
a = 2
n = 2
0 = 1
y = 1
w = 1
1 = 1
4 = 1
7 = 1
5 = 1
6 = 1
Could probably nest a few functions there and tidy the code up a bit but it does the job.
If anyone else enters using a "proper" language etc then just take my entry as a non-entry, was just interested to try.
EDIT: I might have a go at this in C if I get time over the next couple of days. I would learn a lot from it I think.
Re: Beginner Programming Challenge #9
After reading #6 post, I thought I wouldn't quailfy. Deleted my code.
Re: Beginner Programming Challenge #9
Quote:
Originally Posted by
JDShu
Just curious, what qualifies as a beginner? I have a minor in CS, learning basic concepts and I write games as a hobby. However, I only come to this part of the forum to ask questions, and this particular problem is not one that I could finish just like that (I consider myself a beginner at least).
If you can't immediately envision a solution to this problem, then I'd say you qualify as a beginner. Have at it! Since you're skeptical as to just how good you are, I encourage you to write this in a language you don't know to provide that extra challenge. :)
Re: Beginner Programming Challenge #9
Re: Beginner Programming Challenge #9
My attempt in PHP:
PHP Code:
<?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/>";
}
?>
-Silver Fox
Re: Beginner Programming Challenge #9
I'll give it a try... although I still have no idea of how to do it.
Re: Beginner Programming Challenge #9
Here it is in python:
Code:
#!/usr/bin/env python
def main():
theFile = open("input_9.txt", "r")
theSum = 0
theCharDict = {}
for theLine in theFile:
theLine = theLine.strip()
if theLine.isdigit():
theSum = theSum + int(theLine)
else:
theCharDict[theLine] = theCharDict.get(theLine, 0) + 1
print "Sum = " + str(theSum)
for theKey, theValue in sorted(theCharDict.items()):
print theKey + " = " + str(theValue)
if __name__ == "__main__":
main()