Skip to content

Instantly share code, notes, and snippets.

@Codehunter-py
Created March 12, 2022 17:14
Show Gist options
  • Save Codehunter-py/d264e146e371b1dd2ef273275223899a to your computer and use it in GitHub Desktop.
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…
@Agyemang73
Copy link

Agyemang73 commented Oct 13, 2022

import re
def transform_comments(line_of_code):
result = re.sub(r"[#]+","//",line_of_code)
return result

@DewanSK
Copy link

DewanSK commented Nov 16, 2022

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)"

@zdrj-me31
Copy link

zdrj-me31 commented Aug 6, 2023

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