Skip to content

Commit 1967666

Browse files
authored
Merge pull request #1 from codecrafters-io/remove-loops-from-starter
Refactor shell starter templates and solutions
2 parents f9efdcf + 48731a8 commit 1967666

File tree

13 files changed

+83
-89
lines changed

13 files changed

+83
-89
lines changed

compiled_starters/go/cmd/myshell/main.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ func main() {
1010
// You can use print statements as follows for debugging, they'll be visible when running tests.
1111
fmt.Println("Logs from your program will appear here!")
1212

13-
reader := bufio.NewReader(os.Stdin)
14-
for {
15-
// Uncomment this block to pass the first stage
16-
// fmt.Fprint(os.Stdout, "$ ")
17-
_, _ = reader.ReadString('\n')
18-
}
13+
// Uncomment this block to pass the first stage
14+
// fmt.Fprint(os.Stdout, "$ ")
15+
16+
// Wait for user input
17+
bufio.NewReader(os.Stdin).ReadString('\n')
1918
}

compiled_starters/python/app/main.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ def main():
55
# You can use print statements as follows for debugging, they'll be visible when running tests.
66
print("Logs from your program will appear here!")
77

8-
while True:
9-
# Uncomment this block to pass the first stage
10-
# sys.stdout.write("$ ")
11-
# sys.stdout.flush()
12-
input()
8+
# Uncomment this block to pass the first stage
9+
# sys.stdout.write("$ ")
10+
# sys.stdout.flush()
11+
12+
# Wait for user input
13+
input()
1314

1415

1516
if __name__ == "__main__":

compiled_starters/rust/src/main.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
#[allow(unused_imports)]
12
use std::io::{self, Write};
23

34
fn main() {
45
// You can use print statements as follows for debugging, they'll be visible when running tests.
56
println!("Logs from your program will appear here!");
67

78
// Uncomment this block to pass the first stage
8-
// let stdin = io::stdin();
9-
// loop {
10-
// print!("$ ");
11-
// io::stdout().flush().unwrap();
12-
// let mut input = String::new();
13-
// stdin.read_line(&mut input).unwrap();
14-
// }
9+
// print!("$ ");
10+
// io::stdout().flush().unwrap();
11+
12+
// Wait for user input
13+
let stdin = io::stdin();
14+
let mut input = String::new();
15+
stdin.read_line(&mut input).unwrap();
1516
}

solutions/go/01-oo8/code/cmd/myshell/main.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import (
77
)
88

