Find Number of Subsequences with i, j, and k Letters in Python

Arnab Chakraborty
Updated on 09-Nov-2020 10:41:06

191 Views

Suppose we have a string s with "x", "y" and "z"s, we have to find the number of subsequences that have i number of "x" characters, followed by j number of "y" characters and followed by k number of "z" characters where i, j, k ≥ 1.So, if the input is like s = "xxyz", then the output will be 3, as we can make two "xyz" and one "xxyz"To solve this, we will follow these steps:n := size of sx := 0, y := 0, z := 0for i in range 0 to n, docount := 0if s[i] is ... Read More

Take HTML Input from User and Display in JavaScript

AmitDiwan
Updated on 09-Nov-2020 10:38:22

2K+ Views

The HTML input value is a string. To convert the string to integer, use parseInt().ExampleFollowing is the code − Live Demo            Document        GetANumber    function result() {       var numberValue = document.getElementById("txtInput").value;       if (!isNaN(numberValue))          console.log("The value=" + parseInt(numberValue));       else          console.log("Please enter the integer value..");    } To run the above program, save the file name “anyName.html(index.html)”. Right click on the file and select the option “Open with ... Read More

Compare Two DataFrames in Python Pandas with Missing Values

Kiran P
Updated on 09-Nov-2020 10:36:56

2K+ Views

IntroductionPandas uses the NumPy NaN (np.nan) object to represent a missing value. This Numpy NaN value has some interesting mathematical properties. For example, it is not equal to itself. However, Python None object evaluates as True when compared to itself.How to do it..Let us see some examples to understand how np.nan behaves.import pandas as pd import numpy as np # Python None Object compared against self. print(f"Output *** {None == None} ")Output*** True# Numpy nan compared against self. print(f"Output *** {np.nan == np.nan} ")Output*** False# Is nan > 10 or 1000 ? print(f"Output *** {np.nan > ... Read More

Find Next Board Position After Sliding Direction in Python

Arnab Chakraborty
Updated on 09-Nov-2020 10:36:31

190 Views

Suppose we have a 2048 game board representing the initial board and a string direction representing the swipe direction, we have to find the next board state. As we know in the 2048 game, we are given a 4 x 4 board of numbers (some of them are empty, represented in here with 0) which we can swipe in any of the 4 directions ("U", "D", "L", or "R"). When we swipe, all the numbers move in that direction as far as possible and identical adjacent numbers are added up exactly once.So, if the input is likedirection = "L", then ... Read More

Efficiently Turn All Keys of an Object to Lower Case in JavaScript

AmitDiwan
Updated on 09-Nov-2020 10:35:30

210 Views

Let’s say the following is our object −var details = {    "STUDENTNAME": "John",    "STUDENTAGE": 21,    "STUDENTCOUNTRYNAME": "US" }As you can see above, the keys are in capital case. We need to turn all these keys to lower case. Use toLowerCase() for this.ExampleFollowing is the code −var details = {    "STUDENTNAME": "John",    "STUDENTAGE": 21,    "STUDENTCOUNTRYNAME": "US" } var tempKey, allKeysOfDetails = Object.keys(details); var numberOfKey = allKeysOfDetails.length; var allKeysToLowerCase = {} while (numberOfKey--) {    tempKey = allKeysOfDetails[numberOfKey];    allKeysToLowerCase[tempKey.toLowerCase()] = details[tempKey]; } console.log(allKeysToLowerCase);To run the above program, you need to use the following command −node ... Read More

Get ID from TR Tag and Display it in a New TD with JavaScript

AmitDiwan
Updated on 09-Nov-2020 10:33:19

4K+ Views

Let’s say the following is our table −           StudentName       StudentCountryName               JohnDoe       UK               DavidMiller       US     To get id from tr tag and display it in a new td, use document.querySelectorAll(table tr).ExampleFollowing is the code − Live Demo            Document    td,    th,    table {       border: 1px solid black;       margin-left: 10px;       ... Read More

Write Functions in Python that Accept Any Number of Arguments

Kiran P
Updated on 09-Nov-2020 10:32:00

2K+ Views

ProblemYou want to write a function that accepts any number of input arguments.SolutionThe * argument in python can accepts any number of arguments. We will understand this with an example of finding out the average of any given two or more numbers. In the below example, rest_arg is a tuple of all the extra arguments (in our case numbers) passed. The function treats the arguments as a sequence in performing average calculation.# Sample function to find the average of the given numbers def define_average(first_arg, *rest_arg): average = (first_arg + sum(rest_arg)) / (1 + len(rest_arg)) print(f"Output *** The average for ... Read More

Use the Subprocess Module in Python

Kiran P
Updated on 09-Nov-2020 10:29:33

1K+ Views

Understanding Process -When you code and execute a program on Windows, MAC or Linux, your Operating System creates a process(single).It uses system resources like CPU, RAM, Disk space and also data structures in the operating system’s kernel. A process is isolated from other processes—it can’t see what other processes are doing or interfere with them.Note: This code has to be run on Linux like sytems. When executed on windows might throw exceptions.Goals of Operating System -The main twin goals of OS are to spread the work of the process fairly and be responsive to the user. These are acheived by ... Read More

Convert Each Element in Row and Column to Zero for Zero Values in Python

Arnab Chakraborty
Updated on 09-Nov-2020 10:28:02

529 Views

Suppose we have a 2D matrix of numbers, now for each zero in the given matrix and replace all values in its row and column with zero, and return the final matrix.So, if the input is like matrix, then the output will be matrix as the 0th, 2nd and 3rd rows contain 0 and the final matrix contains 0 in those rows. Similarly 0th, 1st and 2nd columns contain 0 and the final matrix contains 0 in those columns.To solve this, we will follow these steps:n := row count, m := column count res := make a matrix of size ... Read More

Process Iterators in Parallel Using Zip

Kiran P
Updated on 09-Nov-2020 10:27:45

370 Views

IntroductionList comprehensions make it easy to take a source list and get a derived list by applying an expression. For example, say that I want to multiply each element in a list with 5. Here, I do this by using a simple for loop.a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] multiply_by_5 = [] for x in a: multiply_by_5.append(x*5) print(f"Output *** {multiply_by_5}")Output*** [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]With a list comprehension, I can achieve the same outcome by specifying the expression and the input sequence to loop over.# List comprehension multiply_by_5 ... Read More

Advertisements