代做homework | shell | 代写assignment | go代做 – ECS 140A Programming Languages

ECS 140A Programming Languages

代做homework | shell | 代写assignment | go代做 – 这是一个关于shell/go的题目, 主要考察了关于go的内容,是一个比较经典的题目, 是有一定代表意义的go等代写方向, 这个项目是assignment代写的代写题目

shell代写代写 代写shell

Spring 2021

homework 1 due Wednesday April 21st at 5pm

About This Assignment
  • This assignment asks you to complete programming tasks using the Go programming language.
  • You are only allowed to use the subset of Go that we have discussed in class. You will get no credit if any of your problem solutions use constructs not discussed in class.Please use Piazza for any clarifications regarding this issue.
  • To complete the assignment (i) downloadhw1-handout.zipfrom Canvas, (ii) modify the.gofiles in thehw1-handoutdirectory as per the instructions in this document, and (iii) zip thehw1-handoutdirectory intohw1-handout.zipand upload thiszip file to Canvas by the due date. Do not change the file names, create new files, or change the directory structure ofhw1-handout.
  • This assignment has to be worked on individually.
  • We will be usingGo version 1.11.4, which can be downloaded from https://golang.org/dl/. Run the commandgo versionto verify that you have the correct version installed: $ go version go version go1.11.4
  • Go 1.11.4 has been installed on allCSIF machinesin the directory/usr/local/go/; thegobinary is, thus, at/usr/local/go/bin/go. The default version of Go on the CSIF machines is 1.10.4 so you should consider adding /usr/local/go/bin/to your PATH. Either type export PATH=/usr/local/go/bin:$PATHfor every new shell that you spawn, or look up instructions for how to set the PATH variable automatically for every new shell.
  • Information about using CSIF computers, such as how to remotely login to CSIF computers from home and how to copy files to/from the CSIF computers using your personal computer, can be found athttp://csifdocs.cs.ucdavis.edu/about-us/ csif-general-faq.
  • Begin working on the homework early.
1
Cindy Rubio Gonzalez 2021
  • Apart from the description in this document, look at the unit tests provided to under- stand the requirements for the code you have to write.
  • Post questions on piazza if you require any further clarifications. Use private posts if your question contains part of the solution to the homework.

1 triangle (5 points)

  • Modify the unit tests in theTestGetTriangleTypefunction inhw1-handout/triangle/triangle_test.go. One unit test has already been written for you.
  • The goal is to write enough unit tests to get 100% code coverage for the code in hw1-handout/triangle/triangle.go. All unit tests should pass.
  • From thehw1-handout/triangledirectory, run thego test -covercommand to see the current code coverage.
  • From thehw1-handout/triangledirectory, run the following two commands to see which statements are covered by the unit tests: $ go test -coverprofile=temp.cov $ go tool cover -html=temp.cov
  • Do NOT modify the code inhw1-handout/triangle/triangle.go.

2 min (5 points)

  • Modify theMinfunction inhw1-handout/min/min.go.
  • The functionMinshould return the minimum/lowest value in the argumentarr, and 0 if the argument isnil.
  • Some unit tests are provided for you inhw1-handout/min/min_test.go. From thehw1-handout/mindirectory, run thego testcommand to run the unit tests.
  • If needed, add more unit tests inhw1-handout/min/min_test.goto get 100% code coverage for the code inhw1-handout/min/min.go.
  • From thehw1-handout/mindirectory, run thego test -covercommand to see the current code coverage.
  • From thehw1-handout/mindirectory, run the following two commands to see which statements are covered by the unit tests: $ go test -coverprofile=temp.cov $ go tool cover -html=temp.cov
2
Cindy Rubio Gonzalez 2021

3 nfa (10 points)

A non-deterministic finite automaton (NFA) is defined by a set of states, symbols in an alphabet, and a transition function. A state is represented by an integer. A symbol is represented by a rune, i.e., a character. Given a state and a symbol, a transition function returns the set of states that the NFA can transition to after reading the given symbol. This set of next states could be empty. A graphical representation of an NFA is shown below:

0
1
2
b a
a
b

In this example,{ 0 , 1 , 2 }are the set of states,{a, b}are the set of symbols, and the transition function is represented by labelled arrows between states.

  • If the NFA is in state 0 and it reads the symbola, then it can transition to either state 1 or to state 2.
  • If the NFA is in state 0 and it reads the symbolb, then it can only transition to state
  • If the NFA is in state 1 and it reads the symbolb, then it can only transition to state
  • If the NFA is in state 1 and it reads the symbola, it cannot make any transitions.
  • If the NFA is in state 2 and it reads the symbolaorb, it cannot make any transitions.

