Created
March 17, 2018 22:58
-
-
Save ysnerdem/78a7a1840adfa3a8266bde2d507f9c60 to your computer and use it in GitHub Desktop.
Print Pattern
This file contains 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
def pattern1(num) | |
(1..num).each do |x| | |
puts (x.to_s) * x | |
end | |
end | |
def pattern2(num) | |
n = 0 | |
while n < num | |
n += 1 | |
(1..n).each {|i| print i} | |
print "\n" | |
end | |
end | |
def pattern3(num) | |
n = 1 | |
while n < num * 2 | |
(1..n).each {|i| print i if i % 2 != 0} | |
n += 2 | |
print "\n" | |
end | |
end | |
def pattern4(num) | |
lst = [] | |
n = 0 | |
while lst.length < num | |
lst << n | |
lst.each {|i| print i} | |
print "\n" | |
if n == 0 | |
n += 1 | |
else | |
n -= 1 | |
end | |
end | |
end | |
def pattern1_rev(num) | |
(1..num).reverse_each do |x| | |
puts (x.to_s)*x | |
end | |
end | |
def pattern2_rev(num) | |
while 1 <= num | |
(1..num).each {|x| print x} | |
print "\n" | |
num -= 1 | |
end | |
end | |
def pattern3_rev(num) | |
num *= 2 | |
while 1 <= num | |
(1..num).each {|x| print x if x % 2 != 0} | |
num -= 2 | |
print "\n" | |
end | |
end | |
def pattern4_rev(num) | |
n = 0 | |
(1..num).reverse_each do |x| | |
(1..x).each do |i| | |
print n | |
if n == 0 | |
n += 1 | |
else | |
n -= 1 | |
end | |
end | |
print "\n" | |
end | |
end | |
pattern1 9 | |
pattern2 9 | |
pattern3 9 | |
pattern4 9 | |
puts "*********" | |
pattern1_rev 9 | |
pattern2_rev 9 | |
pattern3_rev 9 | |
pattern4_rev 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment