Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - cameyo

#151
Thanks fdb!!!

I use notepad++, but i'd like to use VSCode on mac :-)

I'll try it as soon as possible.

It's possible to execute the current line on newLISP REPL?

It's possible to execute the active selection on newLISP REPL?

Thanks again.

cameyo
#152
Hi rickyboy and ralph.ronnquist,

you're very friendly and competent.

And yes, newLISP is fun.

Have a nice day.

cameyo
#153
Hi rickyboy,

the sublist must contains only contiguous elements.

But your functions are useful anyway :-)
#154
I need more time to understand all this...
#155
Maximum Product Sublist

Given a list that contains both positive and negative integers, find the product of the maximum product sublist.
(define (maxProd lst)
  (local (n maxprod pos neg)
    (setq n (length lst))
    (setq pos (array n '(0)))
    (setq neg (array n '(0)))
    (setq (pos 0) (lst 0)) ; pos[i] contains the positive product up to lst[i]
    (setq (neg 0) (lst 0)) ; neg[i] contains the negative product up to lst[i]
    (setq maxprod (lst 0))
    (for (i 0 (- n 1))
      ; the maximum of the three values
      (setq (pos i) (max (max (* (pos (- i 1)) (lst i)) (* (neg (- i 1)) (lst i))) (lst i)))
      ; the minimum of the three values
      (setq (neg i) (min (min (* (pos (- i 1)) (lst i)) (* (neg (- i 1)) (lst i))) (lst i)))
      (setq maxprod (max maxprod (pos i)))
    )
    maxprod
  )
)

(setq lst '(6 -3 -10 0 2))
(maxProd lst)
;-> 180
Sublist: (6 -3 -10)

(setq lst '(-1 -3 -10 0 60))
(maxProd lst)
;-> 60
Sublist: (60)

(setq lst '(-2 -3 0 -2 -40))
(maxProd lst)
;-> 80
Sublist: (-2 -40)

(setq lst '(-1 -2 -3))
(maxProd lst)
;-> 6

(setq lst '(0 -1))
(maxProd lst)
;-> 0

(setq lst '(0 0 0 0))
(maxProd lst)
;-> 0

Please, post your version :-)

cameyo



p.s. The "Maximum Sum sublist" problem is solved with the Kadane algorithm
#156
Thanks to all. I have learned a lot.
#157
damn :-)
#158
I use this function to group the elements of a list:
(define (take n lst) (slice lst 0 n))
(define (drop n lst) (slice lst n))

(define (group-by-num n lst)
   (if (null? lst) '()
      (cons (take n lst) (group-by-num n (drop n lst)))
   )
)

(setq lst '(0 1 2 3 4 5 6 7 8 9))

(group-by-num 2 lst)
;-> ((0 1) (2 3) (4 5) (6 7) (8 9))

(group-by-num 3 lst)
;-> ((0 1 2) (3 4 5) (6 7 8) (9))

(setq lst '(1 2 3 4 5 6 7 8 9 10 11 12))

(group-by-num 2 (group-by-num 2 lst))
;-> (((1 2) (3 4)) ((5 6) (7 8)) ((9 10) (11 12)))

Please, post your version.
#159
Wow!!!

Thanks fdb
#160
The magic of "map" :-)
#161
I use the following function to calculate the maximum nesting deep of a list:
(define (nesting lst)
  (cond ((null? lst) 0)
        ((atom? lst) 0)
        (true (max (+ 1 (nesting (first lst)))
                   (nesting (rest lst))))
  )
)

(nesting '(a (((b c (d)))) (e) ((f)) g))
;-> 5

Is there a better/faster way to do this?

Thanks.
#162
Grazie mille

Thank you so much
#163
Appunti sul linguaggio newLISP

00) Indice

01) newLISP in generale

02) Funzioni varie (44)

03) newLISP 99 problemi (28)

04) Rosetta code (26)

05) Project eulero (50)

06) Problemi vari (53)

07) Domande per assunzione di programmatori (28)

08) Librerie (4)

09) Appendici
#164
Thanks Lutz.

newLISP anywhere :-)
#165
Thanks Lutz.
> (setq a 1)
;-> 1
> (context 'A1)
;-> A1
A1> (symbols)
;-> ()
A1> a
;-> nil
A1> (symbols)
;-> (a)