File tree Expand file tree Collapse file tree 3 files changed +51
-9
lines changed Expand file tree Collapse file tree 3 files changed +51
-9
lines changed Original file line number Diff line number Diff line change @@ -75,6 +75,10 @@ scripts = 'scripts'
7575leetcode pick 1
7676```
7777
78+ ``` sh
79+ leetcode pick --name " Two Sum"
80+ ```
81+
7882``` sh
7983[1] Two Sum is on the run...
8084
Original file line number Diff line number Diff line change @@ -122,6 +122,19 @@ impl Cache {
122122 Ok ( p)
123123 }
124124
125+ /// Get problem from name
126+ pub fn get_problem_id_from_name ( & self , problem_name : & String ) -> Result < i32 , Error > {
127+ let p: Problem = problems
128+ . filter ( name. eq ( problem_name) )
129+ . first ( & mut self . conn ( ) ?) ?;
130+ if p. category != "algorithms" {
131+ return Err ( Error :: FeatureError (
132+ "Not support database and shell questions for now" . to_string ( ) ,
133+ ) ) ;
134+ }
135+ Ok ( p. fid )
136+ }
137+
125138 /// Get daily problem
126139 pub async fn get_daily_problem_id ( & self ) -> Result < i32 , Error > {
127140 parser:: daily (
Original file line number Diff line number Diff line change @@ -47,6 +47,14 @@ impl Command for PickCommand {
4747 ClapCommand :: new ( "pick" )
4848 . about ( "Pick a problem" )
4949 . visible_alias ( "p" )
50+ . arg (
51+ Arg :: new ( "name" )
52+ . short ( 'n' )
53+ . long ( "name" )
54+ . value_parser ( clap:: value_parser!( String ) )
55+ . help ( "Problem name" )
56+ . num_args ( 1 ) ,
57+ )
5058 . arg (
5159 Arg :: new ( "id" )
5260 . value_parser ( clap:: value_parser!( i32 ) )
@@ -127,15 +135,32 @@ impl Command for PickCommand {
127135 None
128136 } ;
129137
130- let fid = m
131- . get_one :: < i32 > ( "id" )
132- . copied ( )
133- . or ( daily_id)
134- . unwrap_or_else ( || {
135- // Pick random without specify id
136- let problem = & problems[ rand:: thread_rng ( ) . gen_range ( 0 ..problems. len ( ) ) ] ;
137- problem. fid
138- } ) ;
138+ let fid = match m. contains_id ( "name" ) {
139+ //check for name specified
140+ true => {
141+ match m. get_one :: < String > ( "name" ) . map ( |name| name) {
142+ Some ( quesname) => match cache. get_problem_id_from_name ( quesname) {
143+ Ok ( p) => p,
144+ Err ( _) => 1 ,
145+ } ,
146+ None => {
147+ // Pick random without specify id
148+ let problem = & problems[ rand:: thread_rng ( ) . gen_range ( 0 ..problems. len ( ) ) ] ;
149+ problem. fid
150+ }
151+ }
152+ }
153+ false => {
154+ m. get_one :: < i32 > ( "id" )
155+ . copied ( )
156+ . or ( daily_id)
157+ . unwrap_or_else ( || {
158+ // Pick random without specify id
159+ let problem = & problems[ rand:: thread_rng ( ) . gen_range ( 0 ..problems. len ( ) ) ] ;
160+ problem. fid
161+ } )
162+ }
163+ } ;
139164
140165 let r = cache. get_question ( fid) . await ;
141166
You can’t perform that action at this time.
0 commit comments