美文网首页
Python代写:CS110 Anonymous Functio

Python代写:CS110 Anonymous Functio

作者: wubopo | 来源:发表于2019-04-21 20:05 被阅读0次

    练习Python的匿名函数也就是lambda表达式的写法,属于Python代写里面比较麻烦的作业类型之一。RequirementIn the problems from the last homework, we did a bit of rudimentary Scrabble scoring. Your ultimate task here is to write a function that takes as input a rack - a list of letters - and returns the highest scoring word that can be made with those letters. This is the key ingredient in a computerized Scrabble game!In this problem, you may use recursion as well as the built-in higher-order functions map, filter, and reduce. In fact, if you write a function that needs to process a long list (e.g. a dictionary) with more than a few hundred items in it, you are much better off letting map, filter, or reduce cruise through those lists since they have been optimized to work very fast.There will be a few places here where using anonymous functions (lambda) is probably the cleanest and nicest way to do business. Use it when it’s the cleanest way to proceed.One of the objectives of this problem is to have you think about designing a more complicated program that involves several functions. You have complete autonomy in deciding what functions to write in order to reach the ultimate goal (see more on the two required functions below). Try to think carefully about which functions you will need and try to implement those functions so that they are as simple and clean as possible. Part of your score on this problem will be based on the elegance of your overall design and individual functions.Our solution has fewer than 9 functions, two of which we modified from our solution to Homework 1. The remaining 7 (or so) functions range from one to four lines of code per function. While you are not required to have the same number of functions and you may have a few slightly longer functions, this is intended to indicate that there is not much code that needs to be written here!Be sure to have a comment at the top of the file with your name(s), the filename, and the date. Any function that is even a bit complicated should at least have a short comment explaining to the reader how this function works. Also, include a docstring for every function that you write.All of your work should go into a single file named scrabble.py. Any tests and their results should go into this file, commented out of course. Make sure your file loads without producing any output. We will test your program by loading this file and testing your functions. Be sure they are properly named.Firstly, rewrite your definition of letterScore(letter, scoreList> so it makes use of the built-in function filter.Secondly, rewrite your definition of wordScore(word, scoreList) so that it makes use of the built-in map and reduce.As an exercise in writing list comprehensions, define the following variables, using expressions made up of list comprehensions:words – all of the words in the list Dictionary.twoLetterWords – all of the words in the list Dictionary having exactly two letters.lengths – a list of only the lengths of all words in Dictionary.Now the main problemA bit later in this problem, we’ll give you a fairly large dictionary of English words to use. For now, we recommend that you use the following tiny dictionary during the course of testing and development. Include this line near the top of your file. Now, it’s a global variable that can be used by any of your functions in that file. Similarly, you should include the scrabble letter score list from Homework 1.1Dictionary = [&"a&", &"am&", &"at&", &"apple&", &"bat&", &"bar&", &"babble&", &"can&", &"foo&", &"spam&", &"spammy&", &"zzyzva&"]& Don’t try to change the values of these global variables! As we’ll see soon, that can make Python angry. However, there’s no need to change the contents of the Dictionary or the scrabble letter scores during execution of the program.The detailsUltimately, there are two functions that we will be testing.scoreList(Rack) takes as input a Rack which is a list of lower-case letters and returns a list of all of the words in the global Dictionary that can be made from those letters and the score for each one. Specifically, this function returns a list of lists, each of which contains a string that can be made from the Rack and its Scrabble score. Here are some examples using the tiny Dictionary above:>>> scoreList(["a", "s", "m", "t", "p"]) [[apos;aapos;, 1], [apos;amapos;, 4], [apos;atapos;, 2], [apos;spamapos;, 8]] >>> scoreList(["a", "s", "m", "o", "f", "o"]) [[apos;aapos;, 1], [apos;amapos;, 4], [apos;fooapos;, 6]] The order in which the words are presented is not important.bestWord(Rack) takes as input a Rack as above and returns a list with two elements: the highest possible scoring word from that Rack followed by its score. If there are ties, they can be broken arbitrarily. Here is an example, again using the tiny Dictionary above:>>> bestWord(["a", "s", "m", "t", "p"]) [apos;spamapos;, 8] Aside from these two functions, all of the other helper functions are up to you! Some of those helper functions may be functions that you wrote in Homework 1 or slight variants of those functions. However, you might find it useful to use a strategy in which you write a function that determines whether or not a given string can be made from the given Rack list. Then, another function can use that function to determine the list of all strings in the dictionary that can be made from the given Rack. Finally, another function might score those words.Remember to use map, reduce, or filter where appropriate - they are powerful and they are optimized to be very fast.Test each function carefully with small test inputs before you proceed to write the next function. This will save you a lot of time and aggravation!A reminder about in: Imagine that you are writing a function that attempts to determine if a given string S can be made from the letters in the Rack list. You might be tempted to scramble (“permute” to use a technical term) the Rack in every possible way as part of this process, but that is more work than necessary. Instead, you can test if the first symbol in your string, S[0], appears in the rack with the statement:1234if S[0] in Rack: # if True you will end up hereelse: # if False you will end up here& That in feature is very handy. Now, recursion will let you do the rest of the work without much effort on your part!Although we don’t expect that you will end up doing huge recursive calls, you should know that Python can get snippy when there are a lot of recursive calls. By default, Python generally complains when there are 1000 or more recursive calls of the same function. To convince Python to be friendlier, you can use the following at the top of your file.123import syssys.setrecursionlimit(10000) # Allows up to 10000 recursive calls; the maximum permitted ranges from system to system& Trying it with a bigger dictionaryFinally, if you want to test out a big dictionary (it’s more fun than the little one above), you can download the file. It contains a large list of words (a bit over 4000 words). The list is still called Dictionary. After you have downloaded the file and placed it in the same directory as your Python file, you should do the following at the top of your Python file:1from dict import *& Now, you can refer to Dictionary as a global variable. This is much nicer than cutting-and-pasting that very large list into your Python file. Here are some examples using that Dictionary.>>> scoreList([apos;wapos;, apos;yapos;, apos;lapos;, apos;eapos;, apos;lapos;, apos;oapos;]) [[apos;leoapos;, 3], [apos;lowapos;, 6], [apos;lowlyapos;, 11], [apos;owapos;, 5], [apos;oweapos;, 6], [apos;owlapos;, 6], [apos;weapos;, 5], [apos;wellapos;, 7], [apos;woeapos;, 6], [apos;yellapos;, 7], [apos;yoapos;, 5]] Notice that “yellow” is not in this dictionary. We “cropped” off words of length 6 or more to keep the dictionary from getting too large. Finally, here is an example of bestWord in action:>>> bestWord([apos;wapos;, apos;yapos;, apos;lapos;, apos;eapos;, apos;lapos;, apos;oapos;]) [apos;lowlyapos;, 11] >>> bestWord(["s", "p", "a", "m", "y"]) [apos;mayapos;, 8] >>> bestWord(["s", "p", "a", "m", "y", "z"]) [apos;zapapos;, 14] And an even bigger dictionary!Want to try this with an even bigger dictionary? We’ve got one! The bad news is that it causes IDLE to crash on some computers. However, if you are running Python without IDLE (e.g. from the Unix shell) then this dictionary should work just fine!Now you can use that in your program by including the line:1from bigdict import *& The dictionary is still called Dictionary and it is available as a global variable. Here is an example of it in use:>>> scoreList([apos;aapos;, apos;bapos;, apos;vapos;, apos;xapos;, apos;yapos;, apos;yapos;, apos;zapos;, apos;zapos;, apos;zapos;]) [[apos;abapos;, 4], [apos;abyapos;, 8], [apos;axapos;, 9], [apos;ayapos;, 5], [apos;baapos;, 4], [apos;bayapos;, 8], [apos;byapos;, 7], [apos;yaapos;, 5], [apos;yayapos;, 9], [apos;zaapos;, 11], [apos;zaxapos;, 19], [apos;zyzzyvaapos;, 43], [apos;zzzapos;, 30]] 本团队核心人员组成主要包括BAT一线工程师,精通德英语!我们主要业务范围是代做编程大作业、课程设计等等。我们的方向领域:window编程 数值算法 AI人工智能 金融统计 计量分析 大数据 网络编程 WEB编程 通讯编程 游戏编程多媒体linux 外挂编程 程序API图像处理 嵌入式/单片机 数据库编程 控制台 进程与线程 网络安全 汇编语言 硬件编程 软件设计 工程标准规等。其中代写编程、代写程序、代写留学生程序作业语言或工具包括但不限于以下范围:C/C++/C#代写Java代写IT代写Python代写辅导编程作业Matlab代写Haskell代写Processing代写Linux环境搭建Rust代写Data Structure Assginment 数据结构代写MIPS代写Machine Learning 作业 代写Oracle/SQL/PostgreSQL/Pig 数据库代写/代做/辅导Web开发、网站开发、网站作业ASP.NET网站开发Finance Insurace Statistics统计、回归、迭代Prolog代写Computer Computational method代做因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:99515681@qq.com 微信:codehelp

    相关文章

      网友评论

          本文标题:Python代写:CS110 Anonymous Functio

          本文链接:https://www.haomeiwen.com/subject/agajgqtx.html