def linear_searching(list,size,target): for i in range(0,size): if(list[i]==target): return i; return -1单元测试

时间: 2024-05-10 18:14:21 浏览: 6
您需要为这个函数编写单元测试吗?如果是的话,可以使用以下代码作为参考: ``` def test_linear_searching(): # Test case 1 list1 = [1, 2, 3, 4, 5] size1 = len(list1) target1 = 3 assert linear_searching(list1, size1, target1) == 2 # Test case 2 list2 = [10, 20, 30, 40, 50] size2 = len(list2) target2 = 60 assert linear_searching(list2, size2, target2) == -1 # Test case 3 list3 = [100, 200, 300, 200, 400] size3 = len(list3) target3 = 200 assert linear_searching(list3, size3, target3) == 1 # Test case 4 list4 = [] size4 = len(list4) target4 = 10 assert linear_searching(list4, size4, target4) == -1 print("All test cases passed") test_linear_searching() ``` 这个测试函数包含了4个测试用例,分别测试了函数的正常情况和一些边界情况。如果函数通过了所有测试用例,就意味着它的实现是正确的。

相关推荐

def compute_mAP(trn_binary, tst_binary, trn_label, tst_label): """ compute mAP by searching testset from trainset https://github.com/flyingpot/pytorch_deephash """ for x in trn_binary, tst_binary, trn_label, tst_label: x.long() AP = [] Ns = torch.arange(1, trn_binary.size(0) + 1) Ntest = torch.arange(1, tst_binary.size(0) + 1) print("trn_binary.size(0):",trn_binary.size(0)) print("tst_binary.size(0):", tst_binary.size(0)) print("Ns:",Ns) print("Ns:", Ntest) # print("Ns(train):",Ns) for i in range(tst_binary.size(0)): query_label, query_binary = tst_label[i], tst_binary[i] # 把测试图像编码和标签赋值给->查询图像编码和标签 _, query_result = torch.sum((query_binary != trn_binary).long(), dim=1).sort() # 判断查询图像编码是否等于训练图像编码,相等的总和,并排序。 print("查询标签-----------------------------------------------------:",query_label) print("查询二进制:", query_binary) print(len(query_binary)) print("查询结果:",query_result) print("是否相等:",query_binary != trn_binary) print("查询结果1:", torch.sum((query_binary != trn_binary).long(), dim=1)) print("查询结果2:",torch.sum((query_binary != trn_binary).long(), dim=1).sort()) correct = (query_label == trn_label[query_result]).float() # 正确匹配的二进制编码个数 print("trn_label[query_result]:",trn_label[query_result]) num_ones = torch.sum(correct == 1) print("查询正确的个数:",num_ones) print("查询正确:",correct) P = torch.cumsum(correct, dim=0) / Ns print("torch.cumsum(correct, dim=0)",torch.cumsum(correct, dim=0)) print("查询正确/Ns",torch.Tensor(P)) #每个位置的精度 P AP.append(torch.sum(P * correct) / torch.sum(correct)) # print("---:",AP) acc = num_ones / tst_binary.size(0) print("ACC================================== ", acc) mAP = torch.mean(torch.Tensor(AP)) return mAP 请问怎么将这段代码改成EER评估指标的代码

