프로그래밍

 3198, 1/160 회원가입  로그인  
   smile_mut
   [PHP] hackthissite.org 질문

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



    
smile_mut 쓰고 나니 쫌 (엄청) 길어 졌네요... 수고해주세용 :) 2011/04/27  
멍멍 엇 질문은 뭐세욤~? 2011/04/27  
코털뽑기 ㅋㅋㅋ 2011/04/27  
smile_mut 아, 예상했던데로 프로그램이 안돌아가네요.
어디가 문제인지 모르겠어요
2011/04/27  
prosper 1723개의 사전 단어와 10개의 랜덤문자열을 비교해서 30초이내 푸는 문제군요

php문제는 아니고 프로그래밍하여 30초내에 풀어서 1~10문자를 , 로 정렬해서 적으면 풀리는문제.

알고리즘은 잘했네요 ^^
2011/05/09  
prosper http://www.hackerschool.org/HS_Boards/zboard.php?id=Free_Board&page=1&sn1=&divpage=4&sn=off&ss=on&sc=on&select_arrange=headnum&desc=asc&no=20822 2011/05/09  
smile_mut prosper 님 감사합니다 :) 2011/05/15