Posts

A* Pathfinding Algorithm Java Implementation

The A Star Class : import java.util.ArrayList; public class astar {     // grid [y-coordinate][x-coordinate]     public static void main(String[] args) {         // Map to be traversed :         int y = 5, x = 5;         node[][] grid = new node[y][x];         // Populate grid (Otherwise the grid will contain NULL values) :         // Set x and y values :         for (int i = 0; i < y; i++)             for (int j = 0; j < x; j++)                 grid[i][j] = new node(i, j);         // Add walls :         grid[1][1].setWalkable(false);         grid[2][2].setWalkable(false); ...

Ducci Sequence - Programming challenge [Java]

There was a challenge, it went like this: A Ducci sequence is a sequence of n-tuples of integers, sometimes known as "the Diffy game", because it is based on sequences. Given an n-tuple of integers (a_1, a_2, ... a_n) the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers. Ducci sequences are named after Enrico Ducci (1864-1940), the Italian mathematician credited with their discovery. Some Ducci sequences descend to all zeroes or a repeating sequence. An example is (1,2,1,2,1,0) -> (1,1,1,1,1,1) -> (0,0,0,0,0,0). I did this: import java.util.ArrayList; public class ducci_sequence {     public int calculate(int[] sS) {         ArrayList<String> pS = new ArrayList<String>();         String t = "";         for (int i = 0; i < sS.length; i++)             t = ...

Perfectly balanced - Programming Challenge [Java]

There was a challenge, it goes like this:  "Given a string containing only the characters x and y , find whether there are the same number of x s and y s." This is what I did:     // Given a string containing only the characters x and y, find whether there     // are the same number of x's and y's.     public boolean balanced(String s) {         int x = 0, y = 0;         for (int i = 0; i < s.length(); i++)             if (s.charAt(i) == 'x')                 x++;             else if (s.charAt(i) == 'y')                 y++;         if (x == y)             return true;     ...

Additive Persistence - Programming Challenge [Java]

There is this awesome subreddit called r/dailyprogrammer, that posts programming challenges. There was this post about additive persistence . The challenge goes like this: Take an integer N: Add its digits Repeat until the result has 1 digit The total number of iterations is the additive persistence of N.  This is what I did: 1.    public int calculate(int n){ 2.        int c = 0; 3.        while(n > 9){ 4.            c++; 5.            String s = n + ""; 6.            n = 0; 7.            for (int i = 0; i < s.length(); i++) 8.                n += Integer.parseInt(s.charAt(i) + ""); 9.        } 10.       return c; 11.   } This is how it Work...