约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. spring mvc 深入学习

    参考文章: 第二章 Spring MVC入门 —— 跟开涛学SpringMVC Spring MVC 3.0 深入总结:http://blog.csdn.net/sunitjy/article/det ...

  2. 测试dockerfile

    测试dockerfile是否ok(比如我的Dockerfile在deploy目录下) docker build -t my_image -f deploy/Dockerfile . docker im ...

  3. php中的gethostbyname函数有问题

    在根据域名获取ip的批量执行中,gethostbyname有些域名得到的ip是不正确的,不知道是不是版本的bug. 解决办法是,使用执行命令的方式获取 echo exec("host dom ...

  4. JVM--标记-清除算法Mark-Sweep

    前言 垃圾自动回收机制的出现使编程更加的简单,使得我们不需要再去考虑内存分配和释放的问题,而是更加的专注在我们产品功能的实现上.但是我们还是需要花时间去了解下垃圾收集机制是怎么工作的,以便后面能够更好 ...

  5. GDB 调试遇到??的问题

    今天总算解决了一个大的bug,爽! 我的程序crash,有了coredump文件,在Linux PC上用arm-linux-gdb debug it. The result is: #0  0x402 ...

  6. 每天一个 Linux 命令(22):find 命令的参数详解

    find一些常用参数的一些常用实例和一些具体用法和注意事项. 1.使用name选项: 文件名选项是find命令最常用的选项,要么单独使用该选项,要么和其他选项一起使用.  可以使用某种文件名模式来匹配 ...

  7. sql 中 left join 的使用

    left join .是以左表为基础,查询右表的值.如果在右表中没用没有数据,则为NULL. 这里有三张表. 线路bs_line:id,name(id主键) 线路段bs_seg:id,l_id,nam ...

  8. [AIR] 新建窗口的方法

    有时根据需要,我们需要在AIR程序中开多个窗口window 以下新建一个窗口: var opion:NativeWindowInitOptions = new NativeWindowInitOpti ...

  9. Codeforces 732D [二分 ][贪心]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...

  10. (Skill)238. Product of Array Except Self

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...