Decode Run-Length String into Normal Form in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:29:32

2K+ Views

Suppose we have a string s. The s is a run-length encoded string, we have to find the decoded version of it. As we know, run-length encoding is a fast and simple method of encoding strings. The idea is as follows − The repeated successive elements (characters) as a single count and character. For example, if the string is like "BBBBAAADDCBB" would be encoded as "4B3A2D1C2B".So, if the input is like s = "4B3A2D1C2B", then the output will be "BBBBAAADDCBB"To solve this, we will follow these steps −output := blank stringnum:= blank stringfor each character i in s, doif i ... Read More

Find Distinct Rotation Groups for Words in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:25:34

298 Views

Suppose we have rotation group for a string that holds all of its unique rotations. If the input is like, "567" then this can be rotated to "675" and "756" and they are all in the same rotation group. Now if we have a list of strings words, we have to group each word by their rotation group, and find the total number of groups.So, if the input is like words = ["xyz", "ab", "ba", "c", "yzx"], then the output will be 3, as There are three rotation groups − ["xyz", "yzx"], ["ab", "ba"], ["c"].To solve this, we will follow ... Read More

Check Every Rotation of a Number for Primality in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:23:26

708 Views

Suppose we have a number n, we have to check whether every rotation of n is prime or not.So, if the input is like n = 13, then the output will be True, as 13 is prime, 31 is also prime.To solve this, we will follow these steps −n := n as stringdo a loop for size of n times, doif n is not a prime number, thenreturn Falsen := n[from index 1 to end] concatenate first character of nreturn TrueLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, n):       ... Read More

Replace Before First Forward Slash in JavaScript

AmitDiwan
Updated on 03-Oct-2020 15:38:48

829 Views

Let’s say the following is our string with forward slash −var queryStringValue = "welcome/name/john/age/32"To replace before first forward slash, use replace() along with regular expressions.ExampleFollowing is the code −var regularExpression = /^[^/]+/ var queryStringValue = "welcome/name/john/age/32" var replacedValue = queryStringValue.replace(regularExpression, 'index'); console.log("Original value="+queryStringValue); console.log("After replacing the value="+replacedValue);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo245.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo245.js Original value=welcome/name/john/age/32 After replacing the value=index/name/john/age/32Read More

Get First Word of Object's Value in JavaScript

AmitDiwan
Updated on 03-Oct-2020 15:34:13

425 Views

Let’s say the following is our object −const employeeDetails = [    {       employeeName: "John Smith",       employeeTechnology: "JavaScript HTML"    },    {       employeeName: "David Miller",       employeeTechnology: "Java Angular"    } ]You can use split() on the basis of space.ExampleFollowing is the code −const employeeDetails = [    {       employeeName: "John Smith",       employeeTechnology: "JavaScript HTML"    },    {       employeeName: "David Miller",       employeeTechnology: "Java Angular"    } ] const objectValues = employeeDetails.map(emp => {    var ... Read More

Remove Duplicate Elements Within an Array in JavaScript

AmitDiwan
Updated on 03-Oct-2020 15:31:39

269 Views

Let’s say the following are our array elements −10,20,10,50,60,10,20,40,50To remove duplicate elements, use … new Set().ExampleFollowing is the code −var arrayWithNoDuplicateNumbers = [...new Set([10,20,10,50,60,10,20,40,50])]; console.log("No Duplicate values=") console.log(arrayWithNoDuplicateNumbers);To run the above program, use the following command −node fileName.js. Here, my file name is demo243.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo243.js No Duplicate values= [ 10, 20, 50, 60, 40 ]

Compare the Triplets in JavaScript

AmitDiwan
Updated on 03-Oct-2020 15:30:31

187 Views

For this, you need to use if condition to compare triplets.Let’s say we are passing the following values −35, 36, 37, 33, 48, 50ExampleFollowing is the code −function tripletsSolution(first, second, third, fourth, fifth, sixth) {    var storedResult = []    if (first > fourth || second > fifth || third > sixth) {       storedResult = storedResult + 1;    }    if (first < fourth || second < fifth || third < sixth) {       storedResult = storedResult + 1;    }    return storedResult.split(''); } console.log(tripletsSolution(35, 36, 37, 33, 48, 50));To run the above program, use the following command −node fileName.js.Here, my file name is demo242.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo242.js [ '1', '1' ]

Split Hyphen-Delimited String with Negative Numbers in JavaScript

AmitDiwan
Updated on 03-Oct-2020 15:25:31

468 Views

Let’s say we have the following hyphen delimited string with negative or range of numbers −var firstValue = "John-Smith-90-100-US"; var secondValue = "David-Miller--120-AUS";To split, use regular expressions. Following is the code −ExampleFollowing is the code −var firstValue = "John-Smith-90-100-US"; var secondValue = "David-Miller--120-AUS"; var regularExpression = /-(?=[A-Za-z-]|\d+-\d)/; var result1 = firstValue.split(regularExpression); var result2 = secondValue.split(regularExpression); console.log(result1); console.log(result2);To run the above program, use the following command −node fileName.js. Here, my file name is demo241.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo241.js [ 'John', 'Smith', '90-100', 'US' ] [ 'David', 'Miller', '-120', 'AUS' ]Read More

Filter and Word Replacement Method in JavaScript

AmitDiwan
Updated on 03-Oct-2020 15:24:22

279 Views

There is no in-built function to replace all occurrences of word. You need to create your own function.Let’s say the following is our string −var sentence = "Yes,  My Name is John Smith. I live in US. Yes,  My Favourite Subject is JavaScript";ExampleFollowing is the code −var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript"; console.log(sentence); function replaceYesWithHi(word,  sentence,  replaceValue) {    return sentence.split(word).join(replaceValue); } var replacementofYesWithHi = replaceYesWithHi("Yes", sentence, "Hi"); console.log(replacementofYesWithHi);To run the above program, use the following command −node fileName.js.Here, my file name is demo240.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo240.js Yes, My Name is John Smith. I live in US. Yes, My Favourite ... Read More

Convert String with Zeros to Number in JavaScript

AmitDiwan
Updated on 03-Oct-2020 15:22:19

389 Views

Let’s say the following is our string”var stringValue="453.000.00.00.0000";To convert the above string with zeroes to number, use parseInt() along with replace() −var numberValue = parseInt(stringValue.replace(/\./gm, '')); ExampleFollowing is the complete code −var stringValue="453.000.00.00.0000"; var numberValue = parseInt(stringValue.replace(/\./gm, '')); console.log("Original value="+stringValue) console.log("After modification the value="+numberValue);To run the above program, use the following command −node fileName.js. Here, my file name is demo239.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo239.js Original value=453.000.00.00.0000 After modification the value=45300000000000

Advertisements