Pergunta de entrevista da empresa Meta

Implement a method called printNonComments() which prints out a extract of text with comments removed. For example, the input: hello /* this is a multi line comment */ all Should produce: hello all You have access to a method called getNextLine() which returns the next line in the input string.

Respostas da entrevista

Sigiloso

12 de nov. de 2014

//need to use and also "/*" and "*/" need to be followed by space " ". int main() { string s = "hello /* this is a multi line comment */ all "; istringstream iss(s); string tmp; while(iss>>tmp){ if(tmp=="/*"){ while(iss>>tmp){ if(tmp=="*/"){ iss>>tmp; break; } } } cout<

Sigiloso

25 de nov. de 2014

def echo(what): sys.stdout.write(what) def gobble(what): pass funsrc = cycle([(echo, "/*"), (gobble, "*/")]) consumer_fun, delim = funsrc.next() while True: line = getNextLine() if not line: break ix = line.find(delim) while ix >= 0: prefix = line[:ix] line = line[ix+len(delim):] consumer_fun(prefix) consumer_fun, delim = funsrc.next() ix = line.find(delim) consumer_fun(line)

Sigiloso

11 de mai. de 2015

With vector instead of the getNextLine() to make test easier. using namespace std; void printNonComment(const vector& v) { bool show = true; for(vector::const_iterator itr = v.begin(); itr != v.end(); ++itr) { string s = *itr; for(int i = 0 ; i v; v.push_back("hello /*"); v.push_back("*/ there"); v.push_back("hello /*adf*/ me"); v.push_back("/*ha /*ha /*ha */ haha"); printNonComment(v); }

Sigiloso

13 de ago. de 2014

// NOTE: Not tested on an editor, may not compile. Think as a pseudo code // There is condition where this code fails: If there are two comments on the same line like // some text /* hi there */ some more and /* another comment */ even more text public static void main(String[] args){ final String COMMENT_START = "/*"; final String COMMENT_END = "*/"; bool isCommentStarted = false; String line = getNextLine(); while(line != null && line.length() >0){ int index = line.indexOf(COMMENT_START); if(index != -1 && isCommentStarted == false){ // print up until the comment System.out.print(line.subString(0, index)); isCommentStarted = true; } index = line.indexOf(COMMENT_END); if(index != -1){ // print what we have after the comment end System.out.print(line.subString(index)); isCommentStarted = false; } line = getNextLine(); System.out.println(""); // write on next line } }

Sigiloso

25 de nov. de 2014

import re lines = [] while True: line = getNextLine() if line == None: break lines += line print re.sub(r'/\*(.|\n)*?\*/','',lines.join('\n'))