Sujet : Chatbot programming: An example De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram) Groupes :comp.lang.python Date : 22. Apr 2025, 12:14:02 Autres entêtes Organisation : Stefan Ram Message-ID :<code-20250422121253@ram.dialup.fu-berlin.de>
I wrote,
I have a list of 4-tuples of str objects. These 4-tuples are called "rules". A rule is a full-word rule if its component 0 starts with "<" and its component 2 ends with ">". The complexity of a rule is the length of the str object that results from concatenating the first three components of that rule.
Write code to sort this is list as follows: First the full-word rules ordered by decreasing complexity, then all the other rules ordered by decreasing complexity.
, the chatbot replied:
|def is_full_word_rule(rule): | return rule[0].startswith("<") and rule[2].endswith(">") | |def rule_complexity(rule): | return len(rule[0] + rule[1] + rule[2]) | |def custom_sort(rules): | # Separate full-word and other rules | full_word_rules = [r for r in rules if is_full_word_rule(r)] | other_rules = [r for r in rules if not is_full_word_rule(r)] | | # Sort each list by decreasing complexity | full_word_rules.sort(key=rule_complexity, reverse=True) | other_rules.sort(key=rule_complexity, reverse=True) | | # Concatenate the sorted lists | return full_word_rules + other_rules
. The code of the chatbot is better than the code I would have written manually, because it's sweet how he represented my English concepts "full-word rule" and "complexity" by a def each, which I would not have the patience to write myself had I written the code manually! I'd just have inlined the expression in "custom_sort". I also might not have written such nice comments . . .
Note how it helped to make the code and comments meaningful that I created a mini terminology in my prompt.
(Disclaimer: Bryce Adelstein Lelbach [and maybe Frances Buontempo and many others too] already used ChatGPT in 2023 to generate code.)