Valid Phone Number

Coderswar刷题第二波

一、Valid Phone Number

  1. 题目描述:
    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个数字,如下面的举例。

  2. 举例:
    validPhoneNumber("(123) 456-7890")  =>  returns true
    validPhoneNumber("(1111)555 2345")  => returns false
    validPhoneNumber("(098) 123 4567")  => returns false

    (123) 456-7890:符合这种格式的就输出true,否则就输出false

  3.  解题思路:
  4. 测试数据:
    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"));
      }
    }
  5. 实现代码

    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;
  }
}

 总结:这次写的有点崩溃,不知道富文本编辑器是不是炸了,还是浏览器炸了。每次后面部分每次插入代码那部分都没有响应,又得重新开页面了,之前写好的有些又没有保存到。

猜你喜欢

转载自blog.csdn.net/weixin_36605200/article/details/84668897