Found 34469 Articles for Programming

How to Get the Nth Word in a Given String using Python?

Rohan Singh
Updated on 18-Jul-2023 16:04:43

660 Views

We can get the Nth Word in a Given String in Python using string splitting, regular expressions, split() method, etc. Manipulating strings is a common task in programming, and extracting specific words from a string can be particularly useful in various scenarios. In this article, we will explore different methods to extract the Nth word from a given string using Python. Method 1:Splitting the String This method involves splitting the string into a list of words and accessing the desired word based on its index. Syntax words = string.split() Here, the split() method splits the string based on whitespace ... Read More

Selfish Round Robin CPU Scheduling

Pranavnath
Updated on 18-Jul-2023 16:27:40

405 Views

Scheduling algorithms in the operating system execute the processes based on their arrival time or by their priority. Each algorithm chooses the processes that are in waiting to ready queue by preemption or non-preemption methods. Preemptive algorithms provide access to the CPU to the process which has higher priority and preempt any other process which is already running with lower priority. But in the case of non-preemptive scheduling, when processes start their execution, it cannot be preemptive even when higher priority processes are in the ready state. The traditional round-robin scheduling algorithm is a preemptive one where each process gets ... Read More

Sum of numbers formed by consecutive digits present in a given string

Shubham Vora
Updated on 18-Jul-2023 17:31:33

223 Views

Problem Statement We have given a string str containing the numeric and alphabetical characters. We need to find the sum of all numbers represented by a continuous sequence of digits available in the given string. Sample Examples Input str = “12were43” Output 55 Explanation The sum of 12 and 43 is equal to 55. Input str = “1a2c3d” Output 6 Explanation The sum of 1, 2, and 3 is 6. Input str = “werderfrewsf” Output 0 Explanation It gives 0 in the output as the string contains no digit. Our logic to solve the ... Read More

Modify a Sentence By Reversing Order Of Occurrences Of All Palindrome Words

Shubham Vora
Updated on 18-Jul-2023 17:27:29

97 Views

Problem Statement We have given a string str containing a total of N words. We need to find all palindrome words in the given string and create a new string by reversing the order of all palindrome words. Sample Examples Input str = ‘nayan was gone to navjivan eye hospital’ Output ‘eye was gone to navjivan nayan hospital’ Explanation The string contains three palindrome words: nayan, navjivan, and eye. We have reversed the order of all three words and kept all other words the same. Input ‘Hello, users! How are you?’ Output ‘Hello, users! How are you?’ ... Read More

Minimum Removals Required To Place All 0s Before 1s In a Binary String

Shubham Vora
Updated on 18-Jul-2023 17:25:35

138 Views

Problem Statement We have given binary string str, and we require to remove minimum characters from the string such that we can place all zeros before 1. Sample Examples Input str = ‘00110100111’ Output 3 Explanation Here, we can achieve output 3 in two ways. We can either remove arr[2], arr[3], and arr[5] or arr[4], arr[6], and arr[7] from the string. Input str = ‘001101011’ Output 2 Explanation We can remove arr[4] and arr[6] to place all zeros before 1. Input str = ‘000111’ Output 0 Explanation In the given str, all ... Read More

Python - Leaf and Non-Leaf Nodes Dictionary

Asif Rahaman
Updated on 18-Jul-2023 14:43:05

218 Views

Dictionary is an important data structure in Python which consists of key value pairs. They were originally designed to be non−iterable objects. However recently Python extended its support for iteration of the dictionary objects too. A nested dictionary has nodes, leaf, non−nodes etc. In this article we will understand how to manipulate leaf and non−leaf nodes using dictionaries in Python. What are leaf and non leaf nodes? Before we deep dive into the codes we first need to understand what are the leaf and non leaf nodes in dictionary data types. Leaf node:Those nodes which do not ... Read More

Minimum Number That Can Be Obtained By Applying '+' And '*' Operations On Array Elements

Shubham Vora
Updated on 18-Jul-2023 17:16:15

58 Views

Problem Statement We have given an array of length ‘N’ containing some positive integers. Also, we have given the string of length ‘N-1’ containing only ‘*’ and ‘+’ characters, where ‘*’ is a multiplication operator, and ‘+’ is an addition operator. We require to perform the arithmetic operation with array elements in such a way that we can get a minimum positive integer value. Sample Examples Input array = [1, 2, 3], str = “*+” Output 5 Explanation It is the resultant value of ((1*2) + 3). Input array = [3, 3, 3, 3], str = ‘*++’ ... Read More

Python - Lambda function to find the smaller value between two elements

Asif Rahaman
Updated on 18-Jul-2023 14:41:34

226 Views

Lambda functions are popular functions in Python which have no name. They are designed to help us when we need to apply any small operation but we know we won't be reusing the code elsewhere. In this article we will learn how to find the smaller value between two elements using the lambda function. We will explore several methods like min, sorted etc along with the lambda function to perform the same. Using The if else Statement The if else statement is called the conditional operator in Python. It gives us the ability to make certain decisions based on certain ... Read More

Python - Lambda Function to Check if a value is in a List

Asif Rahaman
Updated on 18-Jul-2023 14:39:21

1K+ Views

Lambda functions in Python are nameless functions which are useful when we want to combine multiple expressions in a single line. The lambda function is convenient when we are sure that we won't be reusing the code anywhere else in the program. If however the expression is long we may want to switch to regular functions instead. In this article we will understand how to check if a value exists in a list using the lambda function. Using The filter Method The filter method in Python is a built−in function which creates new elements in the list depending upon certain ... Read More

Make a String Non-Palindromic By Inserting a Given Character

Shubham Vora
Updated on 18-Jul-2023 17:13:31

78 Views

Problem Statement We have given a string str and character c in the input. We need to insert the given character c in the string at the index so that we can convert the string to non-palindromic. If we can’t convert the string to non-palindrome, print ‘-1’. Sample Examples Input str = ‘nayan’, c = ‘n’ Output ‘nnayan’ Explanation There can be multiple output strings as we can insert ‘n’ at any index in the given string. So, the output string can be ‘nnayan’, ‘nanyan’, ‘naynan’, ‘nayann’, etc. Input str = ‘sss’, c = ‘s’ Output ‘-1’ ... Read More

Advertisements