Implement
append
in Scheme, TypeScript, Python, and Kotlin
Here is an implementation of the append
function in Scheme:
(define (append lst1 lst2)
(if (null? lst1)
lst2
(cons (car lst1) (append (cdr lst1) lst2))))
Here is an implementation of the append
function in TypeScript:
function append(list1: any[], list2: any[]): any[] {
return list1.concat(list2);
}
Here is an implementation of the append
function in Python:
def append(lst1, lst2):
return lst1 + lst2
Here is an implementation of the append
function in Kotlin:
fun append(list1: List<Any>, list2: List<Any>): List<Any> {
return list1 + list2
}
It is worth noting that in the Python and Kotlin implementations, the +
operator is used to concatenate the two input lists, while in Scheme and TypeScript the built-in concatenation function is used.
Implement
most-popular
that takes a list of items like[1, 2, 1, 3, 5, 7, 9, 5, 5]
and an argument like the number to return like2
and gives back[5, 1]
in Scheme, Python, TypeScript, Kotlin, and Clojure
Here is an implementation of the most-popular
function in Scheme:
(define (most-popular lst n)
(let ((counter (make-hash-table)))
(for-each (lambda (x) (hash-table-update! counter x add1 0)) lst)
(let ((sorted-items (sort (hash-table->list counter) (lambda (a b) (> (cdr a) (cdr b))))))
(let loop ((n n) (sorted-items sorted-items) (result '()))
(if (or (zero? n) (null? sorted-items))
result
(loop (- n 1) (cdr sorted-items) (cons (car (car sorted-items)) result))))))
Here is an implementation of the most-popular
function in Python:
from collections import Counter
def most_popular(lst, n):
counter = Counter(lst)
return [item[0] for item in counter.most_common(n)]
Here is an implementation of the most-popular
function in TypeScript:
function mostPopular(lst: any[], n: number): any[] {
const counter = new Map<any, number>();
for (const item of lst) {
if (counter.has(item)) {
counter.set(item, counter.get(item) + 1);
} else {
counter.set(item, 1);
}
}
return Array.from(counter.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, n)
.map((item) => item[0]);
}
Here is an implementation of the most-popular
function in Kotlin:
fun mostPopular(lst: List<Any>, n: Int): List<Any> {
val counter = mutableMapOf<Any, Int>()
for (item in lst) {
val count = counter.getOrDefault(item, 0)
counter[item] = count + 1
}
return counter.toList()
.sortedByDescending { (_, value) -> value }
.take(n)
.map { it.first }
}
Here is an implementation of the most-popular
function in Clojure:
(defn most-popular [lst n]
(let [counter (frequencies lst)]
(->> (sort-by val > counter)
(take n)
(map key))))
Please note that in the TypeScript implementation I used Map and in the Kotlin one I used a MutableMap to count the items. In the Python implementation, I used the collections.Counter
library, in the Scheme implementation I used the built-in make-hash-table
and in the clojure one I used frequencies
function to count items.
Implement
(gcd n)
in Scheme, Python, TypeScript, Kotlin, and Clojure
Here is an implementation of the gcd
function in Scheme:
(define (gcd n)
(define (iter m r)
(if (= r 0)
m
(iter r (modulo m r))))
(iter n (modulo n (sub1 n))))
Here is an implementation of the gcd
function in Python:
def gcd(n: int) -> int:
while n != 0:
n, m = m % n, n
return m
Here is an implementation of the gcd
function in TypeScript:
function gcd(n: number): number {
let m = n - 1;
while (m !== 0) {
[n, m] = [m, n % m];
}
return n;
}
Here is an implementation of the gcd
function in Kotlin:
fun gcd(n: Int): Int {
var m = n - 1
while (m != 0) {
val temp = m
m = n % m
n = temp
}
return n
}
Here is an implementation of the gcd
function in Clojure:
(defn gcd [n]
(loop [m (dec n) r (mod n (dec n))]
(if (zero? r)
m
(recur r (mod m r)))))
Please note that in the Scheme and Python implementation, the built-in modulo
and %
operator are used to find the remainder of the division between two numbers and in the TypeScript, Kotlin and Clojure implementation the modulo operator %
is used to find the remainder of the division. Also, note that the gcd
function uses the Euclidean algorithm to compute the greatest common divisor.