Hw05 cs61a

Submission: When you are done, submit with python3 ok --submit. You may submit more than once before the deadline; only the final submission will be scored. Check that you have successfully submitted your code on okpy. See Lab 0 for more instructions on submitting assignments. Using Ok: If you have any questions about using Ok, please refer to this guide. Readings: You might find the following references useful:.

CS 61A: Structure and Interpretation of Computer Programs

Grading: Homework is graded based on effort, not correctness. However, there is no partial credit; you must show substantial effort on every problem to receive any points. This homework is out of 2 points. Create a class called VendingMachine that represents a vending machine for some product. A VendingMachine object returns strings describing its interactions. Fill in the VendingMachine class, adding attributes and methods as appropriate, such that its behavior matches the following doctests:.

You may find Python string formatting syntax useful.

cs61a-hw05

A quick example:. The following diagram shows the order that the nodes would get printed, with the arrows representing function calls. Each path should be represented as a list of the labels along that path in the tree. You may yield the paths in any order.

We have provided a partial skeleton for you. You do not need to use this skeleton, but if your implementation diverges significantly from it, you might want to think about how you can get it to fit the skeleton.

Hint: If you're having trouble getting started, think about how you'd approach this problem if it wasn't a generator function. What would your recursive calls be?

With a generator function, what happens if you make a "recurisve call" within its body? Complete the Mint and Coin classes so that the coins created by a mint have the correct year and worth. You can assume the list already has at least one node containing value and the first element is never removed. Notice that you are not returning anything, so you should mutate the list. It returns a new linked list with the same structure as linkbut with f applied to any element within link or any Link instance contained in link.

Hint : You may find the built-in isinstance function useful. Due by pm on Tuesday, October 22 Instructions Submission: When you are done, submit with python3 ok --submit.

