View RSS Feed

fatabass

isTheStringPalinDrome, reverseTheWordOrder, fibonaciWithoutRecursion, removeDuplicates

Rating: 1 votes, 1.00 average.
by , 04-04-2012 at 12:51 AM (2300 Views)
Java Code:
package assignmentOne;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class PartOneAssignments {
	
	// This method accepts a String as the argument and checks whether it is palindrome or not
	static boolean isTheStringPalinDrome(String s) {
		
		// Be positive and assume that the String provided is Palindrome
		boolean stringIsPalinDrome = true;
		
		// Break the provided string to chars...
		char[] charArrayOfTheProvidedString = s.toCharArray();
		
		int increasingIterator = 0;
		int decreasingIterator = (charArrayOfTheProvidedString.length-1);
		
		// Iterate through the array you have
		// Grab a char from the lefthandside, grab a char from the righthandside and check whether they are equal
		// If not set the boolean to false, if so keep doing this until the iterator that started from the beginning
		// has a higher value then the iterator that started from the end. 
		
		while(increasingIterator<decreasingIterator) {
			if(charArrayOfTheProvidedString[increasingIterator]!=charArrayOfTheProvidedString[decreasingIterator]) {
				stringIsPalinDrome = false;
				break;
			}
			increasingIterator++; decreasingIterator--;
		}
		// Return the value
		return stringIsPalinDrome;
	}

	// This method accepts a String and returns an array of Strings
	// which has the words in reversed order from the provided String
	static String[] reverseTheWordOrder(String s) {
		
		// First split the provided String to a set of Strings and hold them
		// in an array of Strings
		String[] stringToBeSplit = s.split(" ");
		
		// Create a new array of Strings that is equal to the previous array in length
		String[] splitString = new String[stringToBeSplit.length];
		
		// We will be iterating through the array of String we have. 
		// Get the last element in the stringToBeSplit, add it to first in the new array..
		
		int k = stringToBeSplit.length;
		int i = 0;
		
		do {
			splitString[i] = stringToBeSplit[k-1]+" ";
			k--;i++;
		} while(k!=0);
		
		// Return the new array of Strings.
		return splitString;
	}
	
	// This method returns -1 if an integer less than 1 is provided...
	// Otherwise returns the (provided number)th fibonacci value
	static int fibonaciWithoutRecursion(int nthNumber) {
		
		// Check if the provided integer is valid
		if(nthNumber<=0) {
			return -1;
		}
	
		// An arraylist with all Fibonacci Numbers
		ArrayList<Integer> untilNth = new ArrayList<Integer>();
		
		// We must add the first two numbers..
		untilNth.add(0);
		untilNth.add(1);
		
		
		while(nthNumber>untilNth.size()) {
			int fNumbers = untilNth.get(untilNth.size()-1) + untilNth.get(untilNth.size()-2);
			untilNth.add(fNumbers);
		}
		
		// Return the last value in the ArrayList:
		return (untilNth.get(nthNumber-1));
	}
	
	static void tokenizeMySearch() {
		
	}
	
	static int[] removeDuplicatesInTheArray(int[] o) {
		
		// We will need a counter
		int counter = 0;
		
		HashMap<Integer,Boolean> myMap = new HashMap<Integer,Boolean>();
		while(counter<o.length) {
			myMap.put(o[counter], true);
			counter++;
		}
		
		Set<Integer> mySet = myMap.keySet();
		int returnedArray[] = new int[mySet.size()];
		Iterator<Integer> mySetIterator = mySet.iterator();
		counter = 0;
		while(mySetIterator.hasNext()) {
			returnedArray[counter] = (int) (Integer) mySetIterator.next();
			counter++;
		}
		return returnedArray;
	}
	
	public static void main(String[] args) {
		
		// Test palindromes: 
		String providedString = new String("abxba");
		boolean isMyStringPalinDrome = PartOneAssignments.isTheStringPalinDrome(providedString);
		System.out.println("Is the String '" + providedString + "' I provided palindrome? " + isMyStringPalinDrome);
		
		
		providedString = new String("abba");
		isMyStringPalinDrome = PartOneAssignments.isTheStringPalinDrome(providedString);
		System.out.println("Is the String '" + providedString + "' I provided palindrome? " + isMyStringPalinDrome);
		
		
		providedString = new String("able");
		isMyStringPalinDrome = PartOneAssignments.isTheStringPalinDrome(providedString);
		System.out.println("Is the String '" + providedString + "' I provided palindrome? " + isMyStringPalinDrome);
		
		System.out.println();
		
		// Test finding nth fibonacci number
		int question = 8;
		int answer = PartOneAssignments.fibonaciWithoutRecursion(question);
		switch(answer) {
			case -1: System.out.println("You provided: " + question + " Please provide a positive integer"); break;
			default: System.out.println("You provided: " + question + ". The fibonacci number with index #" + question + " is: " + answer);
		}
		
		question = 0;
		answer = PartOneAssignments.fibonaciWithoutRecursion(question);
		switch(answer) {
			case -1: System.out.println("You provided: " + question + ". Please provide a positive integer."); break;
			default: System.out.println("The fibonacci number with index #" + question + " is: " + answer);
		}
		
		question = 1;
		answer = PartOneAssignments.fibonaciWithoutRecursion(question);
		switch(answer) {
		case -1: System.out.println("You provided: " + question + " Please provide a positive integer"); break;
		default: System.out.println("You provided: " + question + ". The fibonacci number with index #" + question + " is: " + answer);
		}
		
		question = 2;
		answer = PartOneAssignments.fibonaciWithoutRecursion(question);
		switch(answer) {
		case -1: System.out.println("You provided: " + question + " Please provide a positive integer"); break;
		default: System.out.println("You provided: " + question + ". The fibonacci number with index #" + question + " is: " + answer);
		}
	
		System.out.println();
		
		// Test reversteTheWordOrder
		String stringToBeReversed= "Hello world, You seem a bit angry today. Unlucky with the girls?";
		System.out.println("String to be reversed:\n" + stringToBeReversed);
		String[] ans = PartOneAssignments.reverseTheWordOrder(stringToBeReversed);
		int i = 0 ;
		while(i<ans.length) {
			System.out.print(ans[i]);
			i++;
		}
		
		System.out.println();
		
		// Test removing duplicates
		int[] hasDuplicates = new int[]{1,2,3,4,5,3,8,9,1,2,5,6,2,2,2};
		int[] withoutDuplicates = PartOneAssignments.removeDuplicatesInTheArray(hasDuplicates);
		i=0;
		while(i<withoutDuplicates.length) {
			System.out.print(withoutDuplicates[i]+" ");
			i++;
		}
	}
}

