How do I find the greatest digit in a number? Racket - racket

(define hmaxDigit
(lambda (n b)
(cond
((< n 10) n)
((> n (* 10 b)) (hmaxDigit n (* b 10)))
((< 10 (remainder b))
((> (quotient n b) (remainder n b)) (quotient n b))
((< (quotient n b) (remainder n b)) (remainder n b)))))
This is what I have, but it only works with 2 digit numbers.

If n<10 then n is the_maximum_digit
else the_maximum_digit is the maximum of
the first_digit and
the maximum of the remaining digits.
(define (maximum-digit n)
(cond
[(< n 10) n]
[else (max (first-digit n) (maximum-digit (remaining-digits n)))]))
Details such as first-digit and remaining-digits needs to be filled out.

Related

Make a program that determines the average of its digits from a number

I must make a program that averages the digits of a number, but it is impossible for me, especially the module of a division (remainder).
What I can do?
(define (digitos number)
(cond
[(and (>= number 1) (< number 11)) 1]
[else (+ 1 (digitos (/ number 10)))]))
(define (modulo_ n m)
(cond
[(real? n) (- n (* (floor (/ n m)) m))]
[else (remainder n m)]))
(define (sumatoria number)
(cond
[(equal? number 0) 0]
[else (+ (modulo_ number 10) (sumatoria (/ number 10)))]))
(define (promedio number)
(/ (sumatoria number) (digitos number)))
;(promedio 40) ;3
update:
your question is repeated
How to count number of digits?
Count digits in list Racket
Return the sum of odd digits of a number
Convert number to list of digits
; only consider nature number 0,1,2,3,...
; if you want input Decimal use number->string
(define (cut-right-digit n)
(/ (- n (remainder n 10)) 10))
(define (sum-each-digit n result)
(cond
[(<= 0 n 9)
(+ result n)]
[else
(sum-each-digit (cut-right-digit n) (+ result (remainder n 10)))]))
(define (digits n result)
(cond
[(<= 0 n 9)
result]
[else
(digits (cut-right-digit n) (+ 1 result))]))
(define (avg-digits n)
(/ (sum-each-digit n 0) (digits n 1)))
;;; TEST
(avg-digits 0) ; 0
(avg-digits 1) ; 1
(avg-digits 11111) ; 1
(avg-digits 10203040506789) ; (/ 55 14)
(avg-digits 101001) ; 1/2

How to make fibonacci sequence in racket using abstract list functions

I am trying to write a racket program that computes the sum of the first n terms in a fibonacci sequence without using recursion, and only using abstract list functions (so map, builld-list, foldr, foldl). I can use helper functions.
I'm stuck on how to make a list of the fibonacci numbers without using recursion. I thought I could use a lambda function:
(lambda (lst) (+ (list-ref lst (- (length lst) 1)) (list-ref lst (- (length lst 2)))))
But I am not sure how to generate the input list/how to add this to a function.
Once I have a fibonacci sequence I know I can just use (foldl + (car lst) (cdr lst)) to find the sum.
Could anyone explain to me how to make the fibonacci sequence/give me a hint?
; This is how I figure out
#|
(1 2 3 4 (0 1))
-> (1 2 3 (1 1))
-> (1 2 (1 2))
-> (1 (2 3))
-> (3 5)
|#
(define (fib n)
(cond
[(= n 0) 0]
[(= n 1) 1]
[(> n 1)
(second
(foldr (λ (no-use ls) (list (second ls) (+ (first ls) (second ls))))
'(0 1)
(build-list (- n 1) (λ (x) x))))]))
(fib 10)
(build-list 10 fib)
Upgrade version 2
(define (fib-v2 n)
(first
(foldr (λ (no-use ls) (list (second ls) (+ (first ls) (second ls))))
'(0 1)
(build-list n (λ (x) x)))))
(build-list 10 fib-v2)
fib-seq produces a list of first n fibonacci numbers and fib-sum produces the sum of first n fibonacci numbers.
; Number -> [List-of Number]
(define (fib-seq n)
(cond [(= n 0) '()]
[(= n 1) '(0)]
[else (reverse
(for/fold ([lon '(1 0)]) ([_ (in-range (- n 2))])
(cons (apply + (take lon 2)) lon)))]))
; Number -> Number
(define (fib-sum n)
(if (= n 0) 0 (add1 (apply + (take (fib-seq n) (sub1 n))))))
Note: fib-sum is equivalent to the following recursive versions:
(define (fib0 n)
(if (< n 2) n (+ (fib0 (- n 1)) (fib0 (- n 2)))))
(define (fib1 n)
(let loop ((cnt 0) (a 0) (b 1))
(if (= n cnt) a (loop (+ cnt 1) b (+ a b)))))
(define (fib2 n (a 0) (b 1))
(if (= n 0) 0 (if (< n 2) 1 (+ a (fib2 (- n 1) b (+ a b))))))
Once I have a fibonacci sequence I know I can just use (foldl + (car lst) (cdr lst)) to find the sum.
Note that you don't have to generate an intermediate sequence to find the sum. Consider the (fast) matrix exponentiation solution:
(require math/matrix)
(define (fib3 n)
(matrix-ref (matrix-expt (matrix ([1 1] [1 0])) n) 1 0))
Testing:
(require rackunit)
(check-true
(let* ([l (build-list 20 identity)]
[fl (list fib0 fib1 fib2 fib3 fib-sum)]
[ll (make-list (length fl) l)])
(andmap (λ (x) (equal? (map fib0 l) x))
(map (λ (x y) (map x y)) fl ll))))

Binary search in lisp with higher level function

I am trying to write a (higher order function) which takes a vector and a function and makes a binary search accroding to that function, i.e. if it returns -1, we need to go lower, for 1 -- higher, for 0 we found the right place.
I came up with something like this, but it seems that I did something wrong with passing function as an argument:
(defun bin-search (ls fpred)
(let ((l (length ls))
(x (aref ls (floor (length ls) 2))))
(labels (binsearch (ls fpred l m)
(case (funcall #'fpred (aref ls m))
(-1 (binsearch (ls fpred l (floor (- m l) 2))))
(0 (return-from binsearch m))
(1 (binsearch (ls fpred m (+ m (floor (- m l) 2)))))))
(binsearch ls fpred 0 l))))
Compiler says that variable FPRED is defined but never used. What is wrong?
(defun bin-search (ls fpred)
Please use meaningful names, you have many short names or abbreviations, which is difficult to read. For example, ls makes me think of a list, which could be simply named list, but apparently you are working with vectors, so maybe vec or vector?
(let ((l (length ls))
(x (aref ls (floor (length ls) 2))))
If you want to reuse the length l defined in the same let, you can use a let* instead, and write l instead of the second occurrence of (length ls).
(labels (binsearch (ls fpred l m)
The syntax for labels is a list of bindings (name (<args>) <body>), so you need to add another pair of parentheses, like (labels ((binsearch (<args>) <body>)) ...
Addditionally, you do not need to pass fpred as a parameter, it does not change from one invocation of binsearch to another. You can just refer to bin-search's fpred parameter from inside your local function.
(case (funcall #'fpred (aref ls m))
When you write #'fpred, which is equivalent to (function fpred), you are looking for fpred in the function namespace. Here you want to access the function associated with the variable named fpred, and so you can drop the #' part.
(-1 (binsearch (ls fpred l (floor (- m l) 2))))
When you write (binsearch (ls fpred ...)), that means: call binsearch with one value, obtained by calling function ls with arguments fpred, .... Parentheses are significant, and you need to remove them here.
(0 (return-from binsearch m))
(1 (binsearch (ls fpred m (+ m (floor (- m l) 2)))))))
(binsearch ls fpred 0 l))))
Fixed (supposedly) everything, it works now. Thanks a lot.
(defun bin-search (vec fpred)
(let* ((l (length vec)))
(labels ((binsearch (vec l m)
(case (funcall fpred (aref vec m))
(-1 (binsearch vec l (+ l (floor (- m l) 2))))
(0 (return-from binsearch m))
(1 (binsearch vec m (+ m (floor (- m l) 2)))))))
(binsearch vec 0 (floor l 2)))))
Improved:
let instead of let*
name of the internal function changed
return-from is not needed
applied:
(defun bin-search (vec fpred)
(let ((l (length vec)))
(labels ((bin-search-aux (vec l m)
(case (funcall fpred (aref vec m))
(-1 (bin-search-aux vec l (+ l (floor (- m l) 2))))
( 0 m)
( 1 (bin-search-aux vec m (+ m (floor (- m l) 2)))))))
(bin-search-aux vec 0 (floor l 2)))))
let replaced by &aux arg -> one indentation level less
vec does not need to be passed
applied:
(defun bin-search (vec fpred &aux (l (length vec)))
(labels ((bin-search-aux (l m)
(case (funcall fpred (aref vec m))
(-1 (bin-search-aux l (+ l (floor (- m l) 2))))
( 0 m)
( 1 (bin-search-aux m (+ m (floor (- m l) 2)))))))
(bin-search-aux 0 (floor l 2)))))
Test:
CL-USER > (bin-search #(1 2 3 4 5 6 7 8 9)
(lambda (x)
(if (< x 7) 1 (if (> x 7) -1 0))))
6

Return the sum of odd digits of a number

For class, I have to write a function that takes positive integer n and returns the sum of n’s odd digits in scheme. So far, I have my base case such that if n equals 0 then 0. But I am not sure on how to continue.
(define sumOddDigits
(lambda (n)
(if (= n 0)
0
Test cases:
(sumOddDigits 0) → 0
(sumOddDigits 4) → 0
(sumOddDigits 3) → 3
(sumOddDigits 1984) → 10
You could do it efficiently using one functional loop:
(define (sumOddDigits n)
(let loop ([n n])
(cond [(zero? n) 0]
[else
(let-values ([(q r) (quotient/remainder n 10)])
(+ (if (odd? r) r 0)
(loop q)))])))
One can get list of digits using following function which uses 'named let':
(define (getDigits n)
(let loop ((ol '()) ; start with an empty outlist
(n n))
(let-values (((q r) (quotient/remainder n 10)))
(if (= q 0) (cons r ol)
(loop (cons r ol) q)))))
Then one can apply a filter using odd? function to get all odd elements of list- and then apply 'apply' function with '+' to add all those elements:
(apply + (filter
(lambda(x)
(odd? x))
digitList))
Together following can be the full function:
(define (addOddDigits N)
(define (getDigits n)
(let loop ((ol '())
(n n))
(let-values (((q r) (quotient/remainder n 10)))
(if (= q 0) (cons r ol)
(loop (cons r ol) q)))))
(define digitList (getDigits N))
(println digitList)
(apply + (filter
(lambda(x)
(odd? x))
digitList)))
Testing:
(addOddDigits 156)
Output:
'(1 5 6)
6
Your basecase is if n < 10. Because you are then on the last digit.
You then need to check if it's odd, and if so return it. Else, return the addition qualifier(0).
If n > 10, you remainder off the first digit, then test it for odd.
If odd, then add it to a recursive call, sending in the quotient of 10(shaves off the digit you just added).
Else, you recursively call add-odds with the quotient of 10, without adding the current digit.
Here it is in a recursive form(Scheme LOVES recursion) :
(define add-odds
(lambda (n)
(if(< n 10)
(if(= (remainder n 2) 1)
n
0)
(if(= (remainder (remainder n 10) 2) 1)
(+ (remainder n 10) (add-odds (quotient n 10)))
(add-odds(quotient n 10))))))
First get a (reversed) list of digits with simple recursive implementation:
(define (list-digits n)
(if (zero? n) '()
(let-values ([(q r) (quotient/remainder n 10)])
(cons r (list-digits q)))))
then filter the odd ones and sum them:
(define (sum-of-odd-digits n)
(apply + (filter odd? (list-digits n))))
Note: (list-digits 0) returns '() but it is ok for later usage.
More accurate list-digits iterative implementation (produce list of digits in right order):
(define (list-digits n)
(define (iter n acc)
(if (zero? n) acc
(let-values ([(q r) (quotient/remainder n 10)])
(iter q (cons r acc)))))
(iter n '()))

Racket - Transform a natural number to a specific base [duplicate]

I want to show the result of my function as a list not as a number.
My result is:
(define lst (list ))
(define (num->base n b)
(if (zero? n)
(append lst (list 0))
(append lst (list (+ (* 10 (num->base (quotient n b) b)) (modulo n b))))))
The next error appears:
expected: number?
given: '(0)
argument position: 2nd
other arguments...:
10
I think you have to rethink this problem. Appending results to a global variable is definitely not the way to go, let's try a different approach via tail recursion:
(define (num->base n b)
(let loop ((n n) (acc '()))
(if (< n b)
(cons n acc)
(loop (quotient n b)
(cons (modulo n b) acc)))))
It works as expected:
(num->base 12345 10)
=> '(1 2 3 4 5)