@@ -62,37 +62,42 @@ def generate_table_rows(problems, badge_func):
6262 for slug , number , title , difficulty , tags in problems :
6363 diff_badge = badge_func (difficulty )
6464 tags_str = ", " .join (tags )
65- solution_link = f"https://leetcode.romitsagu.com/solutions/leetcode { number } /"
65+ solution_link = f"https://leetcode.romitsagu.com/solutions/{ number } /"
6666 icon_md = f"[{ LINK_ICON } ]({ solution_link } )"
6767 rows .append (f"| { title } | { icon_md } | { diff_badge } | { tags_str } |" )
6868 return "\n " .join (rows )
6969
7070def update_readme (readme_path , rows_str ):
71- """Update the README table; creates one if not present ."""
71+ """Update the README table; replaces existing table completely, or creates a new one ."""
7272 if not readme_path .exists ():
7373 raise FileNotFoundError (f"{ readme_path } does not exist" )
7474
7575 readme_text = readme_path .read_text ()
76- # Match the full table
76+
77+ # Match full table: header + separator + any number of rows
7778 table_pattern = re .compile (
78- r'(\| Problem \| Solution \| Difficulty \| Tags \s*\|\n\|[-| ]+\|\n)(.*?)(?=\n\| |\Z)' ,
79+ r'(\| Problem \| Solution \| Difficulty \| Tags \s*\|\n' # header
80+ r'\|[-| ]+\|\n)' # separator
81+ r'(?:\|.*\|\n?)*' , # rows
7982 re .DOTALL
8083 )
8184
85+ new_table = (
86+ "| Problem | Solution | Difficulty | Tags |\n "
87+ "|---------|---------|------------|------|\n "
88+ + rows_str + "\n "
89+ )
90+
8291 if table_pattern .search (readme_text ):
83- updated_text = table_pattern .sub (r"\1" + rows_str , readme_text )
92+ updated_text = table_pattern .sub (new_table , readme_text )
8493 print (f"✅ Updated table rows in { readme_path .name } " )
8594 else :
86- new_table = (
87- "| Problem | Solution | Difficulty | Tags |\n "
88- "|---------|---------|------------|------|\n "
89- + rows_str
90- )
9195 updated_text = readme_text .strip () + "\n \n " + new_table
9296 print (f"✅ Added new table in { readme_path .name } " )
9397
9498 readme_path .write_text (updated_text )
9599
100+
96101def main ():
97102 problems = []
98103
0 commit comments