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

Google python string1.py(python 2.7)

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

# Basic string exercises
# Fill in the code for the functions below. main() is already set up
# to call the functions with a few different inputs,
# printing 'OK' when each function is correct.
# The starter code for each function includes a 'return'
# which is just a placeholder for your code.
# It's ok if you do not complete all the functions, and there
# are some additional functions to try in string2.py.


# A. donuts
# Given an int count of a number of donuts, return a string
# of the form 'Number of donuts: <count>', where <count> is the number
# passed in. However, if the count is 10 or more, then use the word 'many'
# instead of the actual count.
# So donuts(5) returns 'Number of donuts: 5'
# and donuts(23) returns 'Number of donuts: many'
def donuts(count):
  string=''
  if count>=10:
    string='Number of donuts: many'
  else:
    string='Number of donuts: %d'%count
  return string



# B. both_ends
# Given a string s, return a string made of the first 2
# and the last 2 chars of the original string,
# so 'spring' yields 'spng'. However, if the string length
# is less than 2, return instead the empty string.
def both_ends(s):
  if len(s)<2:
    return ''
  else:
    s=s[0]+s[1]+s[len(s)-2]+s[len(s)-1]
  return s



# C. fix_start
# Given a string s, return a string
# where all occurences of its first char have
# been changed to '*', except do not change
# the first char itself.
# e.g. 'babble' yields 'ba**le'
# Assume that the string is length 1 or more.
# Hint: s.replace(stra, strb) returns a version of string s
# where all instances of stra have been replaced by strb.
def fix_start(s):
 
  if len(s)>1:
    s=s[0]+s[1:].replace(s[0],'*')
  return s


# D. MixUp
# Given strings a and b, return a single string with a and b separated
# by a space '<a> <b>', except swap the first 2 chars of each string.
# e.g.
#   'mix', pod' -> 'pox mid'
#   'dog', 'dinner' -> 'dig donner'
# Assume a and b are length 2 or more.
def mix_up(a, b):
 
  if len(a)>=2 and len(b)>=2:
    a=b[:2]+a[2:]+' '+a[:2]+b[2:]
  return a



# Provided simple 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))


# Provided main() calls the above functions with interesting inputs,
# using test() to check if each result is correct or not.
def main():
  print 'donuts'
  # Each line calls donuts, compares its result to the expected for that call.
  test(donuts(4), 'Number of donuts: 4')
  test(donuts(9), 'Number of donuts: 9')
  test(donuts(10), 'Number of donuts: many')
  test(donuts(99), 'Number of donuts: many')

  print
  print 'both_ends'
  test(both_ends('spring'), 'spng')
  test(both_ends('Hello'), 'Helo')
  test(both_ends('a'), '')
  test(both_ends('xyz'), 'xyyz')

 
  print
  print 'fix_start'
  test(fix_start('babble'), 'ba**le')
  test(fix_start('aardvark'), 'a*rdv*rk')
  test(fix_start('google'), 'goo*le')
  test(fix_start('donut'), 'donut')

  print
  print 'mix_up'
  test(mix_up('mix', 'pod'), 'pox mid')
  test(mix_up('dog', 'dinner'), 'dig donner')
  test(mix_up('gnash', 'sport'), 'spash gnort')
  test(mix_up('pezzy', 'firm'), 'fizzy perm')


# Standard boilerplate to call the main() function.
if __name__ == '__main__':
  main()
分享到:
评论

