Get Root Directory Path of a PHP Project

AmitDiwan
Updated on 12-Oct-2020 12:22:46

23K+ Views

In order to get the root directory path, you can use _DIR_ or dirname().The syntax is as follows −echo _DIR_;The second syntax is as follows−echo dirname(__FILE__);Both the above syntaxes will return the same result.Example Live Demo Output/home/KOq8Zd /home/KOq8Zd

Remove Null Values with PHP

AmitDiwan
Updated on 12-Oct-2020 12:18:25

736 Views

To remove null value in PHP, use array_filter(). It filters the array values. Let’s say the following is our array −$studentDetails = array("firstName" => "John",  "lastName"=> null); echo "The original value is=";print_r($studentDetails);Let’s filter with array_filter() −$result = array_filter($studentDetails);Example Live Demo OutputThe original value is=Array ( [firstName] => John [lastName] => ) After removing null part,the result is=Array ( [firstName] => John )

Convert Array to String in PHP

AmitDiwan
Updated on 12-Oct-2020 12:14:57

388 Views

To convert array to string, use the concept of implode () in PHP. Let’s say the following is our array −$sentence = array('My','Name','is','John');To convert the above array to string −,implode(" ",$sentence)Example Live Demo OutputThe string is = My Name is JohnLet us now see another PHP code wherein we will add separator as well −Example Live Demo OutputThe string is = One*Two*Three

Check Minimum Characters Needed to Make String Palindrome in Python

Arnab Chakraborty
Updated on 12-Oct-2020 12:04:58

489 Views

Suppose we have a string s, we have to find the minimum number of characters needed to be inserted so that the string becomes a palindrome.So, if the input is like s = "mad", then the output will be 2, as we can insert "am" to get "madam".To solve this, we will follow these steps −Define a function dp(). This will take i, jif i >= j, thenreturn 0if s[i] is same as s[j], thenreturn dp(i + 1, j - 1)otherwise, return minimum of dp(i + 1, j) and dp(i, j - 1) + 1From the main method, do the ... Read More

Find Common Ancestor of Two Elements in a Binary Tree Using Python

Arnab Chakraborty
Updated on 12-Oct-2020 12:01:59

249 Views

Suppose we have a binary tree, and we also have two numbers a and b, we have to find the value of the lowest node that has a and b as descendants. We have to keep in mind that a node can be a descendant of itself.So, if the input is likea = 6, b = 2, then the output will be 4To solve this, we will follow these steps −Define a method solve() this will take root and a, bif root is null, thenreturn -1if value of root is either a or b, thenreturn value of rootleft := solve(left ... Read More

Push Positives and Negatives to Separate Arrays in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:51:51

393 Views

We are required to write a function that takes in an array and returns an object with two arrays positive and negative. They both should be containing all positive and negative items respectively from the array.We will be using the Array.prototype.reduce() method to pick desired elements and put them into an object of two arrays.ExampleThe code for this will be −const arr = [97, -108, 13, -12, 133, -887, 32, -15, 33, -77]; const splitArray = (arr) => {    return arr.reduce((acc, val) => {       if(val < 0){          acc['negative'].push(val);       }else{ ... Read More

Find Closest Value in an Array using JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:50:24

241 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument. The function should then return the number from the array which closest to the number given to the function as second argument.ExampleThe code for this will be −const arr = [3, 56, 56, 23, 7, 76, -2, 345, 45, 76, 3]; const num = 37 const findClosest = (arr, num) => {    const creds = arr.reduce((acc, val, ind) => {       let { diff, index } = acc;       ... Read More

JavaScript Array: Index of the Lowest Number

AmitDiwan
Updated on 12-Oct-2020 11:48:57

166 Views

We are required to write a JavaScript function that takes in an array of numbers. Then the function should return the index of the smallest number in the array.ExampleThe code for this will be −const arr = [3, 56, 56, 23, 7, 76, -2, 345, 45, 76, 3]; const lowestIndex = arr => {    const creds = arr.reduce((acc, val, ind) => {       let { num, index } = acc;       if(val < num){          num = val;          index = ind;       };       return { num, index };    }, {       num: Infinity,       index: -1    });    return creds.index; }; console.log(lowestIndex(arr));OutputThe output in the console −6

Grouping Array Values in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:47:14

468 Views

Suppose we have an array of objects containing some years and weeks data like this −const arr = [    {year: 2017, week: 45},    {year: 2017, week: 46},    {year: 2017, week: 47},    {year: 2017, week: 48},    {year: 2017, week: 50},    {year: 2017, week: 52},    {year: 2018, week: 1},    {year: 2018, week: 2},    {year: 2018, week: 5} ];We are required to write a JavaScript function that takes in one such array. The function should return a new array where all the objects that have common value for the "year" property are grouped into ... Read More

Sort Array According to Age in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:44:11

416 Views

We are required to write a JavaScript function that takes in an array of numbers representing ages of some people.Then the function should bring all the ages less than 18 to the front of the array without using any extra memory.ExampleThe code for this will be −const ages = [23, 56, 56, 3, 67, 8, 4, 34, 23, 12, 67, 16, 47]; const sorter = (a, b) => {    if (a < 18) {       return -1;    };    if (b < 18) {       return 1;    };    return 0; } const sortByAdults = arr => {    arr.sort(sorter); }; sortByAdults(ages); console.log(ages);OutputThe output in the console −[    16, 12, 4, 8, 3, 23, 56,    56, 67, 34, 23, 67, 47 ]

Advertisements