Skip to content

Commit

Permalink
new code added
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryptonean committed Sep 22, 2024
1 parent f40cc60 commit 14dce4c
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 4 deletions.
32 changes: 28 additions & 4 deletions validators/factorail.ak
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

fn factorail_1(x: Int) -> Int {
when x is {
0 || 1-> 1
0 | 1 -> 1
_ -> x * factorail_1(x - 1)
}
}
Expand All @@ -16,8 +15,33 @@ test factorial_test() {
}

test factorial_test1() {
let result = factorail_1(3)
expect result == 6
let result = factorail_1(3)
expect result == 6
}

fn max_list(numbers: List<Int>) -> Int {
when numbers is {
[] -> fail
[x] -> x
[x, y] ->
if x > y {
x
} else {
y
}
[x, y, ..] ->
max_list(
[
if x > y {
x
} else {
y
},
],
)
}
}

test max_list_test() {
expect max_list([3, 1]) == 3
}
11 changes: 11 additions & 0 deletions validators/fibonacchi.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn fibonaachi(x:Int) -> Int {
if x == 0 || x == 1{
1
}else {
fibonaachi (x-1) + fibonaachi(x-2)
}
}

test fibonaachi_test () {
fibonaachi(3) == 4
}
26 changes: 26 additions & 0 deletions validators/int.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use aiken/math
use aiken/primitive/int

fn max1(x: Int, y: Int) -> Int {
math.max(x, y)
}

test max_test() {
max1(10, 11) == 11
}

fn min1(x: Int, y: Int) -> Int {
math.min(x, y)
}

test min_test() {
min1(2, 3) == 2
}

fn compare(x:Int, y:Int) {
int.compare(x,y)
}

test compare_test(){
compare(2,3) == Less
}
42 changes: 42 additions & 0 deletions validators/list.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use aiken/collection/list

// adding elem in list
fn add_element(list: List<Int>, elem: Int) -> List<Int> {
list.push(list, elem)
}

test add_element_to_list() {
let initial_list =
[2, 3]
let result = add_element(initial_list, 1)
let expected =
[1, ..[2, 3]]

expect result == expected
}

// concat 2 list
fn concat_lst(x: List<Int>, y: List<Int>) {
list.concat(x, y)
}

test concat_lst_test() {
let left_lst =
[1, 2, 3]
let right_lst =
[4, 5, 6]

let result = concat_lst(left_lst, right_lst)
expect [1, 2, 3, 4, 5, 6] == result
}

fn str(x) {
x
}

test str_test() {
when str("f") is {
"" -> False
x -> True
}
}
133 changes: 133 additions & 0 deletions validators/not_so_basic.ak
Original file line number Diff line number Diff line change
@@ -1 +1,134 @@
use aiken/collection/list
use aiken/option
use aiken/primitive/int

type FixedP =
Pair<Int, Int>

fn fixed_point_division(valorI: Int, interesI: Int, precision: Int) -> FixedP {
let valorFix = valorI * precision_multiplier(precision)
let interesFix = interesI * precision_multiplier(precision)

Pair(
valorFix / interesFix,
valorI % interesI * precision_multiplier(precision) / interesI,
)
}

fn precision_multiplier(precision: Int) -> Int {
when precision is {
0 -> 1
1 -> 10
2 -> 100
3 -> 1000
4 -> 10000
5 -> 100000
6 -> 1000000
_ -> fail
}
}

// FP presentation must be done off-chain
test fixed_point_example_test() {
let fp = fixed_point_division(100, 2000, 6)
let Pair(n, d) = fp
n * 1000000 + d == 50000
}
//


type A {
value: Int
}

type B {
Alpha(Int)
Beta(Int)
}

fn soft_cast(data: Data) -> Int {
if data is A {
data.value
} else if data is Alpha(x): B {
x + 10
} else if data is Beta(y): B {
y + 20
} else {
0 // fallback case
}
}

test test_soft_cast(){
// Test case 1: Data is of type A
let dataA = A( 5 )
soft_cast(dataA) == 5
}

test test_soft_cast_2(){
let dataAlpha = Alpha(5)
soft_cast(dataAlpha) == 5
}

test test_soft_cast_3(){
let dataBeta = Beta(15)
soft_cast(dataBeta) == 35
}


type X {
Value(Int)
Name(String)
}

fn checkX (data:Data){
if data is Value(x):X {
x+10
}
else if data is Name(x):X{
1
}
else{
0
}
}

test checkX_test(){
let dataValue = Value(5)
checkX(dataValue) == 15
}

test checkX_test_1(){
let dataName = Name(@"Fai")
checkX(dataName) == 1
}


// fn sorted1(x, f: fn(x) -> x) {
// x
// |> sort
// |> tail
// }
fn max_num(list: List<Int>, max: Int) -> Int {
let i = option.or_else(list.head(list), 0)
if list == [] {
max
} else if i >= max {
let max = i
max_num(list.drop(list, 1), max)
} else {
max
}
}

fn max_num_n(list: List<Int>) -> Option<Int> {
let len = list.length(list)
list.at(list.sort(list, int.compare), len - 1)
}

test max_test() {
max_num([2, 4, 6, 7, 8, 9, 3, 2], 0) == 9
}

test maxn_test() {
max_num_n([2, 4, 6, 7, 8, 9, 3, 2]) == Some(9)
}
72 changes: 72 additions & 0 deletions validators/palindorme.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use aiken/collection/list
use aiken/primitive/string
use aiken/primitive/bytearray
use aiken/primitive/string

fn string_pa(x)->Bool {
if x == list.reverse(x){
True
}else{
False
}
}

test string_pa_test(){
string_pa(["faizan"])
}


fn palindrom(x:List<Int>)-> Bool {
let reverse =list.reverse(x)
if x == reverse {
True
}else{
False
}

}

test palindrom_test(){
palindrom([1,2,3])
}


fn palindrome(input: ByteArray) -> Bool {
let len = bytearray.length(input)
if bytearray.length(input) <= 1 {
True
} else if bytearray.at(input, 0) == bytearray.at(input, len - 1) {
palindrome(bytearray.slice(input, start: 1, end: len - 2))
} else {
False
}
}

test palindrome_test() {
palindrome("rahar") == True && palindrome("rahat") == False
}


fn reverse_str(x)-> Bool{
let to_byte = string.to_bytearray(x)
let len = bytearray.length(to_byte)
if bytearray.length(to_byte) <= 1{
True
}
else {
bytearray.foldl(to_byte," ",flip(bytearray.push)) == " "
}


// if bytearray.length(to_byte) <= 1 {
// True
// } else if bytearray.at(to_byte, 0) == bytearray.at(to_byte, len - 1) {
// palindrome(bytearray.slice(to_byte, start: 1, end: len - 2))
// } else {
// False
// }
}

test reverse_str_test(){
reverse_str(bytearray.to_string(("faizan"))) == True
}
22 changes: 22 additions & 0 deletions validators/prime_number.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
fn is_prime_recursive(n: Int, i: Int) -> Int {
if i * i > n {
1
} else if n % i == 0 {
0
} else {
is_prime_recursive(n, i + 1)
}
}

fn is_prime(n: Int) -> Int {
if n <= 2 {
0
} else {
is_prime_recursive(n, 2)
}
}

test is_prime_test() {
is_prime(9) == 0

}

0 comments on commit 14dce4c

Please sign in to comment.