C++ Program to Emulate N Dice Roller

Ravi Ranjan
Updated on 15-Apr-2025 15:27:13

1K+ Views

Emulating an n dice roller refers to rolling n number of dice simultaneously. In each roll, all the dice will return a different value from 1 to 6. In this article, we are having 'n' number of dices, our task is to emulate rolling n dices simultaneously. The approaches are mentioned below: Using Loop with rand() Function Using Recursion Using Loop with rand() Function In this approach, we have used the rand() function to generate a random value of dice in each roll. The srand() function with time() is ... Read More

C++ Program to Implement Merge Sort

Ravi Ranjan
Updated on 15-Apr-2025 15:26:32

46K+ Views

The merge sort technique is based on the divide and conquer technique. We divide the whole data set into smaller parts and merge them into a larger piece in sorted order. It is also very effective for worst cases because this algorithm has lower time complexity for worst cases too. In this article, we have an unsorted array of integers and our task is to implement the merge sort to sort the unsorted array. Steps to to Implement Merge Sort We will be using the steps mentioned below to implement the merge sort algorithm: ... Read More

Convert Integer to ASCII Value in Python

Akshitha Mote
Updated on 15-Apr-2025 14:44:38

2K+ Views

Converting Integer to ASCII Using chr() Function In Python, the chr() function converts an integer value to the corresponding ASCII (American Standard Code for Information Interchange) character only if the integer range lies between 0 and 127. The chr() function accepts Unicode values within the range of 0 to 1114111. If a value outside this range is provided, the function raises a ValueError. Example In the following example, we have converted integer values to ASCII values using chr() - print(chr(85)) print(chr(100)) print(chr(97)) Following is the output of the above code - U 100 a Example We can ... Read More

Behavior of ++ and Operators in Python

Akshitha Mote
Updated on 15-Apr-2025 14:28:21

311 Views

In C/C++, Java, etc., ++ and -- operators are defined as increment and decrement operators. In Python, they are not defined as operators. Increment and Decrement Operators in Python In Python, objects are stored in memory. Variables are just labels. Numeric objects are immutable. Hence, they can't be incremented or decremented. However, prefix ++ or -- doesn't give an error but doesn't perform either. As Prefix  In the following example, when have used prefix increment which does not raise an error - x=5 print(++x) print(--x) Following is the output of the above code - 5 5 As Postfix In ... Read More

Reference Python Class Attributes

Akshitha Mote
Updated on 15-Apr-2025 14:01:17

835 Views

In Python, the variables that are declared inside the class are known as attributes. These attributes can be accessed and modified by both the class and its objects. Following are the two types of attributes - Class Attributes: The attributes that are defined globally in the class are known as class attributes and can be accessed throughout the class itself and are shared between all class instances. They can be accessed using the class name. Instance Attributes: The attributes that are defined inside the __init__() method are known as instance attributes. ... Read More

Write Python Regular Expression to Get Numbers Except Decimal

SaiKrishna Tavva
Updated on 15-Apr-2025 13:00:47

249 Views

Python's, regular expressions (regex) allow us to search, extract, and manipulate patterns in strings. Sometimes, If we want to extract only numbers from a string, except decimals, then we can use re.findall() method with a specific regex pattern. The regex pattern was designed to specifically match the integer pattern. Let us first understand the regular expression used: \b\d+\b This pattern matches numbers like 10, 245, 89 but not 12.34 or 0.56 (decimals). \b : Word boundary to ensure we match standalone numbers. \d+ : One or more digits (0–9). ... Read More

Python Program to Capitalize Each Word's First Letter

Akshitha Mote
Updated on 15-Apr-2025 12:58:55

2K+ Views

Let's understand the problem statement: we need to convert a given strings to a title case, which involves capitalizing the first letter of each word while converting the remaining letters to lowercase. The following are the various ways to capitalize each word's first letter in a string - Using title() method Using capitalize() method Using upper() method Importing string module Importing regex module Capitalizing a String in a File Using the title() method to ... Read More

Check Connectivity of Undirected Graph Using BFS in C++

Vrundesha Joshi
Updated on 15-Apr-2025 12:55:54

1K+ Views

The Breadth-First Search (BFS) algorithm is a graph traversal algorithm that starts at the tree root and explores all nodes at the present depth before moving to the nodes at the next level. In this article, we will discuss how to check the connectivity of a graph using BFS traversal algorithm. Understanding Connectivity of a Graph A graph is said to be connected if there is a path between every pair of vertices. To check connectivity of a graph, we will try to traverse all nodes using any traversal algorithm. After completing the traversal, if there is any ... Read More

Concatenate str and int Objects in Python

SaiKrishna Tavva
Updated on 15-Apr-2025 12:39:50

758 Views

In many coding languages, when you try to combine a string with a number, the number is usually converted into a string automatically so both parts can be joined together. This is called implicit type conversion. With the + operator for contaminating a string with a numerical value, Python does not perform the implicit type conversion. Instead, it throws an error (specifically a TypeError) because it expects both operands to be strings. The following are the various methods to do it: Using the str() Function Using f-strings ... Read More

Write Python Regular Expression to Match File Extension

Akshitha Mote
Updated on 15-Apr-2025 12:34:02

939 Views

Using Python, we can easily search for specific types of files from a mixed list of file names using regular expressions. For this, we need to import Python’s built-in module called re using the import keyword. The Regular expression or Regex is a special sequence of characters like \, *, ^, etc, which are used to search for a pattern in a string or a set of strings. It can detect the presence or absence of characters by matching them with a particular pattern and can also split a string into one or more substrings. The following is a syntax ... Read More

Advertisements