约12年年底的时候,接触了python不到半年的样子,入门是直接实现GUI测试case的。今天面试地平线机器人,发现忘得差不多了- -。

当时的问题是这样的 写一个二分查找是实现,我好像不记得二分查找是个啥- -面试官很nice的解释了一遍。当时的写法是这样的。

#!/bin/usr/env python

inputArr=[1,2,3,4,4,5,5,5,6,6,6,7,77,77]

destStr=sys.argv[1]

if inputArr.find(destStr) == -1:

print "the string you find is not in "

else

indexLefr=inputArr.find(destStr)

indexRight=inputArr.index(destStr)

print "width is from"+str(indexLefr)+"to"+str(indexRight)

思路是这样的,如果先从左边查找元素,如果返回-1,表示不存在。否则就分别从左边和右边查找。问题的关键在于既能从左边查找,也能从右边查找,是字符串而不是list。而且写错了#!/bin/usr/env python,应该写错#!/usr/bin/env python。Left拼错了… 然后二分查找应该是对一个排序好的数组(列表),查找给出元素的索引区间,因为是排序好的,所以自然是二分查找效率更好。

对方提示了是用index和count,试了一下

#!/usr/bin/env python

numbers = [1,2,3,4,4,5,5,5,6,6,6,7,77,77]

for number in numbers:

print 'currentValue:'+str(number)+'\t beginIndex:'+str(numbers.index(number))+'\t endIndex:'+str(numbers.index(number)+numbers.count(number)-

1)

输出:

[root@Grace ~]# ./findIndex.py

currentValue:1 beginIndex:0 endIndex:0

currentValue:2 beginIndex:1 endIndex:1

currentValue:3 beginIndex:2 endIndex:2

currentValue:4 beginIndex:3 endIndex:4

currentValue:4 beginIndex:3 endIndex:4

currentValue:5 beginIndex:5 endIndex:7

currentValue:5 beginIndex:5 endIndex:7

currentValue:5 beginIndex:5 endIndex:7

currentValue:6 beginIndex:8 endIndex:10

currentValue:6 beginIndex:8 endIndex:10

currentValue:6 beginIndex:8 endIndex:10

currentValue:7 beginIndex:11 endIndex:11

currentValue:77 beginIndex:12 endIndex:13

currentValue:77 beginIndex:12 endIndex:13

然而发现一个低级错误的问题:只要一把currentValue改成Value,输出就变成了这样,把value换成其他词就好了,估计是那只模块函数或者关键字啥的。

[root@Grace ~]# ./findIndex.py  
Value:1  beginIndex:0    endIndex:0

Value:2  beginIndex:1    endIndex:1

Value:3  beginIndex:2    endIndex:2

Value:4  beginIndex:3    endIndex:4

Value:4  beginIndex:3    endIndex:4

Value:5  beginIndex:5    endIndex:7

Value:5  beginIndex:5    endIndex:7

Value:5  beginIndex:5    endIndex:7

Value:6  beginIndex:8    endIndex:10

Value:6  beginIndex:8    endIndex:10

Value:6  beginIndex:8    endIndex:10

Value:7  beginIndex:11   endIndex:11

Value:77         beginIndex:12   endIndex:13

Value:77         beginIndex:12   endIndex:13

此外二分查找也不应该这样写,至少得有个/2的分半,是我选用了库函数丰富的python简化了很多吧

http://www.weixueyuan.net/view/5945.html

http://blog.csdn.net/antineutrino/article/details/6763565/

http://blog.sina.com.cn/s/blog_47d5f1b801016kx7.html

