Posted on by Kalkicode
Code Pattern

Print the ladder pattern

In this article, we will discuss the ladder pattern and provide an explanation of the problem, along with suitable examples. We will also include the algorithm, pseudocode, and the resultant output of the code, along with an explanation of each statement. Let's get started!

Introduction to the Problem

The ladder pattern is a simple pattern consisting of horizontal and vertical lines, forming a ladder-like structure. The pattern is created by repeating a series of steps and layers. Each step has a symbol, and the layers consist of spaces and symbols arranged in a specific pattern.

Problem Statement

Given an odd size, we need to print the ladder pattern using ASCII characters. The ladder pattern consists of multiple steps and layers. Each step is represented by a vertical line symbol, and the layers are composed of spaces and symbols. The number of steps and layers depends on the size provided.

For example, if the size is 3, the ladder pattern will have three steps and six layers. Each step will be represented by the symbol "┃", and the layers will consist of spaces and the symbol "━". The output for size 3 would be:

    Size: 3

    ┃   ┃
    ┃━━━┃
    ┃   ┃
    ┃━━━┃
    ┃   ┃
    ┃━━━┃
    ┃   ┃
  

The pattern starts with a step, followed by the layers. After the first step, we alternate between two different layers. One layer has spaces on both sides of the symbol, and the other layer has symbols throughout its length. This alternating pattern continues until all the steps and layers are printed.

Algorithm and Pseudocode

Here is the algorithm for printing the ladder pattern:

  1. Check if the given size is negative or even. If it is, exit the function.
  2. Print the size of the ladder.
  3. Initialize two loop variables, j and i, to 0.
  4. Iterate over the number of steps, from 0 to size-1:
    1. If j is greater than 0, print a step consisting of spaces, a vertical line symbol, the layer symbol, and another vertical line symbol.
    2. Increment j to skip the next iteration (used to combine two step ladder patterns).
    3. Iterate over the number of layers, from 0 to size-1:
      1. Print the bottom layers consisting of spaces, a vertical line symbol, and either the layer symbol or spaces depending on the current position.

Below is the pseudocode for the ladder pattern:

procedure space(size)
	for counter = 0 to size-1
	print " "
	end for
end procedure

procedure print_symbol(size)
	for counter = 0 to size-1
	print "━"
	end for
end procedure

procedure print_ladder(size)
	if size < 0 or size is even then
	return
	end if

	print "Size: " + size

	for j = 0 to size-1
	if j > 0 then
		space(2)
		print "┃"
		print_symbol(size)
		print "┃"
		print newline
		increment j
	end if

	for i = 0 to size-1
		space(2)
		print "┃"
		if i is equal to (size / 2) then
		print_symbol(size)
		else
		space(size)
		end if
		print "┃"
		print newline
	end for
	end for
end procedure

main
	print_ladder(3)
	print_ladder(5)
	print_ladder(7)
end main
  

Code Solution

Here given code implementation process.

//C Program 
//Print the 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 ladder pattern of given odd size
void print_ladder(int size)
{
	if (size < 0 || size % 2 == 0)
	{
		return;
	}
	printf("\n  Size : %d \n\n", size);
	int i = 0, j = 0;
	for (j = 0; j < size; j++)
	{
		if (j > 0)
		{
			//This condition are used to combine two step ladder pattern
			space(2);
			printf("┃");
			print_symbol(size);
			printf("┃");
			printf("\n");
			j++;
		}
		for (i = 0; i < size; i++)
		{
			//print the bottom layers
			space(2);
			printf("┃");
			if (i == (size / 2))
			{
				print_symbol(size);
			}
			else
			{
				space(size);
			}
			printf("┃");
			printf("\n");
		}
	}
}
int main()
{
	//Test Cases
	print_ladder(3);
	print_ladder(5);
	print_ladder(7);
	return 0;
}

Output

  Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃

  Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

  Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
/*
  Java Program
  Print the 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 ladder pattern of given odd size
	public void print_ladder(int size)
	{
		if (size < 0 || size % 2 == 0)
		{
			return;
		}
		System.out.print("\n Size : " + size + " \n\n");
		int i = 0, j = 0;
		for (j = 0; j < size; j++)
		{
			if (j > 0)
			{
				//This condition are used to combine two step ladder pattern
				space(2);
				System.out.print("┃");
				print_symbol(size);
				System.out.print("┃");
				System.out.print("\n");
				j++;
			}
			for (i = 0; i < size; i++)
			{
				//print the bottom layers
				space(2);
				System.out.print("┃");
				if (i == (size / 2))
				{
					print_symbol(size);
				}
				else
				{
					space(size);
				}
				System.out.print("┃");
				System.out.print("\n");
			}
		}
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Cases
		obj.print_ladder(3);
		obj.print_ladder(5);
		obj.print_ladder(7);
	}
}

Output

 Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
/*
  C++ Program
  Print the 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 ladder pattern of given odd size
	void print_ladder(int size)
	{
		if (size < 0 || size % 2 == 0)
		{
			return;
		}
		cout << "\n Size : " << size << " \n\n";
		int i = 0, j = 0;
		for (j = 0; j < size; j++)
		{
			if (j > 0)
			{
				//This condition are used to combine two step ladder pattern
				this->space(2);
				cout << "┃";
				this->print_symbol(size);
				cout << "┃";
				cout << "\n";
				j++;
			}
			for (i = 0; i < size; i++)
			{
				//print the bottom layers
				this->space(2);
				cout << "┃";
				if (i == (size / 2))
				{
					this->print_symbol(size);
				}
				else
				{
					this->space(size);
				}
				cout << "┃";
				cout << "\n";
			}
		}
	}
};
int main()
{
	MyPattern obj = MyPattern();
	//Test Cases
	obj.print_ladder(3);
	obj.print_ladder(5);
	obj.print_ladder(7);
	return 0;
}

Output

 Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
/*
  C# Program
  Print the 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 ladder pattern of given odd size
	public void print_ladder(int size)
	{
		if (size < 0 || size % 2 == 0)
		{
			return;
		}
		Console.Write("\n Size : " + size + " \n\n");
		int i = 0, j = 0;
		for (j = 0; j < size; j++)
		{
			if (j > 0)
			{
				//This condition are used to combine two step ladder pattern
				space(2);
				Console.Write("┃");
				print_symbol(size);
				Console.Write("┃");
				Console.Write("\n");
				j++;
			}
			for (i = 0; i < size; i++)
			{
				//print the bottom layers
				space(2);
				Console.Write("┃");
				if (i == (size / 2))
				{
					print_symbol(size);
				}
				else
				{
					space(size);
				}
				Console.Write("┃");
				Console.Write("\n");
			}
		}
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Cases
		obj.print_ladder(3);
		obj.print_ladder(5);
		obj.print_ladder(7);
	}
}

Output

 Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
<?php
/*
  Php Program
  Print the 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 ladder pattern of given odd size
	public	function print_ladder($size)
	{
		if ($size < 0 || $size % 2 == 0)
		{
			return;
		}
		echo("\n Size : ". $size ." \n\n");
		$i = 0;
		$j = 0;
		for ($j = 0; $j < $size; $j++)
		{
			if ($j > 0)
			{
				//This condition are used to combine two step ladder pattern
				$this->space(2);
				echo("┃");
				$this->print_symbol($size);
				echo("┃");
				echo("\n");
				$j++;
			}
			for ($i = 0; $i < $size; $i++)
			{
				//print the bottom layers
				$this->space(2);
				echo("┃");
				if ($i == (intval($size / 2)))
				{
					$this->print_symbol($size);
				}
				else
				{
					$this->space($size);
				}
				echo("┃");
				echo("\n");
			}
		}
	}
}

function main()
{
	$obj = new MyPattern();
	//Test Cases
	$obj->print_ladder(3);
	$obj->print_ladder(5);
	$obj->print_ladder(7);
}
main();

Output

 Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
/*
  Node Js Program
  Print the 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 ladder pattern of given odd size
	print_ladder(size)
	{
		if (size < 0 || size % 2 == 0)
		{
			return;
		}
		process.stdout.write("\n Size : " + size + " \n\n");
		var i = 0;
		var j = 0;
		for (j = 0; j < size; j++)
		{
			if (j > 0)
			{
				//This condition are used to combine two step ladder pattern
				this.space(2);
				process.stdout.write("┃");
				this.print_symbol(size);
				process.stdout.write("┃");
				process.stdout.write("\n");
				j++;
			}
			for (i = 0; i < size; i++)
			{
				//print the bottom layers
				this.space(2);
				process.stdout.write("┃");
				if (i == (parseInt(size / 2)))
				{
					this.print_symbol(size);
				}
				else
				{
					this.space(size);
				}
				process.stdout.write("┃");
				process.stdout.write("\n");
			}
		}
	}
}

function main(args)
{
	var obj = new MyPattern();
	//Test Cases
	obj.print_ladder(3);
	obj.print_ladder(5);
	obj.print_ladder(7);
}
main();

Output

 Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
# Python 3 Program
# Print the 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 ladder pattern of given odd size
	def print_ladder(self, size) :
		if (size < 0 or size % 2 == 0) :
			return
		
		print("\n Size : ", size ," \n\n", end = "")
		i = 0
		j = 0
		while (j < size) :
			if (j > 0) :
				# This condition are used to combine two step ladder pattern
				self.space(2)
				print("┃", end = "")
				self.print_symbol(size)
				print("┃", end = "")
				print("\n", end = "")
				j += 1
			
			i = 0
			while (i < size) :
				# print the bottom layers
				self.space(2)
				print("┃", end = "")
				if (i == (int(size / 2))) :
					self.print_symbol(size)
				else :
					self.space(size)
				
				print("┃", end = "")
				print("\n", end = "")
				i += 1
			
			j += 1
		
	

def main() :
	obj = MyPattern()
	# Test Cases
	obj.print_ladder(3)
	obj.print_ladder(5)
	obj.print_ladder(7)


if __name__ == "__main__": main()

Output

 Size :  3

  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size :  5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size :  7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
# Ruby Program
# Print the 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 ladder pattern of given odd size
	def print_ladder(size)
	
		if (size < 0 || size % 2 == 0)
		
			return
		end
		print("\n Size : ", size ," \n\n")
		i = 0
		j = 0
		while (j < size)
		
			if (j > 0)
			
				# This condition are used to combine two step ladder pattern
				self.space(2)
				print("┃")
				self.print_symbol(size)
				print("┃")
				print("\n")
				j += 1
			end
			i = 0
			while (i < size)
			
				# print the bottom layers
				self.space(2)
				print("┃")
				if (i == (size / 2))
				
					self.print_symbol(size)
				else
				
					self.space(size)
				end
				print("┃")
				print("\n")
				i += 1
			end
			j += 1
		end
	end
end
def main()

	obj = MyPattern.new()
	# Test Cases
	obj.print_ladder(3)
	obj.print_ladder(5)
	obj.print_ladder(7)
end
main()

Output

 Size : 3 

  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size : 5 

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size : 7 

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
/*
  Scala Program
  Print the 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 ladder pattern of given odd size
	def print_ladder(size: Int): Unit = {
		if (size < 0 || size % 2 == 0)
		{
			return;
		}
		print("\n Size : " + size + " \n\n");
		var i: Int = 0;
		var j: Int = 0;
		while (j < size)
		{
			if (j > 0)
			{
				//This condition are used to combine two step ladder pattern
				space(2);
				print("┃");
				print_symbol(size);
				print("┃");
				print("\n");
				j += 1;
			}
			i = 0;
			while (i < size)
			{
				//print the bottom layers
				space(2);
				print("┃");
				if (i == ((size / 2).toInt))
				{
					print_symbol(size);
				}
				else
				{
					space(size);
				}
				print("┃");
				print("\n");
				i += 1;
			}
			j += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Test Cases
		obj.print_ladder(3);
		obj.print_ladder(5);
		obj.print_ladder(7);
	}
}

Output

 Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
/*
  Swift Program
  Print the 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 ladder pattern of given odd size
	func print_ladder(_ size: Int)
	{
		if (size < 0 || size % 2 == 0)
		{
			return;
		}
		print("\n Size : ", size ," \n\n", terminator: "");
		var i: Int = 0;
		var j: Int = 0;
		while (j < size)
		{
			if (j > 0)
			{
				//This condition are used to combine two step ladder pattern
				self.space(2);
				print("┃", terminator: "");
				self.print_symbol(size);
				print("┃", terminator: "");
				print("\n", terminator: "");
				j += 1;
			}
			i = 0;
			while (i < size)
			{
				//print the bottom layers
				self.space(2);
				print("┃", terminator: "");
				if (i == (size / 2))
				{
					self.print_symbol(size);
				}
				else
				{
					self.space(size);
				}
				print("┃", terminator: "");
				print("\n", terminator: "");
				i += 1;
			}
			j += 1;
		}
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Test Cases
	obj.print_ladder(3);
	obj.print_ladder(5);
	obj.print_ladder(7);
}
main();

Output

 Size :  3

  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size :  5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size :  7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃

For each step, it prints the spaces, the vertical line symbol, the layer symbol (if required), and another vertical line symbol. Then it moves to the next line to print the bottom layers. It prints the spaces, the vertical line symbol, and either the layer symbol or spaces, depending on the current position.

The pattern repeats until all the steps and layers are printed. The same process is followed for the test cases with sizes 5 and 7, resulting in the respective ladder patterns.

The time complexity of the code is O(n^2), where n is the size of the ladder. This is because there are nested loops that iterate n times, resulting in a quadratic time complexity.

Comment

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