my algorithm works fast and fine on examples and I can't find any problem in 
it. please help if you see any wrong spot:


########## My Code #############

const readline = require('readline'); 
var rl = readline.createInterface(process.stdin, process.stdout);

var TCNum=0;
var TCmax=null;
var waitFor='T';




rl.on('line', function(data) {
  if(waitFor=='T'){
    TCmax=parseInt(data); 
    waitFor='NL';  
  }else if(waitFor=='NL'){      
    waitFor='STR'; 
  }else if(waitFor=='STR'){
        var list=data.split(' ');

        var key=findKey(list[0],list[1]);


        var primerifiedInfo=primify(key,list);

        var dictionary=creatDictionary(primerifiedInfo.primes);

        var decrypted=decrypt(dictionary,primerifiedInfo.list).join('');


        console.log("Case #"+(TCNum+1)+": "+decrypted);
        waitFor='NL'; 
    checkCount();
  }
  function checkCount()
  {
    TCNum++;
    if(TCNum==TCmax)
    {
      rl.close();
    }
  }
}).on('close',function(){
    process.exit(0);
});

function gcd_recursive(a,b)
{
    if (b){
        return gcd_recursive(b, a % b);
    }else{
        return a;
    }
}


function findKey(a,b,primes)
{
        var gcd=gcd_recursive(a,b);
        return a/gcd;
}

function primify(key,list)
{
        var allPrimes=[];
        var subKey=key;
        var out=[key];
        var thisKey;
        for(let i=0;i<list.length;i++)
        {
                allPrimes=addMemberToSet(subKey,allPrimes);
                thisKey=list[i]/subKey;
                out.push(thisKey);
                subKey=thisKey;         
        }
        allPrimes=addMemberToSet(subKey,allPrimes);
        out={list:out,primes:allPrimes};
        return out;
}

function creatDictionary(primes)
{
        var tmp;
        for(let i=primes.length-1;i>=0;i--)
        {
                for(let ii=0;ii<i;ii++)
                {
                        if(primes[ii+1]<primes[ii])
                        {
                                tmp=primes[ii];
                                primes[ii]=primes[ii+1];
                                primes[ii+1]=tmp;
                        }
                }
        }

        var dictionary={};
        for(let i=0;i<primes.length;i++)
        {
                dictionary[primes[i]]=String.fromCharCode(i+65);
        }

        return dictionary;
}

function decrypt(dictionary,primerifiedList)
{
        var out=[];
        for(let i=0;i<primerifiedList.length;i++)
        {
                out[i]=dictionary[primerifiedList[i]];
        }
        return out;
}




function addMemberToSet(member,toSet)
{
  var repeated=false;
  toSet.forEach(function(token){
    if(token===member)
    {
      repeated=true;
    }
  }); 
  if(repeated)
  {
    return toSet;
  }
  toSet.push(member);
  return toSet;
}

############################

thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/826806ec-0e64-49e8-8e75-69bd6efa1ba8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to