Quick summary
Summarize this blog with AI
Use ChatGPT as a source of explanations and feedback while keeping responsibility for the problem definition and tests. A useful learning cycle is: attempt the problem, ask for a limited hint, explain the fix yourself, and test it without relying on the model’s confidence. This lesson works through one Python task from a plausible beginner mistake to a checked result.
The prompts and replies below are illustrative teaching examples, not a transcript of a measured ChatGPT run. Model replies vary. The Python code and expected results were executed on Python 3.12.14; no API key, paid request or real user data is required.
Define the task before asking for help
Write unique_labels for a list of strings. Trim outer whitespace, discard empty labels, compare duplicates with casefold(), and preserve the first remaining label’s spelling and input order. Reject a non-string element. Do not mutate the input. For [" SQL ", "python", "sql", "", "PYTHON", "R"], the required answer is ["SQL", "python", "R"].
These decisions are part of the exercise. A different application might preserve duplicates or require a different identity rule. Test the stated contract instead of asking for “the best” solution with no definition.
Start from an attempt and request one hint
def first_attempt(labels):
return list(set(labels))
labels = [" SQL ", "python", "sql", "", "PYTHON", "R"]
result = first_attempt(labels)
print("empty retained:", "" in result)
print("raw whitespace retained:", " SQL " in result)
print("number of distinct raw strings:", len(result))
Expected output:
empty retained: True
raw whitespace retained: True
number of distinct raw strings: 6
The set removes exact duplicate raw strings, not duplicates under the exercise’s normalization rule. It also does not provide the required first-seen ordering. The demonstration prints stable properties rather than a hash-dependent set order.
Here is my function and the input/output contract above. Do not write the final function yet. Give one hint about why a set of raw strings cannot satisfy the contract, and ask me to predict the result for an empty input.
A useful hint would separate the comparison key from the label kept in the result. Explain that distinction in your own words before asking for code. If a reply solves the entire exercise despite the hint request, return to a smaller question rather than merely copying it.
Test an attractive but incorrect suggestion
Suppose a model suggests sorting normalized values. This fixes some duplicates but changes both output order and retained spelling:
def suggested_but_wrong(labels):
return sorted({label.strip().casefold() for label in labels if label.strip()})
expected = ["SQL", "python", "R"]
actual = suggested_but_wrong(labels)
print(actual)
print("matches contract:", actual == expected)
Expected output:
['python', 'r', 'sql']
matches contract: False
The test requires first-seen order and original trimmed spelling. Your suggestion returned a sorted lowercase list. Identify which requirements it violates, then describe a repair using a separate seen-key set and output list. Keep the answer focused on those requirements.
Report a concrete counterexample instead of asking whether the model is “sure.” A passing example also does not establish correctness for empty input, repeated calls, wrong types or mutation.
Implement the repair and verify the contract
def unique_labels(labels):
result = []
seen = set()
for label in labels:
if not isinstance(label, str):
raise TypeError("labels must contain strings")
trimmed = label.strip()
key = trimmed.casefold()
if trimmed and key not in seen:
seen.add(key)
result.append(trimmed)
return result
before = labels.copy()
assert unique_labels(labels) == ["SQL", "python", "R"]
assert labels == before
assert unique_labels([]) == []
assert unique_labels(["a", "A", " a "]) == ["a"]
assert unique_labels(["Straße", "STRASSE"]) == ["Straße"]
try:
unique_labels(["sql", None])
except TypeError:
print("non-string rejected")
else:
raise AssertionError("missing type rejection")
print("contract checks passed")
Expected output:
non-string rejected
contract checks passed
Case folding is a chosen Unicode comparison policy; it does not establish that two real-world names identify the same person. The helper allocates new containers and preserves the source list. Its retained state grows with the distinct labels, so it is not an unbounded-stream solution.
Check understanding without assistance
Close the conversation and write duplicate_keys under the same trimming, empty-value and type rules. Return each repeated normalized key once, in the order it first becomes a duplicate. The sample above should return ["sql", "python"]; ["A", "a", "A"] should return ["a"]; an empty list should return an empty list. Explain why the output differs from unique_labels before running your tests. If you cannot implement it independently, revisit the key-versus-display distinction.
Share only code and data you are authorized to provide, with credentials and confidential records removed. Ask for explanations that cite the relevant language reference, then verify them. OpenAI’s prompting guidance emphasizes explicit context and evaluation; it does not turn a prompt into a correctness guarantee.
References and next steps
Official ChatGPT prompting guide, OpenAI prompt engineering, String case folding.