二分查找-python的更多相关文章

  1. 二分查找——Python实现

    一.排序思想 二分(折半)查找思想请参见:https://www.cnblogs.com/luomeng/p/10585291.html 二.python实现 def binarySearchDemo ...

  2. 线性查找与二分查找(python)

    # -*- coding: utf-8 -*- number_list = [0, 1, 2, 3, 4, 5, 6, 7] def linear_search(value, iterable): f ...

  3. 二分查找 python实现

    欢迎回来 [^first blood]. 要求A是升序数组 递归 只能查 数据存不存在,不能返回下标 def binary_find(A, m): if len(A) == 0: return -1 ...

  4. 算法:二分查找(python版)

    #!/usr/bin/env python #coding -*- utf:8 -*- #二分查找#时间复杂度O(logn)#一个时间常量O(1)将问题的规模缩小一半,则O(logn) import ...

  5. python算法之二分查找

    说明:大部分代码是在网上找到的,好几个代码思路总结出来的 通常写算法,习惯用C语言写,显得思路清晰.可是假设一旦把思路确定下来,并且又不想打草稿.想高速写下来看看效果,还是python写的比較快.也看 ...

  6. python函数(4):递归函数及二分查找算法

    人理解循环,神理解递归!  一.递归的定义 def story(): s = """ 从前有个山,山里有座庙,庙里老和尚讲故事, 讲的什么呢? ""& ...

  7. python实现二分查找算法

    二分查找算法也成为折半算法,对数搜索算法,一会中在有序数组中查找特定一个元素的搜索算法.搜索过程是从数组中间元素开始的 如果中间元素正好是要查找的元素,则搜索过程结束:如果查找的数大于中间数,则在数组 ...

  8. Python实现二分查找

    老生常谈的算法了. #!/usr/bin/python # -*- coding:utf-8 -*- # Filename: demo.py # 用python实现二分查找 def binarySea ...

  9. python关于二分查找

    楔子 如果有这样一个列表,让你从这个列表中找到66的位置,你要怎么做? l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72 ...

随机推荐

  1. [Android] ADB操作相关经验

    1.手机必须先root,小米可以安卓开发版系统即可.(注意:usb设置为调试模式) 2.安卓 adb工具(android debug bridge) 3.依次执行下面的命令: #adb root 获得 ...

  2. sp_who_lock

    USE MyDataBase GO /****** Object: StoredProcedure [dbo].[sp_who_lock] Script Date: 4/10/2015 ******/ ...

  3. phpmyadmin修改root密码

    很多人利用phpmyadmin或者命令行来修改了mysql的root密码,重启 后发现mysql登录错误,这是为什么呢?修改mysql的root的密码要在mysql软件中mysql数据库里修改root ...

  4. MyBatis与Hibernate的比较

    Hibernate 与Mybatis都是流行的持久层开发框架,但Hibernate开发社区相对多热闹些,支持的工具也多,更新也快,当前最高版本4.1.8.而Mybatis相对平静,工具较少,当前最高版 ...

  5. 关于redis的主从复制

    redis主从复制需要注意的一个问题 这两天我朋友在使用redis偶尔会遇见一个问题,就是所有的缓存莫名其妙会不见,找了好久都没找到,他一直以为 有人错误执行了什么命令 他跟我说的时候我估计是主从复制 ...

  6. ORA-01747: user.table.column, table.column 或列说明无效

    Oracle.DataAccess.Client.OracleException ORA-01747: user.table.column, table.column 或列说明无效 原因1: 查了一下 ...

  7. Unicode 转成中文

    代码转换如下: if __name__ == "__main__": data = "\u5c71\u5cb3\u548c\u4e00\u5207\u4e18\u9675 ...

  8. 动画--问题追踪:ImageView执行缩放动画ScaleAnimation之后,图像显示不全的问题。

    http://www.bkjia.com/Androidjc/929473.html: 问题追踪:ImageView执行缩放动画ScaleAnimation之后,图像显示不全的问题., 问题:我有一个 ...

  9. WKWebview 拼接tableview,获取web内容高度

    - (void)addWKWebView { _webView = [[WKWebView alloc] initWithFrame:CGRectMake(, , SCREEN_WIDTH, 0.1) ...

  10. Flasky学习笔记(一)

    1. app.run(debug=True,host='10.0.2.15',port=8000)自定义host及port;