Challenges

import java.util.*; /** * * Charlie Barton * 11/2 * v5 * *Note: Probably some problems with palindrome, couldn't quite get it with the substrings. * */ public class ProgrammingChallenge { public ProgrammingChallenge() { String[] letters = {"a","a","a","a","b","c","c","a","a","d","e","e","e","e"}; String[] words = {"civic", "tent","push","level","radar","hello","stats","madam","world"}; removeConsecutives(letters); //isPalindrome(words); } /*"a","a","b","c","c","a" * create an arraylist that removes the consecutive * thus, the above array would become an * arraylist that held the following values * a,b,c,a *  * print the contents of the arraylist to the screen * at end of method. */ public void removeConsecutives(String[] a) { ArrayList<String> nonCon = new ArrayList<String>(); String prev = ""; for(int i = 0; i < a.length - 1; i++){ if (a[i].equals(prev)){ } else { nonCon.add(a[i]); } prev = a[i]; } System.out.println(nonCon); //String[] withoutConsecutives; } // * Is the string a palindrome public void isPalindrome(String[] a){ List<String> pl = new ArrayList<String>(); String test = " "; int first = 0; int second = 0; for (int i = 0; i < pl.size(); i ++){ String ai = a[i]; int length= a[i].length(); for (int o = 0; o < length; o++){ if (o == 0){ test = test + (ai.substring(length- 1)); } test += (ai.substring(length + first, length + second)); first--; second = first - 1; if (length + second < 0){ second = second + 1; }else if(length+ first < 0){ first = first + 1; } String s = a[i]; if(test.equals(s)){ System.out.println("Palindrome"); } } } } public int gcd(int x, int y){ int x2 = 1; if( x > y){ for(int i = 1; i < y; i++){ if(y%i == 0 && x%i == 0){ x2 = i; } } }else{ for(int i = 1; i < x; i++){ if(y % i == 0 && x%i == 0){ x2 = i; } } } return x2; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.