Chatbot programming: An example

[email protected] (Stefan Ram)
Newsgroups comp.lang.python
Organization Stefan Ram
Message-ID <[email protected]>
  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.)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.