Kelish Rai's profileExpert

Kelish Rai

Technical Content Writer @Programiz

Answered 89 questions


About

Hi, I'm Kelish, Technical Content Writer at Programiz. I break down complex programming concepts and turn them into easy-to-understand articles, tutorials, and courses. I'm also a developer at heart—I love solving coding problems, exploring algorithms, and staying updated with the latest tech stuff. If I'm not writing content, there's a good chance I'm working on a side project.

Answered by Kelish Rai
Kelish Rai
Expert
last year
Kelish Rai answered

Firstly, before approaching problems that include loops, you need to understand how loops work. For instance, here are some things to focus on when working with loops:

  1. Loop Condition – Understand when the loop starts and when it should stop. A common mistake is writing a condition that makes the loop run forever.

  2. Loop Variables – Keep track of variables that control the loop, such as counters or iterators. If they’re not updated correctly, the loop may not behave as expected.

  3. Loop Body – Make sure each iteration of the loop is getting you closer to solving the problem.

Now, let's walkthrough a problem to print the following output:

5432*
543*1
54*21
5*321
*4321

Since the output has a structured pattern (rows and columns), we need to use nested loops:

  • An outer loop to handle rows.

  • An inner loop to print elements in each row.

Let's start with a simple version of the pattern:

for i in range(1, 6):
    for j in range(5, 0, -1):
        print(j, end='')
    print()

Here, for j in range(5, 0, -1): ensures we print numbers from 5 to 1 in reverse order. The print(j, end='') ensures the numbers are printed on the same line.

Now, when we run this code, we get:

54321
54321
54321
54321
54321

Next, in the required output, in the first line, 1 is replaced with *, in the second line, 2 is replaced with *, and so on.

If we analyze the pattern, we notice that * appears when the row number matches the digit itself.

To modify our code accordingly:

for i in range(1, 6):
    for j in range(5, 0, -1):
        if i == j:
            print('*', end='')
        else:
            print(j, end='')
    print()

How this works:

  • The outer loop (for i in range(1, 6)) runs 5 times to create rows.

  • The inner loop (for j in range(5, 0, -1)) prints numbers from 5 to 1.

  • The condition if j == 5 - i: checks if the current number should be replaced with *.

  • After finishing a row, print() moves to the next line.

Output

5432*
543*1
54*21
5*321
*4321

With this approach, we've successfully generated the required pattern.

Since you're just getting started, breaking down problems like this might take some time, but as you practice more, you'll get better at recognizing patterns and solving them efficiently.

Python
This question was asked as part of the Getting started with Python course.
Kelish Rai
Expert
last year
Kelish Rai answered

That's alright. Let me break it down simply.

When writing programs, we often need to take input from the user. For example, if we want to ask for a name and then greet the user, we need a way to get that input.

In C++, we use cin to take input and store it in a variable. Here’s how it works:

#include 
using namespace std;

int main() {
    string name;

    cout << "Enter your name: ";

    // Take user's name as input
    // Store user's name in name variable
    cin >> name;

    // Greet user
    cout << "Hello, " << name;

    return 0;
}

If you run this program, you'll get this as output:

Enter your name: 

Now, you need to click on the output and enter your name. Suppose you enter John:

Enter you name: John

Now, cin >> name; stores "John" in the name variable.

Finally, cout << "Hello, " << name; prints:

Hello, John

The thing to note here is that you need to use >> when using cin and << when using cout. Otherwise, you may get an error.

C++
This question was asked as part of the Learn C++ Basics course.
Kelish Rai
Expert
last year
Kelish Rai answered

Here’s the C code for creating nodes in a linked list:

#include 
#include 

typedef struct Node {
    int data;
    struct Node* next;
} Node;

int main() {
    // Create nodes and initialize them
    Node* node1 = (Node*)malloc(sizeof(Node));
    node1->data = 11;
    node1->next = NULL;

    Node* node2 = (Node*)malloc(sizeof(Node));
    node2->data = 2;
    node2->next = NULL;

    Node* node3 = (Node*)malloc(sizeof(Node));
    node3->data = 88;
    node3->next = NULL;

    // Free allocated memory
    free(node1);
    free(node2);
    free(node3);

    return 0;
}

Here,

  • malloc() allows us to allocate memory dynamically.

  • We manually assign values to data for each node.

  • next is initialized to NULL.

  • Before using the pointers, we check if malloc() successfully allocated their memory.

  • free() is used at the end to deallocate memory and prevent memory leaks.

C++
This question was asked as part of the DSA with C++ course.
Kelish Rai
Expert
last year
Kelish Rai answered

In HTML, the slash (/) in a tag indicates that it is the closing tag for an element. This is required because HTML uses pairs of tags to define where an element starts and ends.

For example,

This is a paragraph.

Here,

marks the start of the paragraph, and

marks the end. The content in between these tags—This is a paragraph.—is defined as a paragraph.

Without the closing

tag, HTML wouldn't know where the paragraph ends, which could cause formatting issues or incorrect rendering.

Hope this clears things up. Let me know if you have more questions.

HTML
This question was asked as part of the Learn HTML course.
Kelish Rai
Expert
last year
Kelish Rai answered

One simple way to merge two dictionaries without using update() is by using the unpacking (**) syntax.

For example:

A = {12: 'Kathmandu', 11: 'London', 3: 'Sydney'}
B = {10: 'New York', 2: 'Delhi'}

AB = {**A, **B}
print(AB)

Output

{12: 'Kathmandu', 11: 'London', 3: 'Sydney', 10: 'New York', 2: 'Delhi'}

