Binary Tree

In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child. In this example we will create a rooted binary tree witch has a root node and every node has at most two children, if the node has only one child it is a leaf. interface TreeNode { Boolean isRoot() Boolean isLeaf() List<TreeNode> getChildren() void setParent(TreeNode node) void addChild(TreeNode node) } The inteface helps to define our methods in terms of functionality description [Read More]

Fastman

Write a program that breaks up a string of words with no spaces into a string with appropiate spaces. Consider an example that’s rich enough but no tedious: “fastman” –> “fast man” Consider an empty string or null The words come from custom dictionary Solution class Fastman { def dictionary = ["fast","fat","man","strong"] private String find(String string){ String result = '' if(string?.length() > 0){ for(int i=0;i<string.length()-1;i++){ for(int j=string.length();j!=i;j--){ if(dictionary.contains(string.substring(i,j))){ result += "${string. [Read More]

Algorithms

This section is about solving simple algorithms, coding challenges, puzzles, katas and dojo material in Java. Sum a Collection Biggest Number Most Popular in the array Common Elements in two Collections Number Counter Characters Counter Birthday Cake Candles String Compressor Sock Merchant Smallest Biggest Electronics Shop Square my List Digital Adder Sum a Collection Given an array of integers, find the sum of its elements. import java.util.List; public class CollectionAdder { public int sum(List<Integer> numbers) { return numbers. [Read More]

Algorithms

Simple Algorithms Fastman Problem Binary Tree Stream Merger Is Pangram Get Month Name Matrix Diagonal Difference Grading Students Apple and Orange Kangaroo GitHub Repository Binary Search Is a search algorithm that finds the position of a target value within a sorted array. BinarySearch.java public class BinarySearch{ int logarithmic(Integer[] data, int key) { int startIndex = 0; int endIndex = data.length - 1; while (startIndex < endIndex) { int midIndex = (endIndex - startIndex / 2) + startIndex; int midValue = data[midIndex]; if (key > midValue) { startIndex = midIndex++; } else if (key < midValue) { endIndex = midIndex - 1; } else { return midIndex; } } return -1; } public static void main(String[] args){ BinarySearch binarySearch = new BinarySearch(); Integer[] myData = {1,2,3,4,5,6,7,8,9,10}; System. [Read More]

Replace conditional with polymorphism

The essence of polymorphism is that it allows you to avoid writing an explicit conditional when you have objects whose behavior varies depending on their types. Polymorphism gives you many advantages. The biggest gain occurs when this same set of conditions appears in many places in the program. if you want to add a new type, you have to find and update all the conditionals, but with subclasses you just create a new subclass and provide the appropriate methods. [Read More]

Replace Error Code with Exception

A method returns special code to indicate a wrong path. Throw an exception instead. In computers, as in life, things go wrong occasionally, when things go wrong, you need to do something about it. In the simplest case, you can stop the program with an error code. When a routine finds an error, it needs to let its caller know, and the caller may pass the error up to the chain. [Read More]

Introduce Parameter Object

You have a group of parameters that naturally go together, replace them with an object. Often you see a particular group of parameters that tend to be passed together. Several methods may use this group, either on one class or in several classes. Such a group of classes is a data clump and can be replaced with an object that carries all of this data. This refactoring is useful because it reduces the size of parameter list, and long parameter lists are hard to understand. [Read More]

Replace Temp With Query

You are using temporary variable to hold the result of an expression. Extract the expression into a method. Replace all the references to the temp with the new method. The new method can then be used then in other methods. The problem with temps is that they are temporary and local. Because they can be seen only in the context of the method in wich they are used, temps tend to encourage longer methods, because that’s the only way you can reach the temp. [Read More]

Duplicated code

Number one in the stink parade is duplicated code. If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them. When coding I focus mostly on duplication. When the same thing is done over and over, its a sign that there is an idea in our mind that is no well represented in the code. [Read More]

Refactoring

What is refactor? Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure. It is a disciplined way to clean up code that minimizes the chances of introduce bugs. In essence when you refactor you are improving the design of the code after it has been written. Why do I need refactoring? [Read More]