4chan archive /g/ (index)
2012-11-12 01:32 29003512 Anonymous (python_logo.jpg 222x302 9kB)
/g/, I've converted the following c++ code in Python. When I test my Python translation (which I will post in the next post), the results are not 100% right. What am I missing? Here's the c++

0 min later 29003527 Anonymous
class InternetSecurity { public: vector <string> determineWebsite(vector <string> address, vector <string> keyword, vector <string> dangerous, int threshold) { int N = address.size(); vector< set<string> > kwd(N); for(int i=0; i<N; ++i) { stringstream ss(keyword[i]); for(string s; ss >> s; ) kwd[i].insert(s); } set<string> dng(dangerous.begin(), dangerous.end()); vector<bool> is_danger(N); bool changed = true; while( changed ) { changed = false; for(int i=0; i<N; ++i) if( !is_danger[i] ) { vector<string> buf; set_intersection( kwd[i].begin(), kwd[i].end(), dng.begin(), dng.end(), back_inserter(buf) ); if( buf.size() >= threshold ) { changed = true; dng.insert(kwd[i].begin(), kwd[i].end()); is_danger[i] = true; } } } vector<string> result; for(int i=0; i<N; ++i) if( is_danger[i] ) result.push_back( address[i] ); return result; } };

1 min later 29003541 Anonymous
And the Python: def determineWebsite(address, keyword, dangerous, threshold): res=[] for i in range(len(address)): words = [w.strip() for w in keyword[i].split(" ") if w] if len([w for w in words if w in dangerous]) >= threshold\ and address[i] not in res: res.append(address[i]) dangerous += words return [a for a in address if a in res]

4 min later 29003602 Anonymous
It would help if we knew what it was supposed to do, what the results were, and what you were calling them both with.

21 min later 29003943 Anonymous
There are N websites numbered 0 through N-1. Each website has an address given as address[i]. It also has one or more keywords associated with it. The i-th element of keyword is a String describing all keywords associated with the i-th website. It is formatted as a single space separated list of keywords without leading or trailing spaces, where each keyword consists only of lowercase letters. It is known that some keywords are dangerous. These keywords are given in String[] dangerous, where each elementis a single dangerous keyword. For all other keywords it is not initially known whether they are dangerous or not. The following algorithm is used to identify all dangerous websites: Initially, all websites are considered to be safe. While there exists a website W such that it's considered safe and at least threshold of its keywords are known to be dangerous Website W becomes dangerous. All keywords associated with W become dangerous End While Return a String[] containing the addresses of all dangerous websites found by the algorithm described above sorted in increasing order of website numbers. Return an empty String[] if no dangerous website is found.

1 hours later 29005354 Anonymous
>>29003512 op is that even cpp or java?

0.813 0.047