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
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
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
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
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
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
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
IntroductionPython has different approaches like using threads, subprocesses, generators and other tricks for concurrent programming.Before we go on and implement threads, let us understand what exactly is concurrency.Concurrency is a piece of logic Within a single program that allows to open up many distinct paths of execution, including separate streams of I/O, running SQL queries, so on in a way that the execution seems to be both simultaneous and independent of each other.How to do it..First we create a single thread to go through the site urls and later look at how to use threading concepts to speed up the ... Read More
IntroductionIt seems that the world is ruled by Excel. I've been surprised in my data engineering work to see how many of my colleagues are using Excel as a critical tool for making decisions. While I'm not a big fan of MS Office and their excel spread sheets, i will still show you a neat trick to handle large excel spread sheets effectively.How to do it..Before we jump into the program directly, let us understand few basics on dealing excel spreadsheets with Pandas.1. Installation. Go ahead and install openpyxl and xlwt. If you are unsure if it is installed or ... Read More
ProblemYou need to implement immutable data structures in Python.Introduction..Immutable data structures are very handy when you want to prevent multiple people modifying a piece of data in parallel programming at same time. Mutable data structures( e.g. Array) can be changed at any time while immutable data structures cannot be.How to do it..Let me show you step by step how to deal with immutable and mutable data structures.Example# STEP 01 - Create a Mutable array. # Define an array atp_players = ['Murray', 'Nadal', 'Djokovic'] print(f" *** Original Data in my array is - {atp_players}")*** Original Data in my array is ... Read More