Page 11 of 15 FirstFirst ... 910111213 ... LastLast
Results 101 to 110 of 141

Thread: Beginner Programming Challenge #9

  1. #101
    Join Date
    Aug 2005
    Location
    The Local Group
    Beans
    631

    Re: Beginner Programming Challenge #9

    Quote Originally Posted by howlingmadhowie View Post
    nevertheless, for large datasets reading the whole thing into memory at once does cut time down to a quarter.

    however, one thing to try would be reading in a smaller amount at once, maybe 10MB or something. That should allow fast processing while also saving time allocating huge amounts of ram on a busy system.
    I wrote a C program to test this using the standard buffered IO capabilities of stdio.h. Actually, I found diminishing returns after about 1 kilobyte of buffer (the default being apparently 8192 bytes on my system). For smaller buffer sizes, however, the performance does suffer greatly.

    I'm not posting my test program here, following the guidelines in the OP. However, I can PM it to anyone who wants to experiment with it, or I can simply post it after the challenge is over.

  2. #102
    Join Date
    Dec 2008
    Beans
    45

    Re: Beginner Programming Challenge #9

    Im just trying to put the finishing touches to an entry written in C#. Finally cracked a few things that were getting in my way with the code. Hopefully I should have it finished and tested by this evening. I will post back here tonight!

  3. #103
    Join Date
    Apr 2008
    Location
    Buckinghamshire, UK
    Beans
    141
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: Beginner Programming Challenge #9

    Hi

    Instructions for the (only!) Gambas contribution:

    Gambas does a good job at compiling run-time versions of applications so I have put a deb installer package at deb Installer. I have tested this on Ubuntu 9.04 and 9.10. The program can then be run from the applications menu under education.

    For those with Gambas installed there is a source archive at progChallenge Source.

    Just load the program from Applications,Education. Choose the file from the filechooser and run.

    rgds

  4. #104
    Join Date
    Dec 2008
    Beans
    45

    Re: Beginner Programming Challenge #9

    A bit later than promised but here is my entry. It is written in C# and takes an input file called input.txt in the same working directory as the compile executable.

    Code:
    using System;
    using System.IO;
    using System.Collections;
    using System.Collections.Generic;
    
    namespace Challenge9v3
    {
    	class Challenge9v3
    	{
    		public static int sum;
    		public static Hashtable table = new Hashtable();
    		private const string FILE_NAME = "input.txt";
        	public static void Main(String[] args) {
            	if (!File.Exists(FILE_NAME)) {
                	Console.WriteLine("{0} does not exist. Press any button to exit.", FILE_NAME);
                	Console.ReadKey(true);
                	return;
            	}
    			using (StreamReader sr = File.OpenText(FILE_NAME)) {
    				string s = "";
    				while ((s = sr.ReadLine()) != null) {
    					if(char.IsDigit(Convert.ToChar(s))) {
    						sum = sum + Convert.ToInt32(s);
    					} else {
    						if(table.ContainsKey(s)) {
    							table[s] = (int) table[s] + 1;
    						   } else {
    						   	table.Add(s, 1);
    						   }
    					}
    				}
    				}
    				Console.WriteLine("Sum = " + sum);
    				foreach(DictionaryEntry de in table) {
    					Console.WriteLine("{0} = {1}", de.Key, de.Value);
    				}
    				Console.ReadKey(true);
    		}
    			}
    	}
    Definately not the most elegant solution but it works. Although I have one question, my alpha characters are printed out in the order they appear in the input file, is that OK for the purposes of the challenge? As although the example in the first post shows them sorted alphabetically the rules do not mention it.
    Last edited by kemra; February 24th, 2010 at 04:19 PM. Reason: Fixing punctuation

  5. #105
    Join Date
    Dec 2007
    Beans
    7

    Re: Beginner Programming Challenge #9

    Just stumbled across this, and I don't think I qualify, but wanted to give thumbs up to those giving this a go.

    To those doing it in python... somehow that just feels like cheating

    Though the perl submissions are pretty nice too, I just have trouble reading them.

  6. #106
    Join Date
    Jun 2006
    Beans
    2,930

    Re: Beginner Programming Challenge #9

    Quote Originally Posted by Cooner View Post
    To those doing it in python... somehow that just feels like cheating
    Isn't that the beauty of python though.
    Support 7z in default installs!!!: Click Here

    How to use code blocks to post command output: Click Here
    Official Ubuntu Documentation

  7. #107
    Join Date
    Jun 2006
    Beans
    2,930

    Re: Beginner Programming Challenge #9

    Simplified my python version so that large data files shouldn't be an issue:

    Change the DATAFILE to the appropriate file

    EDIT: replaced data.strip() with data[0] for a drastic improvement in speed
    PHP Code:
    #! /usr/bin/env python
    DATAFILE 'data.txt'


    def main():
        
    # initialize sum and dictionary
        
    sum 0
        charcount
    ={}
        
        
    # iterate through datafile
        
    for data in open(DATAFILE):
            
            
    #c = data.strip()    #remove nonprintable chars
            
    data[0]         # faster than stripping
            
            
    if c.isdigit():
                
    sum+=int(c)
            
            else:               
    # assuming only lowercase letters are the only other type
                
    if c in charcount.keys():
                    
    charcount[c] += 1
                
    else:
                    
    charcount[c] = 1
                    

        
    # Sort keys
        
    chars charcount.keys()
        
    chars.sort()

        
    # Output results
        
    print 'sum =  %s' sum

        
    for c in chars:
            print 
    '%s = %s'%(c,charcount[c])

    if 
    __name__=='__main__':main() 
    Also wrote a script to create a data file:
    PHP Code:
    #! /usr/bin/env python
    DATAFILE 'data.txt'

    #DATALEN = 100000000  #191Mb file
    DATALEN 10000000  #20Mb file

    import random

    def makecharlist
    ():
        
    charlist range(10)
        
    charlist += [ chr(c) for c in range(ord('a'),ord('z')+1) ]
        return 
    charlist

    def main
    ():
        
    charlist makecharlist()
        
    count 0
        file 
    open(DATAFILE,'w')
        
        while 
    count DATALEN:
            
    file.write('%s\n' % (random.choice(charlist)) )
            
    count +=1
        
    if __name__=='__main__':main() 
    I am curious if using list comprehensions can speed this up.
    Last edited by lavinog; February 24th, 2010 at 07:36 PM.
    Support 7z in default installs!!!: Click Here

    How to use code blocks to post command output: Click Here
    Official Ubuntu Documentation

  8. #108
    Join Date
    Jun 2006
    Beans
    2,930

    Re: Beginner Programming Challenge #9

    I was able to reduce the processing time to almost half by using python3.1 and list comprehensions and sets.
    One of the things that I am finding is that converting str to int is getting expensive.
    I have to take a break from this, but the next thing I might try is to put everything into a dictionary, then multiply the count of each digit by the value.

    Loading all of the data in memory does have a speed benefit too. This might be a good time for me to figure out how to use yield to get blocks of data.
    Support 7z in default installs!!!: Click Here

    How to use code blocks to post command output: Click Here
    Official Ubuntu Documentation

  9. #109
    Join Date
    Nov 2009
    Beans
    6
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Beginner Programming Challenge #9

    Here's my solution written in Vala. It will ask for the name of the file to process. I've never used Vala before so I couldn't figure out how to convert a string to an int. That's why I made the messy little toInt() function. Also, the letter counts are sorted so that it prints out the count for a first, then b, and so on. I tested it with the small sample provided and it worked fine.

    However, the first post says
    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
    but mine goes though the file and at each line figures out if it's a number or letter and processes it before going on to the next line. So I don't know if it's a valid entry or not. Anyway, I had fun making it and learned alot about Vala.


    PHP Code:
    using GLib;

    int sum 0;
    int[] lettercount;
    string[] letters;

    int main(string[] args) {
        
    lettercount = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        
    letters = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
        
        
    stdout.printf("Enter the name of the file to parse: ");
        
    string filename "";
        
    stdin.scanf("%s"filename);
        
        var 
    file File.new_for_path(filename);
        if (!
    file.query_exists(null)) {
            
    stderr.printf("Error: file %s does not exist\n"file.get_path());
            return 
    1;
        }
        
        
    processFile(file);
        
    printResults();
        
        return 
    0;
    }
        
    void processFile(File file) {
        try {
            var 
    in_stream = new DataInputStream (file.read(null));
            
    string line;
            while ((
    line in_stream.read_line(nullnull)) != null) {
                
    processLine(line);
            }
        } catch (
    Error e) {
            
    stderr.printf("Error: %s"e.message);
            return;
        }
        
        return;
    }    
    void processLine(string line) {
        for (
    int i=0i<25i++) {
            if (
    line == letters[i]) {
                
    lettercount[i] += 1;
                return;
            }
        }
        
    sum += (toInt(line));
        return;
    }

    void printResults() {
        
    stdout.printf("Sum = %i\n"sum);
        for (
    int i=0i<25i++) {
            if (!(
    lettercount[i] == 0)) {
                
    stdout.printf("%s = %i\n"letters[i], lettercount[i]);
            }
        }
        return;
    }

    int toInt(string str) {
        switch (
    str) {
        case 
    "0":
            return 
    0;
        case 
    "1":
            return 
    1;
        case 
    "2":
            return 
    2;
        case 
    "3":
            return 
    3;
        case 
    "4":
            return 
    4;
        case 
    "5":
            return 
    5;
        case 
    "6":
            return 
    6;
        case 
    "7":
            return 
    7;
        case 
    "8":
            return 
    8;
        case 
    "9":
            return 
    9;
        }
        return 
    0;

    To compile it: Install the vala compiler with "sudo apt-get install valac" and then compile with "valac --pkg gio-2.0 programname.vala"
    Last edited by davidboy; February 24th, 2010 at 08:50 PM. Reason: Added compiling instructions

  10. #110
    Join Date
    Jun 2006
    Beans
    2,930

    Re: Beginner Programming Challenge #9

    This reduces my earlier code from 80s to 10s by counting everything first, then getting the sum from the digit count.
    Also using the builtin count() made a big improvement.

    PHP Code:
    #! /usr/bin/env python
    DATAFILE 'data.txt'
    import math

    def loaddata
    (file):
        try:
                
    datafile open(file)
        
    except IOError:
            print(
    'Error opening file: %s' file)
            return []
        
    data = [ c[0] for c in datafile ]
        return 
    data
        

    def countchars
    (data):
        
    charlist = [ chr(c) for c in range(ord('a'),ord('z')+1) ]
        
    charlist += [ str(d) for d in range(10) ]
        
        
    counts = [ data.count(l) for l in charlist ]

        
    charcount dict(zip(charlist,counts))
        
        return 
    charcount

    def sumdigits
    (data):
        
    sum 0
        
    for c in data.keys():
            if 
    c.isdigit():
                
    sum += data[c] * int(c)
        return 
    sum
            

        
    def main
    ():
        
    data loaddata(DATAFILE)

        if 
    data:
            
    chardata countchars(data)
            
    charkeys = list(chardata.keys())
            
    charkeys.sort()
                
                
            print 
    'sum =  %s' sumdigits(chardata)
            for 
    c in charkeys:
                if 
    c.isdigit():continue
                print 
    '%s = %s'% (c,chardata[c]) 
        
    if 
    __name__=='__main__':main() 
    Support 7z in default installs!!!: Click Here

    How to use code blocks to post command output: Click Here
    Official Ubuntu Documentation

Page 11 of 15 FirstFirst ... 910111213 ... 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
  •