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

Codespace organic spork wr747xw9rv9qc94x9 #14

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
2. **创建ssh key,用于ssh方式克隆github代码**。在linux环境下,使用`ssh-keygen -t rsa -b 4096 -C "你的邮箱"`命令,创建ssh key,下面的选项全部直接敲回车即可。 随后使用` cat ~/.ssh/id_rsa.pub` 命令查看生成的公钥,并完整的复制下来。 在github仓库界面点击自己的头像,选择`settings`。进入到设置页面后,点击左侧的`SSH and GPG keys`选项。点击`New SSH key`选项,并将复制下来的内容粘贴上去,添加该ssh key的描述。随后点击`Add SSH key`,并一路点击确认即可。
3. **本地安装rust**。进入linux环境下,参考Arceos 教程 [Rust 开发环境配置 - ArceOS Tutorial Book (rcore-os.cn)](https://rcore-os.cn/arceos-tutorial-book/ch01-02.html) 中,找到Rust 开发环境配置的章节,相应配置即可,你可以同时将后续需要的环境也配置好.
4. **clone实验仓库到本地**。在前面点击链接生成的仓库中,同样点击醒目的 `code` 绿色按钮,选择`local`下的`ssh`选项,复制下面的链接。随后回到本地linux环境下,使用`git clone 复制的链接`的方式,将目标仓库clone到本地。随后,使用`ls`命令查看自己clone下来的文件夹,再使用`cd`命令进入到该文件夹下,使用 `cargo install --force --path .` 安装rustlings。
5. **练习rustlings**。使用VSCode等编辑器,进入clone下来的目录下的`exercises`文件夹,执行`rustlings watch`依次查看完成情况,并依次完成对应的练习。 执行`rustlings run 练习名称`去运行对应练习,也可以使用`rustlings hint 练习名称`查看题解。
5. **练习rustlings**。使用VSCode等编辑器,进入clone下来的目录中,执行`rustlings watch`依次查看完成情况,并依次完成对应的练习。 执行`rustlings run 练习名称`去运行对应练习,也可以使用`rustlings hint 练习名称`查看题解。
6. **提交完成情况**。当做完部分或所有练习之后,在rustlings目录下执行 `git add .; git commit -m "update"; git push` 命令,把更新提交到GithubClassroom的CI进行自动评测。你可以在github仓库页面的actions分页看到你的CI提交结果,或者训练营官网查看自己的评分。
* 在线环境:

Expand Down
52 changes: 45 additions & 7 deletions exercises/algorithm/algorithm1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
single linked list merge
This problem requires you to merge two ordered singly linked lists into one ordered singly linked list
*/
// I AM NOT DONE


use std::fmt::{self, Display, Formatter};
use std::ptr::NonNull;
Expand All @@ -29,13 +29,13 @@ struct LinkedList<T> {
end: Option<NonNull<Node<T>>>,
}

impl<T> Default for LinkedList<T> {
impl<T: std::cmp::PartialOrd> Default for LinkedList<T> {
fn default() -> Self {
Self::new()
}
}

impl<T> LinkedList<T> {
impl<T: std::cmp::PartialOrd> LinkedList<T> {
pub fn new() -> Self {
Self {
length: 0,
Expand Down Expand Up @@ -71,13 +71,51 @@ impl<T> LinkedList<T> {
}
pub fn merge(list_a:LinkedList<T>,list_b:LinkedList<T>) -> Self
{
//TODO
let mut node_a=list_a.start;
let mut node_b=list_b.start;
let mut list_temp=Self::merge_two_lists(node_a,node_b);
let mut length = 0;
let mut current = list_temp.as_ref().cloned();
let mut tail = None;

while let Some(node_ptr) = current.take() {
tail = Some(node_ptr.clone()); // Update tail to the current node
current = unsafe { (*node_ptr.as_ptr()).next.as_ref() }.cloned(); // Move to the next node
length += 1;
} ;
Self {
length: 0,
start: None,
end: None,
length: length,
start: list_temp,
end: tail,
}
}

pub fn merge_two_lists(l1: Option<NonNull<Node<T>>>, l2: Option<NonNull<Node<T>>>, ) -> Option<NonNull<Node<T>>> {
match (l1, l2) {
(None, None) => None,
(None, Some(mut r)) => Some( r),
(Some(mut l), None) => Some( l),
(Some(mut l), Some(mut r)) => {
unsafe{
if l.as_ref().val <= r.as_ref().val {
let next = Self::merge_two_lists(l.as_ref().next, Some(r));
unsafe {
let mut l_ptr = l.as_mut();
l_ptr.next = next;
Some(NonNull::new_unchecked(l_ptr))
}
} else {
let next = Self::merge_two_lists(Some(l), r.as_ref().next);
unsafe {
let mut r_ptr = r.as_mut();
r_ptr.next = next;
Some(NonNull::new_unchecked(r_ptr))
}
}
}
}
}
}
}

impl<T> Display for LinkedList<T>
Expand Down
36 changes: 31 additions & 5 deletions exercises/algorithm/algorithm10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
graph
This problem requires you to implement a basic graph functio
*/
// I AM NOT DONE


use std::collections::{HashMap, HashSet};
use std::fmt;
Expand All @@ -29,19 +29,45 @@ impl Graph for UndirectedGraph {
&self.adjacency_table
}
fn add_edge(&mut self, edge: (&str, &str, i32)) {
//TODO
self.add_node(edge.0);
self.add_node(edge.1);

self.adjacency_table
.entry(edge.0.to_string())
.and_modify(|e| {
e.push((edge.1.to_string(), edge.2));
});
self.adjacency_table
.entry(edge.1.to_string())
.and_modify(|e| {
e.push((edge.0.to_string(), edge.2));
});

}
}
pub trait Graph {
fn new() -> Self;
fn adjacency_table_mutable(&mut self) -> &mut HashMap<String, Vec<(String, i32)>>;
fn adjacency_table(&self) -> &HashMap<String, Vec<(String, i32)>>;
fn add_node(&mut self, node: &str) -> bool {
//TODO
true
match self.adjacency_table().get(node) {
None => {
self.adjacency_table_mutable()
.insert((*node).to_string(), Vec::new());
true
}
_ => false,
}
}
fn add_edge(&mut self, edge: (&str, &str, i32)) {
//TODO
self.add_node(edge.0);
self.add_node(edge.1);

self.adjacency_table_mutable()
.entry(edge.0.to_string())
.and_modify(|e| {
e.push((edge.1.to_string(), edge.2));
});
}
fn contains(&self, node: &str) -> bool {
self.adjacency_table().get(node).is_some()
Expand Down
46 changes: 43 additions & 3 deletions exercises/algorithm/algorithm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
double linked list reverse
This problem requires you to reverse a doubly linked list
*/
// I AM NOT DONE


use std::fmt::{self, Display, Formatter};
use std::ptr::NonNull;
Expand Down Expand Up @@ -73,8 +73,48 @@ impl<T> LinkedList<T> {
}
}
pub fn reverse(&mut self){
// TODO
}
let mut current = self.start;
let mut next = None;
let mut prev = None;

while let Some(current_ptr) = current {
let current_node_ptr = current_ptr.as_ptr();

// 保存下一个节点的指针
next = unsafe { (*current_node_ptr).next };

// 交换 prev 和 next 指针
unsafe {
(*current_node_ptr).next = prev;
(*current_node_ptr).prev = next;
}

// 移动到下一个节点
prev = Some(current_ptr);
current = next;
}

// 更新链表的头和尾指针
self.start = prev;
if let Some(end_ptr) = self.start {
// 最后一个节点的 next 应该是 None
if let Some(last_next) = unsafe { (*end_ptr.as_ptr()).prev } {
unsafe {
(*last_next.as_ptr()).next = None;
}
}
}
self.end = if let Some(start_ptr) = self.start {
if let Some(start_prev) = unsafe { (*start_ptr.as_ptr()).prev } {
Some(start_prev)
} else {
None // 如果链表只有一个节点,start_prev 应该是 None
}
} else {
None // 链表为空
};
}

}

impl<T> Display for LinkedList<T>
Expand Down
12 changes: 10 additions & 2 deletions exercises/algorithm/algorithm3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
This problem requires you to implement a sorting algorithm
you can use bubble sorting, insertion sorting, heap sorting, etc.
*/
// I AM NOT DONE

fn sort<T>(array: &mut [T]){

fn sort<T: std::cmp::PartialOrd + Clone>(array: &mut [T]){
//TODO
let n=array.len();
for i in 0..(n-1) {
for j in 1..(n-i) {
if array[j-1] > array[j] {
array.swap(j-1, j);
}
}
}
}
#[cfg(test)]
mod tests {
Expand Down
44 changes: 36 additions & 8 deletions exercises/algorithm/algorithm4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This problem requires you to implement a basic interface for a binary tree
*/

//I AM NOT DONE

use std::cmp::Ordering;
use std::fmt::Debug;

Expand Down Expand Up @@ -50,13 +50,26 @@ where

// Insert a value into the BST
fn insert(&mut self, value: T) {
//TODO
if let Some(ref mut root)=self.root{
root.insert(value);
}else{
self.root = Some(Box::new(TreeNode::new(value)));
}
}

// Search for a value in the BST
fn search(&self, value: T) -> bool {
//TODO
true
fn serach_node<T:Ord>(node:&Option<Box<TreeNode<T>>>,value:T)->bool{
match node{
Some(ref n)=>match value.cmp(&n.value){
Ordering::Less=>serach_node(&n.left,value),
Ordering::Greater=>serach_node(&n.right,value),
Ordering::Equal=>true,
},
None=>false,
}
}
serach_node(&self.root,value)
}
}

Expand All @@ -66,7 +79,24 @@ where
{
// Insert a node into the tree
fn insert(&mut self, value: T) {
//TODO
match value.cmp(&self.value){
Ordering::Less=>{
if let Some(ref mut left)=self.left{
left.insert(value);
}else{
self.left=Some(Box::new(TreeNode::new(value)));
}
}
Ordering::Greater=>{
if let Some(ref mut right)=self.right{
right.insert(value);
}else{
self.right=Some(Box::new(TreeNode::new(value)));
}
}
Ordering::Equal=>{
}
}
}
}

Expand Down Expand Up @@ -121,6 +151,4 @@ mod tests {
None => panic!("Root should not be None after insertion"),
}
}
}


}
32 changes: 23 additions & 9 deletions exercises/algorithm/algorithm5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
This problem requires you to implement a basic BFS algorithm
*/

//I AM NOT DONE
use std::collections::VecDeque;

use std::collections::VecDeque;
use std::collections::HashSet;
// Define a graph
struct Graph {
adj: Vec<Vec<usize>>,
Expand All @@ -26,13 +26,27 @@ impl Graph {
}

// Perform a breadth-first search on the graph, return the order of visited nodes
fn bfs_with_return(&self, start: usize) -> Vec<usize> {

//TODO

let mut visit_order = vec![];
visit_order
}
fn bfs_with_return(&self, start: usize) -> Vec<usize> {
let mut visit_order = vec![];
let mut queue = VecDeque::new();
let mut visited = HashSet::new();

queue.push_back(start);
visited.insert(start);

while let Some(node) = queue.pop_front() {
visit_order.push(node);

for &neighbor in &self.adj[node] {
if !visited.contains(&neighbor) {
queue.push_back(neighbor);
visited.insert(neighbor);
}
}
}

visit_order
}
}


Expand Down
14 changes: 11 additions & 3 deletions exercises/algorithm/algorithm6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
This problem requires you to implement a basic DFS traversal
*/

// I AM NOT DONE

use std::collections::VecDeque;
use std::collections::HashSet;

struct Graph {
Expand All @@ -22,8 +23,15 @@ impl Graph {
self.adj[dest].push(src);
}

fn dfs_util(&self, v: usize, visited: &mut HashSet<usize>, visit_order: &mut Vec<usize>) {
//TODO
fn dfs_util(&self, v: usize, visited: &mut HashSet<usize>, visit_order: &mut Vec<usize>) {
visited.insert(v);
visit_order.push(v);

for &neighbor in &self.adj[v] {
if !visited.contains(&neighbor) {
self.dfs_util(neighbor, visited, visit_order);
}
}
}

// Perform a depth-first search on the graph, return the order of visited nodes
Expand Down
Loading