Created
June 5, 2018 10:01
-
-
Save khajer/cfb08c56b5bc39b9a0fe9369ccb8fd5f to your computer and use it in GitHub Desktop.
gen star
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * | |
| *** | |
| ***** | |
| ******* | |
| ********* | |
| *********** | |
| ********* | |
| ******* | |
| ***** | |
| *** | |
| * | |
| */ | |
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| func replaceStr(ch string, n int) string{ | |
| txt := "" | |
| for i:=0; i<n;i++{txt+=ch} | |
| return txt | |
| } | |
| func genLevel(level int) string{ | |
| txt := "" | |
| ch := " " | |
| star := "*" | |
| for i:=0; i< (level*2)+1; i++{ | |
| if i < level{ | |
| txt += replaceStr(ch, level - i)+replaceStr(star, (i*2)+1) +"\n" | |
| }else{ | |
| txt += replaceStr(ch, i-level)+replaceStr(star, (level*2)-((i-level)*2) +1) +"\n" | |
| } | |
| } | |
| return txt | |
| } | |
| func main() { | |
| fmt.Println(genLevel(5)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment