Skip to content

S3Q2 · Total Size of Image Files

⚡ Quick Reference

Type: File-in, stdout-out

Core idea: parse each line, extract the file extension, sum sizes for .jpg, .jpeg, .png, .gif (case-insensitive).

import tempfile, sys

_, filename = tempfile.mkstemp(prefix="case")
with open(filename, 'w') as f:
    f.write(sys.stdin.read())

img_extensions = {'jpg', 'jpeg', 'png', 'gif'}
total = 0

with open(filename) as f:
    for line in f:
        line = line.strip()
        if not line:
            continue
        size, name = line.split(",", 1)
        ext = name.rsplit(".", 1)[-1].lower()
        if ext in img_extensions:
            total += int(size)

print(total)

Key rules: - Case-insensitive: .JPEG matches - use .lower() - Extension = part after the last . - use rsplit(".", 1)[-1] - Only sum sizes for image extensions - img_extensions is a set for O(1) lookup


Problem Statement

Problem (File I/O → stdout)

Read a comma-separated file of size,filename pairs. Sum the sizes of all files with image extensions (.jpg, .jpeg, .png, .gif), case-insensitively. Print the total.

Example:

Input file
2000,file1.jpg
890,file2.txt
30500,file3.JPEG
12000,file4.png
40000,file5.gif
490,file6.docx
Output
84500

Tracing the example

Line size name ext (lowered) Image? Added
2000,file1.jpg 2000 file1.jpg jpg 2000
890,file2.txt 890 file2.txt txt -
30500,file3.JPEG 30500 file3.JPEG jpeg 30500
12000,file4.png 12000 file4.png png 12000
40000,file5.gif 40000 file5.gif gif 40000
490,file6.docx 490 file6.docx docx -

Total: 2000 + 30500 + 12000 + 40000 = 84500


Solution approaches

import tempfile, sys

_, filename = tempfile.mkstemp(prefix="case")
with open(filename, 'w') as f:
    f.write(sys.stdin.read())

img_extensions = {'jpg', 'jpeg', 'png', 'gif'}
total = 0

with open(filename) as f:
    for line in f:
        line = line.strip()
        if not line:
            continue
        size, name = line.split(",", 1)
        ext = name.rsplit(".", 1)[-1].lower()
        if ext in img_extensions:
            total += int(size)

print(total)
import tempfile, sys

_, filename = tempfile.mkstemp(prefix="case")
with open(filename, 'w') as f:
    f.write(sys.stdin.read())

img_extensions = {'jpg', 'jpeg', 'png', 'gif'}
total = 0

with open(filename) as f:
    for line in f:
        line = line.strip()
        if not line:
            continue
        # Split into size and filename
        parts = line.split(",", 1)
        size  = int(parts[0])
        name  = parts[1]

        # Get extension (after last dot), lowercase
        ext = name.split(".")[-1].lower()

        if ext in img_extensions:
            total += size

print(total)
import tempfile, sys

_, filename = tempfile.mkstemp(prefix="case")
with open(filename, 'w') as f:
    f.write(sys.stdin.read())

img_extensions = {'jpg', 'jpeg', 'png', 'gif'}

def is_image(name):
    return name.rsplit(".", 1)[-1].lower() in img_extensions

with open(filename) as f:
    lines = [l.strip() for l in f if l.strip()]

total = sum(
    int(line.split(",")[0])
    for line in lines
    if is_image(line.split(",", 1)[1])
)

print(total)
import tempfile, sys

_, filename = tempfile.mkstemp(prefix="case")
with open(filename, 'w') as f:
    f.write(sys.stdin.read())

img_extensions = {'jpg', 'jpeg', 'png', 'gif'}

get_ext  = lambda name: name.rsplit(".", 1)[-1].lower()
is_image = lambda line: get_ext(line.split(",", 1)[1]) in img_extensions

with open(filename) as f:
    lines = [l.strip() for l in f if l.strip()]

print(sum(int(l.split(",")[0]) for l in filter(is_image, lines)))

Key takeaways

cat >> /home/claude/python-buddy/docs/oppe2/2025/jan/set3/2025_jan_3_s3q2.md << 'ENDOFFILE'

01

rsplit(".", 1)[-1] - safe extension extraction

name.rsplit(".", 1) splits from the right on the last dot - giving the extension even for filenames like "my.photo.jpg". Using split(".")[-1] also works but rsplit makes the intent clearer.

02

.lower() for case-insensitive matching

Extensions like .JPEG, .Jpg, or .PNG all become lowercase before the set lookup. The img_extensions set contains only lowercase strings, so lowercasing the extracted extension is essential.

03

Set membership check is O(1)

ext in {'jpg','jpeg','png','gif'} - using a set instead of a list gives constant-time lookup regardless of how many extensions are in the set. Idiomatic for membership checks with a fixed collection of values.