# EXECUTE THIS ONE FIRST!
from pathlib import Path
# Constants for the exercises:
WORKING_DIR = Path.cwd()
DATA_DIR = WORKING_DIR.parent / "data"
Fill ____ pieces of the code below. sum_numbers_in_file
function takes a input file path as argument, reads the numbers listed in the input file and returns the sum of those numbers. You can assume that each line contains exactly one numeric value.
def sum_numbers_in_file(input_file):
total = 0
with ____(input_file) as ____:
for line in ____:
____ = line.strip() # Remove potential white space
total += float(____)
return _____
in_file = DATA_DIR / "numbers.txt"
assert sum_numbers_in_file(in_file) == 189.5
Implement find_first_words
function which takes an input file path as argument. The function should find the first word of each line in the file and return these words as a list. If a line is empty, the returned list should contain an empty string for that line.
# Your implementation here
in_file1 = DATA_DIR / "simple_file.txt"
in_file2 = DATA_DIR / "simple_file_with_empty_lines.txt"
expected_file_1 = ["First", "Second", "Third", "And"]
assert find_first_words(in_file1) == expected_file_1
expected_file_2 = ["The", "", "First", "nonsense", "", "Then"]
assert find_first_words(in_file2) == expected_file_2