Here,

  • The {**A, **B} syntax unpacks the key-value pairs from both dictionaries A and B and combines them into a new dictionary, AB.

  • This method will include all keys and values from both dictionaries.

Note: If there are overlapping keys, the value from the second dictionary (B) will overwrite the value from the first dictionary (A). For example,

A = {1: 'apple', 2: 'banana'}
B = {2: 'orange', 3: 'grape'}

AB = {**A, **B}
print(AB)

Output

{1: 'apple', 2: 'orange', 3: 'grape'}

Notice that key 2 has the value 'orange' from dictionary B, overwriting 'banana' from dictionary A.

In conclusion, the ** syntax is a very clean and Pythonic way to merge dictionaries, especially when you want to avoid using update().

Python
This question was asked as part of the Practice: Python Basics course.
Kelish Rai
Expert
last year
Kelish Rai answered

The difference between SQL and SQL server is simple:

SQL (Structured Query Language) is a language used to interact with databases. It allows you to retrieve, insert, update, and delete data. This is what you'll be learning in this course.

SQL server, on the other hand, is a database management system (DBMS). It's a software that stores, organizes, and processes data, allowing you to execute SQL commands efficiently.

For example,

SELECT * FROM Customers;

If you run this command, SQL Server is the system that reads this SQL command, fetches the data from the database, and gives you the result.

Think of it like this: SQL is the language, and SQL server is a tool that understands and runs SQL commands.

Note: Besides SQL Server, there are other popular database systems too like MySQL, PostgreSQL, and Oracle DB. They all use SQL (with slight differences), but are different pieces of software.

SQL
This question was asked as part of the Learn SQL Basics course.
Kelish Rai
Expert
last year
Kelish Rai answered

If you're asking about spaces within a single line of code, like this:

age = 19

or

age     =     19

Then no, the extra spaces don’t matter—both lines work the same way.

However, spaces inside a string do matter:

"My age is:"

is not the same as

"My     age     is:"

In the first example, there's a single space between "My" and "age", while in the second example, multiple spaces exist between the words. Python will treat these as completely different strings.

Python Indentation

The most important aspect of spacing in Python is indentation. Python uses indentation (spaces or tabs) to define code blocks. Unlike some other programming languages that use {} or similar syntax to group code, Python relies on indentation to structure the code.

For example:

if age > 18:
    print("You are an adult!")

Notice the indentation (four spaces) before print(). This tells Python that print() is inside the if block. If the indentation is incorrect or missing, you'll get an IndentationError.

Incorrect indentation:

if age > 18:
print("You are an adult!")  # This will cause an error

Python requires consistent indentation, usually four spaces per level, to properly understand the structure of your code.

Key Takeaways:

  • Spaces around operators in expressions generally don’t matter.

  • Spaces inside strings are important.

  • Indentation is crucial for defining code blocks in Python and will cause errors if incorrect.

Python
This question was asked as part of the Getting started with Python course.
Kelish Rai
Expert
last year
Kelish Rai answered

That's right. Quadratic time complexity (O(n²)) is commonly associated with nested loops, where the number of operations grows in proportion to the square of the input size.

For example, consider this Python code:

# This function runs n * n times
def print_pairs(arr):
    for i in range(len(arr)):
        for j in range(len(arr)):  
            print(i, j)

lst = [1, 2, 3, 4, 5]

print_pairs()

Here, for every element in the list, another loop runs through all elements again, leading to O(n²) complexity.

Python
This question was asked as part of the Complexity Calculation course.
Vivaan KOHLI
last year
Vivaancountry asked
Kelish Rai
Expert
last year
Kelish Rai answered

Simply put, variables are containers for data.

For example, let's say your favorite book is Harry Potter and the Sorcerer’s Stone, and you want to use its name multiple times in a program. Instead of typing it every time like this:

print("Harry Potter and the Sorcerer’s Stone")
print("Harry Potter and the Sorcerer’s Stone")
print("Harry Potter and the Sorcerer’s Stone")

You can store it in a variable and use it whenever needed:

book = "Harry Potter and the Sorcerer’s Stone"

print(book)
print(book)
print(book)

This makes the code cleaner, easier to work with, and more efficient. If you ever need to change the book name, you only need to update the variable instead of modifying multiple lines of code.

It's okay if you don't fully understand how this works yet. As you continue with the course, you'll get a clearer understanding of variables and how they help in programming.

Let me know if anything is unclear—I’m happy to help.

Python
This question was asked as part of the Getting started with Python course.
Kelish Rai
Expert
last year
Kelish Rai answered

F-strings are particularly useful when you want to insert variables directly into a string in a clean, readable, and concise way. They allow you to easily embed expressions inside string literals, which can make your code more readable and maintainable.

For example:

name = "Alice"
age = 25

print(f"My name is {name} and I am {age} years old.")

In this case, {name} and {age} are placeholders within the string, and when the code runs, these placeholders get replaced by the actual values of the name and age variables.

Output:

My name is Alice and I am 25 years old.

The key benefits of using f-strings are:

  1. Clarity: It’s easy to understand what the code is doing.

  2. Efficiency: It's faster than using string concatenation or other string formatting methods.

  3. Flexibility: You can also perform expressions within the curly braces, not just simple variable replacements.

Example with an expression:

x = 5
y = 10
print(f"The sum of {x} and {y} is {x + y}.")

Output:

The sum of 5 and 10 is 15.

So, whenever you need to include variables or expressions inside a string, f-strings are a great option to use in Python.

Python
This question was asked as part of the Getting started with Python course.