/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Problem 14 Generalizing one- and two-dimens... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

Generalizing one- and two-dimensional tables, show how to implement a table in which values are stored under an arbitrary number of keys and different values may be stored under different numbers of keys. The lookup and insert! procedures should take as input a list of keys used to access the table.

Short Answer

Expert verified
Use nested dictionaries to implement multi-key tables.

Step by step solution

01

Define the Table Structure

We need a flexible data structure that allows storing values with an arbitrary number of keys. We can use a nested dictionary or a hash-map, where each level corresponds to a different key. Each key at a given level leads to another dictionary until the value is reached.
02

Implement the Insert! Procedure

To implement the `insert!` procedure, traverse through the nested dictionary using each key in the provided list. If a key does not exist at a certain level, instantiate an empty dictionary for it. Continue this until the final key from the list, and store the value at that level of the dictionary.
03

Implement the Lookup Procedure

The `lookup` procedure will similarly traverse the nested dictionary using the list of keys provided. Start from the top-level dictionary and use each key to move deeper into the nested dictionaries. If all keys exist till the last key, return the value found; otherwise, return an indication that the value does not exist (e.g., `null`).

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with 91Ó°ÊÓ!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Nested Dictionaries
In programming, a nested dictionary is a dictionary where some values are themselves dictionaries. This allows for the representation of data in a hierarchical manner. When you implement a table structure that requires multiple levels of keys to access a value, a nested dictionary is an effective choice.

This data structure resembles a tree, where each key at one level branches out to another dictionary on a lower level, eventually leading to a final value. This structure is particularly useful when the number of keys is dynamic, as in this exercise. Each level of a nested dictionary can represent a different category or dimension of your data. This makes them ideal for complex hierarchical data storage.
  • Advantages: They provide a clear structure for organizing data with multiple dimensions.
  • Flexibility: Easily expandable; you can add new keys or dictionaries as your data grows.
When working with nested dictionaries, it's important to handle exceptions. If you try to access a non-existent key, you'll encounter an error. Therefore, either check if the key exists or use methods like `get()` to return a default value instead of an error.
Keys
In dictionaries, keys are the identifiers used to access specific values. They are like labels that help you retrieve data quickly and efficiently. In a nested dictionary setup, keys become even more crucial, as they define the path to the stored values.

It is vital to remember that keys in a dictionary must be immutable, meaning their values cannot be changed. Common immutable types used as keys are strings and tuples. A sequence of keys in a nested dictionary forms a path, drafting the quick route to the data point.
  • Usage: Properly structured keys enhance the readability and maintainability of your code.
  • Path: In nested dictionaries, a correct path of keys is essential for successful data retrieval or insertion.
Using proper notation and structure when defining keys ensures efficient access and manipulation of the data within the nested structures. Think of keys as addresses in a vast dynamic data landscape, enabling targeted access.
Dynamic Tables
Dynamic tables allow us to store values associated with a varying number of keys. Unlike static tables, which have a fixed number of dimensions, dynamic tables can adjust based on the data they need to store. This flexibility is crucial for handling complex datasets with changing patterns.

In essence, using nested dictionaries for dynamic tables provides adaptability. The table expands as new data with different numbers of keys is inserted. This is ideal for scenarios where the dataset doesn't conform to a fixed schema or is highly variable. The insert and lookup procedures use lists of keys, moving through each level of the nested dictionary, adapting as needed.
  • Flexibility: Ability to adapt to various lengths and types of key chains.
  • Scalability: Efficiently handles expansion as more elements are added with different keys.
Adopting dynamic tables in your programming toolkit can significantly simplify complex systems where data structures need to evolve. They promote a more natural data modeling, easily accommodating changing dimensions.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

