Valid Phone Number
Coderswar刷題第二波
一、Valid Phone Number
- 題目描述:
Write a function that accepts a string, and returns true if it is in the form of a phone number. Assume that any integer from 0-9 in any of the spots will produce a valid phone number. Only worry about the following format: (123) 456-7890 (don't forget the space after the close parentheses)
題目大意是說,給一字串去判斷是不是有效的電話數字,有效的電話數字的格式是括號括上3個數字+空格+3個數字+"-"+4個數字,如下面的舉例。
- 舉例:
validPhoneNumber("(123) 456-7890") => returns true validPhoneNumber("(1111)555 2345") => returns false validPhoneNumber("(098) 123 4567") => returns false
(123) 456-7890:符合這種格式的就輸出true,否則就輸出false
- 解題思路:
- 測試資料:
import org.junit.Test; import static org.junit.Assert.assertEquals; public class PhoneTests { @Test public void basicTests() { String msg = "Follow the formatting instructions carefully"; assertEquals(msg, true, Kata.validPhoneNumber("(123) 456-7890")); assertEquals(msg, false, Kata.validPhoneNumber("(1111)555 2345")); assertEquals(msg, false, Kata.validPhoneNumber("(098) 123 4567")); assertEquals(msg, false, Kata.validPhoneNumber("(123)456-7890")); } @Test public void formCharTests() { String msg = "Pay attention to the formatting of the string and surrounding characters"; assertEquals(msg, false, Kata.validPhoneNumber("abc(123)456-7890")); assertEquals(msg, false, Kata.validPhoneNumber("(123)456-7890abc")); assertEquals(msg, false, Kata.validPhoneNumber("abc(123)456-7890abc")); } @Test public void charTests() { String msg = "Be careful with characters surrounding the phone number"; assertEquals(msg, false, Kata.validPhoneNumber("abc(123) 456-7890")); assertEquals(msg, false, Kata.validPhoneNumber("(123) 456-7890abc")); assertEquals(msg, false, Kata.validPhoneNumber("abc(123) 456-7890abc")); } @Test public void badCharTests() { String msg = "Be careful with non-digit characters inside phone number"; assertEquals(msg, false, Kata.validPhoneNumber("(123) 456-78f0")); assertEquals(msg, false, Kata.validPhoneNumber("(123) 4e6-7890")); assertEquals(msg, false, Kata.validPhoneNumber("(*23) 456-7890")); } }
-
實現程式碼
public class Kata { public static boolean validPhoneNumber(String phoneNumber) { return phoneNumber.matches("^\\([0-9]{3}\\)\\s[0-9]{3}\\-[0-9]{4}$"); } }
二、PaginationHelper
For this exercise you will be strengthening your page-fu mastery. You will complete the PaginationHelper class, which is a utility class helpful for querying paging information related to an array. The class is designed to take in an array of values and an integer indicating how many items will be allowed per each page. The types of values contained within the collection/array are not relevant.
- 題目
- 例子
PaginationHelper<Character> helper = new PaginationHelper(Arrays.asList('a', 'b', 'c', 'd', 'e', 'f'), 4);
helper.pageCount(); //should == 2 helper.itemCount(); //should == 6 helper.pageItemCount(0); //should == 4 helper.pageItemCount(1); // last page - should == 2 helper.pageItemCount(2); // should == -1 since the page is invalid // pageIndex takes an item index and returns the page that it belongs on helper
.pageIndex(5); //should == 1 (zero based index) helper.pageIndex(2); //should == 0 helper.pageIndex(20); //should == -1 helper.pageIndex(-10); //should == -1
- 測試
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class PaginationHelperTest {
List<Integer> collection = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24);
PaginationHelper<Integer> helper = new PaginationHelper<>(collection, 10);
@Test
public void testPageCount() throws Exception {
assertEquals("pageCount is returning incorrect value", 3, helper.pageCount());
}
@Test
public void testPageItemCount() throws Exception {
assertEquals("pageItemCount is returning incorrect value", 10, helper.pageItemCount(1));
assertEquals("pageItemCount is returning incorrect value", 4, helper.pageItemCount(2));
assertEquals("pageItemCount is returning incorrect value", -1, helper.pageItemCount(3));
}
@Test
public void testPageIndex() throws Exception {
assertEquals("pageIndex is returning incorrect value when provided a itemIndex argument that was out of range",
-1, helper.pageIndex(40));
assertEquals("pageIndex is returning incorrect value", 2, helper.pageIndex(22));
assertEquals("pageIndex is returning incorrect value", 0, helper.pageIndex(3));
assertEquals("pageIndex is returning incorrect value", 0, helper.pageIndex(0));
assertEquals("pageIndex is returning incorrect value", -1, helper.pageIndex(-1));
assertEquals("pageIndex is returning incorrect value", -1, helper.pageIndex(-23));
assertEquals("pageIndex is returning incorrect value", -1, helper.pageIndex(-15));
}
@Test
public void testItemCount() throws Exception {
assertEquals("itemCount is returning incorrect value", 24, helper.itemCount());
}
@Test
public void testEmptyCollection() throws Exception {
PaginationHelper empty = new PaginationHelper(new ArrayList<Integer>(), 10);
assertEquals("pageIndex(0) called when there was an empty collection", -1, empty.pageIndex(0));
}
}
- 程式碼實現
import java.util.List;
// TODO: complete this object/class
public class PaginationHelper<I> {
private List<I> list=null;
private int itemsPerPage=0;
/**
* The constructor takes in an array of items and a integer indicating how many
* items fit within a single page
*/
public PaginationHelper(List<I> collection, int itemsPerPage) {
this.list=collection;
this.itemsPerPage=itemsPerPage;
}
/**
* returns the number of items within the entire collection
*/
public int itemCount() {
return list.size();
}
/**
* returns the number of pages
*/
public int pageCount() {
int s=list.size();
if(s==0||this.itemsPerPage==0)return -1;
int i=s/this.itemsPerPage;
if(s%this.itemsPerPage!=0)return i+1;
return i;
}
/**
* returns the number of items on the current page. page_index is zero based.
* this method should return -1 for pageIndex values that are out of range
*/
public int pageItemCount(int pageIndex) {
int count=pageCount();
if(pageIndex+1>count)return -1;
else if(pageIndex+1==count){
int s=this.list.size();
if(s<this.itemsPerPage)return s;
else return(s-this.itemsPerPage*pageIndex);
}
else return this.itemsPerPage ;
}
/**
* determines what page an item is on. Zero based indexes
* this method should return -1 for itemIndex values that are out of range
*/
public int pageIndex(int itemIndex) {
if(itemIndex<0 || itemIndex>this.list.size()-1)return -1;
return itemIndex/this.itemsPerPage;
}
}
總結:這次寫的有點崩潰,不知道富文字編輯器是不是炸了,還是瀏覽器炸了。每次後面部分每次插入程式碼那部分都沒有響應,又得重新開頁面了,之前寫好的有些又沒有儲存到。