A given final state is said to bereachable from a given start state via a given input sequence of symbols if there exists a sequence of transitions such that if the NFA starts at the start state it would reach the final state after reading the entire sequence of input symbols. In the example NFA above:

  • The state 1 is reachable from the state 0 via the input sequenceabababa.
  • The state 1 isnotreachable from the state 0 via the input sequenceababab.
  • The state 2 is reachable from state 0 via the input sequenceabababa.

The transition function for the NFA described above is represented by theexpTransitions function inhw1-handout/nfa/nfa_test.go. Some unit tests have also been given to you inhw1-handout/nfa/nfa_test.go. From thehw1-handout/nfadirectory, run thego test command to run the unit tests.

3
Cindy Rubio Gonzalez 2021
  • Write an implementation of theReachablefunction inhw1-handout/nfa/nfa.gothat returns trueif a final state is reachable from the start state after reading an input sequence of symbols, andfalse, otherwise.
  • If needed, write new tests, inhw1-handout/nfa/nfa_test.goto ensure that you get 100% code coverage for your code. From thehw1-handout/nfadirectory, run thego test -covercommand to see the current code coverage. From thehw1-handout/nfadirectory, run the following two commands to see which statements are covered by the unit tests: $ go test -coverprofile=temp.cov $ go tool cover -html=temp.cov

Working with expression AST

  • For parts 4 and 5, you will be using the exprpackage in the hw1-handout/expr directory.
  • You should NOT modify any code in thehw1-handout/exprdirectory.
  • hw1-handout/expr/ast.godefines an abstract syntax tree (AST) for arithmetic ex- pressions.
  • Theexpr.Parsefunction parses a string into an AST; seehw1-handout/expr/parse.go.
  • The expr.Evalmethod evaluates an expression given a mapping from variables to values; seehw1-handout/expr/eval.go.
  • Theexpr.Formatfunction formats an expression AST as a string; seehw1-handout/expr/print.go.
  • Note the use of type switches and type assertions in this code for inspecting the concrete types that anExprinterface type represents.
  • This code is also described in Chapter 7 of The Go Programming Language book: http://gopl.io.

4 depth (10 points)

  • Implement the functionDepthinhw1-handout/depth/depth.gothat, given an AST represented byexpr.Expr, returns the maximum number of AST nodes between the root of the tree and any leaf (literal or variable) in the tree.
4
Cindy Rubio Gonzalez 2021
  • Unit tests have been provided inhw1-handout/depth/depth_test.go. Note that theDepthfunction shouldpanicwhen the input is not an expr.Literal, expr.Var, expr.Unary,orexpr.Binary, as illustrated by the TestDepth_Failtest case inhw1-handout/depth/depth_test.go. From thehw1-handout/depthdirectory, run thego testcommand to run the unit tests.
  • If needed, write new tests, inhw1-handout/depth/depth_test.goto ensure that you get 100% code coverage for your code inhw1-handout/depth/depth.go. From thehw1-handout/depthdirectory, run thego test -covercommand to see the current code coverage. From thehw1-handout/depthdirectory, run the following two commands to see which statements are covered by the unit tests: $ go test -coverprofile=temp.cov $ go tool cover -html=temp.cov

5 simplify (20 points)

  • Implement theSimplifyfunction inhw1-handout/simplify/simplify.go. Given an arithmetic expression as anexpr.Exprand an environmentexpr.Env,Simplify simplifies the given expression using the values of the variables in the environment and by performing some algebraic simplifications.
  • Your implementation ofSimplifyshould perform the following simplifications:
    • AnyVarwhose name is in the provided map should be replaced with its value.
    • Any unary operator whose operand is a Literal should be replaced with a Literalrepresenting the result.
    • Anybinaryoperator whose operands are bothLiterals should be replaced with aLiteralrepresenting the result.
    • Anybinary operator performing addition, where one operand is 0 , should be reduced. (0 +X=X=X+ 0)
    • Anybinary operator performing multiplication, where one operand is 1 or 0 , should be reduced. (1X=X=X1 and 0X= 0 =X0)
No other simplifications should be performed.
  • Unit tests have been provided inhw1-handout/simplify/simplify_test.go. From thehw1-handout/simplifydirectory, run thego testcommand to run the unit tests.
5
Cindy Rubio Gonzalez 2021
  • If needed, write new tests, inhw1-handout/simplify/simplify_test.goto ensure that you get 100% code coverage for your code inhw1-handout/simplify/simplify.go. From thehw1-handout/simplifydirectory, run thego test -covercommand to see the current code coverage. From thehw1-handout/simplifydirectory, run the following two commands to see which statements are covered by the unit tests: $ go test -coverprofile=temp.cov $ go tool cover -html=temp.cov

Working with the Go AST

  • For parts 6 and 7, you will be writing Go code that works with the abstract syntax tree (AST) of the Go language itself.
  • Information about thego/astpackage can be found at https://golang.org/pkg/go/ast/, which lists the different types of AST nodes along with their fields. You might also find it useful to take a look at the source code forgo/ast, which can be found athttps://golang.org/src/go/ast/.
  • Sample code illustrating how to build the AST given Go code and then print it using ast.Printcan be found athttps://play.golang.org/p/1g-lt3D1N6a.
  • Sample code computing the number of function calls usingast.Inspectcan be found athttps://play.golang.org/p/cq2OI6CA6v_n.
  • An alternative toast.Inspectis to useast.Walk, which uses theast.Visitorinter- face type.
  • Sample code that modifies Go code programmatically can be found at https://play.golang.org/p/mRKeN7SZD8g.
  • An introduction to thego/astpackage can be found at https://zupzup.org/go-ast-traversal/.

6 branch (20 points)

  • Modifyhw1-handout/branch/branch.goby implementing thebranchCountfunction that returns the count of the number of branching statements in a Go function.
  • Branching statements are those where the program has a choice of what to execute next; e.g.,ifandforstatements.
6
Cindy Rubio Gonzalez 2021
  • The branchCount function is called from theComputeBranchFactor function that takes a Go program as a string, and for each function in that program, counts the number of branching statements in the Go function by callingbranchCount. ComputeBranchFactorreturns amap[string]uintfrom the name of the function to the number of branching statements it contains. You should not need to modify the implementation ofComputeBranchFactor.
  • Unit tests have been provided inhw1-handout/branch/branch_test.go. From thehw1-handout/branchdirectory, run thego testcommand to run the unit tests.
  • If needed, write new tests, inhw1-handout/branch/branch_test.goto ensure that you get 100% code coverage for your code inhw1-handout/branch/branch.go. From thehw1-handout/branchdirectory, run thego test -covercommand to see the current code coverage. From thehw1-handout/branchdirectory, run the following two commands to see which statements are covered by the unit tests: $ go test -coverprofile=temp.cov $ go tool cover -html=temp.cov

7 rewrite (30 points)

  • Modify therewriteCallsfunction inhw1-handout/rewrite/rewrite.goto rewrite occurrences of calls toexpr.ParseAndEvalin the input Go code so as to simplify the first argument toexpr.ParseAndEval. The definition of the functionexpr.ParseAndEvalcan be found in hw1-handout/expr/parse_and_eval.go.
  • As an example of the rewrite, the following statement in the input Go program: x := expr.ParseAndEval("2 + 3", env) would be rewritten to x := expr.ParseAndEval("5", env)
  • TherewriteCallsfunction you have to implement has to do the following:
    • Identify calls toexpr.ParseAndEvalin the input Go code with two arguments.
    • If the first argument to this call is a string literal, then parse this string into an expr.Expr.
    • Simplify thisexpr.Exprusing thesimplify.Simplifyfunction you implemented in part 5 (using an emptyexpr.Env).
    • Format the resultingexpr.Exprinto a string.
7
Cindy Rubio Gonzalez 2021
  • Replace the first argument inexpr.ParseAndEvalwith this string representing the simplified expression in the input Go code.
  • Unit tests have been provided in hw1-handout/rewrite/rewrite_test.go, which read inputs and outputs from thehw1-handout/rewrite_testdirectory. From thehw1-handout/rewritedirectory, run thego testcommand to run the unit tests.
  • If needed, write new tests, inhw1-handout/rewrite/rewrite_test.goto ensure that you get 100% code coverage for your code inhw1-handout/rewrite/rewrite.go. From thehw1-handout/rewritedirectory, run thego test -covercommand to see the current code coverage. From the hw1-handout/rewritedirectory, run the following two commands to see which statements are covered by the unit tests: $ go test -coverprofile=temp.cov $ go tool cover -html=temp.cov
8
Cindy Rubio Gonzalez 2021