|
http://www.hackerschool.org/HS_Boards/zboard.php?AllArticle=true&no=2970 [복사]
안녕하세요.
하루전 에 질문올려놓고 갚자기 프로그램이 제대로 돌아가서 피해만준 넘입니다 ㅋ.
제가 algorithms.php 을 만든이유는 hackthissite.org 에 있는 programming mission 1을 하기 위해서 였는데요.
막상 하려니 안돼네요. (다른 언어로 만들수는 있지만 지금은 php 공부중이여서 꼭 php 로 만들고 싶어요 ^0^ )
이틀동안 고려 봤는데 안돼네요 ㅠㅠ.
미션 설명 = 제공한 txt 파일에는 2000 여개의 단어가 있는데요. 미션을 시작하면 그중 10개의 단어를 무작위로 선택하여서
단어순을 섞어 버립니다. 파일을 이용해서 original 단어를 찾아야 돼는거죠 [한국말이 서투러서 잘 설명이 됐는지 모르겠네요]
index.php 코드
<?php
require_once('algorithms.php');
/* Getting the txt file and putting it in to array $word_bank*/
if(isset($_POST['submit'])){
$text_file = $_FILES['text_file']['name'];
$word_bank = array();
/* Get the file & Make $word_bank array */
if(!empty($text_file)){
$file_tmp_name = $_FILES['text_file']['tmp_name'];
$file = fopen($file_tmp_name, "r");
while(!feof($file))
array_push($word_bank, fgets($file));
fclose($file);
@unlink($file_tmp_name);
}
else{
echo '<p>Please choose your file.</p>';
exit();
}
/* Receive the words and put them in to array $word*/
$words = $_POST['words'];
$word = explode("\n",$words);
find_word($word, $word_bank);
exit();
}
?>
<!--Form that receives the randomly picked words and the text file-->
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype = "multipart/form-data">
<fieldset>
<legend>Programming Mission 1</legend>
<label for="words">Words:</label><br>
<textarea name="words" id="words" cols="30" rows="4"></textarea>
<br>
<label for="text_file">Choose the file</label>
<input type="file" name="text_file">
</fieldset>
<input type="submit" name="submit" value="Done">
</form>
algorithms.php 파일 코드
<?php
function find_word($a, $b){
// $a = scrambled word
// $b = one getting compared to (from the original list)
for($i=0; $i<count($a); $i++){
for($ii=0; $ii<count($b); $ii++){
$a_split= str_split($a[$i]);
$b_split= str_split($b[$ii]);
sort($a_split);
sort($b_split);
$new_a = implode('',$a_split);
$new_b = implode('',$b_split);
if($new_a == $new_b){
echo $b[$ii].', ';
}
}
}
}
?> |
Hit : 4258 Date : 2011/04/27 09:01
|