Skip to main content

Coding solutions

Problems and solutions explained and implemented with multiple programming languages

Image for post: Two Sum

Two Sum

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 More
Image for post: Mutliples of 3 and 5

Mutliples of 3 and 5

The 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 More
Image for post: Singleton: pattern or anti-pattern?

Singleton: pattern or anti-pattern?

Many 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 More
Image for post: Prime Number

Prime Number

Let'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 More
Image for post: Leap Year

Leap Year

Let'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 More
Image for post: Factorial

Factorial

The factorial function (symbol: !) says to multiply all whole numbers from our chosen number down to 1.Examples:4! = 4 × 3 × 2 × 1 = 247! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 50401! = 1Wikipedia.PHP: using gmp_fact functionA native PHP function called gmp_fact is ava... [...]

Read More
Image for post: Palindrome words

Palindrome words

A 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 More
Image for post: FizzBuzz

FizzBuzz

The 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 More
Image for post: Exchange array keys \ values

Exchange array keys \ values

Exchange 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