Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Word Count #304

Merged
merged 65 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
91d8a54
Added introduction for strings
Ephraim-nonso Jul 10, 2024
e90852c
Added blurb for strings
Ephraim-nonso Jul 10, 2024
ac52fe1
Changed to review
Ephraim-nonso Jul 10, 2024
e865f05
One change
Ephraim-nonso Jul 10, 2024
96d9bca
Change wordings for clarity
Ephraim-nonso Jul 10, 2024
52de5af
Enums Data Type
Ephraim-nonso Jul 14, 2024
74f359c
Merge branch 'exercism:main' into main
Ephraim-nonso Jul 14, 2024
c96e3f4
Update:Changes on Enums Data Type
Ephraim-nonso Jul 16, 2024
009cf95
Merge branch 'exercism:main' into main
Ephraim-nonso Jul 16, 2024
8a8a8cf
Update:Changes on Enums Data Type
Ephraim-nonso Jul 16, 2024
978ac07
New:Changes on Enums Data Type
Ephraim-nonso Jul 16, 2024
70ebe7e
Merge branch 'exercism:main' into main
Ephraim-nonso Jul 16, 2024
8a4a4e7
Reorder sentences in Enum Traits section
Jul 16, 2024
e3544cb
Match basics concept
Ephraim-nonso Jul 30, 2024
49d20ee
Merge branch 'main' into main
Ephraim-nonso Jul 30, 2024
321923e
reorder enum & match basics
Ephraim-nonso Jul 30, 2024
89d0d01
resolve enumn clash
Ephraim-nonso Jul 30, 2024
9456bba
Merge branch 'exercism:main' into main
Ephraim-nonso Jul 30, 2024
d031586
amend to requested changes
Ephraim-nonso Aug 1, 2024
2262bd4
Merge remote-tracking branch 'refs/remotes/origin/main'
Ephraim-nonso Aug 1, 2024
d77ac92
minor changes
Ephraim-nonso Aug 2, 2024
100e118
Merge branch 'exercism:main' into main
Ephraim-nonso Aug 2, 2024
98ddc2c
nit: code update
Ephraim-nonso Aug 6, 2024
df51470
Merge branch 'exercism:main' into main
Ephraim-nonso Aug 6, 2024
ecdd0b6
Apply suggestions from code review
Aug 7, 2024
b8adcde
traits concept
Ephraim-nonso Aug 26, 2024
63bddac
Merge remote-tracking branch 'refs/remotes/origin/main'
Ephraim-nonso Aug 26, 2024
b1a4c1f
Merge branch 'main' into main
Ephraim-nonso Aug 26, 2024
066787c
correction to traits concept
Ephraim-nonso Sep 16, 2024
9ac273e
Merge branch 'exercism:main' into main
Ephraim-nonso Sep 16, 2024
0727ecc
MD047 lint: fixed
Ephraim-nonso Sep 17, 2024
69dab2f
few correction on traits
Ephraim-nonso Sep 20, 2024
3b851a6
Merge branch 'exercism:main' into main
Ephraim-nonso Sep 25, 2024
eca178d
wordy exercise
Ephraim-nonso Oct 14, 2024
35e9e18
pass checks
Ephraim-nonso Oct 14, 2024
2b54929
Merge branch 'main' into main
Ephraim-nonso Oct 14, 2024
26ff5d6
Wordy: update fmt
Ephraim-nonso Oct 17, 2024
925e6af
Wordy: update fmt
Ephraim-nonso Oct 17, 2024
008fd0e
wordy: formatted
Ephraim-nonso Oct 17, 2024
281fd43
wordy: formatted
Ephraim-nonso Oct 17, 2024
9f03b7f
wordy: formatted
Ephraim-nonso Oct 17, 2024
df78767
wordy: error handling.
Ephraim-nonso Oct 18, 2024
2470417
Update exercises/practice/wordy/src/lib.cairo
0xNeshi Oct 18, 2024
0c7ed6b
Update difficulty to 4
0xNeshi Oct 18, 2024
3219156
rename max->num in parse_int
0xNeshi Oct 18, 2024
4a876f0
Exercises: Word count
Ephraim-nonso Nov 7, 2024
62f50c7
Exercises: Word count
Ephraim-nonso Nov 7, 2024
75d5220
Merge branch 'main' into main
Ephraim-nonso Nov 7, 2024
deb3b7f
word count:changes requested
Ephraim-nonso Nov 11, 2024
7abdaab
word-count: new changes
Ephraim-nonso Nov 11, 2024
4d8892c
word-count: new changes
Ephraim-nonso Nov 11, 2024
77b5c4e
word-count: new changes
Ephraim-nonso Nov 11, 2024
2b7d363
word-count: new changes
Ephraim-nonso Nov 11, 2024
12fae98
word-count: new changes
Ephraim-nonso Nov 11, 2024
ac4b1ab
word-count: new changes
Ephraim-nonso Nov 11, 2024
44213ae
redesign the split function
Ephraim-nonso Nov 15, 2024
4dd9516
build passes
Ephraim-nonso Nov 16, 2024
8ae1404
Merge branch 'main' into main
Ephraim-nonso Nov 16, 2024
435ff6a
remove dup and update
Ephraim-nonso Nov 17, 2024
05a2367
merge new
Ephraim-nonso Nov 17, 2024
401954a
formatted: new
Ephraim-nonso Nov 17, 2024
cdbd0fe
remove dup
Ephraim-nonso Nov 17, 2024
2ef1886
Merge branch 'exercism:main' into main
Ephraim-nonso Nov 17, 2024
5f31d61
assert_unordered->assert_unordered_eq + verify both are subsets of ea…
0xNeshi Nov 18, 2024
55de0ef
Check if prev. and next are alphanumeric
0xNeshi Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@
"uuid": "5fded933-439a-4faa-bfb6-18ec7b7c8469",
"practices": [],
"prerequisites": [],
"difficulty": 1
"difficulty": 4
},
{
"slug": "knapsack",
Expand Down
105 changes: 53 additions & 52 deletions exercises/practice/word-count/.meta/example.cairo
Original file line number Diff line number Diff line change
@@ -1,27 +1,57 @@
use core::dict::Felt252Dict;

#[derive(Debug, PartialEq, Clone, Drop)]
pub struct WordResult {
pub word: ByteArray,
pub count: u64,
}

// word_count returns the dictionary of frequency of words based on the input phrase.
pub fn word_count(phrase: ByteArray) -> Felt252Dict<u64> {
let mut result: Felt252Dict<u64> = Default::default();
pub fn count_words(phrase: ByteArray) -> Span<WordResult> {
let mut results: Array<WordResult> = ArrayTrait::new();
let words = split_phrase_into_words(phrase);

let mut arr_to_felt: Array<felt252> = ArrayTrait::new();

let mut i = 0;
while i < words.len() {
arr_to_felt.append(bytearray_to_felt252(words[i]));
let mut found = false;

let mut j = 0;
while j < results.len() {
if results[j].word == words[i] {
let updated_result = WordResult {
word: results[j].word.clone(), count: *results[j].count + 1,
};

results = remove_index_from_array(results, j);
results.append(updated_result);
found = true;
break;
}
j += 1;
};

let frequency = result.get(*arr_to_felt[i]);
result.insert(*arr_to_felt[i], frequency + 1);
if !found {
let word_and_count = WordResult { word: words[i].clone(), count: 1 };
results.append(word_and_count);
}

i += 1;
};

result
results.span()
}

fn remove_index_from_array(arr: Array<WordResult>, index: u32) -> Array<WordResult> {
let mut new_arr: Array<WordResult> = ArrayTrait::new();

let mut i = 0;
while i < arr.len() {
if i != index {
new_arr.append(arr[i].clone());
}
i += 1;
};

new_arr
}

// split phrase into words
fn split_phrase_into_words(phrase: ByteArray) -> Array<ByteArray> {
let mut words: Array<ByteArray> = ArrayTrait::new();
let mut current_word = "";
Expand All @@ -32,15 +62,14 @@ fn split_phrase_into_words(phrase: ByteArray) -> Array<ByteArray> {
if is_alphanumeric_or_apostrophe(lower_case) {
current_word.append_byte(lower_case);
} else if current_word.len() > 0 {
current_word = split_apostrophe_from_word(current_word); // all checks happen here
current_word = split_apostrophe_from_word(current_word);
words.append(current_word);
current_word = "";
}

i += 1;
};

// Final word check after the loop
if current_word.len() > 0 {
current_word = split_apostrophe_from_word(current_word);
words.append(current_word);
Expand All @@ -49,27 +78,26 @@ fn split_phrase_into_words(phrase: ByteArray) -> Array<ByteArray> {
words
}

// Trim apostrophes at the start or end or both sides of the current word
fn split_apostrophe_from_word(mut current_word: ByteArray) -> ByteArray {
0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
let mut i = 0;
let mut new_word = "";
while i < current_word.len() {
0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
if current_word[0] == '\'' && current_word[current_word.len() - 1] == '\'' {
if i > 0 && current_word[i] == '\'' && i < current_word.len() - 1 {
new_word.append_byte(get_value(current_word.at(i)));
new_word.append_byte(current_word.at(i).unwrap_or(0));
} else if current_word[i] != '\'' {
new_word.append_byte(get_value(current_word.at(i)));
new_word.append_byte(current_word.at(i).unwrap_or(0));
}
} else if current_word[0] == '\'' {
if i != current_word.len() - 1 {
new_word.append_byte(get_value(current_word.at(i + 1)));
new_word.append_byte(current_word.at(i + 1).unwrap_or(0));
}
} else if current_word[current_word.len() - 1] == '\'' {
if current_word[i] != '\'' {
new_word.append_byte(get_value(current_word.at(i)));
new_word.append_byte(current_word.at(i).unwrap_or(0));
}
} else {
new_word.append_byte(get_value(current_word.at(i)));
new_word.append_byte(current_word.at(i).unwrap_or(0));
}

i += 1;
Expand All @@ -78,41 +106,14 @@ fn split_apostrophe_from_word(mut current_word: ByteArray) -> ByteArray {
new_word
}

// get u8 value from an option
fn get_value(option: Option<u8>) -> u8 {
match option {
Option::Some(value) => value, // Extracts and returns the u8 value
Option::None => {
// Handle None case appropriately; here we return a default of 0
0
}
}
}

// convert byteArray to felt252
fn bytearray_to_felt252(words: @ByteArray) -> felt252 {
let mut result: felt252 = 0;

let mut i = 0;
while i < words.len() {
let byte: u8 = words[i];

result = result * 256 + (byte.into());
i += 1;
};

result
}

fn is_alphanumeric_or_apostrophe(char: u8) -> bool {
// Check if character is alphanumeric or an apostrophe
('0' <= char && char <= '9') || ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') || char == '\''
fn is_alphanumeric_or_apostrophe(ch: u8) -> bool {
('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ch == '\''
}

fn to_lowercase(char: u8) -> u8 {
fn to_lowercase(ch: u8) -> u8 {
// Convert ASCII uppercase letters to lowercase
if 'A' <= char && char <= 'Z' {
char + 32 // Convert to lowercase by ASCII offset
if 'A' <= ch && ch <= 'Z' {
ch + 32 // Convert to lowercase by ASCII offset
} else {
ch
}
Expand Down
7 changes: 7 additions & 0 deletions exercises/practice/word-count/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "word_count"
version = "0.1.0"
edition = "2024_07"

[dev-dependencies]
cairo_test = "2.8.2"
6 changes: 3 additions & 3 deletions exercises/practice/word-count/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub fn word_count(phrase: ByteArray) -> Felt252Dict<u64> {
panic!("implement `word_count`")
}
pub fn count_words(phrase: ByteArray) -> Span<WordResult> {
0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
panic!("implement `count_words`")
}
Loading