99
func main() {
10-
reader := bufio.NewReader(os.Stdin)
11-
for {
12-
fmt.Fprint(os.Stdout, "$ ")
13-
_, _ = reader.ReadString('\n')
14-
}
10+
fmt.Fprint(os.Stdout, "$ ")
11+
12+
// Wait for user input
13+
bufio.NewReader(os.Stdin).ReadString('\n')
1514
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@@ -1,19 +1,15 @@
1+
@@ -1,18 +1,14 @@
22
package main
33

44
import (
@@ -11,11 +11,10 @@
1111
- // You can use print statements as follows for debugging, they'll be visible when running tests.
1212
- fmt.Println("Logs from your program will appear here!")
1313
-
14-
reader := bufio.NewReader(os.Stdin)
15-
for {
16-
- // Uncomment this block to pass the first stage
17-
- // fmt.Fprint(os.Stdout, "$ ")
18-
+ fmt.Fprint(os.Stdout, "$ ")
19-
_, _ = reader.ReadString('\n')
20-
}
14+
- // Uncomment this block to pass the first stage
15+
- // fmt.Fprint(os.Stdout, "$ ")
16+
+ fmt.Fprint(os.Stdout, "$ ")
17+
18+
// Wait for user input
19+
bufio.NewReader(os.Stdin).ReadString('\n')
2120
}

solutions/python/01-oo8/code/app/main.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33

44
def main():
5-
while True:
6-
sys.stdout.write("$ ")
7-
sys.stdout.flush()
8-
input()
5+
sys.stdout.write("$ ")
6+
sys.stdout.flush()
7+
8+
# Wait for user input
9+
input()
910

1011

1112
if __name__ == "__main__":

solutions/python/01-oo8/diff/app/main.py.diff

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
@@ -1,16 +1,12 @@
1+
@@ -1,17 +1,13 @@
22
import sys
33

44

55
def main():
66
- # You can use print statements as follows for debugging, they'll be visible when running tests.
77
- print("Logs from your program will appear here!")
88
-
9-
while True:
10-
- # Uncomment this block to pass the first stage
11-
- # sys.stdout.write("$ ")
12-
- # sys.stdout.flush()
13-
+ sys.stdout.write("$ ")
14-
+ sys.stdout.flush()
15-
input()
9+
- # Uncomment this block to pass the first stage
10+
- # sys.stdout.write("$ ")
11+
- # sys.stdout.flush()
12+
+ sys.stdout.write("$ ")
13+
+ sys.stdout.flush()
14+
15+
# Wait for user input
16+
input()
1617

1718

1819
if __name__ == "__main__":
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
#[allow(unused_imports)]
12
use std::io::{self, Write};
23

34
fn main() {
5+
print!("$ ");
6+
io::stdout().flush().unwrap();
7+
8+
// Wait for user input
49
let stdin = io::stdin();
5-
loop {
6-
print!("$ ");
7-
io::stdout().flush().unwrap();
8-
let mut input = String::new();
9-
stdin.read_line(&mut input).unwrap();
10-
}
10+
let mut input = String::new();
11+
stdin.read_line(&mut input).unwrap();
1112
}
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
@@ -1,15 +1,11 @@
1+
@@ -1,16 +1,12 @@
2+
#[allow(unused_imports)]
23
use std::io::{self, Write};
34

45
fn main() {
56
- // You can use print statements as follows for debugging, they'll be visible when running tests.
67
- println!("Logs from your program will appear here!");
78
-
89
- // Uncomment this block to pass the first stage
9-
- // let stdin = io::stdin();
10-
- // loop {
11-
- // print!("$ ");
12-
- // io::stdout().flush().unwrap();
13-
- // let mut input = String::new();
14-
- // stdin.read_line(&mut input).unwrap();
15-
- // }
16-
+ let stdin = io::stdin();
17-
+ loop {
18-
+ print!("$ ");
19-
+ io::stdout().flush().unwrap();
20-
+ let mut input = String::new();
21-
+ stdin.read_line(&mut input).unwrap();
22-
+ }
10+
- // print!("$ ");
11+
- // io::stdout().flush().unwrap();
12+
+ print!("$ ");
13+
+ io::stdout().flush().unwrap();
14+
15+
// Wait for user input
16+
let stdin = io::stdin();
17+
let mut input = String::new();
18+
stdin.read_line(&mut input).unwrap();
2319
}

solutions/rust/01-oo8/explanation.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@ Study and uncomment the relevant code:
44

55
```rust
66
// Uncomment this block to pass the first stage
7-
let stdin = io::stdin();
8-
loop {
9-
print!("$ ");
10-
io::stdout().flush().unwrap();
11-
let mut input = String::new();
12-
stdin.read_line(&mut input).unwrap();
13-
}
7+
print!("$ ");
8+
io::stdout().flush().unwrap();
149
```
1510

1611
Push your changes to pass the first stage:

starter_templates/go/cmd/myshell/main.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ func main() {
1010
// You can use print statements as follows for debugging, they'll be visible when running tests.
1111
fmt.Println("Logs from your program will appear here!")
1212

13-
reader := bufio.NewReader(os.Stdin)
14-
for {
15-
// Uncomment this block to pass the first stage
16-
// fmt.Fprint(os.Stdout, "$ ")
17-
_, _ = reader.ReadString('\n')
18-
}
13+
// Uncomment this block to pass the first stage
14+
// fmt.Fprint(os.Stdout, "$ ")
15+
16+
// Wait for user input
17+
bufio.NewReader(os.Stdin).ReadString('\n')
1918
}

starter_templates/python/app/main.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ def main():
55
# You can use print statements as follows for debugging, they'll be visible when running tests.
66
print("Logs from your program will appear here!")
77

8-
while True:
9-
# Uncomment this block to pass the first stage
10-
# sys.stdout.write("$ ")
11-
# sys.stdout.flush()
12-
input()
8+
# Uncomment this block to pass the first stage
9+
# sys.stdout.write("$ ")
10+
# sys.stdout.flush()
11+
12+
# Wait for user input
13+
input()
1314

1415

1516
if __name__ == "__main__":

starter_templates/rust/src/main.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
#[allow(unused_imports)]
12
use std::io::{self, Write};
23

34
fn main() {
45
// You can use print statements as follows for debugging, they'll be visible when running tests.
56
println!("Logs from your program will appear here!");
67

78
// Uncomment this block to pass the first stage
8-
// let stdin = io::stdin();
9-
// loop {
10-
// print!("$ ");
11-
// io::stdout().flush().unwrap();
12-
// let mut input = String::new();
13-
// stdin.read_line(&mut input).unwrap();
14-
// }
9+
// print!("$ ");
10+
// io::stdout().flush().unwrap();
11+
12+
// Wait for user input
13+
let stdin = io::stdin();
14+
let mut input = String::new();
15+
stdin.read_line(&mut input).unwrap();
1516
}

0 commit comments

Comments
 (0)