Skip to content

Commit

Permalink
Merge pull request #6 from BhupalPrajapati/git@github.com-BhupalPraja…
Browse files Browse the repository at this point in the history
…pati/Rust-Full-Code-from-Beginner-to-Advanced.git

Git@GitHub.com bhupal prajapati/rust full code from beginner to advanced.git
  • Loading branch information
BhupalPrajapati authored Aug 6, 2024
2 parents a636a50 + 2b88818 commit 377334e
Show file tree
Hide file tree
Showing 16 changed files with 2,320 additions and 136 deletions.
1,683 changes: 1,575 additions & 108 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions real_life_problem_with_dsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
mysql = "25.0.1"
Binary file not shown.
184 changes: 184 additions & 0 deletions real_life_problem_with_dsa/src/Most_recently_used_product.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// Most Recently used product
// -Description
// A business is interesting in knowing the product that has been purchased more recently by customers

// used
// -Hashmap and Doubly Linkedlist



use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
#[derive(Debug)]
struct Node{
prod_id : i32,
prev:Link,
next: Link,
}
impl Node {
fn new(elem:i32)->Rc<RefCell<Self>>{
Rc::new(RefCell::new(Node{
prod_id:elem,
prev:None,
next:None,
}))
}
}
type Link = Option<Rc<RefCell<Node>>>;

#[derive(Debug,Default)]
struct List{
head:Link,
tail:Link,
}
impl List {
fn new()->List{
List{
head:None,
tail:None,
}
}

// here we want to push back to the element
pub fn push_back(&mut self,elem:i32)->Link{
let new_tail = Node::new(elem);
match self.tail.take() {
Some(old_tail)=>{
old_tail.borrow_mut().next=Some(new_tail.clone());
new_tail.borrow_mut().prev=Some(old_tail);
self.tail=Some(new_tail.clone());
}
None=>{
self.head=Some(new_tail.clone());
self.tail=Some(new_tail.clone());
}

}
self.tail.clone()
}

pub fn remove_front(&mut self)->Option<Link>{
self.head.take().map(|old_head|{
// this statement is gives as new head
match old_head.borrow_mut().next.take() {
Some(new_head)=>{
new_head.borrow_mut().prev.take();
self.head=Some(new_head);
self.head.clone()
}
None=>{
self.tail.take();
None
}
}
})
}

// here we are define the move to tail function for the iterating the node from head to tail

fn move_to_tail(&mut self,node:&Rc<RefCell<Node>>){
// to grep the previous node details to need to the borrow function accoordinds that
// nd access the previous field which contains the previous information

let prev = node.borrow().prev.as_ref().map(|a|Rc::clone(a));

// with the same way we can also grep the next node information
let next = node.borrow().next.as_ref().map(|a|Rc::clone(a));

match (prev,next) {
(None,None) =>{

}
(Some(_),None)=>{

}
(None,Some(next))=>{
node.borrow_mut().next=None;
next.borrow_mut().prev=None;
self.head = Some(next.clone());

// next we uupdate necessary tail of the linked list
let prev_tail = self.tail.as_ref().unwrap();
// next we will update the next pointer of the previous tail
prev_tail.borrow_mut().next = Some(node.clone());
//next set the previous of the node to that of prev_tail
node.borrow_mut().prev = Some(prev_tail.clone());
//finaly update the tail

self.tail=Some(node.clone());
}
(Some(prev), Some(next))=>{
// set next of the node to null
node.borrow_mut().next = None;
prev.borrow_mut().next = Some(next.clone());
next.borrow_mut().prev = Some(prev.clone());

// do the reference of the prev node
let prev_tail = self.tail.as_ref().unwrap();
// prev_tail wii be become the node
prev_tail.borrow_mut().next = Some(node.clone());
node.borrow_mut().prev = Some(prev_tail.clone());
self.tail = Some(node.clone());

}

}

}
}

#[derive(Debug)]
struct MR_Item {
map: HashMap<i32, Rc<RefCell<Node>>>,
item_list: List,
size: i32,
capacity: i32,
}

