Skip to content

Tasks All

Alan Heanue edited this page Apr 8, 2018 · 1 revision

Task1

  1. Write, from scratch, a function in Racket that uses a brute-force algorithm that takes a single positive integer and return true if the number is a prime and false otherwise. Call the function decide-prime.

solution

#lang racket

; setting range for a prime num needs to be more than 1 
(define range 2) 
(define (decide-prime? n);called function decide-prime 
  
;setting condition 
   (not   (for/or [(d (in-range range n))]=
   ; check if is fully dividable
      (= 0 (remainder n d))))) 
     
; range to 10 
(for [(m (in-range range 10))]
  (display "\n")(display m)(display "= ")
  ; pass current number to fucntion
  (display (decide-prime? m))) 

Task2

solution

(display "Problem 2 \n")
(define (collatz-list n);define function
  (if (= n 1);check if n = 1
      
      (cons n true)
      
   (cons n(if (integer? (/ n 2))

          (collatz-list (/ n 2));recursion 
          (collatz-list (+ 1 (* n 3)))))
      )
  )

 ;Print out list for testing
(display "\nResults: ")
(collatz-list 5)
(display "\nResults: ")
(collatz-list 9)
(display "\nResults: ")
(collatz-list 2) 

Task3

solution

;create a function for left cycle called lcycle 
(define (lcycle l)
     (if (null? l) ;  list = null
      '() ; output null
      (append (cdr l) (list (car l))))) ;  append it to list

;create 2nd fuction for right cycle called rcycle 
(define (rcycle l)
      (if (null? l) ; if list = null then..
      '() ; output null
      (append (cons (last l)(remove-last l))))) 



; remove-last from list
(define (remove-last l)
  (if (null? (cdr l))
        '() ;output null
        (cons (car l) (remove-last (cdr l))))) ; newly allocated list 
  


; Print out results 
(lcycle(list 1 2 3 4 5))

(rcycle(list 1 2 3 4 5)) 

Task4

solution

(define sublists
  (match-lambda**
   [(0 _)           '(())]
   [(_ '())         '()]
   [(m (cons x xs)) (append (map (curry cons x) (sublists (- m 1) xs)) 
                            (sublists m xs))]))
 
(define (combinations n m)
  (sublists n (range m)))

Task5

solution

#lang racket

(define n 0)

(define (hamming-weight l)
  (if (null? l);if list returns nothing 
   n

   (if (= 1 (car l))(+ 1 (hamming-weight (cdr l)));checking to see if its a one 
      (hamming-weight (cdr l))))); if true add again (recursion)

;display the number of 1 s in list 
(displayln '(Here is the results below :))
(hamming-weight (list 1 0 0 1 1 1 1 1))
(hamming-weight (list 1 0 0 0 0 0 0 1))
(hamming-weight (list 1 1 1 1 1 1 1 1))
(hamming-weight (list 0 1 0 1 0 1 1 0))
(hamming-weight (list 0 0 0 0 0 0 0 0))

Task6

solution

#lang racket

(define n 0)
; define function = 2 lists 
(define (hamming-distance l t)
; if statement to see if not equal to 1  
  (if (not (pair? l))
; returns the difference 
      n 
      ; if the same keep calling 
      (if (= (car l) (car t)) (hamming-distance (cdr l) (cdr t)) 
      ;else we add the distance by 1 
      (+ 1 (hamming-distance (cdr l) (cdr t)))))) 


;print out the distance 
(displayln '(Here is the distance between 2 list))
(hamming-distance (list 1 1 1 1 0 0 0 0) (list  1 0 1 0 1 1 1 0))

Task7

solution

#lang racket
;Alan Heanue

; define function 
(define (maj x y z)
  ;x = null you have reached end of list 
  (if (null? x)
     ; 
      '()
      r y)) (cons (car x) (maj (cdr x) (cdr y) (cdr z))))
        ((= (car x) (car z)) (cons (car x) (maj (cdr x) (cdr y) (cdr z))))
        ;= otherwise 
        (else (cons (car y) (maj (cdr x) (cdr y) (cdr z))))
        )))

;print results
(displayln '(MAJ results :))
(maj (list 0 0 0 0 1 1 1 1) (list 0 0 1 1 0 0 1 1) (list 0 1 0 1 0 1 0 1))

Task8

solution

#lang racket
;Alan Heanue

;define function
(define (chse x y z)
  (if (null? y) ; exit statement if y is null we've reached the end of the list
      
      '() ; return
      ;do condition for the problem y for x whereever 1 is and elements of Z 
      (cond
        ((= 1 (car x)) (cons (car y)(chse (cdr x) (cdr y) (cdr z))))
        (else (cons (car z)(chse (cdr x) (cdr y) (cdr z)))))))


(displayln '(here is outcome for chse))
(chse (list 0 0 0 0 1 1 1 1) (list 0 0 1 1 0 0 1 1) (list 0 1 0 1 0 1 0 1))

Task9

solution

#lang racket

;define function with 3 lists 
(define (sod2 x y z)
    (cond [(null? x) '()] ;checking the list til null
        [(= (modulo (+ (car x)(car y)(car z)) 2) 0) (cons 0 (sod2 (cdr x)(cdr y)(cdr z)))] ; add 
        [else (cons 1 (sod2 (cdr x)(cdr y)(cdr z)))] ; else statement
    )
)

(display "(list 0 0 0 0 1 1 1 1) (list 0 0 1 1 0 0 1 1) (list 0 1 0 1 0 1 0 1) \n  =>  ")
(sod2 (list 0 0 0 0 1 1 1 1) (list 0 0 1 1 0 0 1 1) (list 0 1 0 1 0 1 0 1))

Task10

solution

#lang racket

(define (square x)
  (* x x )
)
   
; lstq function
(define (lstq x y)
  (if(null? x)
  0
  ;https://stackoverflow.com/questions/22560573/how-to-do-square-in-racket
  (+ (square(- (car x) (car y))) (lstq (cdr x) (cdr y)))
  )                                                   
)  


;define lists 
(define x (list 4.5 5.1 6.2 7.8))
(define y (list 1.1 -0.1 6.1 3.8))

;display result
(lstq x y)
Clone this wiki locally