Page 1 of 15 12311 ... LastLast
Results 1 to 10 of 141

Thread: Beginner Programming Challenge #9

  1. #1
    Join Date
    Nov 2007
    Beans
    706
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    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!
    Last edited by Sinkingships7; February 16th, 2010 at 08:55 AM.
    Programming is an art. Learn it. Live it. Love it.

  2. #2
    Join Date
    Jun 2009
    Beans
    355

    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).

  3. #3
    Join Date
    Nov 2009
    Beans
    125
    Distro
    Ubuntu 10.04 Lucid Lynx

    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).
    http://www.digit4l.net/ - news, tech, gaming and commentary

  4. #4
    Join Date
    Nov 2009
    Beans
    125
    Distro
    Ubuntu 10.04 Lucid Lynx

    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
    Code:
    php process.php
    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.
    Last edited by Richard1979; February 16th, 2010 at 05:59 AM.
    http://www.digit4l.net/ - news, tech, gaming and commentary

  5. #5
    Join Date
    Nov 2009
    Location
    /dev/null
    Beans
    74

    Re: Beginner Programming Challenge #9

    After reading #6 post, I thought I wouldn't quailfy. Deleted my code.
    Last edited by lostinxlation; February 16th, 2010 at 09:14 AM.

  6. #6
    Join Date
    Nov 2007
    Beans
    706
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Beginner Programming Challenge #9

    Quote Originally Posted by JDShu View Post
    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.
    Programming is an art. Learn it. Live it. Love it.

  7. #7
    Join Date
    May 2009
    Beans
    16
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Beginner Programming Challenge #9

    what's the deadline?

  8. #8

    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($data1) as $i => $val) {
        
    //Display count of each letter
        
    echo chr($i). ' = '.$val."<br/>";

    ?>
    -Silver Fox
    Last edited by s.fox; February 17th, 2010 at 12:24 AM.

  9. #9
    Join Date
    Mar 2009
    Location
    Brazil
    Beans
    475
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Beginner Programming Challenge #9

    I'll give it a try... although I still have no idea of how to do it.
    Ubuntu User #27453 | Linux User #490358
    "Don't preach Linux, mention it"
    "Linux is not Windows"
    73% of statistics in forums are made up on the spot

  10. #10
    Join Date
    Oct 2007
    Beans
    74

    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()

Page 1 of 15 12311 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •