Created
August 2, 2024 20:48
-
-
Save faytey/3228816516bdb07c04116c7190616bcd to your computer and use it in GitHub Desktop.
cairo learnings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use core::option::OptionTrait; | |
| use core::traits::TryInto; | |
| use core::array::ArrayTrait; | |
| use core::traits::Into; | |
| use core::fmt::{Display, Formatter, Error}; | |
| fn mul(x: u16, y: u16) -> u32 { | |
| let z: u32 = x.into() * y.into(); | |
| z | |
| } | |
| // ASSIGNMENT | |
| // 1. Create a subtraction and an addition function | |
| // 2. Create a 6 member array, | |
| // 2a. Add all items in the array. | |
| // 2b. Subtract all items in the array. | |
| fn mul_arr() -> u32 { | |
| let mut test: Array<u16> = ArrayTrait::new(); | |
| let a: u16 = 2; | |
| let b: u16 = 3; | |
| let c: u16 = 4; | |
| let d: u16 = 5; | |
| let e: u16 = 6; | |
| test.append(a); | |
| test.append(b); | |
| test.append(c); | |
| test.append(d); | |
| test.append(e); | |
| println!("The test array: {:?}", test); | |
| let mut i = 0; | |
| let mut stored = 0; | |
| let mut new_arr = array![]; | |
| while !test.is_empty() && test.len() > 1 { | |
| println!("The test@i: {:?}", *test.at(i)); | |
| println!("The test@i + 1: {:?}", *test.at(i + 1)); | |
| // println!("The test array: {:?}", test); | |
| stored = mul(*test.at(i), *test.at(i + 1)); | |
| println!("The stored value is: {:?}", stored); | |
| test.pop_front().unwrap(); | |
| test.pop_front().unwrap(); | |
| println!("The test array remains: {:?}", test); | |
| while !test | |
| .is_empty() { | |
| new_arr.append(test.pop_front().unwrap()); | |
| println!("The test array remain: {:?}", test); | |
| println!("The new array contains: {:?}", new_arr); | |
| }; | |
| test.append(stored.try_into().unwrap()); | |
| println!("The test array has: {:?}", test); | |
| while !new_arr | |
| .is_empty() { | |
| test.append(new_arr.pop_front().unwrap()); | |
| println!("The test array remains: {:?}", test); | |
| println!("The new array contains: {:?}", new_arr); | |
| }; | |
| }; | |
| println!("stored value is: {:?}", stored); | |
| stored | |
| } | |
| fn main() { | |
| mul_arr(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`
use core::array::ArrayTrait;
fn multiply() {
}
fn addition() {
let mut add_arry: Array = ArrayTrait::new();
let a = 2_u16;
let b = 3_u16;
let c = 4_u16;
let d = 5_u16;
let e = 6_u16;
}
fn subtraction() {
let mut sub_arry: Array = ArrayTrait::new();
let a = 2_i16;
let b = 3_i16;
let c = 4_i16;
let d = 5_i16;
let e = 6_i16;
}
fn main() {
multiply();
addition();
subtraction();
}`