In this module we introduce a set of problems whose solution does not solely rely on the structure of our data but rather on a condition involving our data that we need to discover. We show how to design algorithms that use general recursion and how to informally argue for correctness and termination of our algorithm using a termination argument and a halting measure.
- Explain the difference between structural and general recursion.
- Identify algorithms that cannot be solved using structural recursion.
- Summarize the purpose of a termination argument.
- Summarize the purpose of a halting measure.
- Use rackunit to write tests.
- Use rackunit to debug a function.
- Explain the difference between structural and general recursion.
- Identify algorithms that cannot be solved using structural recursion.
- Summarize the purpose of a termination argument.
- Summarize the purpose of a halting measure.
- DUE DATE: April. 6th, 2016 @ 12:00pm (NOON)
Problem 1
Design a function called list->chunks
that will consume a non-empty list and a
positive integer n
and returns a list of lists of size n, each list of size n is a
sub-sequences of the input. You may assume that the length of the input list is divisible n
> (list->chunks (list "a" "b" "c" "d" "e" "f") 3)
(list (list "a" "b" "c")
(list "d" "e" "f"))
Problem 2
DNA is often modelled by strings of the characters
A, C, G and T. They are very long and so often
need to be compressed. A simple way to do this is
to replace all substrings of identical consecutive
characters with the character followed by the length
of the substring. These substrings must be as long
as possible. For example, the run-length encoding
of the string "AAGCCCCTTAAAAAAAAAA" is the string
"A2G1C4T2A10". This is the unique run-length encoding
– something like "A2G1C4T2A4A6" is not valid. Use
generative recursion to write a function called
dna-encode
that consumes a DNA string and produces
its run-length encoding.
Here are a couple of examples
> (dna-encode "AAGCCCTTAAAAAAAAAA")
"A2G1C3T2A10"
> (dna-encode "")
""
You may assume that the input string consists of capital letters only.
Also design the function dna-decode
that will
perform the opposite operation, i.e., given a run-length encoding return the full
string.
> (dna-decode "A2G1C3T2A10")
"AAGCCCTTAAAAAAAAAA"
> (dna-decode "")
""
Problem 3
Design a function that implements bubble sort. Your function should consume a list of numbers and produce the same list of numbers but in sorted order, from smallest to largest, using the algorithm for bubble sort.
- DUE DATE: April. 15th, 2016 @ 12:00pm (NOON)
Graphs and paths
You are given the following data definitions for a graph
;; A Node is a Symbol
;; INTERP: represents the name of a node in a graph
;; A Distance is a PosInt
;; INTERP: represents distance in miles
;; An Edge is (list Node Distance Node)
;; e.g. (list 'A 10 'B)
;; INTERP: represents an edge from 'A to 'B with the distance from 'A to 'B being
;; 10 miles
;; A Path is a List<Edge>
;; A Graph is a Set<Edge>
;; NOTE: you can use the definition of Set from your previous assignment.
You are asked to provide the following functions on Graphs
-
valid-path?
that consumes a graph and a path, and returns true if the path is valid for the graph, and false otherwise. A path is valid for a graph if it is possible to follow the each edge in order from the path on the graph, i.e., the graph contains these edge from the path and we can follow them in the order given by the path. -
valid-st-path?
that consumes a graphg
, a start nodes
, an end nodet
, and a pathp
, and returns true if starting at nodes
ing
and following, in order, the edges inp
, will lead us tot
. The function should return false otherwise. -
find-st-path
that consumes a graphg
, a start nodes
and an end nodet
and returns a pathp
that starts ats
and ends att
in the graphg
. The function should return false if there is no such path. -
find-shortest-distance-st-path
that consumes a graphg
, a start nodes
and an end nodet
and returns the pathp
that starts ats
ends att
in the graphg
and when we add the distances of each edge isp
that is the shortest distance froms
tot
ing
. The function should return false if there is no such path.
Your solutions should also work for graphs that contain cycles.