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

Optimize/frontend #8

Merged
merged 2 commits into from
Oct 14, 2023
Merged
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
6 changes: 6 additions & 0 deletions frontend/run_dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
cd ../runtime
cargo run
cp undeads.json ../frontend/
cd ../frontend
yarn dev
2 changes: 2 additions & 0 deletions frontend/src/App.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
.items-header {
display: flex;
width: 60%;
min-width: 800px;
padding: 1rem 0;
opacity: 0.8;
}
Expand All @@ -43,6 +44,7 @@
display: flex;
flex-wrap: wrap;
width: 60%;
min-width: 800px;
border-top: solid #ffffff12;

.item {
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ interface Item {
title: string;
description: string;
labels: string[];
solutions: string[];
}

// @ts-ignore
const items: Item[] = Undeads.sort((a, b) => a.id - b.id);

const SolutionUrlBase =
"https://github.com/SKTT1Ryze/Valhalla/blob/main/leetcode/src/solutions/";

function App() {
return (
<div className={styles.container}>
Expand Down Expand Up @@ -51,9 +55,11 @@ function App() {
<div className={styles.id}>{item.id}</div>
<div className={styles["problem-title"]}>{item.title}</div>
<div className={styles.solution}>
<a href="https://github.com/SKTT1Ryze/Valhalla">
<FileDoneOutlined />
</a>
{item.solutions.map((solution) => (
<a href={SolutionUrlBase + solution}>
<FileDoneOutlined />
</a>
))}
</div>
<div
className={classNames(styles["problem-difficulty"], {
Expand Down
13 changes: 13 additions & 0 deletions leetcode/src/solutions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ pub mod two_sum;
pub trait Solution: Send + Sync {
fn problem_id(&self) -> usize;
fn name(&self) -> String;
fn location(&self) -> String;
fn test(&self) -> Result<()>;
fn benchmark(&self) -> Result<usize>;
}

#[macro_export]
macro_rules! location {
() => {
return std::path::Path::new(file!())
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.to_string()
};
}
4 changes: 3 additions & 1 deletion leetcode/src/solutions/add_two_numbers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::Solution;
use crate::list::{assert_eq_list, ListNode};

pub struct AddTwoNumbers;

impl Solution for AddTwoNumbers {
Expand All @@ -10,6 +9,9 @@ impl Solution for AddTwoNumbers {
fn problem_id(&self) -> usize {
2
}
fn location(&self) -> String {
crate::location!();
}
fn test(&self) -> anyhow::Result<()> {
let testcases = [
(
Expand Down
6 changes: 6 additions & 0 deletions leetcode/src/solutions/longest_substring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ impl Solution for LongestSubstring {
fn problem_id(&self) -> usize {
3
}
fn location(&self) -> String {
crate::location!();
}
fn test(&self) -> anyhow::Result<()> {
for (input, expect) in TESTCASES {
let output = Self::length_of_longest_substring(input.to_string());
Expand Down Expand Up @@ -66,6 +69,9 @@ impl Solution for LongestSubstringHashMap {
fn problem_id(&self) -> usize {
3
}
fn location(&self) -> String {
crate::location!();
}
fn test(&self) -> anyhow::Result<()> {
for (input, expect) in TESTCASES {
let output = Self::length_of_longest_substring(input.to_string());
Expand Down
3 changes: 3 additions & 0 deletions leetcode/src/solutions/two_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ impl Solution for TwoSum {
fn problem_id(&self) -> usize {
1
}
fn location(&self) -> String {
crate::location!();
}
fn test(&self) -> anyhow::Result<()> {
let testcases = [
(vec![2, 7, 11, 15], 9, [0, 1]),
Expand Down
10 changes: 9 additions & 1 deletion runtime/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::{
sync::{Arc, Mutex},
};

use crate::serder::Undead;

mod container;
mod registration;
mod serder;
Expand Down Expand Up @@ -43,8 +45,14 @@ fn main() -> anyhow::Result<()> {
log::info!("Solution {} test passed!", solution.name());
// TODO: bencmark
}

// serde as json
undeads.push(problem.into());
let mut undead: Undead = problem.into();
undead.solutions = solutions
.into_iter()
.map(|solution| solution.location())
.collect();
undeads.push(undead);
}
}

Expand Down
4 changes: 3 additions & 1 deletion runtime/src/serder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use std::sync::Arc;
use leetcode::problems::Problem;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Default)]
pub struct Undead {
id: usize,
difficulty: String,
topic: String,
title: String,
description: String,
labels: Vec<String>,
pub solutions: Vec<String>,
}

impl From<Arc<dyn Problem>> for Undead {
Expand All @@ -22,6 +23,7 @@ impl From<Arc<dyn Problem>> for Undead {
title: value.title(),
description: value.description(),
labels: value.labels(),
..Default::default()
}
}
}