Created
March 12, 2022 17:14
-
-
Save Codehunter-py/d264e146e371b1dd2ef273275223899a to your computer and use it in GitHub Desktop.
The transform_comments function converts comments in a Python script into those usable by a C compiler. This means looking for text that begins with a hash mark (#) and replacing it with double slashes (//), which is the C single-line comment indicator. For the purpose of this exercise, we'll ignore the possibility of a hash mark embedded inside…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
import re
def transform_comments(line_of_code):
result = re.sub(r'###',r'//', line_of_code)
return result
print(transform_comments("### Start of program"))
Should be "## Start of program"
print(transform_comments(" number = 0 ## Initialize the variable"))
Should be " number = 0 // Initialize the variable"
print(transform_comments(" number += 1 # Increment the variable"))
Should be " number += 1 // Increment the variable"
print(transform_comments(" return(number)"))
Should be " return(number)"
just go ahead with re.sub(r'#+", r"//", line_of_code)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import re
def transform_comments(line_of_code):
result = re.sub(r"[#]+","//",line_of_code)
return result