Compare commits

..

4 Commits

Author SHA1 Message Date
d1dead52e1 Updated Recursive-Grid-Tiling w/ README 2025-09-13 22:11:14 -04:00
21446d78aa Updated Palindrome-Partitioning w/ README 2025-09-13 21:41:59 -04:00
0474d82f94 Corrected Circular-Search README 2025-09-13 21:35:53 -04:00
603b360f6f Updated Foxsays with Lab 3 README 2025-09-13 21:32:41 -04:00
14 changed files with 290 additions and 3 deletions

View File

@@ -8,6 +8,10 @@ and ideas.
Remember, that you should design how the algorithm will work on paper before Remember, that you should design how the algorithm will work on paper before
coding. coding.
Some sample code showing show to read in a line of numbers and store them as a
list of ints can be found here:
[cs412_reading_input.py](https://canvas.jmu.edu/courses/2112008/files/178177051?wrap=1)
## Logarithmic Search in a Circularly Sorted List ## Logarithmic Search in a Circularly Sorted List
A list $A[0..(n-1)]$ is circularly sorted if there is an index $i$ such that the A list $A[0..(n-1)]$ is circularly sorted if there is an index $i$ such that the
@@ -16,9 +20,9 @@ list. For example $\{7, 8, 10, 1, 2, 3, 4\}$ is circularly sorted, since the
subarray $A[3..6]$ concatenated with the subarray $A[0..2]$ is the array subarray $A[3..6]$ concatenated with the subarray $A[0..2]$ is the array
$\{1, 2, 3, 4, 7, 8, $\{1, 2, 3, 4, 7, 8,
10\}$, which is sorted. Your task is to write a recursive algorithm, which given 10\}$, which is sorted. Your task is to write a recursive algorithm, which given
a a circularly sorted array with no duplicate values and a query integer $q$ a a circularly sorted array with **no duplicate** values and a query integer $q$
determines and returns the index of $q$ in the array if it exists, or returns determines and returns the index of $q$ in the array if it exists, or returns
$-1$ otherwise. Your algorithm should run in $O(log n)$ time. $-1$ otherwise. **Your algorithm should run in $O(log n)$ time**.
### Input ### Input
@@ -48,4 +52,5 @@ if it exists, or -1 if it does not exist in the list.
### Turning it in ### Turning it in
Save your solution `cs412_circular_sort_search.py` and turn it in to Gradescope. Save your solution **cs412_circular_sort_search.py** and turn it in to
Gradescope.

View File

@@ -99,3 +99,65 @@ Your code will be checked for speed and a leaderboard posted within Gradescope.
## Acknowledgments ## Acknowledgments
Problem from Kattis are used under their educational license. Problem from Kattis are used under their educational license.
# Lab 3: Empirical Analysis with Profiling
This lab is intended to be started in class, where additional background
information may be provided.
## Requirements
You will need to use a UNIX based system can run shell scripts. **STU** would be
a good resource.
## Goal
Utilize run-time profilers to identify poorly performing sections of a program
and then potential these areas.
## Process
### Part 1
Utilize the two programs from your "What does the Fox Say" homework. Run each of
these with the profiling information from the lecture slides
([cs412_03_EmpiricalAnalysis.pdf](https://canvas.jmu.edu/courses/2112008/files/178177075?wrap=1))
and gather/analyze this information. For the dict version of the program,
identify and try and to improve the portions of your program that take up the
most time.
### Part 2
Using the large examples files from "What does the Fox Say" homework and the
**split**, **wc**, **and head** UNIX commands, create input files of size 200K,
400K, 600K, 800K. To find out more about these UNIX commands, you can use Google
or the man pages from stu.
Write a shell script that performs this work and then runs the dict version of
your program capturing the execution time using the **time** UNIX command.
Create a plot of this run time where the Y axis is the run time and the X axis
is the input size. The shell script only needs to run your work and produce the
plots (it does not need to do the file manipulation, you can do that on your
own).
An example python script for creating the plot is here:
[line_plot.py](./line_plot.py)
## Deliverables
- A PDF (described below)
- Python script to create your plot
- Shell script to run your test cases
> Eclypse's Note: Did not need to submit a Shell script
Create a single PDF that contains the following information with two separate
sections (one for your list and one for your _dict_ versions of the HW 1). Each
section must showcase the results/output of 2 _cProfile_ runs. Then create a 3rd
section within the PDF that discusses the slowest portions of the _dict_
program. This discussion must include steps you tried in order to improve the
run time performance of your program.
For the 4th and final section, show the plot of your run times and discuss what
type of growth rate is experienced by your program and what portion of the
program you believe is responsible for this.

View File

@@ -0,0 +1,63 @@
# Lab 4: Coding Palindrome Partitioning with Backtracking
## Description
In this lab you will develop a recursive algorithm for the palindrome partition
counting problem.
A **palindrome** is a string of letters that is the same backwards as forwards.
For example, "racecar", "eevee", and "12321" are all palindromes. A string with
a single character is trivially a palindrome.
A **palindromic partition** of a string is a partition of the string into
substrings whose concatenation is equal to the original string, but such that
every substring is itself a palindrome.
For example, the string, "seeks" can be broken up into the palindrome partition
["s", "ee", "k", "s"] or as ["s", "e", "e", "k", "s"]. Your task is to design
and implement a recursive algorithm that counts the number of palindromic
partitions of a given string.
## Input
Your input will begin with a single line containing a nonnegative integer n
followed by exactly n lines each of which contains an input string.
## Output
You should output **exactly** n lines, one for each input string with each
ending in a newline character. The value of each line should be the number of
unique palindromic partitions that can be made with the input string.
<table>
<tr>
<td>Sample Input</td>
<td>Sample Output</td>
</tr>
<tr>
<td><pre>3<br>abc<br>bcccb<br>seeks</pre></td>
<td><pre>1<br>5<br>2</pre></td>
</tr>
</table>
## Turning it in
**Submit your Python code as cs412_palindrome_count_bt.py to Gradescope.**
## Hints
- As always, when devising a recursive backtracking algorithm, think of your
output as a series of choices. Almost always you can get to the recursive
solution by having each recursive call brute-force make all possible next
choice and use the recursion fairy to handle the remaining choices.
- What are the choices here? Think about what you need to do to the string to
turn it into a palindromic partition--this will tell you exactly what a
"choice" is in terms of producing palindromic partitions.
- Don't worry about speed here. You are almost certainly going to have a rather
bad runtime. We'll fix this in a few weeks when we talk about dynamic
programming. You probably want to write a tiny helper predicate
isPalindrome(s) that returns true if s is a palindrome and false otherwise.
- How can you easily have a string evaluate to the reverse of itself? Recall
that slicing takes 3 values (start pos, end pos, step size). See this website
for details:
https://www.digitalocean.com/community/tutorials/how-to-index-and-slice-strings-in-python-3

View File

@@ -0,0 +1,157 @@
# Coding 2: Recursive Grid Tiling
## Grid Tiling
Here's an interesting theorem: consider a square grid of squares with sides
given by some power of (i.e. a grid of size $2^n \times 2^n$) like this example
of a $2^3 \times 2^3$ grid:
![t0](./images/t0.png)
If you select exactly one grid square to remove (which is colored black below),
then the remaining squares can be tiled by 3-square L-shape tiles, like this.
![tiling](./images/tiling.png)
The proof of this fact is inductive and leads to a simple recursive algorithm
for producing a tiling. Assume that you can tile any $2^{n-1} \times 2^{n-1}$
grid with a single square removed with L-shaped tiles. Suppose we are given a
$2^n \times 2^n$ grid with one square removed.
Here's an example of what we've got:
![t1](./images/t1.png)
How can we use our inductive hypothesis to show that the entire grid can be
tiled? Well, let's start by splitting our grid vertically and horizontally in
half:
![t2](./images/t2.png)
Notice that this splits our $2^n \times 2^n$ sized grid into four
$2^{n-1} \times 2^{n-1}$ sized sub-grids, and one of them (the top left in our
example above) already has a square removed _but notice that three of them do
not yet have a square removed_. This means we can use our inductive hypothesis
to tile whichever sub-grid contains the square we already wanted removed:
![t3](./images/t3.png)
But now we're a bit stuck, because the conditions of the inductive hypothesis
apply if we have a $2^{n-1} \times 2^{n-1}$ grid with one square removed, but
instead we have three $2^{n-1} \times 2^{n-1}$ grids with no squares removed.
But now notice the following trick. We can place a single L-tile that covers
exactly one grid square in each of three sub-grids that we haven't yet tiled!
![t4](./images/t4.png)
But notice that since this new L-tile covers exactly one square from each of the
three untiled sub-grids, we now have three $2^{n-1} \times 2^{n-1}$ each with a
single square removed and so the inductive hypothesis now applies! We can tile
each quadrant individually using the inductive hypothesis.
![t5](./images/t5.png)
Then
![t6](./images/t6.png)
Then
![t7](./images/t7.png)
And finally, with the red dividing lines removed:
![t8](./images/t8.png)
The base case for this induction are the 2x2 grids, which become L-tiles if we
remove anyone grid square.
## Your Task
You are going to write a program to compute L-tilings of $2^n \times 2^n$ square
grids. You will assign each tile a number starting with 00. The first tile you
generate should have all of its grid cells marked with a 00, the next tile 01,
etc.
### Input
The first line of input will give the grid size parameter n as an integer. The
second line will contain the index (i, j) of the removed tile.
### Output
Your code should output $2^n$ lines, each containing $2^n$ 2-digit numbers
separated by spaces (and formatted so that single digit numbers have a leading
0). The value of each grid location should be the tile index you generated. (You
may assume you won't need 3 digits).
<table>
<tr>
<td>Sample Input</td>
<td>Sample Output</td>
</tr>
<tr>
<td><pre>3<br>0 0</pre></td>
<td>
<pre>
-1 02 03 03 07 07 08 08
02 02 01 03 07 06 06 08
04 01 01 05 09 09 06 10
04 04 05 05 00 09 10 10
12 12 13 00 00 17 18 18
12 11 13 13 17 17 16 18
14 11 11 15 19 16 16 20
14 14 15 15 19 19 20 20</pre>
</td>
</tr>
<tr>
<td><pre>3<br>1 2</pre></td>
<td>
<pre>
02 02 03 03 07 07 08 08
02 01 -1 03 07 06 06 08
04 01 01 05 09 09 06 10
04 04 05 05 00 09 10 10
12 12 13 00 00 17 18 18
12 11 13 13 17 17 16 18
14 11 11 15 19 16 16 20
14 14 15 15 19 19 20 20</pre>
</td>
</tr>
<tr>
<td><pre>4<br>10 10</pre></td>
<td>
<pre>
03 03 04 04 08 08 09 09 24 24 25 25 29 29 30 30
03 02 02 04 08 07 07 09 24 23 23 25 29 28 28 30
05 02 06 06 10 10 07 11 26 23 27 27 31 31 28 32
05 05 06 01 01 10 11 11 26 26 27 22 22 31 32 32
13 13 14 01 18 18 19 19 34 34 35 35 22 39 40 40
13 12 14 14 18 17 17 19 34 33 33 35 39 39 38 40
15 12 12 16 20 17 21 21 36 36 33 37 41 38 38 42
15 15 16 16 20 20 21 00 00 36 37 37 41 41 42 42
45 45 46 46 50 50 51 00 66 66 67 67 71 71 72 72
45 44 44 46 50 49 51 51 66 65 65 67 71 70 70 72
47 44 48 48 52 49 49 53 68 65 -1 69 73 73 70 74
47 47 48 43 52 52 53 53 68 68 69 69 64 73 74 74
55 55 56 43 43 60 61 61 76 76 77 64 64 81 82 82
55 54 56 56 60 60 59 61 76 75 77 77 81 81 80 82
57 54 54 58 62 59 59 63 78 75 75 79 83 80 80 84
57 57 58 58 62 62 63 63 78 78 79 79 83 83 84 84</pre>
</td>
</tr>
</table>
## Visualizing Your Results:
You can use the [plot_tiles.py](./plot_tiles.py) program to visualize your
input. It will draw figures similar to the ones shown in this assignment and may
help you debug your program if it has issues.
## Program Grading and Submission
This assignment is to be submitted into Gradescope. Grading criterion is:
- 10 points for the example problems shown in the assignment
- 8 points on hidden example problems
- 2 points for style

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB