Header Ad

HackerRank Staircase problem solution

In this HackerRank Staircase problem solution,Staircase detail

This is a staircase of size n=4:

   #

  ##

 ###

####

Its base and height are both equal to n. It is drawn using # symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size n.

Function Description

Complete the staircase function in the editor below.

staircase has the following parameter(s):

int n: an integer

Print

Print a staircase as described above.

Input Format

A single integer, n, denoting the size of the staircase.

Constraints

0 < n <= 100

Output Format

Print a staircase of size n using # symbols and spaces.

Note: The last line must have 0 spaces in it.

HackerRank Staircase problem solution


Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the staircase function below.
def staircase(n):
    for i in range(0,n):
        for j in range(0,n):
            if i + j >= n-1:
                print("#",end='') 
            else:
                print(" ",end='')
        print("\r")

if __name__ == '__main__':
    n = int(input())

    staircase(n)




Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {
    
    public static void drawStair(int size) {
        int level = size-1;
        for(int i = 0; i < size; i++) {
            StringBuilder s = new StringBuilder();
            for(int k = 0; k < level; k++) {
                s.append(" ");
            }
            for(int k = 0; k < size - level; k ++){
                s.append("#");
            }
            level -= 1;
            System.out.println(s.toString());
        }
    }

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        Solution sol = new Solution();
        
        int size = in.nextInt();
        sol.drawStair(size);
    }
}



Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n - i - 1; ++j) {
            printf(" ");
        }
        for (int j = n - i -1; j < n; ++j) {
            printf("#");
        }
        printf("\n");
    }
    return 0;
}



Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int height;
    scanf("%d",&height);
    int stair = height - 1;
    
    for (int i = 0; i < height; ++i){
        for (int j = 0; j < height; ++j){
            if (j >= stair){
                printf("#");
            } else{
                printf(" ");
            }
        }
        stair -= 1;
        printf("\n");
    }    
    return 0;
}



Problem solution in JavaScript programming.

function processData(input) {
    
    var steps = parseInt(input);
    
    for (var i = 0; i++ < steps; ) {
        
        var line = '';
        var spaces = steps - i;
        
        for (var j = spaces; j--; ) {
            line += ' ';
        }
        
        for (var j = i; j--; ) {
            line += '#';
        }
        
        console.log(line);
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});


Post a Comment

2 Comments

  1. in python i just simply used these 4 lines:

    arr.sort()
    max_sum = sum(arr[1:])
    min_sum = sum(arr[:4])
    print(f'{min_sum} {max_sum}')

    ReplyDelete
  2. Starcase solution in Javascript:

    function staircase(n) {
    for (let i = 1; i <= n; i++) {
    console.log(' '.repeat(n-i)+ '#'.repeat(i))
    }
    }
    staircase(6)

    i think that is a litle more simple

    ReplyDelete