相关推荐

    libxml2-python-2.7.7.win32-py2.7.exe

    《libxml2与Python2.7的交互:深入解析libxml2-python-2.7.7.win32-py2.7.exe》 libxml2是Linux基金会的一个项目,它是一个开源的XML解析库,提供了丰富的API,用于处理XML、HTML、XSLT等格式的数据。这个库的强大...

    详解python3中用HTMLTestRunner.py报ImportError: No module named 'StringIO'如何解决

    在Python3中使用HTMLTestRunner.py时,可能会遇到一个常见的问题,即报出"ImportError: No module named 'StringIO'"的错误。这个问题的出现,主要是因为在Python3中,StringIO模块已经被io模块中的StringIO类所取代...

    python-3.9.9.tgz安装包

    - `setup.py`:Python脚本,用于构建、安装和打包Python项目。 - `Include`:包含Python头文件,供C扩展模块使用。 - `Lib`:存放Python标准库模块的源代码。 - `DLLs`(Windows系统):包含Python运行时所需的动态...

    Python库 | XlsxWriter-1.2.8-py2.py3-none-any.whl

    XlsxWriter-1.2.8-py2.py3-none-any.whl是该库的一个特定版本的压缩包,它兼容Python 2和Python 3,可以在任何平台上运行。 **Python库的安装与使用**: 在Python环境中,安装XlsxWriter通常通过`pip`命令完成。...

    Python-3.6.5.tgz

    1. **Python 3.6系列的新特性**: - **字符串格式化**:引入了新的`f-string`语法,允许在字符串中直接嵌入表达式,提高了代码可读性和效率。 - **asyncio改进**:增强了异步I/O库asyncio,支持更灵活的协程控制,...

    Python库 | MultiString-0.1.5c-py2.7.egg

    python库。 资源全名:MultiString-0.1.5c-py2.7.egg

    protobuf-python-3.4.0.zip

    protobuf-python-3.4.0.zip是一个包含Python接口的Google Protocol Buffers(简称protobuf)库的压缩包,版本为3.4.0。Protocol Buffers是一种数据序列化协议,它允许开发者定义数据结构,然后可以将这些数据结构...

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

    rpc CreateUser (User) returns (google.protobuf.Empty); rpc GetUser (UserId) returns (User); } message User { string id = 1; string name = 2; } message UserId { string id = 1; } ``` 接下来,...

    Python-3.6.8.tgz

    1. `LICENSE`: 包含Python的许可协议,通常为Python Software Foundation License。 2. `README`: 提供关于如何构建、安装和使用Python的基本信息。 3. `setup.py`: 是Python项目的构建脚本,用于编译和安装Python...

    最新版python Python-3.6.12.tgz

    2. `setup.py`:这是Python项目的标准安装脚本,用于构建、安装和打包Python软件。 3. `LICENSE`或`LICENSE.txt`:包含了Python的许可证信息,通常是PSF(Python Software Foundation)许可证。 4. `README`或`...

    浅析Python的web.py框架中url的设定方法

    ### 浅析Python的web.py框架中URL的设定方法 #### 概述 web.py是一个用Python编写的轻量级Web应用框架,以其简洁、易用的特点受到许多开发者喜爱。本文将详细介绍web.py框架中URL设定的方法,并通过具体示例进行...

    protobuf-python-3.11.2.tar.gz

    1. `setup.py`:这是Python项目的安装脚本,用户可以通过运行`python setup.py install`来安装protobuf库。 2. `src/`:源代码目录,包含了protobuf的Python实现。这里可能有`proto/`子目录,存放.proto文件,用于...

    千峰Python超详细入门教程(百度云盘分享).txt

    │ │ 1、String(字符串).py │ │ 2、String的内置函数.py │ │ 3、布尔值和空值.py │ │ 4、变量的类型问题.py │ │ │ └─video │ 千锋Python教程:17.运算符&字符串1.mp4 │ 千锋Python教程:18.运算符&...

    python 2.7 和 python 3.6 amd64

    Python 2.7是Python 2.x系列的最后一个版本,发布于2010年,并在2020年1月1日停止了官方支持。它的语法相对古老,但拥有大量的现有代码库和项目。Python 2.7支持一些在Python 3中已移除的特性,如旧式的字符串表示法...

    Python库 | pdfkit-0.6.1-py2.7.egg

    **Python库 pdfkit-0.6.1-py2.7.egg** `pdfkit` 是一个用于将 HTML 转换为 PDF 的 Python 库,它利用了 wkhtmltopdf 工具来完成实际的转换工作。wkhtmltopdf 是一个基于 WebKit 渲染引擎的开源命令行工具,能够将 ...

    将文字筛选出来写入另一个文件,xpy.py.py

    标题和描述中提到的"将文字筛选出来写入另一个文件,xpy.py.py"显然涉及到一个Python脚本,它用于从一个源文件(如`string.txt`)中提取特定的文字,并将其写入新的文件。这个过程可以通过Python的内置文件操作函数...

    python-leetcode题解之151-Reverse-Words-in-a-String.py

    python python_leetcode题解之151_Reverse_Words_in_a_String.py

    Assignment.py

    Question 1. A. Write a function named ​triple ​that has 1 input and return 1 value. triple(sentence) - The input ​sentence ​of the function is a string - The function returns a new string where ...

    python web.py开发httpserver解决跨域问题实例解析

    在本文中,我们将深入探讨如何使用Python的web.py框架开发HTTP服务器并解决跨域问题。跨域问题通常在Web开发中出现,尤其是当前端应用(如浏览器中的JavaScript)尝试从一个源向另一个源发送请求时。由于浏览器的...

Global site tag (gtag.js) - Google Analytics