Get Keyword Before a Function in a Class - JavaScript

AmitDiwan
Updated on 09-Nov-2020 11:08:56

458 Views

The get keyword can be used as a getter function like C#, Java and other technologies.We set a function with get like the following in a class −class Employee {    constructor(name) {    this.name = name;    }    get fullName() {       return this.name;    } }ExampleFollowing is the code displaying an example of get −class Employee {    constructor(name) {       this.name = name;    }    get fullName() {       return this.name;    } } var employeeObject = new Employee("David Miller"); console.log(employeeObject.fullName);To run the above program, you need to use ... Read More

Matrix Multiplication Result of Two 2D Arrays in JavaScript

AmitDiwan
Updated on 09-Nov-2020 11:06:59

361 Views

We are required to write a JavaScript function that takes in two 2-D arrays of numbers and returns their matrix multiplication result.Let’s say the following are our two matrices −// 5 x 4 let a = [    [1, 2, 3, 1],    [4, 5, 6, 1],    [7, 8, 9, 1],    [1, 1, 1, 1],    [5, 7, 2, 6] ]; // 4 x 6 let b = [    [1, 4, 7, 3, 4, 6],    [2, 5, 8, 7, 3, 2],    [3, 6, 9, 6, 7, 8],    [1, 1, 1, 2, 3, 6] ];ExampleLet’s ... Read More

Return a Split Array of String Based on Specified Separators in JavaScript

AmitDiwan
Updated on 09-Nov-2020 11:03:56

136 Views

We are required to write a JavaScript function that takes in a string and any number of characters specified as separators. Our function should return a splitted array of the string based on all the separators specified.For example −If the string is −const str = 'rttt.trt/trfd/trtr, tr';And the separators are −const sep = ['/', '.', ', '];Then the output should be −const output = [ 'rttt', 'trt', 'trfd', 'trtr' ];ExampleFollowing is the code −const str = 'rttt.trt/trfd/trtr, tr'; const splitMultiple = (str, ...separator) => {    const res = [];    let start = 0;    for(let i = 0; ... Read More

Improve File Reading Performance in Python with mmap Function

Kiran P
Updated on 09-Nov-2020 11:02:12

773 Views

Introduction..MMAP abbreviated as memory mapping when mapped to a file uses the operating systems virtual memory to access the data on the file system directly, instead of accessing the data with the normal I/O functions. There by improving the I/O performance as it does not require either making a separate system call for each access or copying data between buffers.To a matter of fact anything in memory for instance a SQLlite database when created in-memeory has better performance compared to on disk.Memory-mapped files can be treated as mutable strings or file-like objects, depending on what you want to do.MMAP supports ... Read More

Find Element Appearing Odd Times in JavaScript

AmitDiwan
Updated on 09-Nov-2020 11:01:20

361 Views

Given an array of integers, we are required to write a function that takes this array and finds the one element that appears an odd number of times. There will always be only one integer that appears an odd number of times.We will approach this problem by sorting the array. Once sorted, we can iterate over the array to pick the element that appears for odd number of times.ExampleFollowing is the code −const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOdd = arr => {    let ... Read More

Read Text Files Using Linecache in Python

Kiran P
Updated on 09-Nov-2020 10:59:40

583 Views

Solution..The linecache module implements cache which holds the contents of files, parsed into separate lines, in memory. linecache module returns line/s by indexing into a list, and saves time over repeatedly reading the file and parsing lines to find the one desired.lincecache module is very useful when looking for multiple lines from the same file.Prepare test data. You can get this text by just using Google and searching for sample text.Lorem ipsum dolor sit amet, causae apeirian ea his, duo cu congue prodesset. Ut epicuri invenire duo, novum ridens eu has, in natum meliore noluisse sea. Has ei stet explicari. ... Read More

Compare Files in Python

Kiran P
Updated on 09-Nov-2020 10:57:07

9K+ Views

Problem.You need to compare files in Python.Solution..The filecmp module in python can be used to compare files and directories. 1.cmp(file1, file2[, shallow])filecmp Compares the files file1 and file2 and returns True if identical, False if not. By default, files that have identical attributes as returned by os.stat() are considered to be equal. If shallow is not provided (or is True), files that have the same stat signature are considered equal.cmpfiles(dir1, dir2, common[, shallow])Compares the contents of the files contained in the list common in the two directories dir1 and dir2. cmpfiles returns a tuple containing three lists - match, mismatch, ... Read More

Scrape Through Media Files in Python

Kiran P
Updated on 09-Nov-2020 10:53:30

158 Views

IntroductionIn a real world corporate business setting, most data may not be stored in text or Excel files. SQL-based relational databases such as Oracle, SQL Server, PostgreSQL, and MySQL are in wide use, and many alternative databases have become quite popular.The choice of database is usually dependent on the performance, data integrity, and scalability needs of an application.How to do it..In this example we will how to create a sqlite3 database. sqllite is installed by default with python installation and doesn't require any further installations. If you are unsure please try below. We will also import Pandas.Loading data from SQL ... Read More

Replace Multiple Instances of Text Surrounded by Specific Characters in JavaScript

AmitDiwan
Updated on 09-Nov-2020 10:53:16

309 Views

Let’s say the following is our string. Some text is surrounded by special character hash(#) −var values = "My Name is #yourName# and I got #marks# in JavaScript subject";We need to replace the special character with valid values. For this, use replace() along with shift().ExampleFollowing is the code −var values = "My Name is #yourName# and I got #marks# in JavaScript subject"; const originalValue = ["David Miller", 97]; var result = values.replace(/#([^#]+)#/g, _ => originalValue.shift()); console.log(result);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo298.js.OutputThis will produce the following output ... Read More

Find Start Indices of All Anagrams of a String in Python

Arnab Chakraborty
Updated on 09-Nov-2020 10:51:25

264 Views

Suppose we have two strings S and T, we have to find all the start indices of S's anagrams in T. The strings consist of lowercase letters only and the length of both strings S and T will not be larger than 20 and 100.So, if the input is like S = "cab" T = "bcabxabc", then the output will be [0, 1, 5, ], as the substrings "bca", "cab" and "abc".To solve this, we will follow these steps:Define a map m, n := size of s, set left := 0, right := 0, counter := size of pdefine an ... Read More

Advertisements