Print the horizontal ladder pattern
The problem is to write a program that prints a horizontal ladder pattern of a given odd size. The ladder pattern consists of horizontal lines and joints. The size of the ladder represents the number of horizontal lines and the distance between joints.

Example:
Let's consider an example where the size of the ladder is 5. The output will be:
━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━
In this example, the ladder has 5 horizontal lines and joints. The joints are represented by the symbol '┰' at the top and '┸' at the bottom. The lines between the joints are represented by horizontal dashes '━', and the spaces are denoted by empty characters.
Algorithm
To print the horizontal ladder pattern, we can follow the following algorithm:
- Check if the given size is valid. If it is negative or even, return.
- Print the size of the ladder for reference.
- Iterate from 0 to size-1:
- Print spaces to center-align the pattern.
- Iterate from 0 to size+1:
- If it is the first or last line:
- If it is the first line and not the first joint, print '┰' and horizontal dashes.
- If it is the last line and not the first joint, print '┸' and horizontal dashes.
- Otherwise, print only horizontal dashes.
- Else if it is an internal line:
- Print spaces to align the internal line.
- Print '┃' to represent the internal line.
- Print a new line after each iteration.
Pseudocode
Here's the pseudocode representation of the algorithm:
function space(size):
for counter from 0 to size:
print a space
function print_symbol(size):
for counter from 0 to size:
print '━'
function horizontal_ladder(size):
if size < 0 or size is even:
return
print "Size: " + size
for i from 0 to size-1:
space(size / 2)
for j from 0 to size+1:
if i is 0 or i+1 is size:
if i is 0 and j is not 0:
print '┰' + print_symbol(size)
else if i is not 0 and j is not 0:
print '┸' + print_symbol(size)
else:
print_symbol(size)
else if j+1 is not size:
space(size)
print '┃'
print a new line
main:
horizontal_ladder(3)
horizontal_ladder(5)
horizontal_ladder(7)
Code Solution
Here given code implementation process.
//C Program
//Print the horizontal ladder pattern
#include <stdio.h>
#include <stdlib.h>
/*Include Space of given size*/
void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
printf(" ");
}
}
void print_symbol(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
printf("━");
}
}
//Display horizontal ladder pattern of given odd size
void horizontal_ladder(int size)
{
if (size < 0 || size % 2 == 0)
{
return;
}
printf("\n Size : %d \n\n", size);
int j = 0;
for (int i = 0; i < size; ++i)
{
space(size / 2);
for (j = 0; j < size + 1; j++)
{
if (i == 0 || i + 1 == size)
{
//When displaying top and bottom layer
if (i == 0 && j != 0)
{
//when need to add upper joint
printf("┰");
print_symbol(size);
}
else if (i != 0 && j != 0)
{
//when need to add bottom joint
printf("┸");
print_symbol(size);
}
else
{
print_symbol(size);
}
}
else if (j + 1 != size)
{
//print internal layer
space(size);
printf("┃");
}
}
printf("\n");
}
}
int main()
{
//Test Cases
horizontal_ladder(3);
horizontal_ladder(5);
horizontal_ladder(7);
return 0;
}
Output
Size : 3
━━━┰━━━┰━━━┰━━━
┃ ┃ ┃
━━━┸━━━┸━━━┸━━━
Size : 5
━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━
Size : 7
━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
/*
Java Program
Print the horizontal ladder pattern
*/
class MyPattern
{
public void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
System.out.print(" ");
}
}
public void print_symbol(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
System.out.print("━");
}
}
//Display horizontal ladder pattern of given odd size
public void horizontal_ladder(int size)
{
if (size < 0 || size % 2 == 0)
{
return;
}
System.out.print("\n Size : "+size+" \n\n");
int j = 0;
for (int i = 0; i < size; ++i)
{
space(size / 2);
for (j = 0; j < size + 1; j++)
{
if (i == 0 || i + 1 == size)
{
//When displaying top and bottom layer
if (i == 0 && j != 0)
{
//when need to add upper joint
System.out.print("┰");
print_symbol(size);
}
else if (i != 0 && j != 0)
{
//when need to add bottom joint
System.out.print("┸");
print_symbol(size);
}
else
{
print_symbol(size);
}
}
else if (j + 1 != size)
{
//print internal layer
space(size);
System.out.print("┃");
}
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Test Cases
obj.horizontal_ladder(3);
obj.horizontal_ladder(5);
obj.horizontal_ladder(7);
}
}
Output
Size : 3
━━━┰━━━┰━━━┰━━━
┃ ┃ ┃
━━━┸━━━┸━━━┸━━━
Size : 5
━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━
Size : 7
━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
/*
C++ Program
Print the horizontal ladder pattern
*/
#include<iostream>
using namespace std;
class MyPattern
{
public: void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
cout << " ";
}
}
void print_symbol(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
cout << "━";
}
}
//Display horizontal ladder pattern of given odd size
void horizontal_ladder(int size)
{
if (size < 0 || size % 2 == 0)
{
return;
}
cout << "\n Size : " << size << " \n\n";
int j = 0;
for (int i = 0; i < size; ++i)
{
this->space(size / 2);
for (j = 0; j < size + 1; j++)
{
if (i == 0 || i + 1 == size)
{
//When displaying top and bottom layer
if (i == 0 && j != 0)
{
cout << "┰";
this->print_symbol(size);
}
else if (i != 0 && j != 0)
{
cout << "┸";
this->print_symbol(size);
}
else
{
this->print_symbol(size);
}
}
else if (j + 1 != size)
{
//print internal layer
this->space(size);
cout << "┃";
}
}
cout << "\n";
}
}
};
int main()
{
MyPattern obj = MyPattern();
//Test Cases
obj.horizontal_ladder(3);
obj.horizontal_ladder(5);
obj.horizontal_ladder(7);
return 0;
}
Output
Size : 3
━━━┰━━━┰━━━┰━━━
┃ ┃ ┃
━━━┸━━━┸━━━┸━━━
Size : 5
━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━
Size : 7
━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
/*
C# Program
Print the horizontal ladder pattern
*/
using System;
class MyPattern
{
public void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
Console.Write(" ");
}
}
public void print_symbol(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
Console.Write("━");
}
}
//Display horizontal ladder pattern of given odd size
public void horizontal_ladder(int size)
{
if (size < 0 || size % 2 == 0)
{
return;
}
Console.Write("\n Size : " + size + " \n\n");
int j = 0;
for (int i = 0; i < size; i++)
{
space(size / 2);
for (j = 0; j < size + 1; j++)
{
if (i == 0 || i + 1 == size)
{
//When displaying top and bottom layer
if (i == 0 && j != 0)
{
//when need to add upper joint
Console.Write("┰");
print_symbol(size);
}
else if (i != 0 && j != 0)
{
//when need to add bottom joint
Console.Write("┸");
print_symbol(size);
}
else
{
print_symbol(size);
}
}
else if (j + 1 != size)
{
//print internal layer
space(size);
Console.Write("┃");
}
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Test Cases
obj.horizontal_ladder(3);
obj.horizontal_ladder(5);
obj.horizontal_ladder(7);
}
}
Output
Size : 3
━━━┰━━━┰━━━┰━━━
┃ ┃ ┃
━━━┸━━━┸━━━┸━━━
Size : 5
━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━
Size : 7
━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
<?php
/*
Php Program
Print the horizontal ladder pattern
*/
class MyPattern
{
public function space($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
//Add space
echo(" ");
}
}
public function print_symbol($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
echo("━");
}
}
//Display horizontal ladder pattern of given odd size
public function horizontal_ladder($size)
{
if ($size < 0 || $size % 2 == 0)
{
return;
}
echo("\n Size : ". $size ." \n\n");
$j = 0;
for ($i = 0; $i < $size; ++$i)
{
$this->space(intval($size / 2));
for ($j = 0; $j < $size + 1; $j++)
{
if ($i == 0 || $i + 1 == $size)
{
//When displaying top and bottom layer
if ($i == 0 && $j != 0)
{
//when need to add upper joint
echo("┰");
$this->print_symbol($size);
}
else if ($i != 0 && $j != 0)
{
//when need to add bottom joint
echo("┸");
$this->print_symbol($size);
}
else
{
$this->print_symbol($size);
}
}
else if ($j + 1 != $size)
{
//print internal layer
$this->space($size);
echo("┃");
}
}
echo("\n");
}
}
}
function main()
{
$obj = new MyPattern();
//Test Cases
$obj->horizontal_ladder(3);
$obj->horizontal_ladder(5);
$obj->horizontal_ladder(7);
}
main();
Output
Size : 3
━━━┰━━━┰━━━┰━━━
┃ ┃ ┃
━━━┸━━━┸━━━┸━━━
Size : 5
━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━
Size : 7
━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
/*
Node Js Program
Print the horizontal ladder pattern
*/
class MyPattern
{
space(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
process.stdout.write(" ");
}
}
print_symbol(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
process.stdout.write("━");
}
}
//Display horizontal ladder pattern of given odd size
horizontal_ladder(size)
{
if (size < 0 || size % 2 == 0)
{
return;
}
process.stdout.write("\n Size : " + size + " \n\n");
var j = 0;
for (var i = 0; i < size; ++i)
{
this.space(parseInt(size / 2));
for (j = 0; j < size + 1; j++)
{
if (i == 0 || i + 1 == size)
{
//When displaying top and bottom layer
if (i == 0 && j != 0)
{
//when need to add upper joint
process.stdout.write("┰");
this.print_symbol(size);
}
else if (i != 0 && j != 0)
{
//when need to add bottom joint
process.stdout.write("┸");
this.print_symbol(size);
}
else
{
this.print_symbol(size);
}
}
else if (j + 1 != size)
{
//print internal layer
this.space(size);
process.stdout.write("┃");
}
}
process.stdout.write("\n");
}
}
}
function main(args)
{
var obj = new MyPattern();
//Test Cases
obj.horizontal_ladder(3);
obj.horizontal_ladder(5);
obj.horizontal_ladder(7);
}
main();
Output
Size : 3
━━━┰━━━┰━━━┰━━━
┃ ┃ ┃
━━━┸━━━┸━━━┸━━━
Size : 5
━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━
Size : 7
━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
# Python 3 Program
# Print the horizontal ladder pattern
class MyPattern :
def space(self, size) :
counter = 0
while (counter < size) :
print(" ", end = "")
counter += 1
def print_symbol(self, size) :
counter = 0
while (counter < size) :
print("━", end = "")
counter += 1
# Display horizontal ladder pattern of given odd size
def horizontal_ladder(self, size) :
if (size < 0 or size % 2 == 0) :
return
print("\n Size : ", size ," \n")
j = 0
i = 0
while (i < size) :
self.space(int(size / 2))
j = 0
while (j < size + 1) :
if (i == 0 or i + 1 == size) :
# When displaying top and bottom layer
if (i == 0 and j != 0) :
print("┰", end = "")
self.print_symbol(size)
elif(i != 0 and j != 0) :
print("┸", end = "")
self.print_symbol(size)
else :
self.print_symbol(size)
elif(j + 1 != size) :
# print internal layer
self.space(size)
print("┃", end = "")
j += 1
print(end = "\n")
i += 1
def main() :
obj = MyPattern()
# Test Cases
obj.horizontal_ladder(3)
obj.horizontal_ladder(5)
obj.horizontal_ladder(7)
if __name__ == "__main__": main()
Output
Size : 3
━━━┰━━━┰━━━┰━━━
┃ ┃ ┃
━━━┸━━━┸━━━┸━━━
Size : 5
━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━
Size : 7
━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
# Ruby Program
# Print the horizontal ladder pattern
class MyPattern
def space(size)
counter = 0
while (counter < size)
print(" ")
counter += 1
end
end
def print_symbol(size)
counter = 0
while (counter < size)
print("━")
counter += 1
end
end
# Display horizontal ladder pattern of given odd size
def horizontal_ladder(size)
if (size < 0 || size % 2 == 0)
return
end
print("\n Size : ", size ," \n\n")
j = 0
i = 0
while (i < size)
self.space(size / 2)
j = 0
while (j < size + 1)
if (i == 0 || i + 1 == size)
# When displaying top and bottom layer
if (i == 0 && j != 0)
print("┰")
self.print_symbol(size)
elsif(i != 0 && j != 0)
print("┸")
self.print_symbol(size)
else
self.print_symbol(size)
end
elsif(j + 1 != size)
# print internal layer
self.space(size)
print("┃")
end
j += 1
end
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
# Test Cases
obj.horizontal_ladder(3)
obj.horizontal_ladder(5)
obj.horizontal_ladder(7)
end
main()
Output
Size : 3
━━━┰━━━┰━━━┰━━━
┃ ┃ ┃
━━━┸━━━┸━━━┸━━━
Size : 5
━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━
Size : 7
━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
/*
Scala Program
Print the horizontal ladder pattern
*/
class MyPattern
{
def space(size: Int): Unit = {
var counter: Int = 0;
while (counter < size)
{
print(" ");
counter += 1;
}
}
def print_symbol(size: Int): Unit = {
var counter: Int = 0;
while (counter < size)
{
print("━");
counter += 1;
}
}
//Display horizontal ladder pattern of given odd size
def horizontal_ladder(size: Int): Unit = {
if (size < 0 || size % 2 == 0)
{
return;
}
print("\n Size : " + size + " \n\n");
var j: Int = 0;
var i: Int = 0;
while (i < size)
{
space((size / 2).toInt);
j = 0;
while (j < size + 1)
{
if (i == 0 || i + 1 == size)
{
//When displaying top and bottom layer
if (i == 0 && j != 0)
{
print("┰");
print_symbol(size);
}
else if (i != 0 && j != 0)
{
print("┸");
print_symbol(size);
}
else
{
print_symbol(size);
}
}
else if (j + 1 != size)
{
//print internal layer
space(size);
print("┃");
}
j += 1;
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Test Cases
obj.horizontal_ladder(3);
obj.horizontal_ladder(5);
obj.horizontal_ladder(7);
}
}
Output
Size : 3
━━━┰━━━┰━━━┰━━━
┃ ┃ ┃
━━━┸━━━┸━━━┸━━━
Size : 5
━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━
Size : 7
━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
/*
Swift Program
Print the horizontal ladder pattern
*/
class MyPattern
{
func space(_ size: Int)
{
var counter: Int = 0;
while (counter < size)
{
print(" ", terminator: "");
counter += 1;
}
}
func print_symbol(_ size: Int)
{
var counter: Int = 0;
while (counter < size)
{
print("━", terminator: "");
counter += 1;
}
}
//Display horizontal ladder pattern of given odd size
func horizontal_ladder(_ size: Int)
{
if (size < 0 || size % 2 == 0)
{
return;
}
print("\n Size : ", size ," \n\n", terminator: "");
var j: Int = 0;
var i: Int = 0;
while (i < size)
{
self.space(size / 2);
j = 0;
while (j < size + 1)
{
if (i == 0 || i + 1 == size)
{
//When displaying top and bottom layer
if (i == 0 && j != 0)
{
print("┰", terminator: "");
self.print_symbol(size);
}
else if (i != 0 && j != 0)
{
print("┸", terminator: "");
self.print_symbol(size);
}
else
{
self.print_symbol(size);
}
}
else if (j + 1 != size)
{
//print internal layer
self.space(size);
print("┃", terminator: "");
}
j += 1;
}
print("\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Test Cases
obj.horizontal_ladder(3);
obj.horizontal_ladder(5);
obj.horizontal_ladder(7);
}
main();
Output
Size : 3
━━━┰━━━┰━━━┰━━━
┃ ┃ ┃
━━━┸━━━┸━━━┸━━━
Size : 5
━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━
Size : 7
━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
Time Complexity
The time complexity of the given algorithm is O(n^2), where n is the size of the ladder. This is because we have nested loops that iterate up to the size of the ladder.
Finally
In this article, we have discussed the problem of printing a horizontal ladder pattern. We explained the problem statement, provided a suitable example, presented the algorithm and pseudocode, and analyzed the time complexity of the code. You can use the provided code to print horizontal ladder patterns of different sizes and customize it according to your needs. Happy coding!
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment