• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lilyhe123 / coding-practice / #2

20 May 2025 04:45AM UTC coverage: 92.616% (-0.6%) from 93.243%
#2

push

coveralls-python

lilyhe123
fix test failure

2 of 4 new or added lines in 1 file covered. (50.0%)

1643 of 1774 relevant lines covered (92.62%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

8.7
/src/format.py
1
#
2
# Utility to generate code template for a question.
3
#
4
# Read problem description from a file.
5
# The first line of the file is the question number.
6
# Generate an output file with the description included in a block comment.
7
# with each line less than 80.
8
# and create question function and test function.
9
#
10
def format_question(text: str, num: int, limit: int) -> str:
1✔
11
    output = '\n"""\nquestion ' + str(num) + "\n"
×
12
    cur_line = ""
×
13
    for line in text.splitlines():
×
14
        for token in line.split(" "):
×
15
            token = token.strip()
×
16
            if len(token) == 0:
×
17
                continue
×
18
            if len(cur_line) + 1 + len(token) > limit:
×
19
                # need to create a new line, save current line first
20
                output += cur_line + "\n"
×
21
                cur_line = ""
×
22
            if len(cur_line) > 0:
×
23
                cur_line += " "
×
24
            cur_line += token
×
25
        # add left_over
26
        if len(cur_line) > 0:
×
27
            output += cur_line + "\n\n"
×
28
            cur_line = ""
×
29

30
    # add left_over
31
    if len(cur_line) > 0:
×
32
        output += cur_line + "\n\n"
×
33
    output += '-------------------\n\n\n"""\n'
×
34
    output += "def question" + str(num) + "(): pass \n"
×
35
    output += "def test_" + str(num) + "(): pass \n"
×
36
    output += "test_" + str(num) + "() \n"
×
37
    return output
×
38

39

40
def read_and_format(input_file, output_file):
1✔
41
    with open(input_file, "r") as input_obj:
×
42
        lines = input_obj.readlines()
×
43
        text = ""
×
44
        for i, line in enumerate(lines):
×
45
            if i == 0:
×
46
                num = int(line.strip())
×
47
            else:
48
                text += line
×
49

50
    output = format_question(text, num, 80)
×
51
    test_method = "test_" + str(num) + "()"
×
52
    with open(output_file, "a+") as output_obj:
×
53
        output_obj.seek(0)
×
54
        lines = output_obj.readlines()
×
55
        for line in lines:
×
56
            if test_method in line:
×
57
                print(
×
58
                    "The question is already included in ",
59
                    output_file + ".",
60
                    "Do Nothing.",
61
                )
62
                return
×
63
        output_obj.write(output)
×
64

65

66
def generate():
1✔
NEW
67
    read_and_format("src/question.txt", "src/daily.py")
×
68

69

70
if __name__ == "__main__":
1✔
NEW
71
    generate()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc