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中列表list切分的实现

《python中列表list切分的实现》列表是Python中最常用的数据结构之一,经常需要对列表进行切分操作,本文主要介绍了python中列表list切分的实现,文中通过示例代码介绍的非常详细,对大家... 目录一、列表切片的基本用法1.1 基本切片操作1.2 切片的负索引1.3 切片的省略二、列表切分的高

基于Python实现一个PDF特殊字体提取工具

《基于Python实现一个PDF特殊字体提取工具》在PDF文档处理场景中,我们常常需要针对特定格式的文本内容进行提取分析,本文介绍的PDF特殊字体提取器是一款基于Python开发的桌面应用程序感兴趣的... 目录一、应用背景与功能概述二、技术架构与核心组件2.1 技术选型2.2 系统架构三、核心功能实现解析

通过Python脚本批量复制并规范命名视频文件

《通过Python脚本批量复制并规范命名视频文件》本文介绍了如何通过Python脚本批量复制并规范命名视频文件,实现自动补齐数字编号、保留原始文件、智能识别有效文件等功能,听过代码示例介绍的非常详细,... 目录一、问题场景:杂乱的视频文件名二、完整解决方案三、关键技术解析1. 智能路径处理2. 精准文件名

基于Python开发PDF转Doc格式小程序

《基于Python开发PDF转Doc格式小程序》这篇文章主要为大家详细介绍了如何基于Python开发PDF转Doc格式小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用python实现PDF转Doc格式小程序以下是一个使用Python实现PDF转DOC格式的GUI程序,采用T

Python使用PIL库将PNG图片转换为ICO图标的示例代码

《Python使用PIL库将PNG图片转换为ICO图标的示例代码》在软件开发和网站设计中,ICO图标是一种常用的图像格式,特别适用于应用程序图标、网页收藏夹图标等场景,本文将介绍如何使用Python的... 目录引言准备工作代码解析实践操作结果展示结语引言在软件开发和网站设计中,ICO图标是一种常用的图像

使用Python开发一个图像标注与OCR识别工具

《使用Python开发一个图像标注与OCR识别工具》:本文主要介绍一个使用Python开发的工具,允许用户在图像上进行矩形标注,使用OCR对标注区域进行文本识别,并将结果保存为Excel文件,感兴... 目录项目简介1. 图像加载与显示2. 矩形标注3. OCR识别4. 标注的保存与加载5. 裁剪与重置图像

使用Python实现表格字段智能去重

《使用Python实现表格字段智能去重》在数据分析和处理过程中,数据清洗是一个至关重要的步骤,其中字段去重是一个常见且关键的任务,下面我们看看如何使用Python进行表格字段智能去重吧... 目录一、引言二、数据重复问题的常见场景与影响三、python在数据清洗中的优势四、基于Python的表格字段智能去重

Python中如何控制小数点精度与对齐方式

《Python中如何控制小数点精度与对齐方式》在Python编程中,数据输出格式化是一个常见的需求,尤其是在涉及到小数点精度和对齐方式时,下面小编就来为大家介绍一下如何在Python中实现这些功能吧... 目录一、控制小数点精度1. 使用 round() 函数2. 使用字符串格式化二、控制对齐方式1. 使用

Python如何快速下载依赖

《Python如何快速下载依赖》本文介绍了四种在Python中快速下载依赖的方法,包括使用国内镜像源、开启pip并发下载功能、使用pipreqs批量下载项目依赖以及使用conda管理依赖,通过这些方法... 目录python快速下载依赖1. 使用国内镜像源临时使用镜像源永久配置镜像源2. 使用 pip 的并

Python如何实现读取csv文件时忽略文件的编码格式

《Python如何实现读取csv文件时忽略文件的编码格式》我们再日常读取csv文件的时候经常会发现csv文件的格式有多种,所以这篇文章为大家介绍了Python如何实现读取csv文件时忽略文件的编码格式... 目录1、背景介绍2、库的安装3、核心代码4、完整代码1、背景介绍我们再日常读取csv文件的时候经常