Rotate Square Matrix by 90 Degrees Counterclockwise in Python

Arnab Chakraborty
Updated on 21-Oct-2020 10:32:49

2K+ Views

Suppose we have a square matrix, we have to rotate it 90 degrees counter-clockwise.147258369then the output will be789456123To solve this, we will follow these steps −if matrix is empty, thenreturn a blank listn := row count of matrixfor each row in matrix, doreverse the rowfor i in range 0 to n−1, dofor j in range 0 to i−1, doswap matrix[i, j] and matrix[j, i]return matrixLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, matrix):       if not matrix or not matrix[0]:          return []       n ... Read More

Check If Robot Can Reach Target by Moving on Visited Spots in Python

Arnab Chakraborty
Updated on 21-Oct-2020 10:28:38

260 Views

Suppose we have a robot, that is currently sitting in at position (0, 0) (Cartesian plane). If we have list of its moves that it can make, containing N(North), S(South), W(West), and E(East). However, if the robot reaches a spot it has been in before, it will continue moving in the same direction until it reaches a new unvisited spot. We have to check whether after its moves it will end at (x, y) coordinate or not.So, if the input is likemoves = ['N', 'N', 'E', 'N', 'W', 'S'], coord = [0, -1], then the output will be True, as ... Read More

Maximum Profit by Cutting Rod of Different Lengths in C++

Arnab Chakraborty
Updated on 21-Oct-2020 10:24:57

755 Views

Suppose we have a rod is given of length n. We also have a list, that contains different size and price for each size. We have to find the maximum price by cutting the rod and selling them in the market. To get the best price by making a cut at different positions and comparing the prices after cutting the rod.So, if the input is like prices = [1, 5, 8, 9, 10, 17, 17, 20], n = 8, then the output will be 22, as by cutting the rod in length 2 and 6. The profit is 5 + ... Read More

Find Minimum Number of Rocketships Needed for Rescue in Python

Arnab Chakraborty
Updated on 21-Oct-2020 10:23:11

173 Views

Suppose we have a list of numbers called weights this is representing peoples' weights and a value limit determines the weight limit of one rocket ship. Now each rocketship can take at most two people. We have to find the minimum number of rocket ships it would take to rescue everyone to Planet.So, if the input is like weights = [300, 400, 300], limit = 600, then the output will be 2, as it will take one rocket ship to take the two people whose weights are 300 each, and another to take the person whose weight is 400.To solve ... Read More

Reverse Mapping an Object in JavaScript

AmitDiwan
Updated on 20-Oct-2020 12:19:45

1K+ Views

Suppose, we have an object like this −const products = {    "Pineapple":38,    "Apple":110,    "Pear":109 };All the keys are unique in themselves and all the values are unique in themselves.We are required to write a function that accepts a value and returns its key. Let' say we have created a function findKey().For example, findKey(110) should return "Apple".We will approach this problem by first reverse mapping the values to keys and then simply using object notations to find their values.Therefore, let’s write the code for this function −ExampleThe code for this will be −const products = {    "Pineapple":38, ... Read More

Expanding Numerals in JavaScript

AmitDiwan
Updated on 20-Oct-2020 12:17:49

120 Views

We are required to write a function that, given a number, say, 123, will output an array −[100, 20, 3]Basically, the function is expected to return an array that contains the place value of all the digits present in the number taken as an argument by the function.We can solve this problem by using a recursive approach.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 123; const placeValue = (num, res = [], factor = 1) => {    if(num){       const val = (num % 10) * factor;   ... Read More

Grouping Array of Arrays on the Basis of Elements in JavaScript

AmitDiwan
Updated on 20-Oct-2020 12:15:43

212 Views

Suppose, we have an array of arrays of numbers like this −const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]];Each subarray is bound to contain strictly two elements. We are required to write a function that constructs a new array where all second elements of the subarrays that have similar first value are grouped together.So, for the array above, the output should look like −const output = [    [45, 34, 49],    [34, 67],    [78, 65] ];We can make use of the Array.prototype.reduce() method that takes help of a Map() ... Read More

Construct 2D Array Based on Constraints in JavaScript

AmitDiwan
Updated on 20-Oct-2020 12:13:21

98 Views

We are required to write a JavaScript function that creates a multi-dimensional array based on some inputs.It should take in three elements, namely −row - the number of subarrays to be present in the array, col - the number of elements in each subarray, val - the val of each element in the subarrays, For example, if the three inputs are 2, 3, 10Then the output should be −const output = [[10, 10, 10], [10, 10, 10]];Therefore, let’s write the code for this function −ExampleThe code for this will be −const row = 2; const col = 3; const val ... Read More

Construct Nested JSON Object in JavaScript

AmitDiwan
Updated on 20-Oct-2020 12:09:56

7K+ Views

We have a special kind of string that contains characters in couple, like this −const str = "AABBCCDDEE";We are required to construct an object based on this string which should look like this −const obj = {    code: "AA",    sub: {       code: "BB",       sub: {          code: "CC",          sub: {             code: "DD",             sub: {                code: "EE",                sub: {}   ... Read More

Removing Duplicates and Inserting Empty Strings in JavaScript

AmitDiwan
Updated on 20-Oct-2020 12:05:45

295 Views

We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end.For example:If we find 4 duplicate values we have to remove then all and insert four empty strings at the end.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1]; const deleteAndInsert = arr => {    const creds = arr.reduce((acc, val, ind, array) => {       let ... Read More

Advertisements