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
Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]
Regular expression to validate common Credit Card Numbers
How to count number of repeating words in a given String?
Anonymous Functions in Golang
How to print struct variables data in Golang?
Split a string at uppercase letters using regular expression in Golang