Skip to content

Instantly share code, notes, and snippets.

@Venkat-Swaraj
Created June 16, 2022 08:56
Show Gist options
  • Save Venkat-Swaraj/bbfb59e0b1a78176ccfacccf84cdf064 to your computer and use it in GitHub Desktop.
Save Venkat-Swaraj/bbfb59e0b1a78176ccfacccf84cdf064 to your computer and use it in GitHub Desktop.
SET-1
"""
Remove all one-line comments,
a comment which starts and ends on one line
Example
Input:
# This is a comment
print('remove_comments')
Output:
print('remove_comments')
Assumption (for this program):
All comments start at the beginning of the line
"""
import unittest
# Implement the below function and run this file
# Return the output, No need read input or print the ouput
def remove_comments(lines):
ans = []
lines = lines.split('\n')
for i in lines:
if i.find('#') == -1:
ans.append(i)
return "\n".join(ans)
# DO NOT TOUCH THE BELOW CODE
class TestRemoveComments(unittest.TestCase):
def test_01(self):
code_with_comments = """
# This is a comment
print('remove_comments')
"""
code_without_comments = """
print('remove_comments')
"""
self.assertEqual(remove_comments(
code_with_comments), code_without_comments)
def test_02(self):
code_with_comments = """
print('no comments to remove')
"""
code_without_comments = """
print('no comments to remove')
"""
self.assertEqual(remove_comments(
code_with_comments), code_without_comments)
def test_03(self):
code_with_comments = """
# This is a comment before method
def sum(a, b):
# this is a comment in method
return a + b
sum(3, 4)
"""
code_without_comments = """
def sum(a, b):
return a + b
sum(3, 4)
"""
self.assertEqual(remove_comments(
code_with_comments), code_without_comments)
def test_04(self):
code_with_comments = """
# This is a comment before method
# This is another comment before method
def is_even(n):
# this is a comment in method
return n % 2 == 0
# call is_even
print('Is 5 even?: ', is_even(5))
# program ended
"""
code_without_comments = """
def is_even(n):
return n % 2 == 0
print('Is 5 even?: ', is_even(5))
"""
self.assertEqual(remove_comments(
code_with_comments), code_without_comments)
if __name__ == '__main__':
unittest.main(verbosity=2)
"""
Rename all the functions from the given python code
as explained below:
1. List all the functions
(you can use the code from files_04_list_defs.py)
2. Sort them (ascending order)
3. replace each function with def_# (# from 1 to n)
Input: Python code
Output: Python code with renamed functions
Example 1
Input:
def method_name():
pass
Output:
def def_1():
pass
Example 2
Input:
def method_name():
print('method_name')
def method_name_2():
print('method_name_2')
Output:
def def_1():
print('method_name')
def def_2():
print('method_name_2')
"""
import unittest
# Use the code from files_04_list_defs.py for bellow list_defs function
def list_defs(lines):
s = []
temp = []
st = {}
lines = lines.splitlines()
for line in lines:
if line.find('def') > -1 or line.find('()') > -1:
line = line.split(" ")
for i in line:
if i.find('()') > -1:
temp = i.split('(')
s.append(temp[0])
st = set(s)
s = list(st)
s.sort()
return s
# Implement the below function and run this file
# Return the output, No need read input or print the ouput
def rename_defs(lines):
# This function is called to find the list of function names
ls = list_defs(lines)
mn = []
for i in range(len(ls)):
mn.append('def_'+str(i+1))
print(mn)
# Code
l = lines.split('\n')
i = 0
for i in range(len(l)):
for j in range(len(ls)):
l[i]=l[i].replace(ls[j]+'()',mn[j]+'()')
rs = "\n".join(l)
return rs
# code_with_defs = """
# '''docstring'''
# This is a comment
# def met_01():
# pass
# def met_02():
# pass
# """
# rename_defs(code_with_defs)
# DO NOT TOUCH THE BELOW CODE
class TestRenameDefs(unittest.TestCase):
def test_01(self):
code_with_defs = """
def method_name():
pass
method_name()
"""
code_with_renamed_defs = """
def def_1():
pass
def_1()
"""
self.assertEqual(rename_defs(code_with_defs), code_with_renamed_defs)
def test_02(self):
code_with_defs = """
def method_name():
print('method_name')
def method_name_2():
print('method_name_2')
"""
code_with_renamed_defs = """
def def_1():
print('method_name')
def def_2():
print('method_name_2')
"""
self.assertEqual(rename_defs(code_with_defs),
code_with_renamed_defs)
def test_03(self):
code_with_defs = """
import string
# This is a comment
def met_01():
pass
"""
code_with_renamed_defs = """
import string
# This is a comment
def def_1():
pass
"""
self.assertEqual(rename_defs(code_with_defs), code_with_renamed_defs)
def test_04(self):
code_with_defs = """
'''docstring'''
# This is a comment
def met_01():
pass
def met_02():
pass
"""
code_with_renamed_defs = """
'''docstring'''
# This is a comment
def def_1():
pass
def def_2():
pass
"""
self.assertEqual(rename_defs(code_with_defs), code_with_renamed_defs)
def test_05(self):
code_with_defs = """
from collections import Counter
# This is a comment
def xyz():
print('this is the first function in the file')
def abd():
pass
def abc():
pass
"""
code_with_renamed_defs = """
from collections import Counter
# This is a comment
def def_3():
print('this is the first function in the file')
def def_2():
pass
def def_1():
pass
"""
self.assertEqual(rename_defs(code_with_defs), code_with_renamed_defs)
def test_06(self):
code_with_defs = """
'''docstring'''
# This is a comment
def function1():
pass
def function():
pass
"""
code_with_renamed_defs = """
'''docstring'''
# This is a comment
def def_2():
pass
def def_1():
pass
"""
self.assertEqual(rename_defs(code_with_defs), code_with_renamed_defs)
def test_07(self):
code_with_defs = """
from string import digit, ascii_lowercase
from datetime import datetime as dt, date as d, time as t
# This is a comment
def met_01():
print('first function')
def met_20():
print('second function')
def met_03():
print('third function')
met_20()
pass
"""
code_with_renamed_defs = """
from string import digit, ascii_lowercase
from datetime import datetime as dt, date as d, time as t
# This is a comment
def def_1():
print('first function')
def def_3():
print('second function')
def def_2():
print('third function')
def_3()
pass
"""
self.assertEqual(rename_defs(code_with_defs), code_with_renamed_defs)
if __name__ == '__main__':
unittest.main(verbosity=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment