Skip to content

Commit

Permalink
Merge branch 'master' into ref/string/anagram
Browse files Browse the repository at this point in the history
  • Loading branch information
sozelfist authored Oct 26, 2024
2 parents cff0c5d + 5afbec4 commit 00dbb52
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 52 deletions.
78 changes: 58 additions & 20 deletions src/dynamic_programming/is_subsequence.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,71 @@
// Given two strings str1 and str2, return true if str1 is a subsequence of str2, or false otherwise.
// A subsequence of a string is a new string that is formed from the original string
// by deleting some (can be none) of the characters without disturbing the relative
// positions of the remaining characters.
// (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
pub fn is_subsequence(str1: &str, str2: &str) -> bool {
let mut it1 = 0;
let mut it2 = 0;
//! A module for checking if one string is a subsequence of another string.
//!
//! A subsequence is formed by deleting some (can be none) of the characters
//! from the original string without disturbing the relative positions of the
//! remaining characters. This module provides a function to determine if
//! a given string is a subsequence of another string.
let byte1 = str1.as_bytes();
let byte2 = str2.as_bytes();
/// Checks if `sub` is a subsequence of `main`.
///
/// # Arguments
///
/// * `sub` - A string slice that may be a subsequence.
/// * `main` - A string slice that is checked against.
///
/// # Returns
///
/// Returns `true` if `sub` is a subsequence of `main`, otherwise returns `false`.
pub fn is_subsequence(sub: &str, main: &str) -> bool {
let mut sub_iter = sub.chars().peekable();
let mut main_iter = main.chars();

while it1 < str1.len() && it2 < str2.len() {
if byte1[it1] == byte2[it2] {
it1 += 1;
while let Some(&sub_char) = sub_iter.peek() {
match main_iter.next() {
Some(main_char) if main_char == sub_char => {
sub_iter.next();
}
None => return false,
_ => {}
}

it2 += 1;
}

it1 == str1.len()
true
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test() {
assert!(is_subsequence("abc", "ahbgdc"));
assert!(!is_subsequence("axc", "ahbgdc"));
macro_rules! subsequence_tests {
($($name:ident: $test_case:expr,)*) => {
$(
#[test]
fn $name() {
let (sub, main, expected) = $test_case;
assert_eq!(is_subsequence(sub, main), expected);
}
)*
};
}

subsequence_tests! {
test_empty_subsequence: ("", "ahbgdc", true),
test_empty_strings: ("", "", true),
test_non_empty_sub_empty_main: ("abc", "", false),
test_subsequence_found: ("abc", "ahbgdc", true),
test_subsequence_not_found: ("axc", "ahbgdc", false),
test_longer_sub: ("abcd", "abc", false),
test_single_character_match: ("a", "ahbgdc", true),
test_single_character_not_match: ("x", "ahbgdc", false),
test_subsequence_at_start: ("abc", "abchello", true),
test_subsequence_at_end: ("cde", "abcde", true),
test_same_characters: ("aaa", "aaaaa", true),
test_interspersed_subsequence: ("ace", "abcde", true),
test_different_chars_in_subsequence: ("aceg", "abcdef", false),
test_single_character_in_main_not_match: ("a", "b", false),
test_single_character_in_main_match: ("b", "b", true),
test_subsequence_with_special_chars: ("a1!c", "a1!bcd", true),
test_case_sensitive: ("aBc", "abc", false),
test_subsequence_with_whitespace: ("hello world", "h e l l o w o r l d", true),
}
}
91 changes: 59 additions & 32 deletions src/searching/linear_search.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
use std::cmp::PartialEq;

pub fn linear_search<T: PartialEq>(item: &T, arr: &[T]) -> Option<usize> {
/// Performs a linear search on the given array, returning the index of the first occurrence of the item.
///
/// # Arguments
///
/// * `item` - A reference to the item to search for in the array.
/// * `arr` - A slice of items to search within.
///
/// # Returns
///
/// * `Some(usize)` - The index of the first occurrence of the item, if found.
/// * `None` - If the item is not found in the array.
pub fn linear_search<T: Ord>(item: &T, arr: &[T]) -> Option<usize> {
for (i, data) in arr.iter().enumerate() {
if item == data {
return Some(i);
Expand All @@ -14,36 +23,54 @@ pub fn linear_search<T: PartialEq>(item: &T, arr: &[T]) -> Option<usize> {
mod tests {
use super::*;

#[test]
fn search_strings() {
let index = linear_search(&"a", &["a", "b", "c", "d", "google", "zoo"]);
assert_eq!(index, Some(0));
}

#[test]
fn search_ints() {
let index = linear_search(&4, &[1, 2, 3, 4]);
assert_eq!(index, Some(3));

let index = linear_search(&3, &[1, 2, 3, 4]);
assert_eq!(index, Some(2));

let index = linear_search(&2, &[1, 2, 3, 4]);
assert_eq!(index, Some(1));

let index = linear_search(&1, &[1, 2, 3, 4]);
assert_eq!(index, Some(0));
}

#[test]
fn not_found() {
let index = linear_search(&5, &[1, 2, 3, 4]);
assert_eq!(index, None);
macro_rules! test_cases {
($($name:ident: $tc:expr,)*) => {
$(
#[test]
fn $name() {
let (item, arr, expected) = $tc;
if let Some(expected_index) = expected {
assert_eq!(arr[expected_index], item);
}
assert_eq!(linear_search(&item, arr), expected);
}
)*
}
}

#[test]
fn empty() {
let index = linear_search(&1, &[]);
assert_eq!(index, None);
test_cases! {
empty: ("a", &[] as &[&str], None),
one_item_found: ("a", &["a"], Some(0)),
one_item_not_found: ("b", &["a"], None),
search_strings_asc_start: ("a", &["a", "b", "c", "d", "google", "zoo"], Some(0)),
search_strings_asc_middle: ("google", &["a", "b", "c", "d", "google", "zoo"], Some(4)),
search_strings_asc_last: ("zoo", &["a", "b", "c", "d", "google", "zoo"], Some(5)),
search_strings_asc_not_found: ("x", &["a", "b", "c", "d", "google", "zoo"], None),
search_strings_desc_start: ("zoo", &["zoo", "google", "d", "c", "b", "a"], Some(0)),
search_strings_desc_middle: ("google", &["zoo", "google", "d", "c", "b", "a"], Some(1)),
search_strings_desc_last: ("a", &["zoo", "google", "d", "c", "b", "a"], Some(5)),
search_strings_desc_not_found: ("x", &["zoo", "google", "d", "c", "b", "a"], None),
search_ints_asc_start: (1, &[1, 2, 3, 4], Some(0)),
search_ints_asc_middle: (3, &[1, 2, 3, 4], Some(2)),
search_ints_asc_end: (4, &[1, 2, 3, 4], Some(3)),
search_ints_asc_not_found: (5, &[1, 2, 3, 4], None),
search_ints_desc_start: (4, &[4, 3, 2, 1], Some(0)),
search_ints_desc_middle: (3, &[4, 3, 2, 1], Some(1)),
search_ints_desc_end: (1, &[4, 3, 2, 1], Some(3)),
search_ints_desc_not_found: (5, &[4, 3, 2, 1], None),
with_gaps_0: (0, &[1, 3, 8, 11], None),
with_gaps_1: (1, &[1, 3, 8, 11], Some(0)),
with_gaps_2: (2, &[1, 3, 8, 11], None),
with_gaps_3: (3, &[1, 3, 8, 11], Some(1)),
with_gaps_4: (4, &[1, 3, 8, 10], None),
with_gaps_5: (5, &[1, 3, 8, 10], None),
with_gaps_6: (6, &[1, 3, 8, 10], None),
with_gaps_7: (7, &[1, 3, 8, 11], None),
with_gaps_8: (8, &[1, 3, 8, 11], Some(2)),
with_gaps_9: (9, &[1, 3, 8, 11], None),
with_gaps_10: (10, &[1, 3, 8, 11], None),
with_gaps_11: (11, &[1, 3, 8, 11], Some(3)),
with_gaps_12: (12, &[1, 3, 8, 11], None),
with_gaps_13: (13, &[1, 3, 8, 11], None),
}
}

0 comments on commit 00dbb52

Please sign in to comment.