Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Grr... Last one, I swear! ;)
Old 03-30-2009, 06:06 PM Grr... Last one, I swear! ;)
Skilled Talker

Posts: 68
Name: Avi Zolty
Location: Atlanta
Trades: 0
So my script is done... But it doesn't work
why?!?!

<?PHP
function tryAll($current, $letters) {
for($i = 0; $i < count($letters); $i++) {
$letter = $letters[$i];
$str = $current.$letter;

// Check if $str is a valid word, with a dictionary in a
// database, file or what ever smart solution you may find

$path = 'http://www.liophant.com/Englishwords.txt';
$wordArray = file($path);


if(in_array($str , $wordArray))
{
echo $str . "<br>";
}


// Make a copy of $letters, without the currently one selected
// (since that letter is already used, you dont wanna use it again)
$copy = array();
foreach ($letters as $key => $letter) {
if ($key != $i) {
$copy[] = $letter;
}
}

// run recursive call to same function
tryAll($str, $copy);
}
}

$text = $_REQUEST['theword'];
$length = strlen($text);
$word = array();

for($i = 0; $i < $length; $i++)
{
$word[] = $text[$i];
}

tryAll('', $word);
?>
__________________
"If you say something interesting, people will remember your name" ~ Anonymous


Please login or register to view this content. Registration is FREE
.asp <- Irony
Zoltar1992 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-30-2009, 10:08 PM Re: Grr... Last one, I swear! ;)
athomas's Avatar
Ultra Talker

Posts: 286
Name: Adam
Location: Eastern Shore, MD, USA
Trades: 0
Using the
Code:
 [php]
tag is helpful for forum posts, syntax higlighting and everything makes it easier to read.
athomas is offline
Reply With Quote
View Public Profile
 
Old 03-30-2009, 11:56 PM Re: Grr... Last one, I swear! ;)
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
First off, you'll want to move this part out of the for loop
PHP Code:
$path 'http://www.liophant.com/Englishwords.txt'
$wordArray file($path); 
You only have to read the file once, not for every word.
Secondly, what isn't working? Are there no matching words that gets printed?

EDIT: I cehcked out your Englishwords.txt file, it seems file() includes a new line in every word. Try var_dump() to see what I mean.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 03-31-2009 at 12:09 AM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-31-2009, 02:44 AM Re: Grr... Last one, I swear! ;)
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Needed to reformat to code before I could find the problem:

PHP Code:
<?PHP
function tryAll($current$letters
{
     for(
$i 0$i count($letters); $i++) 
     {
          
$letter $letters[$i];
          
$str $current.$letter;

          
// Check if $str is a valid word, with a dictionary in a
          // database, file or what ever smart solution you may find

          
$path 'http://www.liophant.com/Englishwords.txt'
          
$wordArray file($path); 


          if(
in_array($str $wordArray)) 
          { 
              echo 
$str "<br>";
          } 


          
// Make a copy of $letters, without the currently one selected
          // (since that letter is already used, you dont wanna use it again)
          
$copy = array();
          foreach (
$letters as $key => $letter
          {
               if (
$key != $i
               {
                    
$copy[] = $letter;
               }
          } 

          
// run recursive call to same function
          
tryAll($str$copy);
     }
}

$text $_REQUEST['theword']; 
$length strlen($text); 
$word = array(); 

for(
$i 0$i $length$i++) 

     
$word[] = $text[$i]; 
}

tryAll(''$word); 
?>
Try:
PHP Code:
function tryAll($list $word)
{
     if(
strlen($word) < 1)
          return 
false;

     
$curr $word[0];
     for(
$i 1$i strlen($word); $i++)
     {
          
$temp $curr $word[$i];
          if(
in_array($temp $list))
          {
               echo 
$temp '<br />';
          }
          else
          {
               
$curr $temp;
          }
     }
}

$path 'http://www.liophant.com/Englishwords.txt'
$list file($path);
$word $_REQUEST['theword'];

tryAll($list $word); 
I think that accomplishes what you were going for. One of the problems I noticed with your code was improper use of a foreach loop. The index of an array is not the same as a key in an associative array.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 03-31-2009 at 03:03 AM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-31-2009, 12:38 PM Re: Grr... Last one, I swear! ;)
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I'm not sure your function will give the wanted result, Matt. Let's say the given word is 'blue'. Your function will try b, bl, blu and blue wheras it should try all combinations of letters. That is, it should also try ble, bleu, bu, bul, bule, bue, buel, be, bel, belu, beu, beul, l, lb, lbu, lbue and so on.

Quote:
The index of an array is not the same as a key in an associative array.
Oh, I always thought they were Do you mind explaining the difference?
I've also heard (just from friends, so I'm not sure) that PHP, internally, doesn't have a "true" array data structure but instead represents all arrays as hash maps. Wouldn't that make all arrays equal in structure, weather they are associative or not, since a "normal" array (what's the name of an array that is not associative?) would simply be a hash map with integer keys.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-31-2009, 01:21 PM Re: Grr... Last one, I swear! ;)
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by lizciz View Post
I'm not sure your function will give the wanted result, Matt. Let's say the given word is 'blue'. Your function will try b, bl, blu and blue wheras it should try all combinations of letters. That is, it should also try ble, bleu, bu, bul, bule, bue, buel, be, bel, belu, beu, beul, l, lb, lbu, lbue and so on.
Oh if that's the case my mistake, I couldn't tell what he was going for from the code alone.

Quote:
Originally Posted by lizciz View Post
Oh, I always thought they were Do you mind explaining the difference?
I've also heard (just from friends, so I'm not sure) that PHP, internally, doesn't have a "true" array data structure but instead represents all arrays as hash maps. Wouldn't that make all arrays equal in structure, weather they are associative or not, since a "normal" array (what's the name of an array that is not associative?) would simply be a hash map with integer keys.
An array and an associative array are two different datastructures. I really don't like the term associateive array because it leads to confusion (but that is the norm for PHP), really it should be called a map.

In a traditional array, the index of the array can be related to the point in memory that an element is stored. The point in memory of an item can be determined by adding the base address (the point in memory where the array starts) to the index multiplied by the amount of memory an item uses.

Code:
address = base + (index * size)
What this means is that data in your array can be accessed in constant time, regardless of its position in the array.

A map on the other hand relates keys (often strings) to values. You can think of a map as a table where in one column is the key and in the other is the value it is associated with. Given a key, in order to find a value, you must search the map for the given key. This means that accessing an element of a map takes linear time (accessing the last element takes longer than accessing the first).

I'm not sure if this is the case in PHP (as it is in Java and C++). If it is the case that all of these arrays are actually hash tables, I would be interested in how it is implimented. With a hash table you always have the possibility of a collision (2 keys pointing to the same value). I'm not sure that this would perform any better than just a plain map in many cases.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 03-31-2009 at 01:29 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-31-2009, 02:46 PM Re: Grr... Last one, I swear! ;)
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Hmm. But a hash table can also retrieve values in constant time, presuming that there are no collisions, since it's basically an array where the key is hashed to get an index, right? And with a good hashing function and a big enough array the collisions can be minimized.

