Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #lang scheme
  2.  
  3. (define (task1 matrix)
  4. (define (countZero lst)
  5. (foldl (lambda (x res)
  6. (if (zero? x)
  7. (+ res 1)
  8. res))
  9. 0 lst))
  10.  
  11. (car (foldl (lambda (row res)
  12. (if (>(countZero row) (cdr res))
  13. (cons row (countZero row))
  14. res))
  15. (cons '() 0) matrix)))
  16.  
  17. (define (task2 n)
  18. (define (cddr lst)
  19. (if (null? lst)
  20. '()
  21. (cdr lst)
  22. ))
  23. (define (builder k)
  24. (append (build-list k (λ(x) 1)) (build-list (- (ceiling (/ n 2)) k) (λ(x) 0))))
  25.  
  26. (define iter (build-list (ceiling (/ n 2)) (λ(x) (+ x 1))))
  27. (define raw_res (foldr (lambda (i res)
  28. (if (even? n)
  29. (cons (append (builder i) (reverse (builder i))) res)
  30. (cons (append (builder i) (cdr (reverse (builder i)))) res)))
  31. '() iter))
  32. (if (even? n)
  33. (append raw_res (reverse raw_res))
  34. (append raw_res (cdr (reverse raw_res))))
  35. )
  36.  
  37. (define (task3 matrix)
  38. (define (maxInCol matrix)
  39. (apply map max matrix))
  40. (define (maxInRow row)
  41. (apply min row))
  42. (define (getSaddlePoints row maxColElems)
  43. (define minRowElems (maxInRow row))
  44. (foldl (lambda [elem maxColElems result]
  45. (if (= elem maxColElems minRowElems)
  46. (cons elem result)
  47. result)) '() row maxColElems))
  48.  
  49. (define maxColElems (maxInCol matrix))
  50. (define result (flatten (map (lambda [row]
  51. (getSaddlePoints row maxColElems)) matrix)))
  52. (if (empty? result)
  53. #f
  54. result)
  55. )
  56.  
  57.  
  58.  
  59.  
  60. (define (task4 matrix)
  61. (define len (length (car matrix)))
  62. (define mainDiag (foldl (lambda (row n diag)
  63. (cons (list-ref row n) diag)) null matrix
  64. (build-list len values)))
  65. (define secondDiag (foldl (lambda (row n diag)
  66. (cons (list-ref (reverse row) n) diag)) null (reverse matrix)
  67. (build-list len values)))
  68.  
  69. (define matrT (apply map list matrix))
  70. (define sum (apply + (car matrix)))
  71. (if (foldl (lambda (row res)
  72. (cond ((eq? res #f) #f)
  73. ((= (apply + row) sum) #t)
  74. (else #f))) #t matrix)
  75. (if (foldl (lambda (col res)
  76. (cond ((eq? res #f) #f)
  77. ((= (apply + col) sum) #t)
  78. (else #f))) #t matrT)
  79. (if (= (apply + mainDiag) sum)
  80. (if (= (apply + secondDiag) sum)
  81. #t
  82. #f)
  83. #f)
  84. #f
  85. )
  86. #f
  87. )
  88. )
  89.  
  90.  
  91. (define (task5 matrix)
  92. (define (trans matrix)
  93. (apply map list matrix))
  94.  
  95. (define matrT (trans matrix))
  96. (foldl (lambda [row result]
  97. (if result
  98. result
  99. (if (ormap (lambda [col]
  100. (equal? row col)) matrT)
  101. row
  102. #f))) #f matrix))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement