Posted
Filed under Action Script

[원문]: http://club.filltong.net/codingdojo/23224


package
{
  import flash.utils.ByteArray;

  public class FindPermutation
  {
    protected var s:String = "abcdefgh";
    protected var dict:Array = [];

    public function FindPermutation()
    {
      makeDict();
    }

    public function makeDict():void
    {
      var arr:Array = s.split("");
      dict = permutation(arr)
      dict.map(function(element:*, index:int, arr:Array):void { arr[index] = (element as Array).join("") });
    }

    public function find(words:String):Number
    {
      return dict.indexOf(words) + 1;
    }

    protected function permutation(array:Array):Array
    {
      if(array.length == 1) {
        return [array];
      }

      var result:Array = [];

      array.forEach(function(element:*, index:int, arr:Array):void {
        var tmp:Array = cloneArray(arr);
        tmp.splice(index,1);
        permutation(tmp).forEach(function(e:*, i:int, a:Array):void {
          result.push([element].concat(e));
        });
      });
      return result;
    }

    protected function cloneArray(src:*):* {
      var result:ByteArray = new ByteArray();
      result.writeObject(src);
      result.position = 0;
      return(result.readObject());
    }
  }

2010/04/23 19:54 2010/04/23 19:54