Readings: You might find the following references useful: Section 2. Fill in the VendingMachine class, adding attributes and methods as appropriate, such that its behavior matches the following doctests: class VendingMachine: """A vending machine that vends some product for some price.

Note : This ordering of the nodes in a tree is called a preorder traversal. Q6: Mint Complete the Mint and Coin classes so that the coins created by a mint have the correct year and worth.

Each Mint instance has a year stamp. The create method takes a subclass of Coin and returns an instance of that class stamped with the mint 's year which may be different from Mint. A Coin 's worth method returns the cents value of the coin plus one extra cent for each year of age beyond The update method sets the mint's stamp to Mint. Assume that the first element is never removed.

If an element is an instance of a linked list, recursively apply f inside that linked list as well.The final exam will be replaced by 3 minute in-lecture exams. The Midterm 1 exam and solution are posted.

Week 13 Class Material. Week 13 Resources. Week 12 Class Material. Week 12 Resources. Week 11 Class Material. Week 11 Resources. Week 9 Class Material. Week 9 Resources. Week 8 Class Material. Week 8 Resources. Week 7 Class Material.

Week 7 Resources. Week 6 Class Material. Week 6 Resources.

Week 5 Class Material. Week 5 Resources. Week 4 Class Material. Week 4 Resources. Week 3 Class Material. Week 3 Resources. Week 2 Class Material. Week 2 Resources. Week 1 Class Material. Week 1 Resources. Resource Search Search for terms like "recursion", "homework 3", or "practice". Functions Video full 1pp 8pp Disc Getting Started Solutions. Names Video full 1pp 8pp Control Video full 1pp 8pp Higher-Order Functions Video full 1pp 8pp GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Skip to content. Permalink Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up. Branch: master. Find file Copy path. Raw Blame History.

Only for testing purposes. Assume that the start pole has at least n disks of increasing size, and the end pole is either empty or has a top disk larger than the top n start disks.

Division is implemented as the multiplication of x by the reciprocal of y. Eva is right. Because when an uncertain number is used in computation, the interval span will extend no matter which arithmetic is used sub, add, mul, div.

The less these number is used, the tighter the error bounds would be. The multiple reference problem exists. The true value within a particular interval is fixed though unknown.

Nested combinations that refer to the same interval twice may assume two different true values for the same interval, which is an error that results in intervals that are larger than they should be. No value within this interval, when squared, will give a negative result. Hence, a program like par2 is better than par1 because it never combines the same interval more than once.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Tree definition. There are exactly three poles, and start and end must be different.

Move the top disk from rod 1 to rod 3. Move the top disk from rod 1 to rod 2. Move the top disk from rod 2 to rod 3. Move the top disk from rod 3 to rod 2.

Move the top disk from rod 2 to rod 1. Too hard, cannot figure it our on my own.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Skip to content.

hw05 cs61a

Permalink Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Sign up. Branch: master. Find file Copy path. Cannot retrieve contributors at this time. Raw Blame History. Only for testing purposes. Assume that the start pole has at least n disks of increasing size, and the end pole is either empty or has a top disk larger than the top n start disks.

Division is implemented as the multiplication of x by the reciprocal of y. If the extremum is located inside the interval then Are there more direct pathes? You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window.

There are exactly three poles, and start and end must be different. Move the top disk from rod 1 to rod 3. Move the top disk from rod 1 to rod 2. Move the top disk from rod 2 to rod 3. Move the top disk from rod 3 to rod 2. Move the top disk from rod 2 to rod 1.

Attempts: ['hwat', 'a', 'n00b']". Attempts: ['my', 'secret', 'password']". Extra Questions. Division is implemented as the multiplication of x by the.Download hw The vitamin problems can be found in the vitamin directory and the homework problems can be found in the problems directory. You must run python3 ok --submit twice: once inside the vitamin directory, and once inside the problems directory. Submission: When you are done, submit with python3 ok --submit.

hw05 cs61a

You may submit more than once before the deadline; only the final submission will be scored. Check that you have successfully submitted your code on okpy. See Lab 0 for more instructions on submitting assignments. Readings: You might find the following references useful:.

A classic puzzle called the Towers of Hanoi is a game that consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with n disks in a neat stack in ascending order of size on a start rod, the smallest at the top, forming a conical shape.

The objective of the puzzle is to move the entire stack to an end rod, obeying the following rules:. This interval arithmetic example is based on a classic problem from Structure and Interpretation of Computer Programs, Section 2. Alyssa P. Hacker is designing a system to help people solve engineering problems.

One feature she wants to provide in her system is the ability to manipulate inexact quantities such as measured parameters of physical devices with known precision, so that when computations are done with such approximate quantities the results will be numbers of known precision. Alyssa's idea is to implement interval arithmetic as a set of arithmetic operations for combining "intervals" objects that represent the range of possible values of an inexact quantity.

The result of adding, subracting, multiplying, or dividing two intervals is itself an interval, representing the range of the result. Alyssa postulates the existence of an abstract object called an "interval" that has two endpoints: a lower bound and an upper bound. She also presumes that, given the endpoints of an interval, she can construct the interval using the data constructor interval. Using the constructor and selectors, she defines the following operations:.

Alyssa's program is incomplete because she has not specified the implementation of the interval abstraction. She has implemented the constructor for you; fill in the implementation of the selectors. Louis Reasoner has also provided an implementation of interval multiplication. Beware: there are some data abstraction violations, so help him fix his code before someone sets it on fire. Using reasoning analogous to Alyssa's, define a subtraction function for intervals.

Try to reuse functions that have already been implemented. Alyssa implements division below by multiplying by the reciprocal of y. Ben Bitdiddle, an expert systems programmer, looks over Alyssa's shoulder and comments that it is not clear what it means to divide by an interval that spans zero.

Add an assert statement to Alyssa's code to ensure that no such interval is used as a divisor:.

Mutable functions

After considerable work, Alyssa P.Submission: When you are done, submit with python3 ok --submit. You may submit more than once before the deadline; only the final submission will be scored. Check that you have successfully submitted your code on okpy. See Lab 0 for more instructions on submitting assignments.

Using Ok: If you have any questions about using Ok, please refer to this guide. Readings: You might find the following references useful:. Grading: Homework is graded based on effort, not correctness. However, there is no partial credit; you must show substantial effort on every problem to receive any points. The Fibonacci sequence begins with 0 and then 1, after which each element is the sum of the preceding two.

Use a nonlocal statement! In lecture, we saw how to use functions to create mutable objects. The returned function should take two arguments: an amount to withdraw and a password. A password-protected withdraw function should only process withdrawals that include a password that matches the original. Upon receiving an incorrect password, the function should:. If a withdraw function has been called three times with incorrect passwords p1p2and p3then it is locked. All subsequent calls to the function should return:.

Suppose that our banking system requires the ability to make joint accounts. Both functions draw from the same balance. Incorrect passwords provided to either function will be stored and cause the functions to be locked after three wrong attempts.

Hint : The solution is short less than 10 lines and contains no string literals! The key is to call withdraw with the right password and amount, then interpret the result.

You may assume that all failed attempts to withdraw will return some string for incorrect passwords, locked accounts, or insufficient fundswhile successful withdrawals will return a number. The following diagram shows the order that the nodes would get printed, with the arrows representing function calls. Complete the Mint and Coin classes so that the coins created by a mint have the correct year and worth.This mobile example is based on a classic problem from Structure and Interpretation of Computer Programs, Section 2.

A mobile is a type of hanging sculpture.

hw05 cs61a

A binary mobile consists of two sides. Each side is a rod of a certain length, from which hangs either a weight or another mobile. Implement the balanced function, which returns whether m is a balanced mobile. A mobile is balanced if two conditions are met:. The balanced weights assumption is important, since we will be solving this recursively like many other tree problems even though this is not explicitly a tree.

Base case : if we are checking a weight, then we know that this is balanced. Why is this an appropriate base case? There are two possible approaches to this:. Because we know that our data structures so far are trees, weights are the simplest possible tree since we have chosen to implement them as leaves.

We also know that from an ADT standpoint, weights are the terminal item in a mobile. There can be no further mobile structures under this weight, so it makes sense to stop check here. Otherwise : note that it is important to do a recursive call to check if both sides are balanced. However, we also need to do the basic comparison of looking at the total weight of both sides as well as their length.

For example if both sides are a weight, trivially, they will both be balanced. However, the torque must be equal in order for the entire mobile to balanced i. The PDF solution, which I think is so intellectual!! The Fibonacci sequence begins with 0 and then 1, after which each element is the sum of the preceding two. Use a nonlocal statement! In lecture, we saw how to use functions to create mutable objects.

The returned function should take two arguments: an amount to withdraw and a password. A password-protected withdraw function should only process withdrawals that include a password that matches the original. Upon receiving an incorrect password, the function should:. The PDF solution, which have not a num variable. Suppose that our banking system requires the ability to make joint accounts.

Hint: The solution is short less than 10 lines and contains no string literals! The key is to call withdraw with the right password and amount, then interpret the result. You may assume that all failed attempts to withdraw will return some string for incorrect passwords, locked accounts, or insufficient fundswhile successful withdrawals will return a number. Each path should be represented as a list of the labels along that path in the tree.

You may yield the paths in any order. Then, since a generator is also an iterable, we can iterate over the paths in this generator and yield the result of concatenating it with our current label. This interval arithmetic example is based on a classic problem from Structure and Interpretation of Computer Programs, Section 2. Alyssa P. Hacker is designing a system to help people solve engineering problems.

One feature she wants to provide in her system is the ability to manipulate inexact quantities such as measured parameters of physical devices with known precision, so that when computations are done with such approximate quantities the results will be numbers of known precision. The result of adding, subracting, multiplying, or dividing two intervals is itself an interval, representing the range of the result.

She also presumes that, given the endpoints of an interval, she can construct the interval using the data constructor interval. She has implemented the constructor for you; fill in the implementation of the selectors.


thoughts on “Hw05 cs61a”

Leave a Reply

Your email address will not be published. Required fields are marked *