Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 46 of 2547
Python Program to Rotate dictionary by K
Python dictionaries are ordered collections of key-value pairs. Sometimes you need to rotate a dictionary by K positions, which means shifting all key-value pairs by K positions in their order. This article demonstrates how to rotate a dictionary by K positions using two different approaches. Understanding Dictionary Rotation Dictionary rotation shifts all key-value pairs by K positions. For example, rotating {2: 5, 4: 6, 1: 3} by 1 position gives {1: 3, 2: 5, 4: 6}. Example Input and Output # Input dictionary inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1} ...
Read MorePython Program to Retain first N Elements of a String and Replace the Remaining by K
In this article, we will learn how to retain the first N characters of a string and replace the remaining characters with a specified character K. Python provides several built-in functions like len(), slicing, replace(), and ljust() to accomplish this task efficiently. Problem Overview Given an input string, we need to keep the first N characters unchanged and replace all remaining characters with a replacement character K. Example Let's understand this with a simple example ? # Input input_string = 'tutorialspoint' n = 9 k = "#" # Expected output: 'tutorials#####' print(f"Original: {input_string}") ...
Read MorePython Program to Replace occurrences by K except first character
Python provides several methods to replace all occurrences of a character except the first occurrence. This is useful when you want to preserve the first instance while modifying subsequent occurrences. In Python, strings are immutable sequences of characters. When we need to replace specific occurrences, we can use built-in functions like slicing() and replace() or implement custom logic using loops. Problem Statement Given an input string and a replacement character k, replace all occurrences of the first character except the very first occurrence. Example For the string 'blueforblueforblue' with replacement character '*' ? ...
Read MorePython Program to Find Numbers in Range and not in Set
In Python, we can find numbers that exist within a specific range but are not present in a given set using several approaches: the not operator, set subtraction, or the Counter function. A Python set is a collection of unordered, unique elements. Sets are mutable, meaning you can add or remove elements after creation, but the elements themselves must be immutable and distinct. Problem Example Given an input set and a range, find all numbers in the range that don't exist in the set ? Input input_set = {3, 10, 2, 11, 15, 4, ...
Read MorePython Program to Extract Keys with specific Value Type
Python's implementation of a data structure known more commonly as an associative array is a dictionary. A dictionary is made up of a group of key-value pairs. Each key-value combination corresponds to a key and its corresponding value. In this article, we will learn how to extract keys with specific value type in Python using three different approaches. Methods Used The following are the various methods to accomplish this task − Using for loop and isinstance() method Using list comprehension and isinstance() method Using keys() & type() methods Problem Example Let's consider ...
Read MorePython Program to Expand Character Frequency String
A character frequency string contains characters followed by their frequency count. For example, 'p5y3t6' means 'p' appears 5 times, 'y' appears 3 times, and 't' appears 6 times. Expanding this string produces 'pppppyyytttttt'. In this article, we will learn how to expand character frequency strings using different Python approaches. Problem Statement Given an input string where characters alternate with their frequencies, expand each character according to its frequency count ? Example inputString = 'p5y3t6h2o1n4' print("Input:", inputString) print("Expected Output: pppppyyytttttthhonnnn") Input: p5y3t6h2o1n4 Expected Output: pppppyyytttttthhonnnn Method 1: Using zip() and ...
Read MorePython Program to Concatenate all keys which have similar values
Python's implementation of a data structure known more commonly as an associative array is a dictionary. A dictionary is made up of a group of key-value pairs. Each key-value combination corresponds to a key and its corresponding value. In this article, we will learn how to concatenate all keys which have similar values in Python ? Methods Used The following are the various methods to accomplish this task: Using defaultdict() and join() functions Using dictionary comprehension, groupby() and join() functions Example Assume we have taken an input dictionary. We will now concatenate ...
Read MorePython Program to Add K between case shifts
In Python, we can add a symbol between case shifts (transitions from uppercase to lowercase or vice versa) using built-in string methods like isupper(), islower(), and join(). A case shift occurs when consecutive characters change from uppercase to lowercase or lowercase to uppercase. Understanding Case Shifts Let's see what case shifts look like with an example ? # Example of case shifts in a string text = "tUtoRialsPoInt" print("Original string:", text) print("Case shifts occur between:") print("t -> U (lowercase to uppercase)") print("U -> t (uppercase to lowercase)") print("o -> R (lowercase to uppercase)") ...
Read MorePrint all repeating digits present in a given number in sorted order
Finding repeating digits in a number is a common programming task. Python provides several built-in functions like count(), Counter(), and operator.countOf() that can efficiently identify and sort duplicate digits. Problem Statement Given a number, we need to find all digits that appear more than once and display them in sorted order. Input inputNum = 5322789124199 Expected Output 1 2 9 In the input number 5322789124199, digits 1, 2, and 9 appear multiple times, so they are displayed in ascending order. Method 1: Using count() Function The count() method returns ...
Read MoreHow to Convert Dictionary to Concatenated String using Python?
A dictionary in Python is a built-in data structure that allows you to store and retrieve values using unique keys. Converting a dictionary to a concatenated string means combining all keys and values into a single string without separators. For example ? Input = {'one': 1, 'two': 2, 'three': 3} Output = one1two2three3 Let's explore different methods to achieve this conversion. Using a Loop and F-string This method iterates through the dictionary's key-value pairs and concatenates them using an f-string ? dictionary = {'one': 1, 'two': 2, 'three': 3} concatenated_string = ...
Read More