Programming Articles

Page 309 of 2544

Cell Count After Removing Corner Diagonals in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 171 Views

Suppose we have a number n representing the length of an n x n board. We have to delete all cells that are diagonal to one of the four corners and return the number of empty cells.So, if the input is like n = 4,XOOXOXXOOXXOXOOXThen the output will be 8.To solve this, we will follow this formula −n*n - 2 * n +(n mod 2)Let us see the following implementation to get better understanding −Exampleclass Solution:    def solve(self, n):       return n*n - 2 * n + (n%2) ob = Solution() print(ob.solve(4))Input4Output8

Read More

Program to count number of unique palindromes we can make using string characters in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 324 Views

Suppose we have a string s, we have to find the number of distinct palindromes we can generate using all characters. If the answer is very large then mod the result by 10^9 + 7.So, if the input is like s = "xyzzy", then the output will be 2, as we can make "zyxyz" and "yzxzy"To solve this, we will follow these steps −m = 10^9+7char_freq := a map holding each character of s and their frequenciesodd := 0for each character k and frequency v in char_freq, doif v mod 2 is 1, thenodd := odd + 1if odd > ...

Read More

Reading and storing a list as an array PHP

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 222 Views

For this, you can simply use for loop.ExampleThe PHP code is as follows OutputThis will produce the following output10 20 30 40 50 60

Read More

PHP Defining namespace

Malhar Lathkar
Malhar Lathkar
Updated on 11-Mar-2026 152 Views

IntroductionDeclaration of class, function and constants inside a namespace affects its acess, although any other PHP code can be present in it. PHP's namespace keyword is used to declare a new namespace. A file with .php extension must have namespace declaration in very first line after If namespace declaration is not at the top of file, PHP parser throws fatal errorExample Hello world ?>OutputAbove code now returns name following errorPHP Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the scriptOnly declare construct can appear before namespace declarationExample

Read More

Count Elements x and x+1 Present in List in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 209 Views

Suppose we have a list of numbers called nums, we have to find the number of elements x there are such that x + 1 exists as well.So, if the input is like [2, 3, 3, 4, 8], then the output will be 3To solve this, we will follow these steps −s := make a set by inserting elements present in numscount := 0for each i in nums, doif i+1 in s, thencount := count + 1return countLet us see the following implementation to get better understanding −Exampleclass Solution:    def solve(self, nums):       s = set(nums)   ...

Read More

PHP Inherit the properties of non-class object?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 202 Views

For this, use clone along with set the property name to null.ExampleThe PHP code is as follows OutputThis will produce the following outputobject(stdClass)#1 (2) { ["Name"]=> string(4) "John" ["Age"]=> int(20) } object(stdClass)#2 (2) { ["Name"]=> string(4) "John" ["Age"]=> int(20) } object(stdClass)#2 (2) { ["Name"]=> NULL ["Age"]=> NULL } object(stdClass)#1 (2) { ["Name"]=> NULL ["Age"]=> NULL }

Read More

Counting Number of Dinosaurs in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 287 Views

Suppose we have a string called animals and another string called dinosaurs. Every letter in animals represents a different type of animal and every unique character in dinosaurs string represents a different dinosaur. We have to find the total number of dinosaurs in animals.So, if the input is like animals = "xyxzxyZ" dinosaurs = "yZ", then the output will be 3, as there are two types of dinosaurs y and Z, in the animal string there are two y type animal and one Z type animal.To solve this, we will follow these steps −res := 0dinosaurs := a new set ...

Read More

Replacing specific characters from a matched string in PHP regular expression where we don't know the number of instances of the match?\\n

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 200 Views

For this, use preg_replace() in PHP. You need to also use Regular Expressions. Let’s say the following is our input −FirstName|John |LastName|Smith|SalaryProvided|2000|5000The expected output is as follows wherein we have replaced a specific character “|” with a whitespace. This character was placed between two numbers 2000 and 5000 −FirstName|John |LastName|Smith|SalaryProvided|2000 5000ExampleThe PHP code is as follows OutputThis will produce the following output The result is= FirstName|John |LastName|Smith|SalaryProvided|2000 5000

Read More

Swap Even Index Elements And Odd Index Elements in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 3K+ Views

Suppose we have a list of numbers called nums, we will exchange each consecutive even indexes with each other, and also exchange each consecutive odd index with each other.So, if the input is like [1,2,3,4,5,6,7,8,9], then the output will be [3, 4, 1, 2, 7, 8, 5, 6, 9]To solve this, we will follow these steps −length := size of numsfor i in range 0 to length, increase by 4, doif i+2

Read More

Program to find minimum total cost for equalizing list elements in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 317 Views

Suppose we have two lists of numbers called nums and costs. Now consider, there is an operation where we can increase or decrease nums[i] for cost costs[i]. We can perform any number of these operations, and we want to make all elements equal in the nums. We have to find the minimum total cost required.So, if the input is like nums = [3, 2, 4] costs = [1, 10, 2], then the output will be 5, as if we can decrease the number 3 into 2 for a cost of 1. Then we can decrement 4 two times for a ...

Read More
Showing 3081–3090 of 25,433 articles
« Prev 1 307 308 309 310 311 2544 Next »
Advertisements