# toplevel CMakeLists.txt for a catkin workspace # catkin/cmake/toplevel.cmake cmake_minimum_required(VERSION 3.0.2) project(Project) set(CATKIN_TOPLEVEL TRUE) # search for catkin within the workspace set(_cmd "catkin_find_pkg" "catkin" "${CMAKE_SOURCE_DIR}") execute_process(COMMAND ${_cmd} RESULT_VARIABLE _res OUTPUT_VARIABLE _out ERROR_VARIABLE _err OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE ) if(NOT _res EQUAL 0 AND NOT _res EQUAL 2) # searching fot catkin resulted in an error string(REPLACE ";" " " _cmd_str "${_cmd}") message(FATAL_ERROR "Search for 'catkin' in workspace failed (${_cmd_str}): ${_err}") endif() # include catkin from workspace or via find_package() if(_res EQUAL 0) set(catkin_EXTRAS_DIR "${CMAKE_SOURCE_DIR}/${_out}/cmake") # include all.cmake without add_subdirectory to let it operate in same scope include(${catkin_EXTRAS_DIR}/all.cmake NO_POLICY_SCOPE) add_subdirectory("${_out}") else() # use either CMAKE_PREFIX_PATH explicitly passed to CMake as a command line argument # or CMAKE_PREFIX_PATH from the environment if(NOT DEFINED CMAKE_PREFIX_PATH) if(NOT "$ENV{CMAKE_PREFIX_PATH}" STREQUAL "") if(NOT WIN32) string(REPLACE ":" ";" CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH}) else() set(CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH}) endif() endif() endif() # list of catkin workspaces set(catkin_search_path "") foreach(path ${CMAKE_PREFIX_PATH}) if(EXISTS "${path}/.catkin") list(FIND catkin_search_path ${path} _index) if(_index EQUAL -1) list(APPEND catkin_search_path ${path}) endif() endif() endforeach() # search for catkin in all workspaces set(CATKIN_TOPLEVEL_FIND_PACKAGE TRUE) find_package(catkin QUIET NO_POLICY_SCOPE PATHS ${catkin_search_path} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH) unset(CATKIN_TOPLEVEL_FIND_PACKAGE) if(NOT catkin_FOUND) message(FATAL_ERROR "find_package(catkin) failed. catkin was neither found in the workspace nor in the CMAKE_PREFIX_PATH. One reason may be that no ROS setup.sh was sourced before.") endif() endif() catkin_workspace()在哪添加target_link_libraries(your_target_name avcodec avformat swscale)

最新推荐

recommend-type

grpcio-1.47.0-cp310-cp310-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

小程序项目源码-美容预约小程序.zip

小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序v
recommend-type

MobaXterm 工具

MobaXterm 工具
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用

![MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用](https://img-blog.csdnimg.cn/2020050917173284.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2thbmdqaWVsZWFybmluZw==,size_16,color_FFFFFF,t_70) # 1. MATLAB取整函数概述** MATLAB取整函数是一组强大的工具,用于对数值进行
recommend-type

我想做python的算法工程师,我应该学什么?学习的顺序是什么?网上有什么推荐的免费课程吗?回答具体精确一点不要太笼统

对于想要成为 Python 算法工程师的人来说,他们应该先从掌握 Python 的基础语法开始,然后学习数据结构和算法,并且要深入了解 Python 中的一些科学计算和数据处理库,比如 NumPy、Pandas 等。 学习的顺序可以是先学习基础语法和编程技巧,然后再学习数据结构和算法相关的知识,最后深入了解数据处理和科学计算相关的库。 对于免费课程的推荐,我建议你可以先去 Coursera、edX、Udacity 等网站上寻找相关课程,这些网站上有很多优质的 Python 编程和算法课程,你可以根据自己的需求和学习进度进行选择。此外,还可以考虑一些在线编程网站,如 HackerRank、L
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB取整函数与数据分析的应用:round、fix、floor、ceil在数据分析中的应用

![MATLAB取整函数与数据分析的应用:round、fix、floor、ceil在数据分析中的应用](https://img-blog.csdnimg.cn/img_convert/a12c695f8b68033fc45008ede036b653.png) # 1. MATLAB取整函数概述 MATLAB提供了一系列取整函数,用于对数值进行四舍五入或截断操作。这些函数包括: - `round`:四舍五入到最接近的整数 - `floor`:向下取整到最小的整数 - `ceil`:向上取整到最大的整数 - `fix`:截断小数部分,返回整数部分 这些函数在数据分析、可视化和特定领域(如金融