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
What is Slice Data Type in Go?
Cannot use <variable> (type <type>) as type <new-type> error in Golang
Golang program for implementation of Shell Sort
Go program to find SRV service record of a domain
3 Different Examples - How State Works in React?
Invalid memory address or nil pointer dereference error in Golang