Create a subfolder called lab7
within your
cs111
folder, and put all of the files for this lab in that
folder.
Let’s build a simple circuit that has the following truth table:
x | y | output |
---|---|---|
0 | 0 | 1 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Download the following starter file, storing it in your lab7
folder:
task1.logicly
Open the file in Logicly. (If you haven’t already installed Logicly, you can do so by following our instructions.)
You should see a partial circuit for the above truth table that includes:
x
and y
NOT
gate for the negation of each inputAND
gates needed for the minterm expansion
of the truth tableOR
gate needed for the minterm expansion.In the incomplete circuit – with both input switches off – the light bulb is on, which means that the circuit is outputting a 1. Why does that make sense?
There is still one row of the truth table that needs to be represented in the circuit. Which one?
How can we include that row in the circuit? Go ahead and do so.
Test the completed circuit to make sure that it works. Try different combinations of inputs (turning the switches on or off), and make sure that you are seeing the correct output for each combination of inputs.
Important: There appears to be a bug in Logicly that causes it to occasionally display red wires even after the circuit is fully built. If this happens, save your file, close down Logicly, and then reopen it. This will typically fix the problem.
Now let’s export the completed circuit from Task 1 and use it as a building block in another circuit.
Exporting the circuit:
In your task1.logicly
file, use the Edit->Select All
menu option to select the entire circuit.
Click the building block icon (which looks like a Lego piece) at the top of the screen. (Alternatively, you can select the Edit->Create Integrated Circuit menu option.)
In the window that pops up, give the circuit a name (e.g., My
Circuit
) by entering it in the Full Name text box. You can also
add a Symbol Label, but doing so is not required.
Click the Create IC button to create the integrated circuit.
Use the File->Export Integrated Circuit Library menu option
to export the circuit. Give the file an appropriate name (e.g.,
my_circuit
) and save it in a place where you can easily find it.
Using the exported circuit in another circuit:
Download the following file to your lab7
folder:
task2.logicly
and open it in Logicly.
Use the File->Import Integrated Circuit Library menu option to import the file in which you stored the exported circuit in step 5.
After importing the file, scroll down to the bottom of the
left-hand pane of components. You should see a section labeled
Custom
that contains a lego-like building block for the
circuit that you exported above.
You can insert a copy of your exported building block
by using the Selection Tool to drag the
building block into the circuit. You should see a box that
looks like this:
Add wires to connect:
the input switch for x
to the pin x
in the
building block
the input switch for y
to the pin y
in the
building block
the output of the building block to the output light bulb of the circuit.
Test different combinations of values for the inputs, and you should see that your building block behaves exactly the way that the original circuit did!
In Part II of Problem Set 5, you will write a number of functions that use a loop to process a sequence. This task of the lab helps to prepare you for those problems.
In Spyder, open a new editor window and save it to your lab7
folder
using the name lab7task3.py
.
Earlier in the course, we covered a recursive function called
num_vowels
that took an arbitrary string of lower-case letters
as input and returned the number of vowels in the string. For
example:
>>> num_vowels('hello') result: 2 >>> num_vowels('aloha') result: 3
Write a version of this function that uses a loop instead of recursion. Here’s a template:
def num_vowels(s): """ returns the number of vowels in a string s consisting of lower-case letters """ count = 0 for c in s: # complete the rest of the function!
Note that this function should perform a cumulative computation that gradually builds up the final count of the number of vowels.
We also covered a recursive function called rem_all
that took an
arbitrary list of values vals
and a single element elem
and
that used recursion to create a new list in which all occurrences of
elem
were removed. For example:
>>> rem_all(10, [3, 5, 10, 7, 10]) result: [3, 5, 7] >>> rem_all(4, [2, 4, 8, 4, 4]) result: [2, 8]
Write a version of this function that uses a loop instead of recursion. Here’s a template:
def rem_all(elem, values): """ returns a list in which all occurrences of elem in values have been removed """ result = [] for ___ in ____: # complete the rest of the function!
Note that this function also needs to perform a cumulative
computation! We have called the accumulator variable result
,
and we’ve initialized it to be an empty list. Inside the loop,
you should use list concatenation to gradually build up a
list containing all of the values that belong in
the final result.
Important: Although an element-based loop would work here, you should use an index-based loop to get practice with that type of loop. If you need help starting that type of loop, feel free to ask a staff member.
In lecture, we learned how to use the input()
function to get
a value from the user. Write a function get_pos_int(prompt)
that takes a string prompt
and performs the following tasks:
gets an initial integer value from the user using the input
function and the specified prompt
if the user does not enter a positive integer, keeps asking for additional inputs until the user enters a positive value; these additional inputs (if any) should use the following prompt:
The input must be positive. Try again:
once the user enters a positive value, returns the value.
You may assume that the user always enters an integer.
For example:
>>> get_pos_int('Enter your age: ') Enter your age: -2 The input must be positive. Try again: -5 The input must be positive. Try again: 0 The input must be positive. Try again: 20 result: 20
Hints:
This problem lends itself to an indefinite loop (i.e., a
while
loop). Why?
Don’t forget that input
always returns a string. As a result,
you will need to use the int
function to convert the input
to an integer as discussed in lecture.
Complete any of the problems listed under the Warmup-2 collection of problems on CodingBat. Rather than solving them using recursion, use a loop instead!
Last updated on October 21, 2024.