Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rahat-khan-pathan/hackerrank-custom-checker-code

Hackerrank custom checker code for C programming. Verify function definitions & calls within main function. Enhance code quality. 🚀
https://github.com/rahat-khan-pathan/hackerrank-custom-checker-code

Last synced: about 23 hours ago
JSON representation

Hackerrank custom checker code for C programming. Verify function definitions & calls within main function. Enhance code quality. 🚀

Awesome Lists containing this project

README

        

### Step 1: Extract the source code from a HackerRank file

To extract the source code from a HackerRank file, you can follow these steps:

```python
with open(t_obj.submission_code_path, "r") as code_file:
code_contents = code_file.read()
```

### Step 2: Remove all comments from the source code

To remove comments from the source code, consider using regular expressions or code parsing. Here's a general approach:

- Use regular expressions or code parsing to identify and remove comments in the source code.
- Make sure to handle both single-line (//) and multi-line (/\* \*/) comments.

```python
comment_pattern = r'//.*|/\*[\s\S]*?\*/'
code_contents = re.sub(comment_pattern, '', code_contents)
```

### Step 3: Convert all macros or typedef definitions for data types

To convert macros or typedef definitions for data types, you can use regular expressions to identify and replace them. For example, if you want to convert macros like `#define` or `typedef`:

- Use regular expressions to identify macro or typedef definitions in the source code.
- Replace them with their expanded forms or desired types if necessary.

```python
# convert all defines for data types only
define_pattern = r'#\s*define\s+(\w+)\s+([^#\n]+)'
define_matches = re.findall(define_pattern, code_contents)
define_dict = {name: value.strip() for name, value in define_matches}
def replace_macros(match):
macro_name = match.group(0)
if macro_name in define_dict:
return define_dict[macro_name]
return match.group(0)
code_contents = re.sub(r'\b\w+\b', replace_macros, code_contents)
# convert all typedef definitions of data types only
typedef_dict = {}
typedef_pattern = r'typedef\s+([\w\s]+)\s+(\w+);'
matches = re.findall(typedef_pattern, code_contents, re.DOTALL)
for match in matches:
typedef_dict[match[1]] = match[0].strip()
def replace_typedefs(match):
typedef_alias = match.group(1)
if typedef_alias in typedef_dict:
return typedef_dict[typedef_alias]
return match.group(0)
code_contents = re.sub(r'\b(\w+)\b', replace_typedefs, code_contents)
```

### Step 4: Check if the relevant function is present in the source code

To check if a specific function is present in the source code:

- Make sure to remove spaces from the line to check all cases.
- Make sure you know all the valid function definitions.
- Verify if the function's declaration is found in the source code.

```python
function_name = "intfuntion_name("
function_name2 = "longlongfuntion_name("
function_name3 = "longlongintfuntion_name("
# removed all spaces from line to check all cases
code_without_spaces = re.sub(r'\s', '', code_contents)
if function_name in code_without_spaces or function_name2 in code_without_spaces or function_name3 in code_without_spaces:
# the function is found in the source code
```

### Step 5: If step 4 is okay, check if the function is called inside the main function scope

To check if a function is called inside the `main` function scope:

- Search for the `main` function declaration in the source code.
- Use stack to keep track of the scope of main function.

```python
code_lines = code_contents.split('\n')
inside_main = False
br_found = False
found=False
stack = []
function_call_name = "function_name("
# now check if the function was called inside the main function
for line in code_lines:
if "main(" in line:
inside_main = True
if inside_main:
if len(stack)==0 and br_found==True:
break
if function_call_name in re.sub(r'\s', '', line): # removed all spaces from line to check
# the caller function is found inside the scope of main function
r_obj.message = "Success"
r_obj.result = True
r_obj.score = 1.0
found=True
break
if '{' in line:
stack.append('{')
br_found=True
if '}' in line:
stack.pop()
if found == False:
r_obj.message = "The 'count_before_one()' function is not called inside the main function."
```