Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nmheim committed Jun 7, 2024
1 parent 7f7634d commit 3e821d5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
30 changes: 15 additions & 15 deletions exams/pretty-binary-numbers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ outline: deep

# Pretty Printing of Binary Numbers

The goal of this assignment is to implement a Haskell program that reads a positive integer from the
The goal of this assignment is to implement a Haskell program that reads a positive integer from
standard input, converts it into its binary representation, and displays this representation as a
text image where each digit (i.e., 0 and 1) is represented as a 4x4 table of characters. More
precisely, the text images of digits 0,1 are captured in Haskell as follows:
text image. Each digit (i.e., $0$ and $1$) is represented as a $4 \times 4$ grid of characters. More
precisely, the text images of digits $0$ and $1$ are rendered in Haskell like below:

```haskell
type Img = [String]
Expand All @@ -32,27 +32,27 @@ precisely, the text images of digits 0,1 are captured in Haskell as follows:

Implement a function `main :: IO ()` that works as follows:

1. It displays a message `Enter integer:`,
2. then let the user enter an integer $n$ (you may assume only valid inputs),
3. converts $n$ into its binary representation,
4. displays the binary representation using the above text-images. The particular digits must be
1. Display a message `Enter integer:`.
2. Let the user enter an integer $n$ (you may assume only valid inputs).
3. Convert $n$ into its binary representation.
4. Display the binary representation using the above text-images. The particular digits must be
separated by a column consisting of the character `'.'`.

Below you can see an example if you execute the main function and the user enters
the number 12.
```Haskell
> main
Enter integer:\n
Enter integer:
12
...#....#..##...##.\n
..##...##.#..#.#..#\n
...#....#.#..#.#..#\n
...#....#..##...##.\n
...#....#..##...##.
..##...##.#..#.#..#
...#....#.#..#.#..#
...#....#..##...##.
```

All the displayed lines must end with the new-line character `'\n'` as is
depicted above. So you can display them e.g. by `putStrLn`. No trailing
whitespaces are allowed at the ends of lines.

All the displayed lines must end with the new-line character `'\n'` so you can display them e.g. by
`putStrLn`. No trailing whitespaces are allowed at the ends of lines.

::: details Solution
```haskell
Expand Down
51 changes: 34 additions & 17 deletions exams/sierpinski-carpet/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,50 @@ outline: deep

The *Sierpiński carpet* is a plane fractal first described by Wacław Sierpiński in 1916.
Your task is to generate this fractal in a text format represented as a list of strings.
Each string represent a single row in the picture. The picture $f(n)$ is defined recursively.
For $n=0$, we define $f(0) = \texttt{"\#"}$. For $n>0$, we define $f(n)$ as the picture depicted below.
In other words, $f(n)$ consists of eigth copies of $f(n-1)$ and
the middle box of the same size as $f(n-1)$ filled with spaces.
Each string represents a single row in the picture. The picture $f(n)$ is defined recursively.
For $n=0$, we define $f(0) = \texttt{"\#"}$. For $n>0$, we define $f(n)$ as shown below.

<img src="/img/sierpinski-carpet-construction.svg" style="width: 50%; margin: auto;">


The first iterations $f(1)$ and $f(2)$ look as follows:
In other words, $f(n)$ consists of eight copies of $f(n-1)$ and
the middle box of the same size as $f(n-1)$ filled with spaces.
The first iterations $f(0)$, $f(1)$, $f(2)$, and $f(3)$ look as follows:
```
### #########
# # # ## ## #
### #########
### ###
# # # #
### ###
#########
# ## ## #
#########
# ### ######### ###########################
# # # ## ## # # ## ## ## ## ## ## ## ## #
### ######### ###########################
### ### ### ###### ###### ###
# # # # # # # ## # # ## # # #
### ### ### ###### ###### ###
######### ###########################
# ## ## # # ## ## ## ## ## ## ## ## #
######### ###########################
######### #########
# ## ## # # ## ## #
######### #########
### ### ### ###
# # # # # # # #
### ### ### ###
######### #########
# ## ## # # ## ## #
######### #########
###########################
# ## ## ## ## ## ## ## ## #
###########################
### ###### ###### ###
# # # ## # # ## # # #
### ###### ###### ###
###########################
# ## ## ## ## ## ## ## ## #
###########################
```

The size of the picture grows exponentially with $n$, namely it is $3^n$.
The size of the picture grows exponentially with $3^n$.

## IO Program

In Haskell, implement a function of type `Int -> [String]` that for a given
natural number $n$ returns the Sierpi\'nski carpet $f(n)$ represented as a list of strings.
natural number $n$ returns the Sierpiński carpet $f(n)$ represented as a list of strings.
Further, create an IO program allowing the user to enter the number $n$. As the size of
the carpet quickly grows, it also asks the user to enter a window to be displayed.
More precisely, the user enters four numbers `row1`, `row2`, `col1`,
Expand Down

0 comments on commit 3e821d5

Please sign in to comment.