Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: Beginner's Programming Challenge 17

  1. #11
    Join Date
    Dec 2009
    Beans
    40

    Re: Beginner's Programming Challenge 17

    Don't know how all your programs are so tiny. Looks like I'm gonna hit 600 lines. (C++) Though that does include my own custom Date and Task classes.

    Question though: Should we include input validation

  2. #12
    Join Date
    Apr 2008
    Beans
    286

    Re: Beginner's Programming Challenge 17

    Here's mine:

    PHP Code:
    <?php
        error_reporting
    (0);
        function 
    error($text) {
            echo 
    "ERROR: ".$text."<p><a href='".$_SERVER['PHP_SELF']."'>Click here to go back.</a>";
            die();
        }
    ?>
    <!DOCTYPE html>
    <head><title>Tasks</title></head>
    <body>
    <div style="width: 500px; padding: 20px; border: 1px solid black;">
    <?php
        
    echo "Welcome. Today is " date('l, j F Y') . ".<p>";
        if ( 
    $_GET['action'] == "add" ) {    // If the user has requested to add a new task
            // Parse the parameter and , if possible, save the task
            
    $string trim($_GET['task']);
            if ( empty( 
    $string ) ) {
                
    error("No valid input was found.");
            }
            if ( 
    strpos$string" due " ) === false ){
                
    error("Unable to parse the input.");
            }
            
            
    $date substr$stringstrrpos($string" due ") + strlen(" due ") );         
            
    $timestamp strtotime$date );
            if ( 
    $timestamp == -|| $timestamp === false ) { // PHP's internal could not parse the date, maybe it is the "in N timeunits" format.
                
    if ( strpos$date"in " ) !== false ) {
                    if ( 
    strpos$date" day" ) !== false ) {
                        
    $word " day";
                        
    $multiplier 24 3600;
                    } elseif ( 
    strpos$date" week" ) !== false ) {
                        
    $word " week";
                        
    $multiplier 24 3600;
                    } else {
                        
    error("Unable to parse the date.");
                    }
                    
    $start strpos$date"in " ) + strlen"in " );
                    
    $length strpos$date$word ) - $start;
                    
    $date trim(substr$date$start$length ));
                    if ( 
    is_numeric$date ) && (int)$date == $date && (int)$date ) {
                        
    $date *= $multiplier;
                        
    $timestamp time() + $date;
                    } else {
                           
    error("Unable to parse the date.");
                    }
                } else {
                    
    error("Unable to parse the date.");
                }
            }
            
            
    $new_task substr$string0strrpos($string" due ") );
            
    $new_task =  $new_task " due on " date('l, j F Y'$timestamp) . "\n";
            echo 
    "New task: " $new_task;
            
            @ 
    $fp fopen"taskfile""a+" );
            if ( !
    $fp ) {
                echo 
    "<p>";
                
    error("Unable to open the taskfile for writing.");
            } else {
                
    fputs($fp$new_task);
                echo 
    " has been successfully saved.";
                echo 
    "<p><a href='".$_SERVER['PHP_SELF']."'>Click here to go back.</a>";
            }

        } else { 
    // This is the default action
             // Read the file and display saved entries
            
    $tasks = array();
            @ 
    $fp fopen"taskfile""r" );
            if ( 
    $fp ) {
                while ( !
    feof$fp ) ) {
                    
    $temp fgets$fp999 );
                    if ( 
    $temp ) {
                        
    $tasks[] = $temp;
                    }
                }
            }
            echo 
    "You currently have " count$tasks ) . " tasks.<p>";
            if ( 
    count$tasks ) ) {
                foreach ( 
    $tasks as $key => $value ) {    
                    echo 
    "Task " $key ": $value<br>";
                }
            }
            
    ?>
            <p>
            <div style="padding: 15px; border: 1px dotted gray;">
            Please input a new task in the following format:<br><b>[Task Title] due [Date]</b> and click "Submit"<p>
            <form method="GET">
                    <input type="hidden" name="action" value="add">
                    <input type="text" name="task" style="width: 300px;">
                    <input type="submit" value="Submit" style="width: 100px;">
            </form>
            </div>
            <?php
        
    }
    ?>
    </div>
    </body></html>
    Features:
    * Saves entries to a file and displays them
    * Correctly handles "due" in the title

    Sample input:
    Code:
    foo due now
    foo due today
    bar due wednesday
    bar due tomorrow
    foobar due next week
    foo bar due next friday
    bar foo due 5 dec 2010
    bar goo due 5-12-2010
    foo due in 2 days
    foo due in 2 weeks
    Corresponding output:
    Code:
    foo due on Friday, 3 December 2010
    foo due on Friday, 3 December 2010
    bar due on Wednesday, 8 December 2010
    bar due on Saturday, 4 December 2010
    foobar due on Monday, 6 December 2010
    foo bar due on Friday, 10 December 2010
    bar foo due on Sunday, 5 December 2010
    bar goo due on Sunday, 5 December 2010
    foo due on Sunday, 5 December 2010
    foo due on Friday, 17 December 2010
    Peace
    Last edited by roccivic; December 4th, 2010 at 12:44 AM.

  3. #13

    Re: Beginner's Programming Challenge 17

    put up the revised edition

    Code:
    >>> main()
    input task title and date:foo due tomorrow
    foo due 2010-12-08
    >>> main()
    input task title and date:fue due day after tomorrow
    fue due 2010-12-09
    >>> main()
    input task title and date:fue due 12 nov 2010
    fue due 2010-11-12 00:00:00
    >>> main()
    input task title and date:foo due in 10 days
    foo due 2010-12-17
    >>> main()
    input task title and date:foo due friday
    foo due 2010-12-10
    >>> main()
    input task title and date:foo due in 365 days
    foo due 2011-12-07

  4. #14
    cprofitt's Avatar
    cprofitt is offline νόησις νοήσεως - nóesis noéseos
    Join Date
    Oct 2006
    Location
    平静
    Beans
    1,451
    Distro
    Ubuntu Development Release

    Re: Beginner's Programming Challenge 17

    bump: will this one be concluding and a new one be put up soon?

  5. #15
    Join Date
    Jun 2008
    Beans
    199
    Distro
    Ubuntu Development Release

    Re: Beginner's Programming Challenge 17

    Yes, the contest has closed, and I'm reviewing the entries. A winner will be announced shortly.

  6. #16
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Beginner's Programming Challenge 17

    As an aside - on a project I once worked on we developed a date algebra for our reporting system - so we could set up a repeating schedule of reports with variable dates, and run the same reports over weeks, months etc. The algebra worked like this

    Start -7D
    End -1D

    When this report runs the Start paremeter would be calcualted at 7 days prior to the run date and the End parameter would be calulated as 1 day prior to the run date.

    We also could do Weeks - using the W Identifier, and Months - using the M Identifier - these had wrinkles - W went to the previous Monday (i.e. the start of the business week), unless the current date was a monday), and M went to the start of the Month (unless the current date was the 1st).

    Algebra was calculated left to right so :

    Start : -2W
    End : -1W - 1D

    this would run a report over the last full week - Monday to Sunday

    Start : -2M
    End : -1M -1D

    Would run the report but over the last full month.

    Operators worked intuitively "-" (minus) moved the "clock" backwards, "+" moved it forward.

    so :
    Start : -1W
    End : +1W -1D

    Would run the report for this current weeks data.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  7. #17
    Join Date
    Jun 2008
    Beans
    199
    Distro
    Ubuntu Development Release

    Re: Beginner's Programming Challenge 17

    And the Winner is...
    roccivic, for the nice display, saving of the tasks, and good error handling.

    I would like to give a special shout out to krazyd for the (extremely) concise code. Also, ziekfiguur had a very unique, expandable solution.

    All the code I tried worked (I only had to make some small changes), so great job to all participants!

    As per contest tradition, roccivic is now in charge of submitting the next challenge.

    Good luck to all participants in future challenges!

  8. #18
    Join Date
    Apr 2008
    Beans
    286

    Re: Beginner's Programming Challenge 17

    That's nice, thanks.

    I'll post a new challenge some time soon.

    Rouslan

  9. #19
    Join Date
    Jul 2007
    Location
    Austin, TX
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginner's Programming Challenge 17

    You can play with roccivic's solution live here:

    http://bozosort.com/dloads/roccivic.php

    No hacking my server please

  10. #20
    Join Date
    Apr 2008
    Beans
    286

    Re: Beginner's Programming Challenge 17

    Quote Originally Posted by Queue29 View Post
    You can play with roccivic's solution live here:

    http://bozosort.com/dloads/roccivic.php

    No hacking my server please
    I just exploited an XSS vulnerability on it, lol.
    But that doesn't count as hacking your server, right?

    You might want to change line 16, from:
    PHP Code:
            $string trim($_GET['task']); 
    to:
    PHP Code:
            $string trim(strip_tags($_GET['task'])); 

Page 2 of 2 FirstFirst 12

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
  •