## Problem: Add ### Statement Write a function that returns the sum of two numbers. ### Solutions #### Option 1 ```rust fn add(param1: Int, param2: Int){ param1 + param2 } ``` #### Option 2 ```rust fn add(a: Int, b: Int) -> Int {a + b} ``` --- ## Problem: isLucky ### Statement Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the sum of the first half of the digits is equal to the sum of the second half. Given a ticket number n, determine if it's lucky or not. ### Solutions #### Option 1 ```rust use aiken/list fn isLucky(n) { let digits = int2digits(n) let m = list.length(digits) let left = list.foldl(list.take(digits, m/2), 0, fn(n, total) {n + total}) let right = list.foldl(list.drop(digits, m/2), 0, fn(n, total) {n + total}) left == right } fn int2digits(n) { if (n < 10) { [n] } else { list.concat(int2digits(n/10), [n%10]) } } ``` #### Option 2 ```rust ``` --- ## Problem: alternatingSums ### Statement Several people are standing in a row and need to be divided into two teams. The first person goes into team 1, the second goes into team 2, the third goes into _team 1_again, the fourth into team 2, and so on. You are given an array of positive integers - the weights of the people. Return an array of two integers, where the first element is the total weight of team 1, and the second element is the total weight of _team 2_after the division is complete. ### Solutions #### Option 1 ```rust use aiken/list use aiken/option fn alternatingSums(ar) { let t1 = option.or_else(list.at(ar, 0), 0) let t2 = option.or_else(list.at(ar, 1), 0) if list.length(ar) <= 2 { [t1, t2] } else { let res = alternatingSums(list.drop(ar, 2)) let s1 = option.or_else(list.at(res, 0), 0) let s2 = option.or_else(list.at(res, 1), 0) [t1 + s1, t2 + s2] } } ``` #### Option 2 ```rust ``` --- ## Problem: arrayChange ### Statement You are given an array of integers. On each move you are allowed to increase exactly one of its element by one. Find the minimal number of moves required to obtain a strictly increasing sequence from the input ### Solutions #### Option 1 ```rust use aiken/list use aiken/option fn arrayChange(ar) { if list.length(ar) <= 1 { 0 } else { let t1 = option.or_else(list.at(ar, 0), 0) let t2 = option.or_else(list.at(ar, 1), 0) if t1 < t2 { arrayChange(list.drop(ar, 1)) } else { let inc = t1 + 1 - t2 inc + arrayChange(list.concat([t1+1], list.drop(ar, 2))) } } } ``` #### Option 2 ```rust ``` --- ## Problem: arrayMaximalAdjacentDifference ### Statement Given an array of integers, find the maximal absolute difference between any two of its adjacent elements. ### Solutions #### Option 1 ```rust use aiken/list use aiken/math fn arrayMaximalAdjacentDifference(xs) { let diff = list.map2(xs, list.drop(xs, 1), fn(a, b) { math.abs(a - b) }) list.foldl(diff, 0, fn(n, reduced) { math.max(n, reduced) }) } ``` #### Option 2 ```rust ``` --- ## Problem: alphabeticShift ### Statement Given a string, replace each of its character by the next one in the English alphabet (zwould be replaced by a). ### Solutions #### Option 1 ```rust use aiken/bytearray fn alphabeticShift(s) { bytearray.foldr(s, "", fn(byte, acc) { if byte==122 { bytearray.push(acc, 97)} else {bytearray.push(acc, byte+1)} }) } ``` #### Option 2 ```rust ``` --- ## Problem: digitDegree ### Statement Let's define digit degree of some positive integer as the number of times we need to replace this number with the sum of its digits until we get to a one digit number. Given an integer, find its digit degree. ### Solutions #### Option 1 ```rust fn digitDegree(n) { if n < 10 { 0 } else { 1 + digitDegree(digitSum(n))} } fn digitSum(n) { if n < 10 { n } else {digitSum(n / 10) + n % 10} } ``` #### Option 2 ```rust ``` --- ## buildPalindrome ```rust= use aiken/bytearray use aiken/option fn buildPalindrome(s) { build(s, "") } fn isPalidrome(s) { bytearray.reduce(s, #[], bytearray.push) == s } fn build(s, tail) { let full = bytearray.concat(s, tail) if isPalidrome(full) { full } else { let n = bytearray.length(tail) let newTail = bytearray.concat(bytearray.slice(s, n, n), tail) build(s, newTail) } } ``` --- ## elections_winners ```rust use aiken/list use aiken/math fn elections_winners(xs, k) { let vmax = list.foldl(xs, 0, fn(n, reduced) { math.max(n, reduced) }) if k > 0 { let potentials = list.filter(xs, fn(x) { x + k > vmax }) list.length(potentials) } else { let potentials = list.filter(xs, fn(x) { x >= vmax }) if list.length(potentials) == 1 { 1 } else { 0 } } } ```