Skip to content

Commit

Permalink
refactor the rest
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenad committed Jul 12, 2024
1 parent 895a46f commit c77425d
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ type Position = (i16, i16);

#[generate_trait]
impl PositionImpl of PositionTrait {
fn manhattan(self: @Position) -> i16 {
fn manhattan(self: @Position) -> u16 {
let (x, y) = *self;
x.abs() + y.abs()
}
}

#[generate_trait]
impl AbsImpl of AbsTrait {
fn abs(self: @i16) -> i16 {
if *self < 0 {
fn abs(self: @i16) -> u16 {
let val = if *self < 0 {
*self * -1
} else {
*self
}
};
val.try_into().unwrap()
}
}

Expand Down
11 changes: 0 additions & 11 deletions exercises/concept/low-power-embedded-game/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,5 @@ impl PositionImpl of PositionTrait {
}
}

#[generate_trait]
impl AbsImpl of AbsTrait {
fn abs(self: @i16) -> i16 {
if *self < 0 {
*self * -1
} else {
*self
}
}
}

#[cfg(test)]
mod tests;
19 changes: 9 additions & 10 deletions exercises/concept/low-power-embedded-game/src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,25 @@ mod evens {

mod manhattan {
use core::byte_array::ByteArrayTrait;
use low_power_embedded_game::{Position, PositionTrait, AbsTrait};
use core::fmt::{Debug, Formatter, Error, Display};
use low_power_embedded_game::{Position, PositionTrait};
use core::fmt::{Debug, Formatter, Error};

impl I16Display of Display<i16> {
impl IDebug of Debug<i16> {
fn fmt(self: @i16, ref f: Formatter) -> Result<(), Error> {
let abs_value = self.abs();
if *self < 0 {
f.buffer.append(@"-");
}
let abs_value = if *self < 0 {
*self * -1
} else {
*self
};
let abs_value: u16 = abs_value.try_into().unwrap();
f.buffer.append(@format!("{}", abs_value));
Result::Ok(())
}
}

impl IDebug of Debug<i16> {
fn fmt(self: @i16, ref f: Formatter) -> Result<(), Error> {
I16Display::fmt(self, ref f)
}
}

#[test]
fn origin() {
let p: Position = (0, 0);
Expand Down
50 changes: 26 additions & 24 deletions exercises/practice/protein-translation/.meta/example.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,34 @@ impl CodonsInfoImpl of CodonsInfoTrait {
const TWO_POW_8: u32 = 0x100;
const TWO_POW_16: u32 = 0x10000;

/// This is a helper trait that you may find useful.
///
/// It extracts a codon from a given ByteArray from index `from`.
/// Needs to extract 3 ByteArray characters and convert them to the appropriate
/// felt252 value. It does this by taking the characters' byte value and moving
/// their bits to the left depending on their position in the codon.
///
/// Example:
/// 1. Method call: "AUG".codon_chunk(0)
/// 2. Chars and their byte (hex) values:
/// - "A" = 0x41
/// - "U" = 0x55
/// - "G" = 0x47
/// 3. "A" is the leftmost character, so we "move" it 2 bytes to the left by
/// multiplying it by 2^16 (hex value: 0x10000)
/// 4. "U" is the middle character, so we "move" it 1 byte to the left by
/// multiplying it by 2^8 (hex value: 0x100)
/// 5. "G" is the rightmost character, so we leave it in place
/// 6. Codon = "A" * 2^16 + "U" * 2^8 + "G"
/// = 0x41 * 0x10000 + 0x55 * 0x100 * 0x47
/// = 0x415547
/// 7. (41)(55)(47) are hex values for (A)(U)(G)
///
/// Returns:
/// - Option::Some(codon) -> if the extraction was successful
/// - Option::None -> if the ByteArray was too short from the given index
#[generate_trait]
impl CodonChunk of CodonChunkTrait {
/// Given a ByteArray, extracts a codon from a given index `from`.
/// Needs to extract 3 ByteArray characters and convert them to the appropriate
/// felt252 value. It does this by taking the characters' byte value and moving
/// their bits to the left depending on their position in the codon.
///
/// Example:
/// 1. Method call: "AUG".codon_chunk(0)
/// 2. Chars and their byte (hex) values:
/// - "A" = 0x41
/// - "U" = 0x55
/// - "G" = 0x47
/// 3. "A" is the leftmost character, so we "move" it 2 bytes to the left by
/// multiplying it by 2^16 (hex value: 0x10000)
/// 4. "U" is the middle character, so we "move" it 1 byte to the left by
/// multiplying it by 2^8 (hex value: 0x100)
/// 5. "G" is the rightmost character, so we leave it in place
/// 6. Codon = "A" * 2^16 + "U" * 2^8 + "G"
/// = 0x41 * 0x10000 + 0x55 * 0x100 * 0x47
/// = 0x415547
/// 7. (41)(55)(47) are hex values for (A)(U)(G)
///
/// Returns:
/// - Option::Some(codon) -> if the extraction was successful
/// - Option::None -> if the ByteArray was too short from the given index
fn codon_chunk(self: @ByteArray, from: usize) -> Option<felt252> {
if let Option::Some(char) = self.at(from + 2) {
let codon = char.into()
Expand Down
50 changes: 26 additions & 24 deletions exercises/practice/protein-translation/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,34 @@ impl CodonsInfoImpl of CodonsInfoTrait {
const TWO_POW_8: u32 = 0x100;
const TWO_POW_16: u32 = 0x10000;

/// This is a helper trait that you may find useful.
///
/// It extracts a codon from a given ByteArray from index `from`.
/// Needs to extract 3 ByteArray characters and convert them to the appropriate
/// felt252 value. It does this by taking the characters' byte value and moving
/// their bits to the left depending on their position in the codon.
///
/// Example:
/// 1. Method call: "AUG".codon_chunk(0)
/// 2. Chars and their byte (hex) values:
/// - "A" = 0x41
/// - "U" = 0x55
/// - "G" = 0x47
/// 3. "A" is the leftmost character, so we "move" it 2 bytes to the left by
/// multiplying it by 2^16 (hex value: 0x10000)
/// 4. "U" is the middle character, so we "move" it 1 byte to the left by
/// multiplying it by 2^8 (hex value: 0x100)
/// 5. "G" is the rightmost character, so we leave it in place
/// 6. Codon = "A" * 2^16 + "U" * 2^8 + "G"
/// = 0x41 * 0x10000 + 0x55 * 0x100 * 0x47
/// = 0x415547
/// 7. (41)(55)(47) are hex values for (A)(U)(G)
///
/// Returns:
/// - Option::Some(codon) -> if the extraction was successful
/// - Option::None -> if the ByteArray was too short from the given index
#[generate_trait]
impl CodonChunk of CodonChunkTrait {
/// Given a ByteArray, extracts a codon from a given index `from`.
/// Needs to extract 3 ByteArray characters and convert them to the appropriate
/// felt252 value. It does this by taking the characters' byte value and moving
/// their bits to the left depending on their position in the codon.
///
/// Example:
/// 1. Method call: "AUG".codon_chunk(0)
/// 2. Chars and their byte (hex) values:
/// - "A" = 0x41
/// - "U" = 0x55
/// - "G" = 0x47
/// 3. "A" is the leftmost character, so we "move" it 2 bytes to the left by
/// multiplying it by 2^16 (hex value: 0x10000)
/// 4. "U" is the middle character, so we "move" it 1 byte to the left by
/// multiplying it by 2^8 (hex value: 0x100)
/// 5. "G" is the rightmost character, so we leave it in place
/// 6. Codon = "A" * 2^16 + "U" * 2^8 + "G"
/// = 0x41 * 0x10000 + 0x55 * 0x100 * 0x47
/// = 0x415547
/// 7. (41)(55)(47) are hex values for (A)(U)(G)
///
/// Returns:
/// - Option::Some(codon) -> if the extraction was successful
/// - Option::None -> if the ByteArray was too short from the given index
fn codon_chunk(self: @ByteArray, from: usize) -> Option<felt252> {
if let Option::Some(char) = self.at(from + 2) {
let codon = char.into()
Expand Down
13 changes: 0 additions & 13 deletions exercises/practice/rational-numbers/.meta/example.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use alexandria_math::gcd_of_n_numbers::gcd_two_numbers;
use alexandria_math::fast_power::fast_power;
use alexandria_math::fast_root::fast_nr_optimize;
use core::fmt::{Debug, Formatter, Error};

#[derive(Drop, Debug, Copy)]
struct Rational {
Expand Down Expand Up @@ -116,18 +115,6 @@ impl RationalPow of RationalPowTrait {
}
}

// Enables printing i128 values in tests.
// Note that this will soon be added to the core library.
impl I128Debug of Debug<i128> {
fn fmt(self: @i128, ref f: Formatter) -> Result<(), Error> {
if *self < 0 {
f.buffer.append(@"-");
};
f.buffer.append(@format!("{}", abs(*self)));
Result::Ok(())
}
}

fn abs(n: i128) -> u128 {
let val = if n < 0 {
n * -1
Expand Down
23 changes: 0 additions & 23 deletions exercises/practice/rational-numbers/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::fmt::{Debug, Formatter, Error};

#[derive(Drop, Debug)]
struct Rational {}

Expand Down Expand Up @@ -68,26 +66,5 @@ impl RationalPow of RationalPowTrait {
}
}

// Enables printing i128 values in tests.
// Note that this will soon be added to the core library.
impl I128Debug of Debug<i128> {
fn fmt(self: @i128, ref f: Formatter) -> Result<(), Error> {
if *self < 0 {
f.buffer.append(@"-");
};
f.buffer.append(@format!("{}", abs(*self)));
Result::Ok(())
}
}

fn abs(n: i128) -> u128 {
let val = if n < 0 {
n * -1
} else {
n
};
val.try_into().unwrap()
}

#[cfg(test)]
mod tests;
22 changes: 21 additions & 1 deletion exercises/practice/rational-numbers/src/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
use rational_numbers::{RationalTrait as Rational, RationalAbsTrait, RationalPowTrait, I128Debug};
use core::traits::TryInto;
use core::fmt::{Debug, Formatter, Error};
use rational_numbers::{RationalTrait as Rational, RationalAbsTrait, RationalPowTrait};

// Enables printing i128 values in tests.
// Note that this will soon be added to the core library.
impl I128Debug of Debug<i128> {
fn fmt(self: @i128, ref f: Formatter) -> Result<(), Error> {
if *self < 0 {
f.buffer.append(@"-");
};
let abs_value = if *self < 0 {
*self * -1
} else {
*self
};
let abs_value: u32 = abs_value.try_into().unwrap();
f.buffer.append(@format!("{}", abs_value));
Result::Ok(())
}
}

// Tests of type: Arithmetic

Expand Down
18 changes: 7 additions & 11 deletions exercises/practice/robot-simulator/src/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::fmt::{Debug, Formatter, Error, Display};
use core::traits::TryInto;
use core::fmt::{Debug, Formatter, Error};
use robot_simulator::{Direction, RobotTrait};

#[test]
Expand Down Expand Up @@ -143,23 +144,18 @@ fn moving_east_and_north() {
assert_eq!(robot_end.direction(), @Direction::North);
}

impl I32Display of Display<i32> {
impl IDebug of Debug<i32> {
fn fmt(self: @i32, ref f: Formatter) -> Result<(), Error> {
if *self < 0 {
f.buffer.append(@"-");
}
let abs_value = if *self < 0 {
*self * -1
} else {
*self
};
if *self < 0 {
f.buffer.append(@"-");
}
let abs_value: u32 = abs_value.try_into().unwrap();
f.buffer.append(@format!("{}", abs_value));
Result::Ok(())
}
}

impl IDebug of Debug<i32> {
fn fmt(self: @i32, ref f: Formatter) -> Result<(), Error> {
I32Display::fmt(self, ref f)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Drop, Clone, PartialEq, Debug)]
#[derive(Drop)]
enum LogLevel {
Info,
Warning,
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/semi-structured-logs/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// various log levels
#[derive(Drop, Clone, PartialEq, Eq, Debug)]
#[derive(Drop)]
enum LogLevel {
Info,
Warning,
Expand Down
9 changes: 2 additions & 7 deletions exercises/practice/sublist/.meta/example.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,15 @@ fn contains<T, +PartialEq<T>>(a: Span<T>, b: Span<T>) -> bool {
}

let mut i = 0;
let starts_with = loop {
loop {
if i == b.len() {
break true;
}
if a[i] != b[i] {
break false;
break contains(a.slice(1, a.len() - 1), b);
}
i += 1;
};
if starts_with {
return true;
}

contains(a.slice(1, a.len() - 1), b)
}

#[cfg(test)]
Expand Down

0 comments on commit c77425d

Please sign in to comment.