随笔分类 - python
摘要:在学习了前面python课程的综合知识后,再来做个小游戏。游戏规则:任给一个文件file,实现一个dict,key为file中出现的单词,value为一个由key在file中出现的位置后紧跟的下一个单词组成的list。例如:file中,紧跟单词'and'后可能出现了'then','the','you'等,则dict中包含{'and',['then','the','you']}。详细描述如下:"""Mimic pyquick exercis
阅读全文
摘要:在学习了python的基本数据类型及相关语法后,现综合所学list,tuples,str,dict,sort,file等知识,解决以下问题。题目及代码如下: 1 # -*- coding: cp936 -*- 2 """ 3 1. For the --count flag, implement a print_words(filename) function that counts 4 how often each word appears in the text and prints: 5 word1 count1 6 word2 count2 7 ... 8
阅读全文
摘要:list2里面含两个练习,方法多彩多样。D题实现对list内数据去重操作,E题实现对已排序list实现归并排序,并且要求线性时间复杂度。题目和代码如下: 1 # D. Given a list of numbers, return a list where 2 # all adjacent == elements have been reduced to a single element, 3 # so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or 4 # modify the passed in list. 5 de
阅读全文
摘要:学习完string之后,现在来看看list的用法: 1 # -*- coding: cp936 -*- 2 #A. match_ends 3 # Given a list of strings, return the count of the number of 4 # strings where the string length is 2 or more and the first 5 # and last chars of the string are the same. 6 def match_ends(words): 7 count = 0 8 for item in...
阅读全文
摘要:在string1之后继续推出string2:View Code 1 # -*- coding: cp936 -*- 2 # D. verbing 3 # Given a string, if its length is at least 3, 4 # add 'ing' to its end. 5 # Unless it already ends in 'ing', in which case 6 # add 'ly' instead. 7 # If the string length is less than 3, leave it uncha
阅读全文
摘要:这是google python课程里的练习题一——string 1,通过这几个小程序的实现,足以窥一斑而知全豹,发现python的美。题目及代码如下:# 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 wo
阅读全文

浙公网安备 33010602011771号