Thursday, November 11, 2010

PHP Programming Tutorial-



Requirements:

Familiarity with PHP
Loops
String Functions
File Manipulation
Arrays
Server to execute PHP script

Explanation:

This challenge requires from us to take a list of scrambled words, unscramble them by comparing them to a given list which is called wordlist, and finally return the unscrambled words in the appropriate format.




The big question about this problem is – How in the world are we going to compare the scrambled word with an unscrambled one and figure out if they are equal or not. The trick is to forget about the meaning of the words. Now think about it… what do I mean by that to forget about the meaning of the words? What I mean by that is that if you have an unscrambled word “hlleo wrodl” and trying to compare it with “hello world” you NEED to forget about the meaning as in you do not need to make “hlleo wrodl” into “hello world” and then compare them because that is going to be extremely difficult for you. What you need to do instead is…



spoiler:

Put the scrambled word and the unscrambled one into the same format. How can you do it? Perhaps sorting them both? If you sort them out they will both be in the same format and then you can use the strcmp string function from PHP to compare the strings.




Now you have an idea of how you are going to solve the problem, but now it is time to talk about the details. You do know that there is going to be an input which is going to take the scrambled words. You absolutely know that you would need an input form that will take the data for the processes.


spoiler:


If you do not have an idea on how to make an input then I would advise you to make a text area with html that will take the data in and pass it to the PHP page.




Done with the input part? Great! Now what you would need to do next is to look through the text file until the end of the file in order to be able to compare data from the wordlist to the input data.


spoiler:


You would need to make a nested loop – or a loop inside another loop. First loop will go through the file and get each unscrambled word. The second loop will go through the input and get the scrambled word. That way each scrambled word can be compared to each and every unscrambled word which is what we are looking for.




Now think… you got the input and it was successful, then you started looping through the file and through the input data. Everything is good however one thing is missing. Can you guess what it is?


spoiler:



The thing that you are missing is the part where you actually compare the two words. Now for that I would suggest you to make a separate external function which takes tow argument one being the scrambled word and the other one being the unscrambled one. That external function will compare them and return a Boolean value that either being true or false.




Now you have everything you need to know about this problem and hopefully I did not spoil it too much for you guys. If you have any questions please PM me. This was my first article so I am going to try and make it better and better as I start making more. Happy hacking!

No comments:

Post a Comment

Do comment If you liked it...