`
yy_gy
  • 浏览: 33687 次
  • 性别: Icon_minigender_2
社区版块
存档分类
最新评论

Google python list2.py (python 2.7)

阅读更多
#!/usr/bin/python -tt

# D. Given a list of numbers, return a list where
# all adjacent == elements have been reduced to a single element,
# so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or
# modify the passed in list.
def remove_adjacent(nums):

for num in nums:
    if nums.index(num)<=len(nums)-2 and num ==nums[nums.index(num)+1]:
nums.remove(num)
  return nums



# E. Given two lists sorted in increasing order, create and return a merged
# list of all the elements in sorted order. You may modify the passed in lists.
# Ideally, the solution should work in "linear" time, making a single
# pass of both lists.

# solution 1:
def linear_merge(list1, list2):
  return sorted(list1+list2)


# solution 2:
def linear_merge(list1, list2):
 
  temp=[]
  while len(list1)>0 and len(list2)>0:
    if list1[-1] > list2[-1]:
      temp.append(list1.pop(-1))
    else:
      temp.append(list2.pop(-1))
  temp.extend(sorted(list1,reverse=True))
  temp.extend(sorted(list2,reverse=True))
  temp.reverse()
  return temp

# Note: the solution above is kind of cute, but unforunately list.pop(0)
# is not constant time with the standard python list implementation, so
# the above is not strictly linear time.
# An alternate approach uses pop(-1) to remove the endmost elements
# from each list, building a solution list which is backwards.
# Then use reversed() to put the result back in the correct order. That
# solution works in linear time, but is more ugly.


# Simple provided test() function used in main() to print
# what each function returns vs. what it's supposed to return.
def test(got, expected):
  if got == expected:
    prefix = ' OK '
  else:
    prefix = '  X '
  print '%s got: %s expected: %s' % (prefix, repr(got), repr(expected))


# Calls the above functions with interesting inputs.
def main():
  print 'remove_adjacent'
  test(remove_adjacent([1, 2, 2, 3]), [1, 2, 3])
  test(remove_adjacent([2, 2, 3, 3, 3]), [2, 3])
  test(remove_adjacent([]), [])

  print
  print 'linear_merge'
  test(linear_merge(['aa', 'xx', 'zz'], ['bb', 'cc']),
       ['aa', 'bb', 'cc', 'xx', 'zz'])
  test(linear_merge(['aa', 'xx'], ['bb', 'cc', 'zz']),
       ['aa', 'bb', 'cc', 'xx', 'zz'])
  test(linear_merge(['aa', 'aa'], ['aa', 'bb', 'bb']),
       ['aa', 'aa', 'aa', 'bb', 'bb'])


if __name__ == '__main__':
  main()
分享到:
评论

相关推荐

    python2.7中所用的get-pip.py文件+安装方法

    Python是世界上最受欢迎的编程语言之一,特别是在数据科学、机器学习和Web开发领域。在Python的生态系统中,`pip`是一个至关...通过下载和使用`get-pip.py`,你可以确保在Python 2.7环境中也能享受到`pip`带来的便利。

    python.demo.py

    C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\python.exe "C:\Users\xieqianyun\PyCharm Community Edition 2019.2.5\helpers\pydev\pydevconsole.py" --mode=client --port=55825 import sys; ...

    python运维视频.zip

    04 基本数据类型(str, list).mp4 05 基本数据类型(dict).mp4 06 基本数据类型(set).mp4 07 基本数据类型(tuple).mp4 08 用户交互.mp4 09 if条件判断.mp4 10 while循环.mp4 11 总结.mp4 day201 今日内容大纲和上节...

    基于Django实现的股票交易管理系统

    python3 manage.py update_stock_list python3 manage.py update_stock_top10 创建超级用户 执行:python3 manage.py createsuperuser 运行 执行: python3 manage.py runserver 0.0.0.0:8000 或 uwsgi uwsgi....

    廖雪峰Python2.7教程.pdf

    - Python 2.7是Python 2系列的最后一个版本,之后主要发展为Python 3.x系列。 2. 安装Python: - 用户需要下载并安装Python解释器,该解释器负责执行Python代码。 - 可以通过官方网站下载对应操作系统的安装包,...

    python-2.7777

    Python 2.7 是 Python 编程语言的一个重要版本,发布于2010年,是Python 2.x系列的最后一个主要更新。Python 2.7777(假设这是一个错误的版本号,因为实际没有这个版本,可能是2.7.7或2.7.17)通常指的是2.7系列中的...

    Python库 | solidfire_sdk_python-1.5.0.87-py2.py3-sf-any.whl

    pip install solidfire-sdk-python-1.5.0.87-py2.py3-sf-any.whl ``` 安装完成后,引入模块并初始化连接: ```python from solidfire import Element element = Element("ip_address", "username", "password...

    get-pip.py

    2. **运行脚本**:在命令行界面,定位到`get-pip.py`所在目录,然后使用Python解释器执行该脚本,例如: ``` python get-pip.py ``` 这个脚本会自动下载必要的依赖并安装pip。 3. **环境变量**:安装完成后,...

    ListSort.py

    基于Python的几种基础排序算法,包含冒泡排序、选择排序、插入排序、希尔排序、快速排序、归并排序。

    Python-3.7.11.tgz

    2. **编译过程**:在 `Python` 目录下,你可以看到如何将 AST 编译成字节码,这涉及 `compile.c` 和 `ast.c` 等文件。这些代码解释了 Python 如何生成 `.pyc` 文件,以提高程序启动速度。 3. **虚拟机实现**:...

    Python库 | mo_collections-1.0.17039-py2.7.egg

    《Python库mo_collections-1.0.17039-py2.7.egg详解》 Python作为一门广泛使用的编程语言,拥有丰富的第三方库支持,这些库极大地扩展了其功能,提高了开发效率。其中,`mo_collections`是Python社区中一个重要的库...

    python-start.py:初学者python的挑战和练习

    1. **变量和数据类型**:Python支持多种数据类型,如整数(int)、浮点数(float)、字符串(str)、布尔值(bool)和列表(list)等。初学者通常会从学习如何声明和使用这些基本数据类型开始。 2. **控制结构**:包括条件...

    numpy-MKL-1.7.0.win-amd64-py2.7.zip

    标题中的"numpy-MKL-1.7.0.win-amd64-py2.7.zip"揭示了这个压缩包文件的核心内容,它包含了numpy库的一个特定版本(1.7.0),并集成了Intel的Math Kernel Library(MKL)优化。numpy是Python编程语言中的一个核心库...

    PyPI 官网下载 | randomize-0.14-py2.py3-none-any.whl

    标题提到的“PyPI官网下载 | randomize-0.14-py2.py3-none-any.whl”表明我们正在讨论的是一个可以从PyPI上获取的Python软件包——`randomize`,版本号为0.14。 `randomize`很可能是一个用于生成随机数据或打乱数据...

    Python库 | publicsuffixlist-0.2.6-py2.py3-none-any.whl

    4. **Python 2和Python 3兼容**:`py2.py3-none-any`在wheel文件名中表示这个库适用于Python 2.7到3.x的所有版本,且不依赖于特定操作系统或架构,这大大增强了库的跨平台兼容性。 5. **安装与使用**:在Python环境...

    Python库 | publicsuffixlist-0.7.5-py2.py3-none-any.whl

    在`publicsuffixlist-0.7.5-py2.py3-none-any.whl`这个压缩包中,包含的是该库的特定版本,支持Python 2和Python 3。 公共后缀列表(PSL)是一个包含了所有不推荐用作个人注册的顶级域(TLDs)、二级域和其他更高...

    python_package.rar

    setuptools-1.3.win-amd64-py2.7.exe adodbapi-2.6.0.7.zip Django-1.4.22.tar.gz django-ajax-forms-django-ajax-forms-0.1.1.zip django-ajax-selects-1.4.1.tar.gz django-appconf-1.0.1.tar.gz django-classy-...

    Python库 | kongctl-0.2.0-py2.py3-none-any.whl

    4. **版本兼容性**:由于kongctl-0.2.0支持Python 2和3,这使得它能够广泛地应用于不同的Python项目,无论你使用的是Python 2.7还是Python 3.x。 5. **社区支持**:作为Python库,kongctl很可能有一个活跃的开源...

    Python库 | oci-2.23.4-py2.py3-none-any.whl

    oci-2.23.4-py2.py3-none-any.whl是一个Python库的轮子文件,用于在Python环境中快速安装和使用Oracle Cloud Infrastructure (OCI) SDK。这个SDK允许开发者通过编程方式与Oracle云服务进行交互,包括但不限于计算、...

    Python库 | s3am-1.0a1.dev15-py2.7.egg

    这个名为`s3am-1.0a1.dev15-py2.7.egg`的压缩包文件是该库的一个特定版本,适用于Python 2.7环境。 **Python开发语言** Python是一种高级编程语言,以其简洁明了的语法和强大的库支持而受到开发者喜爱。在后端开发...

Global site tag (gtag.js) - Google Analytics