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
GO Program to Find LCM and GCD of given two numbers
Make Your Retirement Luxurious with These 5 Game-Changing Altcoins
How do you handle HTTP errors in Go?
Cannot call non-function <variable> error in Golang
Comparing Structs with the Different Values Assigned to Data Fields
How do you handle HTTP client caching in Go?