Skip to content

Instantly share code, notes, and snippets.

@laughingclouds
Last active February 5, 2022 11:45
Show Gist options
  • Save laughingclouds/7837a9ee9e03681877f309305013111f to your computer and use it in GitHub Desktop.
Save laughingclouds/7837a9ee9e03681877f309305013111f to your computer and use it in GitHub Desktop.
Pattern Printing In 5 languages | Go | Java | Python | Ruby | TypeScript
/*
go version go1.17 linux/amd64
RUN CODE HERE https://go.dev/play/
simply copy and paste -> click "run"
*/
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(upperStarPattern() + lowerStarPattern())
}
/*Helper function to create row*/
func createPatternRow(nStars int, s string) string {
numOfSpaces := 9 - (2 * nStars)
starStr := strings.Repeat("*", nStars)
spaceStr := strings.Repeat(" ", numOfSpaces)
s += starStr + spaceStr + starStr + "\n"
return s
}
/*Upper half of star pattern*/
func upperStarPattern() string {
// R1 is all stars
s := strings.Repeat("*", 9) + "\n"
// loop from R2 -> R5
i := 0
for i < 4 {
s = createPatternRow(4-i, s)
i++
}
return s
}
/*Lower half of star pattern*/
func lowerStarPattern() string {
s := ""
// loop from R6 -> R8
i := 0
for i < 3 {
s = createPatternRow(i+2, s)
i++
}
// R9 is all "*"
s += strings.Repeat("*", 9)
return s
}
/**
* openjdk 11.0.13 2021-10-19
* pattern printing
* RUN CODE HERE
* https://www.jdoodle.com/online-java-compiler/
* copy-paste
* select jdk 11 and press "execute"
*/
public class pattern {
public static void main(String[] args) {
System.out.println(upperStarPattern() + lowerStarPattern());
}
private static String createPatternRow(int nStars, String s) {
int numOfSpace = 9 - (2 * nStars);
String starStr = "*".repeat(nStars);
String spaceStr = " ".repeat(numOfSpace);
s = s.concat(starStr + spaceStr + starStr + "\n");
return s;
}
/**Upper half of star pattern */
private static String upperStarPattern() {
// R1 is all stars
String s = "*".repeat(9) + "\n";
// loop from R2 -> R5
for (int i = 0; i < 4; i++) {
s = createPatternRow(4-i, s);
}
return s;
}
/**Lower half of star pattern */
private static String lowerStarPattern() {
String s = "";
// loop from R6 -> R8
for (int i = 0; i < 3; i++) {
s = createPatternRow(i+2, s);
}
// R9 is all *
s = s.concat("*".repeat(9));
return s;
}
}
"""pattern to print
*********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
*********
"""
def createPatternRow(nStars: int, s: str, end: str='\n') -> str:
"""Helper function to create row"""
numOfSpaces = 9 - (2 * nStars)
starStr = nStars * "*"
spaceStr = numOfSpaces * " "
s += starStr + spaceStr + starStr + end
return s
def upperStarPattern() -> str:
"""Upper half of star pattern"""
# R1 is all stars
s = "*" * 9 + "\n"
# loop from R2 -> R5
for i in range(4):
s = createPatternRow(4 - i, s)
return s
def lowerStarPattern() -> str:
"""Lower half of star pattern"""
s = ""
# loop from R6 -> R8
for i in range(3):
s = createPatternRow(i + 2, s)
# R9 is all "*"
s += "*" * 9
return s
print(upperStarPattern() + lowerStarPattern())
# Ruby
# RUN CODE HERE https://try.ruby-lang.org/
# copy paste
# then press "run"
# Helper function to create row
def createPatternRow(nStars, s, endStr="\n")
numOfSpaces = 9 - (2 * nStars)
starStr = "*" * nStars
spaceStr = " " * numOfSpaces
s += (starStr + spaceStr + starStr + endStr)
return s
end
# Upper half of star pattern
def upperStarPattern
# R1 is all stars
s = "*" * 9 + "\n"
# loop from R2 -> R5
(0..3).each do |i|
s = createPatternRow(4-i, s)
end
return s
end
# Lower half of star pattern
def lowerStarPattern
s = ""
# loop from R6 -> R8
(0..2).each do |i|
s = createPatternRow(i + 2, s)
end
# R9 is all "*"
s += "*" * 9
return s
end
puts(upperStarPattern() + lowerStarPattern())
/*
TypeScript Version 4.5.5
RUN CODE HERE https://www.typescriptlang.org/play
copy paste, and press run
[no need to set the version]
*/
/**Helper function to create row */
function createPatternRow(nStars: number, s: string, end: string = "\n"): string {
let numOfSpaces = 9 - (2 * nStars);
let starStr = "*".repeat(nStars);
let spaceStr = " ".repeat(numOfSpaces);
s = s.concat(starStr, spaceStr, starStr, end);
return s;
}
/**Upper half of star pattern */
function upperStarPattern(): string {
// R1 is all stars
let s = "*".repeat(9) + "\n";
// loop from R3 -> R5
for (let i = 0; i < 4; i++) {
s = createPatternRow(4 - i, s);
}
return s;
}
/**Lower half of star pattern */
function lowerStarPattern(): string {
let s = "";
// loop from R6 -> R8
for (let i = 0; i < 3; i++) {
s = createPatternRow(i + 2, s);
}
// R9 is all stars
s += "*".repeat(9);
return s;
}
console.log("\n\r" + upperStarPattern() + lowerStarPattern() + "\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment