前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >刷题2:在数组中查找元素的第一个和最后一个位置

刷题2:在数组中查找元素的第一个和最后一个位置

作者头像
雷子
发布2021-03-15 15:21:26
2K0
发布2021-03-15 15:21:26
举报
文章被收录于专栏:雷子说测试开发
代码语言:javascript
复制
题目:给定一个的整数数组 nums,
和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。

题目解析:

1.给定一个数组,确定的是一个数组, 数组是整数,那么我们可以知道,那么target的也是整数。

2.要求target的在数组中开始位置和结束位置,我们可以先找出来target的在list里面的下标位置,把这些下标位置放到list里面,我们去取list里面的第一个元素和最后一个元素,就是对应的开始位置和结束位置。

那么我们就可以上手去实现我们的代码了。

从这期开始,我们的代码将用python 和java两个版本去实现,同时从两方面去提高我们的,同时 也面向了两门语言的学习者。

首先,我们先看python篇:

代码语言:javascript
复制
def find(nums:list,target:int):
    listone=[]
    for i in range(len(nums)):
        if nums[i]==target:
            listone.append(i)
    if len(listone)==0:
        return False
    return listone[0],listone[-1]

测试:

代码语言:javascript
复制
class Testcase(unittest.TestCase):
    def setUp(self) -> None:
        pass
    def tearDown(self) -> None:
        pass
    def testone(self):
        result=find([1,2,3,4],5)
        self.assertFalse(result)
    def testtwo(self):
        result=find([1,2,3,4],1)
        self.assertEqual(result,(0,0))
    def testthree(self):
        result=find([1,2,3,4,1],1)
        self.assertEqual(result, (0, 4))
    def testfour(self):
        result = find([1, 2, 3, 4, 1], "1")
        self.assertEqual(result, False)
    def testfive(self):
        result = find(["1", 2, 3, 4, 1], 1)
        self.assertEqual(result, (4, 4))
    def testsix(self):
        result = find([ 1], 1)
        self.assertEqual(result, (0, 0))
if __name__=="__main__":
    unittest.main()

测试结果:

我们可以看到目前是没有发现问题的。这样,python版本实现完毕,

接下来我们去看看,对应的java版本是怎么实现的。

实现代码:

代码语言:javascript
复制
public class Find {
    public Map<String,Integer> findby(List<Integer> list, Integer targert){
        List<Integer> integerList=new ArrayList<>();

        for (int i=0;i<list.size();i++){
            if(list.get(i).equals(targert)){
                integerList.add(i);
            }
        }
        Map<String,Integer> map=new HashMap<>();
        if (integerList.size()==0){
            map.put("first",null);
            return map;
        }else {
            map.put("first",integerList.get(0));
            map.put("last",integerList.get(integerList.size()-1));
            return map;
        }
    }
}

测试代码:

代码语言:javascript
复制
public class FindTest {

    @org.testng.annotations.Test
    public void testFindby() {
        List<Integer> integerList=new ArrayList<>();
        integerList.add(0);
        Find find=new Find();
        Map<String,Integer>map=find.findby(integerList,1);
        assertEquals(map.get("first"),null);
    }
    @org.testng.annotations.Test
    public void testFindby1() {
        List<Integer> integerList=new ArrayList<>();
        integerList.add(0);
        Find find=new Find();
        Map<String,Integer>map=find.findby(integerList,0);
        assertEquals(map.get("first"),new Integer(0));
        assertEquals(map.get("first"),new Integer(0));
    }
    @org.testng.annotations.Test
    public void testFindby2() {
        List<Integer> integerList=new ArrayList<>();
        integerList.add(0);
        integerList.add(0);
        Find find=new Find();
        Map<String,Integer>map=find.findby(integerList,0);
        assertEquals(map.get("last"),new Integer(1));
        assertEquals(map.get("first"),new Integer(0));
    }
    @org.testng.annotations.Test
    public void testFindby3() {
        List<Integer> integerList=new ArrayList<>();
        integerList.add(0);
        integerList.add(0);
        integerList.add(0);
        Find find=new Find();
        Map<String,Integer>map=find.findby(integerList,0);
        assertEquals(map.get("last"),new Integer(2));
        assertEquals(map.get("first"),new Integer(0));
    }
    @org.testng.annotations.Test
    public void testFindby4() {
        List<Integer> integerList=new ArrayList<>();
        integerList.add(0);
        integerList.add(1);
        integerList.add(0);
        Find find=new Find();
        Map<String,Integer>map=find.findby(integerList,0);
        assertEquals(map.get("last"),new Integer(2));
        assertEquals(map.get("first"),new Integer(0));
    }
}

测试结果:增加了代码覆盖率,

覆盖率

那么我们测试完毕,根据测试覆盖率来说,我们目前的测试是已经完成了覆盖了百分之百的路径和代码。

后续会陆续给大家分享更多的题目,更多的代码,大家一起成长,一起刷题。雷子说测试,带给你不一样的体验。力争所有的代码都做到100%的覆盖率,所有代码都进行单测。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-10-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 雷子说测试开发 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档