How to print rectangle like below using Ruby:
* = = = *
* = = = *
* * * * *
* = = = *
* = = = *
in this case, length of row and column are same and must odd.
Example:
r = rectangle(5)
should print :
* = = = *
* = = = *
* * * * *
* = = = *
* = = = *
and if :
r = rectangle(7)
should print:
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
Thanks in advance.
I'd start by creating the two lines: (there are plenty of ways to achieve this)
size = 7
a = Array.new(size, '*').fill('=', 1..-2).join(' ') #=> "* = = = = = *"
b = Array.new(size, '*').join(' ') #=> "* * * * * * *"
Then I'd define a repeating pattern:
pattern = [a, a, b].cycle
Finally, I'd print the pattern size times:
puts pattern.take(size)
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
[SOLVE BY ME] :D
def square(n)
begin
if n.odd?
1.upto(n) do | row |
if row % 3 != 0
puts "#{'*'} #{'= ' * (n - 2)}#{'*'}"
else
puts "#{'* ' * n}"
end
end
else
puts 'Must odd number!'
end
rescue
puts 'Must integer number!'
end
end
square(5)
Output:
* = = = *
* = = = *
* * * * *
* = = = *
* = = = *
square(7)
Output:
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
square(8)
Output:
Must odd number!
square(8.5)
Output:
Must integer number!
square('blabla')
Output:
Must integer number!
def rectangle(n)
puts("-----------------For #{n}--------------------")
if n % 2 == 1
for i in (1..n)
for j in (1..n)
if j == 1 || j == n || 0 == i % 3
print "* "
else
print "= "
end
end
print("\n")
end
end
end
rectangle(3)
rectangle(5)
rectangle(7)
rectangle(9)
rectangle(11)
-----------------For 3--------------------
* = *
* = *
* * *
-----------------For 5--------------------
* = = = *
* = = = *
* * * * *
* = = = *
* = = = *
-----------------For 7--------------------
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
-----------------For 9--------------------
* = = = = = = = *
* = = = = = = = *
* * * * * * * * *
* = = = = = = = *
* = = = = = = = *
* * * * * * * * *
* = = = = = = = *
* = = = = = = = *
* * * * * * * * *
-----------------For 11--------------------
* = = = = = = = = = *
* = = = = = = = = = *
* * * * * * * * * * *
* = = = = = = = = = *
* = = = = = = = = = *
* * * * * * * * * * *
* = = = = = = = = = *
* = = = = = = = = = *
* * * * * * * * * * *
* = = = = = = = = = *
* = = = = = = = = = *
Just for fun modify #spn answer
def rectangle(count)
return 'Must odd number more than 1' unless count.is_a?(Integer) && count.odd? && count > 1
Array.new(count) { |index| (index + 1) % 3 == 0 ?
"#{'* ' * count}".chomp(" ") :
"#{'*'} #{'= ' * (count - 2)}#{'*'}" }.join("\n")
end
And now
puts rectangle(2) # will print Must odd number more than 1
puts rectangle("asdf") # will print Must odd number more than 1
puts rectangle(9) # will print:
* = = = = = = = *
* = = = = = = = *
* * * * * * * * *
* = = = = = = = *
* = = = = = = = *
* * * * * * * * *
* = = = = = = = *
* = = = = = = = *
* * * * * * * * *
It's better to avoid puts in the methods. So you can use them again in web, telegrambots, etc. Also it's not good idea to duplicate Ruby exceptions by your own (but the same) messages.
Let's first define a method to construct the two types of lines.
def make_line(n, mid_char)
['*', *[mid_char]*(n-2), '*'].join(' ')
end
make_line(5, '*') #=> "* * * * *"
make_line(5, '#') #=> "* # # # *"
Now create a method to draw the lines in the desired pattern. The ith line (base zero) is comprised of stars and spaces only if (i+1) % 3 equals zero; else it is the line containing pound signs as well.
def draw(n)
all_stars = make_line(n, '*')
two_stars = make_line(n, '#')
n.times { |i| puts ((i+1) % 3).zero? ? all_stars : two_stars }
end
draw 5
* # # # *
* # # # *
* * * * *
* # # # *
* # # # *
draw 6
* # # # # *
* # # # # *
* * * * * *
* # # # # *
* # # # # *
* * * * * *
draw 7
* # # # # # *
* # # # # # *
* * * * * * *
* # # # # # *
* # # # # # *
* * * * * * *
* # # # # # *
Related
I want to print triangle & pyramid "*" using for loop.
can somebody help me on this?
Output like :
*****
****
***
**
*
and
*
**
***
****
*****
1: Print triangle using While loop
n = 5
while n >= 1
puts "* " * n
n = n - 1
end
* * * * *
* * * *
* * *
* *
*
n = 1
while n <= 5
puts ("* " * n).rjust(10)
n += 1
end
*
* *
* * *
* * * *
* * * * *
2: Print pyramid using loop
n = 4 # Set number of rows
i = 1
1.upto(n) do
print ' ' * n
print '*' * (2 * i - 1)
print "\n"
n -= 1
i += 1
end
*
***
*****
*******
5.downto(1).each{|n| puts ("*" * n).ljust(5)}
1.upto(5).each{|n| puts ("*" * n).rjust(5)}
I have written an application which will write square with diagonal (from left side) - output:
+ * * * *
* + * * *
* * + * *
* * * + *
* * * * +
Code for first application:
PROGRAM cycle4;
USES CRT;
VAR a,r,s:INTEGER;
BEGIN
CLRSCR;
WRITE (‘Enter the number of lines :‘) ;
READLN(a);
FOR r:= 1 TO a DO
BEGIN
FOR s:=1 TO a DO
IF r = s THEN WRITE(‘+‘)
ELSE WRITE(‘*‘) ;
WRITELN;
END;
READLN;
END.
And now I have to create an application which will write square with diagonal (from right side) - output:
* * * * +
* * * + *
* * + * *
* + * * *
+ * * * *
But I don't know how can I write it. Can you help me?
Thanks :)
The line of code which defines the position of + sign is that:
IF r = s THEN WRITE(‘+‘)
and this is the only line you need to change:
IF r + s = a + 1 THEN WRITE(‘+‘)
I think this should work, check with Pascal compiler, haven't used it for about 10 years :)
How to color halftone using MATLAB code ?
I first used jarvis black and white half tone then modified it color halftone.
My approach :-
function outImg = jarvisColorHalftone(inImg)
[N,M,P] = size(inImg);
outImg = logical(zeros([N,M,P]));
for p = 1:P
outImg(:,:,p) = jarvisHalftone(inImg(:,:,p));
end
outImg;
The function above is not working.
// jarvisHalftone function for grayscale to black and white image
function outImg = jarvisHalftone(inImg)
inImg = double(inImg);
[M,N] = size(inImg);
T = 127.5;
y = inImg;
error = 0;
y= [127.5*ones(M,2) y 127.5*ones(M,2) ; 127.5*ones(2,N+4)];
z = y;
for rows = 1:M
for cols = 3:N+2
z(rows,cols) =255*(y(rows,cols)>=T);
error = -z(rows,cols) + y(rows,cols);
y(rows,cols+2) = 5/48 * error + y(rows,cols+2);
y(rows,cols+1) = 7/48 * error + y(rows,cols+1);
y(rows+1,cols+2) = 3/48 * error + y(rows+1,cols+2);
y(rows+1,cols+1) = 5/48 * error + y(rows+1,cols+1);
y(rows+1,cols+0) = 7/48 * error + y(rows+1,cols+0);
y(rows+1,cols-1) = 5/48 * error + y(rows+1,cols-1);
y(rows+1,cols-2) = 3/48 * error + y(rows+1,cols-2);
y(rows+2,cols+2) = 1/48 * error + y(rows+2,cols+2);
y(rows+2,cols+1) = 3/48 * error + y(rows+2,cols+1);
y(rows+2,cols+0) = 5/48 * error + y(rows+2,cols+0);
y(rows+2,cols-1) = 3/48 * error + y(rows+2,cols-1);
y(rows+2,cols-2) = 1/48 * error + y(rows+2,cols-2);
end
end
outImg = z(1:M,3:N+2);
outImg = im2bw(uint8(outImg));
I am using Ruby 1.9.3. I have done pattern program like follow:
n = 1
while n <= 5
n.downto 1 do |i|
print "* "
end
puts
n += 1
end
Output of above program is like follow:
*
* *
* * *
* * * *
* * * * *
Now I am trying to do pattern program like follow:
*
* *
* * *
* * * *
* * * * *
I am not getting idea how can I do it?
Could anyone help me on this?
Thank you.
You can use rjust:
n = 1
while n <= 5
puts "* " * n
n += 1
end
*
* *
* * *
* * * *
* * * * *
n = 1
while n <= 5
puts ("* " * n).rjust(10)
n += 1
end
*
* *
* * *
* * * *
* * * * *
A shortened version of this will be:
5.times { |i| puts ('* ' * (i+1)) }
and
5.times { |i| puts ('* ' * (i+1)).rjust(10) }
You can do:
1.upto 5 do |n|
print ' ' * (5-n)
print '* ' * n
puts
end
Here's another way:
def print_two_ways(n, spaces=0)
arr = Array.new(n) { |i| Array.new(n) { |j| (i >= j) ? '*' : ' ' } }
print_matrix(arr, spaces)
puts
print_matrix(arr.map(&:reverse), spaces)
end
def print_matrix(arr, spaces = 0)
sep = ' '*(spaces)
arr.each { |r| puts "#{r.join(sep)}" }
end
print_two_ways(5)
*
**
***
****
*****
*
**
***
****
*****
print_two_ways(5,1)
*
* *
* * *
* * * *
* * * * *
*
* *
* * *
* * * *
* * * * *
a = 5
b = 1
while a>0
while b<=5
puts "*"*b
b = b+1
a = a-1
end
end
*
**
***
****
*****
Here, a is 5 and b is 1.
In the first iteration of the outer while loop, a is 1 and the inner while loop is inside the body of the outer while loop. So, the inner while loop will be executed and "*"1 ( b is 1 ) i.e "" will be printed and b will become 2 and a will become 4.
Here is simple set variable and use all programs.
# Set vars
n = 4 # Set number of rows
br = "\n" * 2
# Simple loop
puts "Right triangle:#{br}"
for i in 1..n do
puts "* " * i
end
puts br
=begin
simple loop result:
*
* *
* * *
* * * *
=end
# Countdown loop
puts "Inverted right triangle:#{br}"
n.downto(0) do
puts "* " * n
n -= 1
end
puts br
=begin
countdown loop result:
* * * *
* * *
* *
*
=end
# Function loop
puts "Inverted pyramid:#{br}"
n = 4 # Reset number of rows
for i in 1..n do
# Use a func to reduce repetition
def printer(var, str)
print "#{str}" * (2 * var - 1)
end
printer(i, " ")
printer(n, "* ")
print "\n"
n -= 1
end
puts br
=begin
function loop result:
* * * * * * *
* * * * *
* * *
*
=end
# Count up loop
puts "Close pyramid:#{br}"
n = 4 # Set number of rows
i = 1
1.upto(n) do
#n.times do
# print ' '
#end
print ' ' * n
#(2 * i - 1).times do
# print '*'
#end
print '*' * (2 * i -1)
print "\n"
n -= 1
i += 1
end
print br
=begin
count up loop result:
*
***
*****
*******
=end
I'm still new to Ruby, but this was my solution. Anything wrong with doing it this way?
def staircase(n)
sum_tot = n
n.times do
sum_tot-= 1
space = n - sum_tot
puts ('#' * space).rjust(n)
end
end
I am using open MP to speed up the flux calculation in my program. I basically want OpenMP to carry out both of these left and right flux calculations in parallel. But on the contrary, the following code takes even more time with the #pragma directives. What do i modify to get it right?
#pragma omp parallel num_threads(2)
{
#pragma omp single
{//first condition
//cerr<<"Executed thread 0"<<endl;
if ( (fabs(lcellMach-1.0)<EPSILON) || ( (lcellMach-1.0) > 0.0 ) ){//purpose of Epsilon!!!!
FluxP[0] = rhol * vnl;
FluxP[1] = rhol * ul * vnl + Pl*nx;
FluxP[2] = rhol * vl * vnl + Pl*ny;
FluxP[3] = rhol * wl * vnl + Pl*nz;
FluxP[4] = rhol * ((GAMMA * Pl / (rhol * (GAMMA-1.0))) + ((ul*ul + vl*vl + wl*wl)/2.0)) * vnl;
}else if ( (fabs(lcellMach+1.0)<EPSILON) || ( (lcellMach+1.0) < 0.0 ) ){
FluxP[0] = FluxP[1] = FluxP[2] = FluxP[3] = FluxP[4] = 0.0;// If flow direction is opposite the Flux + is zero
}else {
double ql = (ul*ul + vl*vl + wl*wl);// how did this come
FluxP[0] = rhol * lcell_a * (lcellMach+1.0)*(lcellMach+1.0) / 4.0;
FluxP[1] = FluxP[0] * ( ul + (nx*(0.0-vnl + 2.0*lcell_a)/GAMMA) );
FluxP[2] = FluxP[0] * ( vl + (ny*(0.0-vnl + 2.0*lcell_a)/GAMMA) );
FluxP[3] = FluxP[0] * ( wl + (nz*(0.0-vnl + 2.0*lcell_a)/GAMMA) );
FluxP[4] = FluxP[0] * ( ((ql - vnl*vnl)/2.0) + (((GAMMA-1.0)*vnl + 2.0*lcell_a)*((GAMMA-1.0)*vnl + 2.0*lcell_a) / (2.0*(GAMMA*GAMMA-1.0))) );
}
}//end of 1st
#pragma omp single
{//second condition
//cerr<<"Executed thread 1"<<endl;
if ((fabs(rcellMach+1.0)<EPSILON) || ((rcellMach+1.0) < 0.0)) {
FluxM[0] = rhor * vnr;
FluxM[1] = rhor * ur * vnr + Pr*nx;
FluxM[2] = rhor * vr * vnr + Pr*ny;
FluxM[3] = rhor * wr * vnr + Pr*nz;
FluxM[4] = rhor * ((GAMMA * Pr / (rhor * (GAMMA-1.0))) + ((ur*ur + vr*vr + wr*wr)/2.0)) * vnr;
}else if ((fabs(rcellMach-1.0)<EPSILON) || ((rcellMach-1.0) > 0.0)) {
FluxM[0] = FluxM[1] = FluxM[2] = FluxM[3] = FluxM[4] = 0.0;
}else {
tempFlux[0] = rhor * vnr;
tempFlux[1] = rhor * ur * vnr + Pr*nx;
tempFlux[2] = rhor * vr * vnr + Pr*ny;
tempFlux[3] = rhor * wr * vnr + Pr*nz;
tempFlux[4] = rhor * ((GAMMA * Pr / (rhor * (GAMMA-1.0))) + ((ur*ur + vr*vr + wr*wr)/2.0)) * vnr;
double qr = (ur*ur + vr*vr + wr*wr);
tempFluxP[0] = rhor * rcell_a * (rcellMach+1.0)*(rcellMach+1.0) / 4.0;
tempFluxP[1] = tempFluxP[0] * ( ur + (nx*(0.0-vnr + 2.0*rcell_a)/GAMMA) );
tempFluxP[2] = tempFluxP[0] * ( vr + (ny*(0.0-vnr + 2.0*rcell_a)/GAMMA) );
tempFluxP[3] = tempFluxP[0] * ( wr + (nz*(0.0-vnr + 2.0*rcell_a)/GAMMA) );
tempFluxP[4] = tempFluxP[0] * ( ((qr - vnr*vnr)/2.0) + (((GAMMA-1.0)*vnr + 2.0*rcell_a)*((GAMMA-1.0)*vnr + 2.0*rcell_a) / (2.0*(GAMMA*GAMMA-1.0))) );
for (int j=0; j<O; j++) FluxM[j] = tempFlux[j] - tempFluxP[j];
}
}
}//pragma
Urgent help required. Thanks.
What you need is the sections construct:
#pragma omp parallel sections num_threads(2)
{
#pragma omp section
{
... code that updates FluxP ...
}
#pragma omp section
{
... code that updates FluxM ...
}
}
But your code doesn't seem like it would take much time to do the calculations (no big for loops inside for example) so the overhead that OpenMP will put onto it will most likely be more time consuming than the saving in computation time and hence the parallel version will most likely execute slower than the serial.