- 36
Learning Opportunities
This puzzle can be solved using the following concepts. Practice using these concepts and improve your skills.
Statement
Goal
You are given a source code and you have to analyse it: Output the names of all the called library functions & methods, with the number of occurrences for each of them.Detailed rules: (if you are lazy reading this much, skip it and check the example & test cases...)
* Source might contain multiple whitespaces: space, tab (
* Anything within string literals (within pair of single (
* Anything after single line comments (starting with
* A function call can be identified by a valid name, immediately followed by an opening paranthesis
* Valid function names might contain upper and lower case letters, digits and underscore
* Function names shall be treated case sensitively.
* Following reserved words shall be not treated as function calls and omitted from output:
"
* If a function name is immediately preceded by a
* Class names and user-defined functions shall be excluded. If the source contains "
* Output lines shall be sorted by the function names (by ascending ascii values).
* If no library call encountered, output
Notes:
* While the provided test cases are PHP source files, knowledge of PHP is no pre-requisite at all. Of course, you can solve the puzzle in any language.
* Solution would be very similar for analysing C, C++, C#, Java and Javascript source (only difference is the set of reserved keywords and how user-defined functions can be detected). You can easily adapt your solution to analyze your languages of choice and analyze your own source files. Analysing Python or Haskell might need a bit more change though...
* Useless fun fact: my toplist in 98k source lines of PHP:
* Puzzle was inspired by this blog post:
Input
Line 1: An integer N for the number of source lines.
Next N lines: the source code to analyse (string, might contain whitespaces)
IMPORTANT: Unlike in many other puzzles, you might need to read and process also the linefeeds at end of each line. If your input reader strips the \n from the string, you might want to add it back manually.
Next N lines: the source code to analyse (string, might contain whitespaces)
IMPORTANT: Unlike in many other puzzles, you might need to read and process also the linefeeds at end of each line. If your input reader strips the \n from the string, you might want to add it back manually.
Output
?? lines: separate lines for each library function/method encountered: the name of the function and the count number of calls, space separated.
Output lines shall be sorted by the function names (by ascending ascii values).
If no library call encountered, outputNONE
Output lines shall be sorted by the function names (by ascending ascii values).
If no library call encountered, output
Constraints
1 ≤ N ≤ 200
Example
Input
5 /* test with fscanf() and strlen() */ fscanf(STDIN, "%s", $s); $len = strlen($s); // strlen() gives the length of the string if(strlen($s) == $len) echo "strlen() matching\n";
Output
fscanf 1 strlen 2
A higher resolution is required to access the IDE