impl MR_Item {
fn new(capacity: i32) -> Self {
Self {
map: HashMap::new(),
item_list: List::new(),
size: 0,
capacity: capacity,
}
}

fn purchased(&mut self, prod_id: i32) {
if let Some(node) = self.map.get(&prod_id) {
self.item_list.move_to_tail(node);
} else {
if self.size >= self.capacity {
if let Some(prev_head) = self.item_list.remove_front() {
self.map.remove(&prev_head.unwrap().borrow().prod_id);
}
}
let node = self.item_list.push_back(prod_id).unwrap();
self.map.insert(prod_id, node);
self.size += 1;
}
}

fn print(&self) {
let mut traversal = self.item_list.head.clone();
while let Some(temp) = traversal {
print!("{} ", temp.borrow().prod_id);
traversal = temp.borrow().next.clone();
}
println!();
}
}

fn main() {
let mut items_list = MR_Item::new(3);
items_list.purchased(10);
items_list.print();

items_list.purchased(15);
items_list.print();

items_list.purchased(20);
items_list.print();
}
114 changes: 114 additions & 0 deletions real_life_problem_with_dsa/src/efficient_storage_nd_retrieve_word.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Efficient storage and retrieval of words
// consider a business , system want to desgin that involves the search of particular or fetching the word in a efficiently torage

// we are implement this problem by using the trie data structure
// What is the trie data structure?
// Ans = The trie data structure is also known as the digital tree or prefix tree. it is type of k array search tree which is used to for locating specific keys from within a set
// How you used trie data structure in this problem
// Ans = we have trie data structure which have node and parent and child, if we are follow from the node we go them in child .
// structure is :
// root
// / \
// a t
// / | |
// e i n
// / \ \
// s o g
// struct Node{
// children: HashMap<char, Node>,
// is_word: bool,
//}
// there two children
// Root
//Node:children <T,Node> <A,Node> is_word=false

// lets define the data structure
// we use the hashmap for the fast stroge and traversing
// and the vector for storing the word
use std::collections::HashMap;

// next use the driving the neccessary properties for the node
#[derive(Defult, Debug,PartialEq,Eq,Clone)]
struct Node{
children: HashMap<char, Node>,
is_word: bool,
}
// implement a function which is create the new node
impl Node {
// insie the function ww will
fn new() -> Self {
Node {
is_word: false,
children: HashMap::new(),
}
}
}
// lets create a wrapper around the node which will refer to as a dictionary
// we drive the relevent properties
#[derive(Debug,Defult,PartialEq,Eq,Clone)]
// now define the word dictionary struct word
struct WordDictionary{
// it has ainle field of root, which is type Node
root:Node,
}

// we will now implement this dictionary by using implementations block
impl WordDictionary {
// as usual first we define the function creating the new dictionary

fn new() ->Self{

// jst call the Defual function which is makes the call default values,word dictionary only contains single filed of root, whic is node. so that default of node will be assign
// which is comes from the new function of the node
Self::default()
}
// now declare the insert function
// this function takes one word at a time and inserts it into tree
// the input will be immtauble reference to the self and the word that we want to insert inside the function
fn insert(&mut self,word:&String){
// declare a variable current which is initialized from the root.
let mut current = &mut self.root;
// for each character in the word
// for each letter we will insert it into the HashMap as a key and the values part of new Node

for w in word.chars(){
current = current.children.entry(w).or_insert(Node::new());
}
// if we done insertion of word in tree, then need to update the last node in the tree
if !current.is_word{
current.is_word = true;
}
}

// now look the implementation of search function
fn search(&self,word:&String)->bool{
// the input of function will be a reference to self and the word that we want search in the dictionary
let mut current = &self.root;
// iterate through each letter of the word a
for w in word.chars(){
// determine of nodes have some key in the given hashmap of given childern
if current.children.get(&w).is_some(){
// if we found the key then we move to the next node
current = current.children.get(&w).unwrap(); // unwrap used used because the get function return the an Option
// if none of child key equal to letter then we will return a false bcz it means that word is not present in the entire di

}else {
return false; // if the loop return the successfully value without any fails , then we return last processing node in the tree.;
}
}
current.is_word
}
}

fn main() {
let words = vec!["the","a","there","answer","any","by","bye","their","abc"]
.into_iter().map(|x|String::from(x)).collect::<Vec<String>>();

// call function

let mut d = WordDictionary::new();
for i in 0..words.len() {
d.insert(&words[i]);
}
println!("Searching 'there' in the dictionary results:{}",d.search(&"there".to_string()));
}
Binary file not shown.
Loading

0 comments on commit 377334e

Please sign in to comment.