Submit "isTheStringPalinDrome, reverseTheWordOrder, fibonaciWithoutRecursion, removeDuplicates" to Facebook Submit "isTheStringPalinDrome, reverseTheWordOrder, fibonaciWithoutRecursion, removeDuplicates" to Digg Submit "isTheStringPalinDrome, reverseTheWordOrder, fibonaciWithoutRecursion, removeDuplicates" to del.icio.us Submit "isTheStringPalinDrome, reverseTheWordOrder, fibonaciWithoutRecursion, removeDuplicates" to StumbleUpon Submit "isTheStringPalinDrome, reverseTheWordOrder, fibonaciWithoutRecursion, removeDuplicates" to Google

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments

  1. fatabass's Avatar
    • |
    • permalink


    Output:

    Is the String 'abxba' I provided palindrome? true
    Is the String 'abba' I provided palindrome? true
    Is the String 'able' I provided palindrome? false

    You provided: 8. The fibonacci number with index #8 is: 13
    You provided: 0. Please provide a positive integer.
    You provided: 1. The fibonacci number with index #1 is: 0
    You provided: 2. The fibonacci number with index #2 is: 1

    String to be reversed:
    Hello world, You seem a bit angry today. Unlucky with the girls?
    girls? the with Unlucky today. angry bit a seem You world, Hello
    1 2 3 4 5 6 8 9