Problems and solutions explained and implemented with multiple programming languages
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same element twice.Example:Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,re... [...]
Read MoreThe problem: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.It appears on Project Euler and the solution seems very easy. Maybe it hides some trick or trap but we want to keep the solution and the related... [...]
Read MoreMany people consider the singleton an anti-pattern. Many others think it's part of the creational patterns family. I don't care about its definition and I think a singleton can be useful sometimes in the real world! Singleton design pattern is used when you want to have only one instance of a given class. Let's... [...]
Read MoreLet's find if a number is prime. A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. The first few prime numbers are {2, 3, 5, 7, 11, ….}.Here we have some solutions:PHP: using bcmod and a for loopfunction isPrimeNumber(int $num){ for ($i... [...]
Read MoreLet's check if a year is a leap year.Rules for determining a Leap YearMost years that can be divided evenly by 4 are leap years. (For example, 2016 divided by 4 = 504: Leap year!)Exception: Century years are NOT leap years UNLESS they can be evenly divided by 400. (For example, 1700, 1800, and 1900 were not leap years, b... [...]
Read MoreA palindrome is a word, number, phrase, or other sequences of characters which reads the same backward as forward, such as madam or racecar or the number 10801. Sentence-length palindromes may be written when allowances are made for adjustments to capital letters, punctuation, and word dividers... [...]
Read MoreThe FizzFizz buzz is a group word game for children to teach them about division.[1] Players take turns to count incrementally, replacing any number divisible by three with the word "fizz", and any number divisible by five with the word "buzz".Players generally sit in a circle. The player designated to go first says the numb... [...]
Read MoreExchange keys with values and vice-versa from an associative array can be a very common problem. Let's how we can solve it:PHP: Using the array_flip functionThis native function will do the job on the fly!<?phpvar_dump( array_flip( array('a', 'b', 'c') ) );Javascript: using an underscore.js method... [...]
Read More