ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Make it Small Challenge 1 (Time Output)

August 12, 2008 3:05am

Subscribe [7]
  • #1 / Aug 12, 2008 3:05am

    Lone

    350 posts

    After seeing the reponse to the recent programming challenge put up by Michael I thought I might put up another challenge of perfection - Make it Small. I hope to make this a regular challenge and feel free to PM me any suggestions.

    The goal is to create a php code solution for a given problem and see who can make the smallest snippet of code (by lines and/or characters) to suit. No prizes or giveaways - just a good fun challenge and a way to show off your skill(z). Anyone can create a solution for a problem but at the end of the day its how you achieve it that makes the difference.

    Challenge 1 - Outputing times for a select box

    The Issue
    You need to create a single function that returns an array of all of the possible times from 12:00AM to 11:45PM in 15 minute increments with a prepended zero for hours/minutes less then 10.

    Summary
    * 12:00AM - 11:45PM
    * 15 minute increments
    * Prepended zero for hours/minutes (eg. 01:00AM NOT 1:00AM)

    Sample array output

    $time[0] = '12:00AM';
    $time[1] = '12:15AM';
    $time[2] = '12:30AM';
    $time[3] = '12:45AM';
    $time[4] = '01:00AM';
    $time[5] = '01:00AM';
    ........
    $time[] = '11:30PM';
    $time[] = '11:45PM';

     

    Results so far
    (All submissions close one week from original post time)
    Characters counted excluding spaces and variable names - lines count as one character. Execution time based on being run 200 times on same machine.

    James Gifford
    68 characters
    0.65 seconds

    m4rw3r
    75 characters
    0.63 seconds

    wiredesignz
    113 characters
    1.29 seconds

    xwero
    276 characters
    0.65 seconds

  • #2 / Aug 12, 2008 3:54am

    xwero

    4145 posts

    My take a crack at it in the morning solution

    function times_array($start,$stop,$interval)
    {
        $unix_start =  (substr($start,-2) == 'PM')?strtotime('+12 hours',strtotime(substr($start,0,-2))):strtotime(substr($start,0,-2));
    
         $unix_stop =  (substr($stop,-2) == 'PM')?strtotime('+12 hours',strtotime(substr($stop,0,-2))):strtotime(substr($stop,0,-2));
    
         $interval_sec = $interval*60;
    
         $times = array($start);
    
         $time = $unix_start+$interval_sec;
    
         while($time <= $unix_stop)
         {
             $times[] = date('h:iA',$time);
    
             $time += $interval_sec;
         }
    
         return $times;
    }

    Edit : first i didn’t notice it because i’m not used to the AM/PM format but adding 12:00AM got me : 12:00AM, 12:15PM, 12:30PM, ... So you have to add 00:00AM to get the correct output. I guess this happens because the strtotime function internally translates the time to a 24 hour format?

  • #3 / Aug 12, 2008 4:26am

    m4rw3r

    647 posts

    This one just solves the problem, nothing fancy like xwero’s.

    $r = array();
    $m = range(0, 15, 30, 45);
    
    function c($h, $m, $s)
    {
        return ($h < 10 ? 0 : '').$h.':'.($m < 10 ? 0 : '').$m.' '.$s;
    }
    
    for($i = 0; $i < 24; $i++)
    {
        $j = ($j = $i % 12) == 0 ? 12 : $j;
        $r = array_merge($r, array_map('c', array_fill(0, 4, $j), $m, array_fill(0, 4, $i < 12 ? 'AM' : 'PM')));
    }
    
    print_r($r);
  • #4 / Aug 12, 2008 4:53am

    wiredesignz

    2882 posts

    public function time_arr()
    {
        $time_segs = array(0,15,30,45);
            
        for ($i = 0; $i < 24; $i++):
            
            foreach ($time_segs as $seg):
                
                $time[] = date('h:iA', mktime($i,$seg));
                
            endforeach;
            
        endfor;
            
        return $time;
    }

    :lol: m4w3r

    @xwero, bloat.

  • #5 / Aug 12, 2008 4:59am

    m4rw3r

    647 posts

    I forgot the regular date and time manipulation functions :red:

  • #6 / Aug 12, 2008 5:24am

    xwero

    4145 posts

    wiredesignz how would you do a function with variable start, stop time and interval? i know it’s more than the challenge requires but i wanted to make a functional solution.

    BTW g shows hours not leaded by zeros which was a requirement 😊

  • #7 / Aug 12, 2008 5:29am

    wiredesignz

    2882 posts

    @xwero, KISS :lol:

    Yes you are right, I overlooked the part requiring the leading zero, xwero.

    EDIT: fixed

  • #8 / Aug 12, 2008 5:31am

    xwero

    4145 posts

    the h in the date format of the date function does that for you 😊

  • #9 / Aug 12, 2008 9:42am

    Lone

    350 posts

    Damn that is some awesome examples seen so far - love how xwero just has to make it customisable 😉

    m4rw3r’s example has brought to light for me that I need to specify the requirements just a bit stronger and have updated them above. Sorry for not having in the first place!

    The example will need to be a single function that will return the array needed.

    I will also work out a method of counting which is smaller which is based on the number of characters excluding the variable name characters - any suggestions on this counting method appreciated.

    That way people can be at ease making more readable code with better naming for variables then trying to skimp to reduce size. Also for interests sake only I will do a performance comparison for each example given.

  • #10 / Aug 12, 2008 9:54am

    wiredesignz

    2882 posts

    It’s done Lone. There is no better solution. :lol:

    BTW: this is posted under Creative Commons Licensing. 😉

  • #11 / Aug 12, 2008 10:44am

    Lone

    350 posts

    BTW: this is posted under Creative Commons Licensing

    Most certainly 😊

    Results so far updated… Whilst wiredesignz is half the size its also double the execution time 😉

    Im certain xwero and m4rw3r will have some new samples up soon - and everyone else feel free to chip in!

  • #12 / Aug 12, 2008 10:48am

    wiredesignz

    2882 posts

    Your original request was small not fast, and I don’t believe double execution time. So try this anyway.

    public function time_arr()
    {        
        for ($i = 0; $i < 24; $i++):
                
            $time[] = date('h:iA', mktime($i,0));
            $time[] = date('h:iA', mktime($i,15));
            $time[] = date('h:iA', mktime($i,30));
            $time[] = date('h:iA', mktime($i,45));
            
        endfor;
            
        return $time;
    }
  • #13 / Aug 12, 2008 10:57am

    Lone

    350 posts

    Your original request was small not fast

    Still win based on smallest - just placing execution times for interests sake 😉 Don’t think there’s much chance on bettering yours but we’ll see what comes up!

    Also based on your last sample only seeing a very small increase in execution time (around 2%).

  • #14 / Aug 12, 2008 12:27pm

    m4rw3r

    647 posts

    function hr_list()
    {
        foreach(range(-3600, 81900, 900) as $t)
        {
            $r[] = date('h:i A', $t);
        }
        
        return $r;
    }

    That one is insanely fast, around 2x wiredesignz’ :D

    EDIT: Why is xwero’s 2x faster than this? I cant see why it would.

    EDIT2: Woo hooo!! my first code is 6 x faster than xwero’s !!

  • #15 / Aug 12, 2008 6:44pm

    wiredesignz

    2882 posts

    Now that is really cool m4rw3r, Don’t we love a challenge. :lol:

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases