codingbat

catDog string problem at Codingbat.com

Could anyone check my solution? I want to return true if the string "cat" and "dog" appear the same number of times in the given string. There are various strings with different numbers of "cat" and "dog". public boolean catDog(String str) { int catAnswer = 0; int dogAnswer = 0; int cat_Count = 0; int dog_Count = 0; for (...

codingbat wordEnds using regex

I'm trying to solve wordEnds from codingbat.com using regex. Given a string and a non-empty word string, return a string made of each char just before and just after every appearance of the word in the string. Ignore cases where there is no char before or after the word, and a char may be included twice if it is between two words. w...

codingBat repeatEnd using regex

I'm trying to understand regex as much as I can, so I came up with this regex-based solution to codingbat.com repeatEnd: Given a string and an int N, return a string made of N repetitions of the last N characters of the string. You may assume that N is between 0 and the length of the string, inclusive. public String repeatEnd(Stri...

codingBat plusOut using regex

This is similar to my previous efforts (wordEnds and repeatEnd): as a mental exercise, I want to solve this toy problem using regex only. Description from codingbat.com: Given a string and a non-empty word string, return a version of the original string where all chars have been replaced by pluses ("+"), except for appearances of th...

Creating a New Reverse Java Array

CodingBat > Java > Array-1 > reverse3: Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}. public int[] reverse3(int[] nums) { int[] values = new int[3]; for (int i = 0; i <= nums.length - 1; i++) { for (int j = nums.length-1; j >= 0; j--) { ...

codingBat separateThousands using regex (and unit testing how-to)

This question is a combination of regex practice and unit testing practice. Regex part I authored this problem separateThousands for personal practice: Given a number as a string, introduce commas to separate thousands. The number may contain an optional minus sign, and an optional decimal part. There will not be any superfluous le...