www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32

Thread: Who fancies a code challenge just for fun?

  1. #1
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736

    Who fancies a code challenge just for fun?

    We've done this before, but not for a long time. If a few people are interested I'll dream up a challenge.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,935
    I'm always up for a challenge if (a) it's not too difficult and (b) does not require a lot of coding. (Italicized words are, of course, extremely subjective. )
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Ok, not too difficult (probably could do it in around 20 line of code once any variables are set) but may need a bit of lateral thought. Scrabble type word finder. I'll give you a list of words so we've got a common dictionary (from a spell checker). Input is any string of letters, of variable length. Using all the letters in the string, some letters might be duplicates, find all the entries in the dictionary that contain those exact same letters. So if there is a 'P' in your input string, there will also be a 'P' in the found words. And if there are 2 'S' in the input there will be 2 'S' in the found words. And the found words will be the same length as the input string. You can arrange the dictionary into any form.

    Needs to find all the words and be efficient.

    bokehman.com/wordlist.zip

  4. #4
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Here's a test running a group of 16 letters. Server is pretty low spec so it takes about 5 seconds to run but it is much quicker on my laptop.

    bokehman.com/scrabble/?letters=nomnieoimprsises

    How are you getting on?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,935
    My script to populate the DB table is still running: adding about 15 words per second on my old, slow laptop. Have to tackle the actual search page tomorrow -- already well past my bedtime.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Decided to play as well:

    Search results:

    index.php?letters=test
    SELECT w.name FROM words w WHERE LENGTH(w.name) = 4

    Array
    (
    [0] => stet
    [1] => test
    )
    0.090747833251953
    index.php?letters=nomnieoimprsises
    SELECT w.name FROM words w WHERE LENGTH(w.name) = 16

    Array
    (
    [0] => neoimpressionism
    )
    0.078151941299438
    PHP Code:
    <?php

        $database_id 
    mysql_pconnect('localhost''root''');
        
    mysql_select_db('scrabble'$database_id);

        
    $timer   microtime(true);
        
    $letters strtolower(isset($_GET['letters']) ? $_GET['letters'] : 'test');
        
    $words   = array();

        echo 
    $query '

            SELECT w.name
            FROM words w
            WHERE LENGTH(w.name) = ' 
    strlen($letters) . '

        '
    ;

        
    $query_id mysql_query($query$database_id);
        while (
    $row mysql_fetch_assoc($query_id)) {
            for (
    $i 0$j strtolower($row['name']); $i strlen($letters); ++$i) {
                if ((
    $k strpos($j$letters[$i])) !== false) {$j[$k] = '*';} else {break;}
            }
            if (
    $i >= strlen($letters)) {
                
    $words[] = $row['name'];
            }
        }
        
    mysql_free_result($query_id);

        echo 
    '<pre>';
        
    print_r($words);
        echo (
    microtime(true) - $timer);
        echo 
    '</pre>';

        
    mysql_close($database_id);

    ?>
    Database was too large to upload here, I can mail it to someone if required (limited time obviously).
    Structure was as follows:

    Database: scrabble
    Table: words [name, varchar(255), PK]
    JavaScript: Learn | Validate | Compact

  7. #7
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Hmm! I haven't used a MySQL as it was a PHP challenge. That was really whole the point. Open the data and search through it. Using a DB is just using someone else's code.

    Anyway here's mine. Now I've tidied it up a bit it runs pretty quick.

    bokehman.com/scrabble/?letters=nomnieoimprsises
    Last edited by bokeh; 10-10-2013 at 06:16 AM.

  8. #8
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Quote Originally Posted by bokeh View Post
    Using a DB is just using someone else's code.
    Sorry, I didn't realise it wasn't allowed.
    JavaScript: Learn | Validate | Compact

  9. #9
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Quote Originally Posted by bionoid View Post
    Sorry, I didn't realise it wasn't allowed.
    Well your query is only selecting the word length so not really any different from storing the words in files arranged like that. Either way it looks like most of the work is the read in time of the data.

    For me words around 10 or 11 chars are the hardest going.

    BTW, mine is just running on my NAS. I'm sure on a dedicated webserver it would run miles quicker.
    Last edited by bokeh; 10-10-2013 at 06:52 AM.

  10. #10
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Ok, changed it to only use the word file provided, as-is:

    index.php?letters=test
    Array
    (
    [0] => stet
    [1] => test
    )
    0.17748403549194
    index.php?letters=nomnieoimprsises
    Array
    (
    [0] => neoimpressionism
    )
    0.18036890029907
    index.php
    PHP Code:
    <?php

        $timer   
    microtime(true);
        
    $letters strtolower(isset($_GET['letters']) ? $_GET['letters'] : 'test');
        
    $length  strlen($letters);
        
    $words   = array();

        
    $handle fopen('en.txt''rb');
        while (!
    feof($handle)) {
            if (
    strlen($word trim(fgets($handle))) === $length) {
                for (
    $i 0$j strtolower($word); $i $length; ++$i) {
                    if ((
    $k strpos($j$letters[$i])) !== false) {$j[$k] = '*';} else {break;}
                }
                if (
    $i >= $length) {
                    
    $words[] = $word;
                }
            }
        }
        
    fclose($handle);

        echo 
    '<pre>';
        
    print_r($words);
        echo (
    microtime(true) - $timer);
        echo 
    '</pre>';

    ?>
    A little slower, but I'm still happy with it :P
    This is all running off my laptop at work. (HP ProBook 4740s)
    Last edited by bionoid; 10-10-2013 at 07:33 AM.
    JavaScript: Learn | Validate | Compact

  11. #11
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Just tried again with laptop and timing is 0.015 for 'nomnieoimprsises' and '0.015' for test as well. Compares to 0.07 for the NAS, which is the public facing test. And 'poixaenohb' takes 0.157 on the laptop and 0.870 on the NAS.

    Here's the code that created the dictionaries:
    PHP Code:
    <?php

    set_time_limit
    (300);

    $lines file('EN.txt');
    $words = array();
    foreach(
    $lines as $line)
    {
        
    $word trim(strtolower($line));
        
    $words[strlen($word)][] = $word;
    }

    file_put_contents('EN.words.by.length.php''<?php

    $words = '
    .var_export($wordstrue).'

    ?>'
    );

    echo 
    'done';

    ?>
    Then break it down a little bit more:
    PHP Code:
    <?php

    $file 
    'EN.words.by.length.php';

    require(
    $file);

    foreach(
    $words as $k=>$v)
    {
        
    $newwords = array();
        
    $newwords[$k] = $v;
        
    file_put_contents($k.'.'.$file'<?php'."\n\n".
        
    '$words = '.var_export($newwordstrue)."\n\n".
        
    '?>');
    }
    ?>
    And here's the code I've got running on the website:
    PHP Code:
    <?php

    # default language
    $lang 'English';

    # for the form
    $letters_fieldname 'letters';
    $language_fieldname "language";
    $language_options = array('English''Espaol');
    # end form stuff


    $letters '';

    # validation stuff
    if(isset($_GET[$letters_fieldname]) and is_string($_GET[$letters_fieldname]))
    {
        if(isset(
    $_GET[$language_fieldname]) and $_GET[$language_fieldname] === 'Espaol')
        {
            
    $allowed_chars 'aeioubcdfghjklmnpqrstvwxyz';
            
    $dictionary 'ES.words.by.length.php';
            
    $lang 'Espaol';
        }
        else
        {
            
    $allowed_chars 'a-z\'';
            
    $dictionary 'EN.words.by.length.php';
            
    $lang 'English';
        }
        
    $letters preg_replace('/[^'.$allowed_chars.']/'''strtolower($_GET[$letters_fieldname]));    
    }
    # end validation

    $output '';
    $timer '';

    if(
    strlen($letters)>0)
    {
        
    $timer microtime(true);
        
    $timer microtime(true);
        
    $output scrabble($letters$dictionary);
        
    $timer microtime(true) - $timer;
    }

    ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
         "http://www.w3.org/TR/html4/strict.dtd">

    <html lang="en">
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=ISO-8859-1">
    <title>Scrabble</title>
    </head>

    <body>
    <div id="container">
    <h1>Input</h1>
    <div id="form">
    <form  action="" method="<?php echo strtolower($PostGet); ?>">
    <label for="<?php echo $letters_fieldname ?>">Letters:</label>
    <input class="form_elements" id="<?php echo $letters_fieldname?>" type="text" name="<?php echo $letters_fieldname?>" value="<?php echo $letters ?>">
    <?php echo dropdown($language_fieldname$language_options, isset($_GET[$language_fieldname]) ? $_GET[$language_fieldname] : ''); ?>
    <label for="submit"> </label>
    <input id="submit" type="submit" name="submit" value="send">
    </form>
    </div><!-- end #form -->
    <div id="output">
    <h1>Output</h1>
    <ul>
    <?php echo $output ?>
    </ul>
    <?php if($timer){echo '<p>Search time: '.$timer."</p>\n";} ?>
    </div><!-- end #output -->
    </div><!-- end #container -->
    </body>
    </html><?php

    function scrabble($letters$dictionary)
    {
        
    $len_letters strlen($letters);
        if(
    file_exists($len_letters.'.'.$dictionary))
        {
            require(
    $len_letters.'.'.$dictionary);
        }
        else
        {
            return 
    '';
        }
        
        
    $output '';
        
        
    $alphabet = array("a"=>1,""=>1,"e"=>1,""=>1,"i"=>1,""=>1,"o"=>1,
                            
    ""=>1,""=>1"u"=>1,""=>1,""=>1,"b"=>1,"c"=>1,
                            
    "d"=>1,"f"=>1,"g"=>1,"h"=>1,"j"=>1,"k"=>1,"l"=>1,
                            
    "m"=>1,"n"=>1,""=>1,"p"=>1,"q"=>1,"r"=>1,"s"=>1,
                            
    "t"=>1,"v"=>1,"w"=>1,"x"=>1,"y"=>1,"z"=>1,"'"=>1);
                            
        
    $mutltipliers = array(23571113171923
                                
    2931374143475359
                                
    6167717379838997
                                
    101103107109113127
                                
    131137139149);        
                                
        
    $letters strtolower($letters);
        
    $letters_array str_split($letters);
        
    $unique_letters_array array_values(array_unique($letters_array));
        
    $len_unique_letters count($unique_letters_array);
        
        for(
    $i=0$i<$len_unique_letters$i++)
        {
            
    $alphabet[$unique_letters_array[$i]] = $mutltipliers[$i];
        }
        
        
    $letters_value 1;
        foreach(
    $letters_array as $v)
        {
            
    $letters_value *= $alphabet[$v];
        }
        
        foreach(
    $words[$len_letters] as $v)
        {
            
    $value 1;
            
    $v_split str_split($v);
            foreach(
    $v_split as $v2)
            {
                
    $value *= $alphabet[$v2];
            }
            if(
    $value == $letters_value)
            {
                
    $output .= '<li>'.$v.'</li>'."\r\n";
            }
        }
        return 
    $output;
    }

    function 
    dropdown($fieldname$options_array$selected '')
    {
        
    $return "<select name='".$fieldname."'>\n";
        foreach(
    $options_array as $option)
        {
            
    $return .= '<option value="'.$option.'"'.
                       ((
    $option === $selected) ? ' selected="selected"' '' ).
                       
    '>'.$option.'</option>'."\n";
        }
        return 
    $return."</select>\n";
    }


    ?>

  12. #12
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    index.php?letters=poixaenohb
    Array
    (
    [0] => xenophobia
    )
    0.20592093467712
    JavaScript: Learn | Validate | Compact

  13. #13
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Cool.

    Just out of interest what are you testing on?

    Laptop here is Pentium(R) Dual-Core T4400 @ 2.20 GHz, with 4 GB RAM. Probably around 5 years old.

  14. #14
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Intel Quad Core i5-3230M @2.6GHz. 4 GB memory (2.6 GB usable).
    Windows 7 - 32 bit.

    You know a better use for this program would be to tell you the biggest word you "could" create using the letters you have available (like with real scrabble). For example, if you had the letters "czta", then it would suggest you could make the word "cat" on the board.
    Last edited by bionoid; 10-10-2013 at 08:38 AM.
    JavaScript: Learn | Validate | Compact

  15. #15
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,935
    Quote Originally Posted by bionoid View Post
    Intel Quad Core i5-3230M @2.6GHz. 4 GB memory (2.6 GB usable).
    Windows 7 - 32 bit.

    You know a better use for this program would be to tell you the biggest word you "could" create using the letters you have available (like with real scrabble). For example, if you had the letters "czta", then it would suggest you could make the word "cat" on the board.
    Which is what I'm working on (and it will be using a database -- why use a wrench to pound in a nail when you have a hammer available? ).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles