lambdaway
::
chatGPT
5
|
list
|
login
|
load
|
|
_h1 Chat-GPT {sup (array)} | [[Chat-GPT {sup (list)}|?view=chatGPT2]] _h2 JP _p Please write a python function separate(L) where L is a list of integers. This function returns a list whose elements are increasing or decreasing consecutive sequences of elements of L. For example separate([1,2,3,4,5,3,2,0,0,6,8,1]) should return [[1,2,3,4,5],[3,2,0,0],[6,8],[1]] _h2 1) réponse de Chat-GPT {iframe {@ src="data/separate-gpt.pdf" width="100%" height="500px" style="border:0;"}} _p La suite ici : {a {@ href="data/separate-gpt.pdf"}separate-gpt.pdf}. _h2 2) réponse de λ-talk _p Using the {b array primitives}: {prewrap {A.lib} } {pre '{def separate {def separate.r // récursion : {lambda {:op :a :b} {if {= {A.length :a} 1} // s'il ne reste qu'un élément then {A.addlast! {A.first :a} :b} // retourne :b augmenté du premier élément de :a else {if {:op {A.first {A.rest :a}} {A.first :a}} // sinon si l'ordre change then {A.addlast! {A.first :a} :b} // alors termine et affiche :b augmenté du premier élément de :a {separate {A.rest :a} } // puis recommence sur le reste de :a else {separate.r // sinon recommence avec : :op // le même opérateur {A.rest :a} // sur le reste de :a {A.addlast! {A.first :a} :b} // et :b augmenté du premier élément de :a }}}}} {lambda {:a} // initialisation : {if {= {A.length :a} 1} // s'il ne reste qu'un élément then {A.new {A.first :a}} // alors retourne un tableau le contenant else {separate.r // sinon ouvre la récursion avec : {if {< {A.first :a} {A.first {A.rest :a}}} // si l'ordre est croissant then < else >} // alors :op = < sinon :op = > {A.rest :a} // sur le reste du tableau {A.new {A.first :a}}} // avec un nouveau tableau contenant le premier élément }}} -> {def separate {def separate.r {lambda {:op :a :b} {if {= {A.length :a} 1} then {A.addlast! {A.first :a} :b} else {if {:op {A.first {A.rest :a}} {A.first :a}} then {A.addlast! {A.first :a} :b} {separate {A.rest :a} } else {separate.r :op {A.rest :a} {A.addlast! {A.first :a} :b} }}}}} {lambda {:a} {if {= {A.length :a} 1} then {A.new {A.first :a}} else {separate.r {if {<= {A.first :a} {A.first {A.rest :a}}} then < else >} {A.rest :a} {A.new {A.first :a}}} }}} } _p Tests {pre '{separate {A.new 1 2 3 4 5 3 2 0 0 6 8 1}} -> {separate {A.new 1 2 3 4 5 3 2 0 0 6 8 1}} '{separate {A.new 3 2 1 1 7 8 9 1 4 5 6 3 3 3 2}} -> {separate {A.new 3 2 1 1 7 8 9 1 4 5 6 3 3 3 2}} '{separate {A.new {S.serie 1 20 2} {S.serie 10 1 -2} 1 2}} -> {separate {A.new {S.serie 1 20 2} {S.serie 10 1 -2} 1 2}} '{separate {A.new 1 2 3 4 3 2 1 2 3 4 3 2 1 2 3 4}} -> {separate {A.new 1 2 3 4 3 2 1 2 3 4 3 2 1 2 3 4}} } _p Complément JP : {pre (monotonies '(1 2 3 4 3 2 2 6 7 8 6 4 2 8 10)) ((1 2 3) (4 3) (2 2 6 7) (8 6 4) (2 8 10)) ; et non ((1 2 3 4) (3 2 2.... '{separate {A.new 1 2 3 4 3 2 2 6 7 8 6 4 2 8 10}} -> {separate {A.new 1 2 3 4 3 2 2 6 7 8 6 4 2 8 10}} } _p Voir aussi la version liste, [[chatGPT2]], encore plus simple. _p {i am | 2023/04/10-12} _h2 post-scriptum _p A comparer donc au code python final produit par Chat-GPT : {pre def separate(L): result = [] sub_seq = [L[0]] increasing = None for i in range(1, len(L)): if L[i] == L[i-1] or (increasing is not None and (L[i] == sub_seq[-1] + increasing)): sub_seq.append(L[i]) increasing = L[i] > sub_seq[0] else: result.append(sub_seq) sub_seq = [L[i]] increasing = None result.append(sub_seq) final_result = [] for seq in result: if len(seq) == 1: final_result.append(seq) else: temp = [seq[0]] for i in range(1, len(seq)): if (seq[i] > seq[i-1]) != (temp[-1] > temp[0]): final_result.append(temp) temp = [] temp.append(seq[i]) final_result.append(temp) return final_result } _p Ainsi qu'au code produit par une IH (Intelligence Humaine), JP, prof d'université. {pre 1) Version récursive : (defun croissantep (L) (if (apply '<= L) #'<= #'>=)) (defun monotonies_v1 (L) ; L non vide, récursif enveloppé (if (null (cdr L)) (list L) (let* ((x (car L)) (LM (monotonies_v1 (cdr L))) (M (car LM)) (y (car M))) ; on va prolonger M par x ou créer une nouvelle monotonie réduite à x (if (null (cdr M)) (progn (print "ICI") (cons (list x y) (cdr LM))) (if (funcall (croissantep M) x y) (cons (cons x M) (cdr LM)) (cons (list x) LM)))))) > (monotonies_v1 '(1 2 3 4 3 2 2 6 7 8 6 4 2 8 10)) ; calcul de droite à gauche ! ((1 2 3) (4 3) (2 2 6 7) (8 6 4) (2 8 10)) 2) Version itérative avec un (dolist (x L) ...) : (defun monotonies_v2 (L) ; L non vide, itératif (let* ((M (list (car L))) (LM (list M)) (L (cdr L)) y) (dolist (x L) (setf y (car M)) (if (null (cdr M)) (progn (push x M) (setf LM (cons M (cdr LM)))) (if (funcall (croissantep M) x y) (progn (push x M) (setf LM (cons M (cdr LM)))) (progn (setf M (list x)) (push M LM))))) (reverse (mapcar 'reverse LM)))) > (monotonies_v2 '(1 2 3 4 3 2 2 6 7 8 6 4 2 8 10)) ; calcul de gauche à droite ! ((1 2 3 4) (3 2 2) (6 7 8) (6 4 2) (8 10)) }
lambdaway v.20211111