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和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Python虚拟环境与Conda使用指南分享

《Python虚拟环境与Conda使用指南分享》:本文主要介绍Python虚拟环境与Conda使用指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python 虚拟环境概述1.1 什么是虚拟环境1.2 为什么需要虚拟环境二、Python 内置的虚拟环境工具

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以