Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang scheme
- ;1 c прошлой лабы
- (define (splitting lst n r1 r2 r3 l1 l2 l3)
- (if (and (= n l1) (= n l2) (= n l3)) (list r1 r2 r3)
- ( cond((and (<(length r1)n) (= 0 (remainder (car lst) 3)))
- (splitting (cdr lst) n (cons (car lst) r1) r2 r3 (+ 1 l1) l2 l3))
- ((and (< (length r2) n) (= 1 (remainder (car lst) 3)))
- (splitting (cdr lst) n r1 (cons (car lst) r2) r3 l1 (+ 1 l2) l3))
- ( (and (<(length r3) n) (= 2 (remainder (car lst) 3)))
- (splitting (cdr lst) n r1 r2 (cons (car lst) r3) l1 l2 (+ 1 l3)))
- (else (splitting (cdr lst) n r1 r2 r3 l1 l2 l3)))))
- (define (main lst N)
- (define (threelist lst res)
- (if (empty? (car lst)) res
- (threelist (list(cdar lst) (cdadr lst) (cdaddr lst)) (cons (list(caar lst) (caadr lst) (caaddr lst)) res))))
- (threelist (splitting lst N '() '() '() 0 0 0) '()))
- ;3 задача c прошлой
- (define (Maxtree tree)
- (if (empty? tree) -999999000000
- (max (car tree) ( max (Maxtree (cadr tree)) (Maxtree (caddr tree))))))
- (define (CountMax tree)
- (define Max (Maxtree tree))
- (define (FunctionMax tree1 )
- (if (empty? tree1) 0
- (+ (if (= (car tree1) Max) 1 0)
- (FunctionMax (cadr tree1)) (FunctionMax (caddr tree1)))))
- (FunctionMax tree))
- ;5 задача с прошлой
- (define (KidsTree tree)
- (if (empty? tree) #t
- (cond ((or (and (empty? (cadr tree)) (not (empty? (caddr tree)))) (and (not (empty? (cadr tree))) (empty? (caddr tree))))#f )
- ((and (empty? (cadr tree)) (empty? (caddr tree))) #t)
- (else (and (KidsTree (cadr tree)) (KidsTree (caddr tree)))) )))
- ; 3 pflfxf
- (define (LeftTree tree)
- (define (lefttree tree )
- (if (empty? tree) 0
- (+ (if (not (empty? (cadr tree))) 1 0)
- (lefttree (cadr tree)) (lefttree (caddr tree)))))
- (lefttree tree ))
- ;4 pflfxf
- (define (Minlist tree)
- (if (empty? tree) +inf.0
- (if (and (empty? (cadr tree)) (empty? (caddr tree))) (car tree)
- (min (Minlist (cadr tree) (Minlist (caddr tree)))))))
- (define (left tree)
- (cadr tree))
- (define (right tree)
- (caddr tree))
- (define (symmetric tree)
- (if (empty? tree)
- '()
- (list (car tree)
- (symmetric (right tree))
- (symmetric (left tree)))))
- (define (lefttrees tree)
- (define (help tree i) ;и-кол-во левых потомков
- (cond ;
- ( (null? tree) i ) ; если дерево пусто-то выдает накопленный и
- ( (not(null? (left tree))) (+ (help (right tree) i) (help (left tree) (+ 1 i) ))) ; если не пустое левое поддерево то запускаем две рекурсии,в одной меняем i
- (else (help (right tree) i)))) ;аесли левое пустое ,то вызываем рекури от правого поддерева
- (define (result e) ; вспомогательная функция нужна для умееньшения результата на единицу
- (+ e ;поскольку он считает корень дерева как за левый потомок
- (help tree 0)))
- (result 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement