Rename year directories to allow natural ordering

This commit is contained in:
2023-12-20 03:57:27 +00:00
parent 0ab1f5ad3a
commit 1f7d812b98
1895 changed files with 0 additions and 7188 deletions

View File

@ -0,0 +1,22 @@
#lang racket
;; a cons pair of two numbers
(cons 1 2)
;; a list of 3 numbers using only the cons function
;; this could be more easily done using the single quote `'` (i.e., `'(1 2 3)`) but i don't use it as it seemed against the spirit of the question
(cons 1 (cons 2 (cons 3 empty)))
;; a list containing a string, a number, and a nested list of three numbers using only the cons function
(cons "a string"
(cons 0
(cons (cons 1 (cons 2 (cons 3 empty))) empty)
)
)
;; a list containing a string, a number, and a nested list of three numbers, using only the list function
(list "a string" 0 (list 1 2 3))
;; a list containing a string, a number, and a nested list of three numbers, using only the append function
;; using `'` as the arguments of the `append` function must be themselves lists
(append '("a string") '(0) '((1 2 3)))

View File

@ -0,0 +1,93 @@
#lang racket
(provide ins_beg)
(provide ins_end)
(provide count_top_level)
(provide count_instances)
(provide count_instances_tr)
(provide count_instances_deep)
;; function to insert an element at the beginning of a list
(define (ins_beg el lst)
;; assuming that the second element is always a list
(cons el lst)
)
;; function to insert an element at the end of a list
(define (ins_end el lst)
;; making el into a list if it isn't already
(append lst (cons el empty))
)
;; function to count the number of top-level items in a list
(define (count_top_level lst)
(if (null? lst)
0 ;; return 0 if we've reached the end of the list
(+ 1 (count_top_level (cdr lst))) ;; return 1 plus the count_top_level of the second element of the cons pair (the rest of the list)
)
)
;; non-tail recursive function to count the number of times a given item occurs in a list (assuming items are atomic)
(define (count_instances item lst)
(if (null? lst)
0 ;; return 0 if at the end of the list
(+
(if (equal? item (car lst))
1 ;; if the item is equal to the first element of the list, add 1
0 ;; if the item is not equal to the first element of the list, add 0
)
(count_instances item (cdr lst)) ;; recurse with the remainder of the list
)
)
)
;; helper function for count_instances_tr
(define (count_instances_tr_helper item lst cnt)
(cond
;; return the count if the end of the list is reached (0 for empty list)
((null? lst)
cnt
)
;; if the first element of the list is equal to the item
((eq? (car lst) item)
;; recurse with the remainder of the list and an incremented count
(count_instances_tr_helper item (cdr lst) (+ cnt 1))
)
;; if the first element of the list is not equal to the item
(else
;; recurse with the remainder of the list and an unchanged count
(count_instances_tr_helper item (cdr lst) cnt)
)
)
)
;; tail recursive function to count the number of times a given item occurs in a list (assuming items are atomic)
(define (count_instances_tr item lst)
;; calling helper function with the list and the count so far (0)
(count_instances_tr_helper item lst 0)
)
;; function to count the number of times an item occurs in a list and its sub-lists
(define (count_instances_deep item lst)
(cond
;; return nothing if we've reached the end of the list
((null? lst)
0
)
;; if the first item is a list, recurse through the first element and then the rest and return the sum of the two results
((pair? (car lst))
(+ (count_instances_deep item (car lst)) (count_instances_deep item (cdr lst)))
)
;; if the first element is equal to the item, add 1 to the count and recurse with the rest of the list
((eq? item (car lst)) ; If the first element is equal to the item, increment count
(+ 1 (count_instances_deep item (cdr lst)))
)
;; else if the first element is not equal to the item, recurse with the rest of the list
(else
(count_instances_deep item (cdr lst))
)
)
)

View File

@ -0,0 +1,145 @@
#lang racket
;; function to display the contents of a binary search tree in sorted order
(define (display_contents bst)
(cond
;; if the binary search tree is null, print an empty string (nothing)
[(null? bst) (display "")]
;; if the binary search tree has nodes
[else
;; display the contents of the left sub-tree of the current node
(display_contents (cadr bst))
;; display the current node
(display (car bst))
(newline)
;; display the contents of the right sub-tree of the current node
(display_contents (caddr bst))
]
)
)
;; function to search a tree and tell whether a given item is presesnt in a given tree
(define (search_tree item bst)
(cond
;; return false if we've reached the end of the tree without finding a match
((null? bst) #f)
;; return true if the current node is equal to the item
((equal? item (car bst)) #t)
;; else return whether the item was found in the left sub-tree or the right sub-tree
(else
(or
(search_tree item (cadr bst)) ;; search left sub-tree
(search_tree item (caddr bst)) ;; search right sub-tree
)
)
)
)
;; function to insert an item into a binary search tree
(define (insert_item item bst)
(cond
;; if there are no nodes in the tree, create a new tree with the item as the root
((null? bst)
(list item '() '())
)
;; if the item is less than the current node, insert it into the left-hand side of the tree
((< item (car bst))
;; create new bst with same root node, same right-hand side, but a left-hand side that has had the item inserted
(list (car bst) (insert_item item (cadr bst)) (caddr bst))
)
;; if the item is greater than the current node, insert it into the right-hand side of the tree
((> item (car bst))
;; create new bst with same root node, same left-hand side, but a right-hand side that has had the item inserted
(list (car bst) (cadr bst) (insert_item item (caddr bst)))
)
;; else the item is equal to the current node, so do nothing
(else bst)
)
)
;; function to insert a list of items into a binary search tree
(define (insert_list lst bst)
(if (null? lst)
;; if the list is null, just return the bst with no changes
bst
;; otherwise, recurse with the remainder of the list and the binary tree produced by inserting the first item of the list into bst
(insert_list (cdr lst) (insert_item (car lst) bst))
)
)
;; tree-sort function
(define (tree_sort lst)
;; inserting the list into a tree structure to sort it and then displaying the contents of that tree
(display_contents (insert_list lst '()))
)
;; function to insert an item into a binary search tree based off a sorting function
;; the sorting function should return accept two items and arguments, and return true if they were passed in order, and false otherwise or if they are equal
(define (insert_item_custom item bst sorter)
(cond
;; if there are no nodes in the tree, create a new tree with the item as the root
((null? bst)
(list item '() '())
)
;; if the item is goes before the current node, insert it into the left-hand side of the tree
((sorter item (car bst))
;; create new bst with same root node, same right-hand side, but a left-hand side that has had the item inserted
(list (car bst) (insert_item_custom item (cadr bst) sorter) (caddr bst))
)
;; if the item goes after the current node, insert it into the right-hand side of the tree
((sorter (car bst) item)
;; create new bst with same root node, same left-hand side, but a right-hand side that has had the item inserted
(list (car bst) (cadr bst) (insert_item_custom item (caddr bst) sorter))
)
;; else the item is equal to the current node, so do nothing
(else bst)
)
)
;; sorter function which states whether the two arguments were supplied in strictly ascending order (i.e., if item == item2, return false)
(define (sort_ascending item1 item2)
(if (< item1 item2)
#t
#f
)
)
;; sorter function which states whether the two arguments were supplied in strictly descending order (i.e., if item == item2, return false)
(define (sort_descending item1 item2)
(if (> item1 item2)
#t
#f
)
)
;; sorter function which states whether the two arguments were supplied in strictly ascending order based on the final digit (i.e., if item == item2, return false)
(define (sort_ascending_last item1 item2)
(if (< (modulo item1 10) (modulo item2 10))
#t
#f
)
)
;; function to insert a list of items into a binary search tree in the order determined by a sorting function
(define (insert_list_custom lst bst sorter)
(if (null? lst)
;; if the list is null, just return the bst with no changes
bst
;; otherwise, recurse with the remainder of the list and the binary tree produced by inserting the first item of the list into bst
(insert_list_custom (cdr lst) (insert_item_custom (car lst) bst sorter) sorter)
)
)

View File

@ -0,0 +1,142 @@
%! TeX program = lualatex
\documentclass[a4paper]{article}
% packages
\usepackage{microtype} % Slightly tweak font spacing for aesthetics
\usepackage[english]{babel} % Language hyphenation and typographical rules
\usepackage[final, colorlinks = false, urlcolor = cyan]{hyperref}
\usepackage{changepage} % adjust margins on the fly
\usepackage{fontspec}
\usepackage{minted}
\usemintedstyle{algol_nu}
\usepackage{xcolor}
\usepackage{pgfplots}
\pgfplotsset{width=\textwidth,compat=1.9}
\usepackage{caption}
\newenvironment{code}{\captionsetup{type=listing}}{}
\captionsetup[listing]{skip=0pt}
\setlength{\abovecaptionskip}{5pt}
\setlength{\belowcaptionskip}{5pt}
\usepackage[yyyymmdd]{datetime}
\renewcommand{\dateseparator}{--}
\setmainfont{EB Garamond}
\setmonofont[Scale=MatchLowercase]{Deja Vu Sans Mono}
\usepackage{titlesec}
% \titleformat{\section}{\LARGE\bfseries}{}{}{}[\titlerule]
% \titleformat{\subsection}{\Large\bfseries}{}{0em}{}
% \titlespacing{\subsection}{0em}{-0.7em}{0em}
%
% \titleformat{\subsubsection}{\large\bfseries}{}{0em}{$\bullet$ }
% \titlespacing{\subsubsection}{1em}{-0.7em}{0em}
% margins
\addtolength{\hoffset}{-2.25cm}
\addtolength{\textwidth}{4.5cm}
\addtolength{\voffset}{-3.25cm}
\addtolength{\textheight}{5cm}
\setlength{\parskip}{0pt}
\setlength{\parindent}{0in}
% \setcounter{secnumdepth}{0}
\begin{document}
\hrule \medskip
\begin{minipage}{0.295\textwidth}
\raggedright
\footnotesize
Name: Andrew Hayes \\
E-mail: \href{mailto://a.hayes18@universityofgalway.ie}{\texttt{a.hayes18@universityofgalway.ie}} \hfill\\
ID: 21321503 \hfill
\end{minipage}
\begin{minipage}{0.4\textwidth}
\centering
\vspace{0.4em}
\Large
\textbf{CT331} \\
\end{minipage}
\begin{minipage}{0.295\textwidth}
\raggedleft
\today
\end{minipage}
\medskip\hrule
\begin{center}
\normalsize
Assignment 2: Functional Programming with Scheme
\end{center}
\hrule
\section{Question 1}
\subsection{Part (A): Code}
\begin{code}
\inputminted[texcl, mathescape, breaklines, frame=single]{racket}{../code/assignment_q1.rkt}
\caption{\texttt{assignment\_q1.rkt}}
\end{code}
\subsection{Part (B): Comments}
\begin{figure}[H]
\centering
\includegraphics[width=\textwidth]{./images/question1.png}
\caption{Output of \texttt{assignment\_q1.rkt}}
\end{figure}
Comments on each line of output:
\begin{enumerate}
\item The \mintinline{racket}{cons} function creates a \mintinline{racket}{cons} pair, which is not always a ``list''.
A list is a \mintinline{racket}{cons} pair in which the second element is another itself another list or is \mintinline{racket}{empty}.
When a \mintinline{racket}{cons} pair that is not a list is printed, its elements are delimited by a ``\verb|.|'', as can be seen from the first line of
output.
\item The second section of code produces a list of three numbers using only the \mintinline{racket}{cons} function:
first we create a one-element list with \mintinline{racket}{(cons 3 empty)}, then we create a two-element list by making a \mintinline{racket}{cons} pair
of \mintinline{racket}{2} and the already-created one-element list, and finally we create a three-element list by making a \mintinline{racket}{cons} pair
of \mintinline{racket}{1} and the two-element list.
This could of course be achieved far more simply by just using \mintinline{racket}{(cons 1 '(2 3))} or even justs \mintinline{racket}{'(1 2 3)} but I
felt that this would be against the spirit of the exercise.
\item To create a nested list using only \mintinline{racket}{cons} in the third section of code, we make the \mintinline{racket}{'(1 2 3)} as previously,
\mintinline{racket}{cons} it with \mintinline{racket}{empty} to make a nested list, and then \mintinline{racket}{cons} it with \mintinline{racket}{0}, and
\mintinline{racket}{cons} that with a string literal.
\item Like \mintinline{racket}{cons}, \mintinline{racket}{list} can take either atomics or lists as arguments.
To create the list using only the \mintinline{racket}{list} function, we can simply make a list of \mintinline{racket}{(list 1 2 3)}, and then create a
list consisting of \mintinline{racket}{"a string"}, \mintinline{racket}{0}, \& the aforementioned list.
This is much simpler than using \mintinline{racket}{cons} because \mintinline{racket}{list} can take as many arguments as we want, while
\mintinline{racket}{cons} can only take two arguments.
\item Although I opted not to make use of the ``\mintinline{racket}{'}'' operator to create lists for the previous exercises, I make use of it here as there is
no other way to create a list using only \mintinline{racket}{append} and nothing else, as \mintinline{racket}{append} only accepts lists as arguments.
We make a list consisting of only one element (\mintinline{racket}{"a string"}), another list consisting of only one element (\mintinline{racket}{0}),
and finally a list consisting of three elements \mintinline{racket}{'(1 2 3)} and append them into one to create the desired list.
\end{enumerate}
\section{Question 2}
\begin{code}
\inputminted[breaklines, frame=single]{racket}{../code/assignment_q2.rkt}
\caption{\texttt{assignment\_q2.rkt}}
\end{code}
\section{Question 3}
\begin{code}
\inputminted[breaklines, frame=single]{racket}{../code/assignment_q3.rkt}
\caption{\texttt{assignment\_q3.rkt}}
\end{code}
It is worth noting here that the function \mintinline{racket}{sort_ascending_last} operates in a manner that may be undesirable.
The function determines whether the two numbers passed to it as arguments were passed in strictly ascending order based on the final
digit, i.e. it returns \mintinline{racket}{#t} if the final digit of the first argument is less than the final digit of the second
argument, and \mintinline{racket}{#f} otherwise.
Because this function considers only the final digit of the numbers, it considers two numbers to be equal if they share a final digit,
e.g. it considers the numbers \mintinline{racket}{99} \& \mintinline{racket}{9} to be the same.
Therefore, if one attempts to insert those two values into the binary search tree using this function as the ``sorter'', only the
former value will get inserted, as binary search trees do not allow duplicate values, and the \mintinline{racket}{sort_ascending_last} function
considers those two values to be equal.
However, I thought it would be incorrect for the function to consider the $n-1$\textsuperscript{th} digits in the case of the $n$\textsuperscript{th} digits being identical, as
a) the assignment did not ask for that and b) that would really just be no different to sorting the numbers in ascending order.
\end{document}

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB