write a function that counts the number of times a given int occurs in a linked list code example
Example 1: write a bash script that accepts a text file as argument and calculates number of occurrences of each words in it and return them as key value pairs
file=/home/stefan/ooxml1.txt
for word in $(sed 's/[^A-Za-z]/ /g' $file | tr " " "\n" | sort -u)
do
echo -n "$word "
grep -c $word $file
done | sort -k2 -n
Example 2: Write a function that computes the sum of all proper divisors of a given number lisp
(defun proper-divisors-recursive (product &optional (results '(1))) "(int,list)->list::Function to find all proper divisors of a +ve integer." (defun smallest-divisor (x) "int->int::Find the smallest divisor of an integer > 1." (if (evenp x) 2 (do ((lim (truncate (sqrt x))) (sd 3 (+ sd 2))) ((or (integerp (/ x sd)) (> sd lim)) (if (> sd lim) x sd))))) (defun pd-rec (fac) "(int,int)->nil::Recursive function to find proper divisors of a +ve integer" (when (not (member fac results)) (push fac results) (let ((hifac (/ fac (smallest-divisor fac)))) (pd-rec hifac) (pd-rec (/ product hifac))))) (pd-rec product) (butlast (sort (copy-list results) #'<))) (defun task (method &optional (n 1) (most-pds '(0))) (dotimes (i 19999) (let ((npds (length (funcall method (incf n)))) (hiest (car most-pds))) (when (>= npds hiest) (if (> npds hiest) (setf most-pds (list npds (list n))) (setf most-pds (list npds (cons n (second most-pds)))))))) most-pds) (defun main () (format t "Task 1:Proper Divisors of [1,10]:~%") (dotimes (i 10) (format t "~A:~A~%" (1+ i) (proper-divisors-recursive (1+ i)))) (format t "Task 2:Count & list of numbers <=20,000 with the most Proper Divisors:~%~A~%" (task #'proper-divisors-recursive)))
Example 3: create a function that takes in an array of numbers and returns only the number that are even after 1 is added to the value
const evenAfter = (arr) => {
let i = 0
let newArr = []
while (i <= arr.length) {
if ((arr[i] + 1) % 2 === 0){
newArr.push(arr[i])
}
i++
}
return newArr
}
console.log(evenAfter([3,6,7,8,9,11]))