Hmm, and it just hit me:
For Java (which is the language we use at school) the point of using a hash table instead of a simple array basically is that you can store objects instead of the basic data types (int, char, boolean etc.). And seeing as PHP has dynamic variable types (all variables can be of any type at any time) it actually makes sense if arrays are infact hash tables. Though it may not be as (memory) effective, since you have to allocate twice (if I remeber correctly) the size of the table as your planning on using, in order to avoid collisions.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-31-2009, 04:15 PM Re: Grr... Last one, I swear! ;)
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Well if a collision occurs in a hash table then the keys become ambiguous. In order to avoid collisions on a nearly full array insertion time becomes proportional to the size of the array rather than constant. Not to mention the overhead of the implementation of the hash table itself. Like I said before, I don't know if this is the case in PHP, but I think that the extra memory required to prevent O(n) insertion times, is a waste in many cases.

An array of non-primitives does not contain the object itself, but rather a list of addresses to that object, so varying data types do not prevent you from "storing" an array of all of those data types, so long as the system is able to determine the type of those objects by means other than the type of the array. Honestly though I like my arrays like I like my data types, defined from the start. Unfortunately PHP offers neither.

By the way, in Java you can have an array of objects. It is actually the other way around. A generic data structure (ArrayList, HashTable, etc) cannot be parameterized with primitive types. Java however provides object versions of each primitive type.
Code:
ArrayList<int> myList = new ArrayList<int>(); //incorrect
ArrayList<Integer> myList = new ArrayList<Integer>(); //correct
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 03-31-2009 at 04:24 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-01-2009, 12:04 AM Re: Grr... Last one, I swear! ;)
Skilled Talker

Posts: 68
Name: Avi Zolty
Location: Atlanta
Trades: 0
*nods, pretending to understand*
I see...
Erm, so how do I get around it?
__________________
"If you say something interesting, people will remember your name" ~ Anonymous


Please login or register to view this content. Registration is FREE
.asp <- Irony
Zoltar1992 is offline
Reply With Quote
View Public Profile
 
Old 04-01-2009, 12:12 AM Re: Grr... Last one, I swear! ;)
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Agree, it does seem like a big waste, which is why I were curious at first to ask if that really was the case.

I, too, have begun liking to define variable types from the start. I quess it comes from using Java In my newest PHP project I usually adapted alot of Java's way of programming.

True, you can store object in normal arrays, I got it mixed up

Now, back to the original post, sorry
As I said, all the new lines in the file is stored when using the file() function. I quess you could simply use trim() on all the lines to remove them.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Grr... Last one, I swear! ;)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.48379 seconds with 12 queries