A famous problem, first raised by R. Hamming, is to enumerate, in ascending order with no repetitions, all positive integers with no prime factors other than 2,3 , or 5 . One obvious way to do this is to simply test each integer in turn to see whether it has any factors other than 2,3 , and 5 . But this is very inefficient, since, as the integers get larger, fewer and fewer of them fit the requirement. As an alternative, let us call the required stream of numbers \(S\) and notice the following facts about it. \- S begins with \(1 .\) \- The elements of (scale-stream S 2) are also elements of S. \- The same is true for (scale-stream \(\mathrm{S} 3\) ) and (scale-stream \(5 \mathrm{~S}\) ). \- These are all the elements of \(\mathrm{S}\). Now all we have to do is combine elements from these sources. For this we define a procedure merge that combines two ordered streams into one ordered result stream, eliminating repetitions: (define (merge s1 s2) (cond ((stream-null? s1) s2) ((stream-nul1? s2) s1) (else (let ( s1car (stream-car s1)) (s2car (stream-car s2))) (cond ((< s1car s2car) (cons-stream s1car (merge (stream-cdr s1) s2))) ((> s1car s2car) (cons-stream s2car (merge s1 (stream-cdr s2)))) (else (cons-stream s1car (merge (stream-cdr s1) Then the required stream may be constructed with merge, as follows: (define \(\mathrm{S}\) (cons-stream 1 (merge \(\langle ? ?\rangle\langle ? ?\rangle)\) ) Fill in the missing expressions in the places marked \(\langle ? ?\rangle\) above.

Define a procedure partial-sums that takes as argument a stream \(S\) and returns the stream whose elements are \(S_{0}, S_{0}+S_{1}, S_{0}+S_{1}+S_{2}, \ldots\) For example, (partial-sums integers) should be the stream \(1,3,6,10,15, \ldots\)

Another way to construct an or-gate is as a compound digital logic device, built from and-gates and inverters. Define a procedure or-gate that accomplishes this. What is the delay time of the or-gate in terms of and-gate- delay and inverter-delay?

Alyssa P. Hacker is designing a system to process signals coming from physical sensors. One important feature she wishes to produce is a signal that describes the zero crossings of the input signal. That is, the resulting signal should be \(+1\) whenever the input signal changes from negative to positive, \(-1\) whenever the input signal changes from positive to negative, and 0 otherwise. (Assume that the sign of a 0 input is positive.) For example, a typical input signal with its associated zero-crossing signal would be $$ \begin{array}{lllllllllllllll} \ldots & 1 & 2 & 1.5 & 1 & 0.5 & -0.1 & -2 & -3 & -2 & -0.5 & 0.2 & 3 & 4 & \ldots \\ \ldots & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & \ldots \end{array} $$ In Alyssa's system, the signal from the sensor is represented as a stream sensedata and the stream zero-crossings is the corresponding stream of zero crossings. Alyssa first writes a procedure sign-change-detector that takes two values as arguments and compares the signs of the values to produce an appropriate 0,1 , or \(-1\). She then constructs her zero-crossing stream as follows: (define (make-zero-crossings input-stream last-value) (cons-stream (sign-change-detector (stream-car input-stream) last-value) (make-zero-crossings (stream-cdr input-stream) (stream-car input-stream)))) (define zero-crossings (make-zero-crossings sense-data 0 )) Alyssa's boss, Eva Lu Ator, walks by and suggests that this program is approximately equivalent to the following one, which uses the generalized version of stream-map from exercise 3.50: (define zero-crossings (stream-map sign-change-detector sense-data (expression))) Complete the program by supplying the indicated \langleexpression \(\rangle .\)

When we defined the evaluation model in section 1.1.3, we said that the first step in evaluating an expression is to evaluate its subexpressions. But we never specified the order in which the subexpressions should be evaluated (e.g., left to right or right to left). When we introduce assignment, the order in which the arguments to a procedure are evaluated can make a difference to the result. Define a simple procedure \(f\) such that evaluating (+ (f 0) ( \(f\) ) ) will return 0 if the arguments to \(+\) are evaluated from left to right but will return 1 if the arguments are evaluated from right to left.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.