-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
64 lines (52 loc) · 1.1 KB
/
main.go
File metadata and controls
64 lines (52 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"bytes"
"fmt"
"io"
"os"
"strings"
)
// In this problem, you will modify the printInts function to print out numbers 1 to 100.
// Each number should be separated by a space, and for every 10 numbers printed out, you should make a new line.
// Example Output:
// 1 2 3 4 5 6 7 8 9 10
// 11 12 13 14 15 16 17 18 19 20
// ...
// 91 92 93 94 95 96 97 98 99 100
func printInts() {
// Your code goes here
}
func testUserOutput(output string) {
numbers := true
for i := 1; i <= 100; i++ {
if !strings.Contains(output, fmt.Sprintf("%d", i)) {
numbers = false
break
}
}
newlines := true
for i := 10; i <= 100; i += 10 {
if !strings.Contains(output, fmt.Sprintf("%d", i)) {
newlines = false
break
}
}
if numbers && newlines {
fmt.Println("Correct output. Well done!")
} else {
fmt.Println("Incorrect output. Please try again.")
}
}
func main() {
origStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
printInts()
w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
os.Stdout = origStdout
output := buf.String()
fmt.Print(output)
testUserOutput(output)
}