Sierpinski Carpet in Go Programming Language
Example
package main
import (
"fmt"
"strings"
"unicode/utf8"
)
func main() {
var order = 3
var hash = "#"
carpet := []string{hash}
for ; order > 0; order-- {
hole := strings.Repeat(" ", utf8.RuneCountInString(carpet[0]))
middle := make([]string, len(carpet))
for i, s := range carpet {
middle[i] = s + hole + s
carpet[i] = strings.Repeat(s, 3)
}
carpet = append(append(carpet, middle...), carpet...)
}
for _, r := range carpet {
fmt.Println(r)
}
}
Output
###########################
# ## ## ## ## ## ## ## ## #
###########################
### ###### ###### ###
# # # ## # # ## # # #
### ###### ###### ###
###########################
# ## ## ## ## ## ## ## ## #
###########################
######### #########
# ## ## # # ## ## #
######### #########
### ### ### ###
# # # # # # # #
### ### ### ###
######### #########
# ## ## # # ## ## #
######### #########
###########################
# ## ## ## ## ## ## ## ## #
###########################
### ###### ###### ###
# # # ## # # ## # # #
### ###### ###### ###
###########################
# ## ## ## ## ## ## ## ## #
###########################
Most Helpful This Week
Use Field Tags in the Definition of Struct Type
Example of Fscan, Fscanf, and Fscanln from FMT Package
Different ways for Integer to String Conversions
Pass an Interface as an argument to a function in Go (Golang)
Panic: runtime error: index out of range error in Golang
How do you handle HTTP client server alerting in Go?