google python class exercise

2024-03-03 19:08
文章标签 python google class exercise

本文主要是介绍google python class exercise,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

谷歌 python class 地址 : https://developers.google.com/edu/python/

美国名字: http://www.socialsecurity.gov/OACT/babynames/


1 string1.py

string 拼接的时候,比较难搞

c = a + b 这种方式不能用,不知道为什么!!!


#!/usr/bin/python -tt
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0# Google's Python Class
# http://code.google.com/edu/languages/google-python-class/# 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):# +++your code here+++if count <= 9: return "Number of donuts: %d" % countelse:return "Number of donuts: many"# 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):# +++your code here+++if len(s)<2:return ''else:return s[0:2] + s[-2:]# 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):# +++your code here+++first_char = s[0]repl = s.replace(s[0], "*")s = s[0] + repl[1:]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):# +++your code here+++a_tmp = a[0:2] + b[2:]b_tmp = b[0:2] + a[2:]return_tmp = "%s %s" % (b_tmp, a_tmp)return return_tmp# 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')printprint 'both_ends'test(both_ends('spring'), 'spng')test(both_ends('Hello'), 'Helo')test(both_ends('a'), '')test(both_ends('xyz'), 'xyyz')printprint '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')printprint '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()


2,list 

#!/usr/bin/python -tt
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0# Google's Python Class
# http://code.google.com/edu/languages/google-python-class/# Basic list 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 list2.py.# A. match_ends
# Given a list of strings, return the count of the number of
# strings where the string length is 2 or more and the first
# and last chars of the string are the same.
# Note: python does not have a ++ operator, but += works.
def match_ends(words):# +++your code here+++match_cnt = 0for var in words:if len(var)>=2:if var[0] == var[-1]:match_cnt += 1return match_cnt# B. front_x
# Given a list of strings, return a list with the strings
# in sorted order, except group all the strings that begin with 'x' first.
# e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
# ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
# Hint: this can be done by making 2 lists and sorting each of them
# before combining them.def front_x(words):# +++your code here+++words_z=[];words_abc=[];for var in words:if var[0] == "x":words_z.append(var)else :words_abc.append(var)return sorted(words_z) + sorted(words_abc)
#	words_abc = sorted(words_abc)
#	print words_abc
#	words_z = sorted(words_z)
#	print words_z
#	words_z.extend(words_abc)
#	print words_z
#	return words_z# C. sort_last
# Given a list of non-empty tuples, return a list sorted in increasing
# order by the last element in each tuple.
# e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
# [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
# Hint: use a custom key= function to extract the last element form each tuple.
def Last_char(s):return s[-1]def sort_last(tuples):# +++your code here+++return sorted(tuples, key=Last_char)# 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 'match_ends'test(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3)test(match_ends(['', 'x', 'xy', 'xyx', 'xx']), 2)test(match_ends(['aaa', 'be', 'abc', 'hello']), 1)printprint 'front_x'test(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']),['xaa', 'xzz', 'axx', 'bbb', 'ccc'])test(front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']),['xaa', 'xcc', 'aaa', 'bbb', 'ccc'])test(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']),['xanadu', 'xyz', 'aardvark', 'apple', 'mix'])printprint 'sort_last'test(sort_last([(1, 3), (3, 2), (2, 1)]),[(2, 1), (3, 2), (1, 3)])test(sort_last([(2, 3), (1, 2), (3, 1)]),[(3, 1), (1, 2), (2, 3)])test(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]),[(2, 2), (1, 3), (3, 4, 5), (1, 7)])if __name__ == '__main__':main()





这篇关于google python class exercise的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/770591

相关文章

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

一文带你搞懂Python中__init__.py到底是什么

《一文带你搞懂Python中__init__.py到底是什么》朋友们,今天我们来聊聊Python里一个低调却至关重要的文件——__init__.py,有些人可能听说过它是“包的标志”,也有人觉得它“没... 目录先搞懂 python 模块(module)Python 包(package)是啥?那么 __in

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

Python实现特殊字符判断并去掉非字母和数字的特殊字符

《Python实现特殊字符判断并去掉非字母和数字的特殊字符》在Python中,可以通过多种方法来判断字符串中是否包含非字母、数字的特殊字符,并将这些特殊字符去掉,本文为大家整理了一些常用的,希望对大家... 目录1. 使用正则表达式判断字符串中是否包含特殊字符去掉字符串中的特殊字符2. 使用 str.isa

python中各种常见文件的读写操作与类型转换详细指南

《python中各种常见文件的读写操作与类型转换详细指南》这篇文章主要为大家详细介绍了python中各种常见文件(txt,xls,csv,sql,二进制文件)的读写操作与类型转换,感兴趣的小伙伴可以跟... 目录1.文件txt读写标准用法1.1写入文件1.2读取文件2. 二进制文件读取3. 大文件读取3.1

使用Python实现一个优雅的异步定时器

《使用Python实现一个优雅的异步定时器》在Python中实现定时器功能是一个常见需求,尤其是在需要周期性执行任务的场景下,本文给大家介绍了基于asyncio和threading模块,可扩展的异步定... 目录需求背景代码1. 单例事件循环的实现2. 事件循环的运行与关闭3. 定时器核心逻辑4. 启动与停

基于Python实现读取嵌套压缩包下文件的方法

《基于Python实现读取嵌套压缩包下文件的方法》工作中遇到的问题,需要用Python实现嵌套压缩包下文件读取,本文给大家介绍了详细的解决方法,并有相关的代码示例供大家参考,需要的朋友可以参考下... 目录思路完整代码代码优化思路打开外层zip压缩包并遍历文件:使用with zipfile.ZipFil

Python处理函数调用超时的四种方法

《Python处理函数调用超时的四种方法》在实际开发过程中,我们可能会遇到一些场景,需要对函数的执行时间进行限制,例如,当一个函数执行时间过长时,可能会导致程序卡顿、资源占用过高,因此,在某些情况下,... 目录前言func-timeout1. 安装 func-timeout2. 基本用法自定义进程subp