Programming Challenges - Page 8


Page 8 of 19 FirstFirst ... 45678910111218 ... LastLast
Results 106 to 120 of 279

Thread: Programming Challenges

  1. #106
    Join Date
    Mar 2003
    Location
    Tampa, FL USA
    Posts
    2,193
    Originally posted by tecknophreak
    Aren't there any C/C++/JAVA programers who want to represent?
    Calling me out?
    Heheh. I would, but I just accidentally deleted my whole hd. Partitioning error without backing up. (I know, I know... stupid idea)

    I'm gonna be spending the next few days setting up my new Knoppix/Debian system. If no one has come up with anything, then I'll give it a shot.

    While I'm here, I might as wel post another challenge. How about something simple :
    Create a program that inputs a file, replaces all tabs with 3 spaces, and outputs it to another file.

  2. #107
    Join Date
    May 2001
    Location
    Uh, I'm somewhere where I don't know where I am.
    Posts
    1,228
    yes i am!
    Attached Files Attached Files
    if (i_forgot && this_is_about_code)
    language = c++;

  3. #108
    Join Date
    Dec 2000
    Location
    Glasgow, Scotland
    Posts
    4,361
    Originally posted by Sepero
    While I'm here, I might as wel post another challenge. How about something simple :
    Create a program that inputs a file, replaces all tabs with 3 spaces, and outputs it to another file.
    Another moment to stretch my Python fingers again: (Amendments from Strike to follow )

    Code:
    #!/usr/bin/python
    
    import os, re
    
    inputfile="/home/mrben/input.txt"
    outputfile="/home/mrben/output.txt"
    
    data=(open(inputfile,'r').read())
    output=open(outputfile,'w')
    
    output.write(re.sub('\t','   ',data)
    
    output.close()
    (Strike - could you tell me whether a file gets automatically closed if you read it as part of the open statement, and if not, how to close it?)

    You could, of course, implement this in a single Python line from the Python prompt (having imported the relevant libraries)

    Code:
    open('/home/mrben/output.txt','w').write(re.sub('\t','   ',(open('/home/mrben/input.txt','r').read())))
    ....but you wouldn't want to do that
    mrBen "Carpe Aptenodytes"

    Linux User #216794

    My blog page

    3rd year running - get yourself to LugRadio Live 7th-8th July 2007, Wolverhampton, UK. The premier FLOSS community event.

  4. #109
    Join Date
    Jan 2000
    Location
    Houston, TX, USA
    Posts
    9,994
    You can do whole thing in one line if you want.

    Code:
    file('/home/mrben/output.txt','w').write(__import__('re').sub('\t','   ',(file('/home/mrben/input.txt','r').read())))
    Notes: use file, not open. Also, if you want to make it neater, you could have something like a NUM_SPACES constant variable and then just sub it with ' ' * NUM_SPACES
    We love Sensei!!
    A 1:1 ratio of Arby's sauce to Horsey sauce must be maintained.
    Like Linux? Want to like it more? Try Debian
    Best. (Python.) IRC bot. ever.
    Geekology

  5. #110
    Join Date
    Dec 2000
    Location
    Glasgow, Scotland
    Posts
    4,361
    Originally posted by Strike
    You can do whole thing in one line if you want.

    Code:
    file('/home/mrben/output.txt','w').write(__import__('re').sub('\t','   ',(file('/home/mrben/input.txt','r').read())))
    Notes: use file, not open. Also, if you want to make it neater, you could have something like a NUM_SPACES constant variable and then just sub it with ' ' * NUM_SPACES
    Cheers Strike; knew I could rely on you

    Python Rocks!
    mrBen "Carpe Aptenodytes"

    Linux User #216794

    My blog page

    3rd year running - get yourself to LugRadio Live 7th-8th July 2007, Wolverhampton, UK. The premier FLOSS community event.

  6. #111
    Join Date
    Jun 2003
    Posts
    173
    Here is the perl way to the replace tab with three spaces challenge.
    Other than opening the file, basically one line to replace and one to write to file.

    Code:
    #! /usr/sbin/perl
    
    open (INFILE, "$ARGV[0]") or die "Cannot open file: $ARGV[0] $!";
    open (OUTFILE, "> $ARGV[1]") or die "Cannot open file: $ARGV[1] $!";
    
    
    while(<INFILE>) {
      $_ =~ s/\t/   /g;
      print OUTFILE "$_";
    }

  7. #112
    Join Date
    Jun 2003
    Posts
    173
    Here is the perl code for the ip parser challenge:

    Code:
    #! /usr/sbin/perl
    
    $ip="123.234.21.123";
    my $iplong;
    my $ip2;
    my $delim;
    my @len;
    
    if($ip =~ /(\d{2,3})(\.|:)(\d{2,3})(\.|:)(\d{2,3})(\.|:)(\d{2,3})/) {
       $iplong = $1 . $3 . $5 . $7;
       $delim = $2;
       
       if($1/100 >= 1) {$len[0] = 3;} else {$len[0] = 2;}
       if($3/100 >= 1) {$len[1] = 3;} else {$len[1] = 2;}
       if($5/100 >= 1) {$len[2] = 3;} else {$len[2] = 2;}
       if($7/100 >= 1) {$len[3] = 3;} else {$len[3] = 2;}
    
       print "$iplong\n";
    }
    
    if($iplong =~ /\b(\d{$len[0]})(\d{$len[1]})(\d{$len[2]})(\d{$len[3]})\b/) {
      $ip2 = $1 . $delim . $2 . $delim . $3 . $delim . $4;
      print "$ip2\n";
    }
    Last edited by lagdawg; 10-02-2003 at 12:08 PM.

  8. #113
    Join Date
    Sep 2001
    Location
    Just for kicks
    Posts
    1,831
    Ok, I might make a fool of myself giving such a simple challenge, but I'm no guru.

    Write a program to compute 253 to the 9427th power (or other such ridiculously large numbers).

    I've attached the answer, no cheating now!
    Attached Files Attached Files
    "You cannot invade the mainland United States. There would be a rifle behind each blade of grass." --Admiral Yamamoto, 1941

  9. #114
    Join Date
    Oct 2003
    Location
    Chicago, Illinois, USA
    Posts
    15

    I have one...

    Okay... I've been working on this for a couple of days now, more off than on. It's a simple pattern matching sort of thing I call MatchScrabble. It works as follows:

    A user enters a String of scrambled characters such as "garmrop." The application loads a wordList (a flat file in this case) against which it must find a possible matching word. The basic idea is to unscramble the word and see if exists in the word list. To continue the example -- say we have a wordList constructed as follows:
    ---------
    program
    entry
    situation
    telling
    stop
    progrom
    tron
    more
    vortex
    teeth
    seriously
    poor
    people
    pudding
    stopping
    starting
    eating
    drinking
    --------

    The user enters the string "garmrop." The application should attempt to find a match for this in the word list. In this case the match would be on the first entry: "program" so your app, given that input against that wordList, should return a match on the word 'program.' I'd like to make it efficient enough so that it scales to very long wordLists, eventually using the entries in the Oxford English Dictionary as the wordList.

    Simple, right?
    insert witty signature here....

  10. #115
    Join Date
    Jan 2003
    Location
    Oregon
    Posts
    53
    Originally posted by Sepero
    Calling me out?
    Heheh. I would, but I just accidentally deleted my whole hd. Partitioning error without backing up. (I know, I know... stupid idea)

    I'm gonna be spending the next few days setting up my new Knoppix/Debian system. If no one has come up with anything, then I'll give it a shot.

    While I'm here, I might as wel post another challenge. How about something simple :
    Create a program that inputs a file, replaces all tabs with 3 spaces, and outputs it to another file.
    Though this doesn't really fit into the programming challenge I'd use sed to accomplish this:

    cat $file |sed 's/[TAB]/[3SPACES/' > $newfile

    Sometimes the best programming is using the tools already present

  11. #116
    Join Date
    Aug 2002
    Location
    Essex, UK
    Posts
    937
    #!/usr/bin/perl -w

    while(<>) {
    s/\t/ /;
    print;
    }
    Registered Linux User #325947

    Check out Feather Linux, my distro.
    (Yes, it's shameless self promotion, deal with it )

  12. #117
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,947
    Originally posted by hotleadpdx
    cat $file |sed 's/[TAB]/[3SPACES/' > $newfile
    You don't even need to use cat for that (this is one of the Useless Uses of Cat, congratulations! )

    You can do this instead:

    sed -e 's/[TAB]/[3 SPACES]/' <$file >$newfile

    Or, better yet, do it in-place (only works with recent versions of sed):

    sed -i -e 's/[TAB]/[3 SPACES]/' $file

  13. #118
    Join Date
    Jan 2003
    Location
    Oregon
    Posts
    53
    "Useless Uses of Cat" How amusing Where's the list kept? Using cat is a bad habit of mine from working with compressed files all day (zcat $file | blah blah...). But! If there's a sed command line option that allows it to read a compressed file I'll change my ways

  14. #119
    Join Date
    May 2001
    Location
    Uh, I'm somewhere where I don't know where I am.
    Posts
    1,228

    Re: I have one...

    Originally posted by Ratboy68
    Okay... I've been working on this for a couple of days now, more off than on. It's a simple pattern matching sort of thing I call MatchScrabble. It works as follows:
    I cheated and used /usr/shared/dict/words as the dictionary, it can be modified to look where ever it wants, just add a -f /location to the args and look for it.

    [edit]I don't want to hear anyone yelling at me about using "using namespace std;", cause it's just a little prog[/edit]
    Attached Files Attached Files
    if (i_forgot && this_is_about_code)
    language = c++;

  15. #120
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,947

    Re: Re: I have one...

    Originally posted by tecknophreak
    [edit]I don't want to hear anyone yelling at me about using "using namespace std;", cause it's just a little prog[/edit]
    Aww, come on! I want to yell at you about it! (... or, uh, something...)



    Originally posted by hotleadpdx
    "Useless Uses of Cat" How amusing Where's the list kept?
    http://rhols66.adsl.netsonic.fi/era/unix/award.html

Posting Permissions

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