Added Graded Assignment 2 exercise 2 in Latex

This commit is contained in:
Claudio Maggioni (maggicl) 2019-03-24 17:29:32 +01:00
parent 809a1166a7
commit 13030cc6bc
3 changed files with 408 additions and 0 deletions

263
.gitignore vendored
View file

@ -1,3 +1,266 @@
## Core latex/pdflatex auxiliary files:
*.aux
*.lof
*.log
*.lot
*.fls
*.out
*.toc
*.fmt
*.fot
*.cb
*.cb2
.*.lb
## Intermediate documents:
*.dvi
*.xdv
*-converted-to.*
# these rules might exclude image files for figures etc.
# *.ps
# *.eps
# *.pdf
## Generated if empty string is given at "Please type another file name for output:"
.pdf
## Bibliography auxiliary files (bibtex/biblatex/biber):
*.bbl
*.bcf
*.blg
*-blx.aux
*-blx.bib
*.run.xml
## Build tool auxiliary files:
*.fdb_latexmk
*.synctex
*.synctex(busy)
*.synctex.gz
*.synctex.gz(busy)
*.pdfsync
## Build tool directories for auxiliary files
# latexrun
latex.out/
## Auxiliary and intermediate files from other packages:
# algorithms
*.alg
*.loa
# achemso
acs-*.bib
# amsthm
*.thm
# beamer
*.nav
*.pre
*.snm
*.vrb
# changes
*.soc
# comment
*.cut
# cprotect
*.cpt
# elsarticle (documentclass of Elsevier journals)
*.spl
# endnotes
*.ent
# fixme
*.lox
# feynmf/feynmp
*.mf
*.mp
*.t[1-9]
*.t[1-9][0-9]
*.tfm
#(r)(e)ledmac/(r)(e)ledpar
*.end
*.?end
*.[1-9]
*.[1-9][0-9]
*.[1-9][0-9][0-9]
*.[1-9]R
*.[1-9][0-9]R
*.[1-9][0-9][0-9]R
*.eledsec[1-9]
*.eledsec[1-9]R
*.eledsec[1-9][0-9]
*.eledsec[1-9][0-9]R
*.eledsec[1-9][0-9][0-9]
*.eledsec[1-9][0-9][0-9]R
# glossaries
*.acn
*.acr
*.glg
*.glo
*.gls
*.glsdefs
# gnuplottex
*-gnuplottex-*
# gregoriotex
*.gaux
*.gtex
# htlatex
*.4ct
*.4tc
*.idv
*.lg
*.trc
*.xref
# hyperref
*.brf
# knitr
*-concordance.tex
# TODO Comment the next line if you want to keep your tikz graphics files
*.tikz
*-tikzDictionary
# listings
*.lol
# luatexja-ruby
*.ltjruby
# makeidx
*.idx
*.ilg
*.ind
*.ist
# minitoc
*.maf
*.mlf
*.mlt
*.mtc[0-9]*
*.slf[0-9]*
*.slt[0-9]*
*.stc[0-9]*
# minted
_minted*
*.pyg
# morewrites
*.mw
# nomencl
*.nlg
*.nlo
*.nls
# pax
*.pax
# pdfpcnotes
*.pdfpc
# sagetex
*.sagetex.sage
*.sagetex.py
*.sagetex.scmd
# scrwfile
*.wrt
# sympy
*.sout
*.sympy
sympy-plots-for-*.tex/
# pdfcomment
*.upa
*.upb
# pythontex
*.pytxcode
pythontex-files-*/
# tcolorbox
*.listing
# thmtools
*.loe
# TikZ & PGF
*.dpth
*.md5
*.auxlock
# todonotes
*.tdo
# vhistory
*.hst
*.ver
# easy-todo
*.lod
# xcolor
*.xcp
# xmpincl
*.xmpi
# xindy
*.xdy
# xypic precompiled matrices
*.xyc
# endfloat
*.ttt
*.fff
# Latexian
TSWLatexianTemp*
## Editors:
# WinEdt
*.bak
*.sav
# Texpad
.texpadtmp
# LyX
*.lyx~
# Kile
*.backup
# KBibTeX
*~[0-9]*
# auto folder when using emacs and auctex
./auto/*
*.el
# expex forward references with \gathertags
*-tags.tex
# standalone packages
*.sta
# Created by https://www.gitignore.io/api/python # Created by https://www.gitignore.io/api/python
# Edit at https://www.gitignore.io/?templates=python # Edit at https://www.gitignore.io/?templates=python

BIN
GA2/ga2.pdf Normal file

Binary file not shown.

145
GA2/ga2.tex Normal file
View file

@ -0,0 +1,145 @@
% vim: set ts=2 sw=2 tw=80 et:
\documentclass[12pt]{article}
\usepackage[margin=3cm]{geometry}
\usepackage{xcolor}
\usepackage{lmodern}
\usepackage{listings}
\title{Graded Assignment 2 -- DSA}
\author{Claudio Maggioni}
% listings configuration
\lstset{
basicstyle=\small\ttfamily,
frame=shadowbox,
rulesepcolor=\color{black},
columns=fullflexible,
commentstyle=\color{gray},
keywordstyle=\bfseries,
keywords={,while,if,elif,else,FUNCTION,return,for,from,to,TRUE,FALSE},
mathescape=true,
aboveskip=2em,
captionpos=b,
abovecaptionskip=1em,
belowcaptionskip=1em
}
\begin{document}
\maketitle
\tableofcontents
\lstlistoflistings
\newpage
\section{Exercise 2}
\subsection{Exercise a}
The pseudocode for \textit{Sum of two} can be found in listing \ref{lst:sum2}.
The total cost of this algorithm in the worst case is the sum of the worst case
of mergesort ($O(n log(n))$) and the cost of the worst case in the partition
done afterwards (which is equivalent to not finding the sum, i.e. $2 n =
O(n)$). Therefore, the total cost is $\theta(n log(n))$.
\begin{lstlisting}[caption=Sum of two in pseudocode, label={lst:sum2}]
FUNCTION SUM-OF-TWO(A, s):
A $\gets$ mergesort(A)
i $\gets$ 1
j $\gets$ A.length
while i < j:
sum $\gets$ $A_i$ + $A_j$
if sum = s:
return TRUE
elif sum > s:
j $\gets$ j - 1
else:
i $\gets$ i + 1
return FALSE
\end{lstlisting}
\subsection{Exercise b}
The pseudocode for \textit{Sum of three} can be found in listing \ref{lst:sum3}.
\textsc{search-two} has a time cost of $O(n)$ in the worst case (if no elements
are found), and the loop of \textsc{search} has an added cost of $O(n)$. The
total cost in the worst case then, including mergesort, is $n^2 + n log(n)
= \theta(n^2)$.
\begin{lstlisting}[caption=Sum of three in pseudocode, label={lst:sum3}]
FUNCTION SEARCH-TWO(A, sum2, i_skip):
i $\gets$ 1
j $\gets$ A.length
while i < j:
if i = i_skip:
i $\gets$ i + 1
elif j = i_skip:
j $\gets$ j - 1
else:
sum $\gets$ $A_i$ + $A_j$
if sum = sum2:
return TRUE
elif sum > sum2:
j $\gets$ j - 1
else:
i $\gets$ i + 1
return FALSE
FUNCTION SUM-OF-THREE(A, s):
A $\gets$ mergesort(A)
l $\gets$ A.length
for i from 1 to l:
if SEARCH-TWO(A, s - $A_i$, i):
return TRUE
return FALSE
\end{lstlisting}
\subsection{Exercise c}
The \textit{Python} code used to implement \textit{Sum of three} can be found in
the listing \ref{lst:sum3py}.
\begin{lstlisting}[caption=Sum of three in Python, label={lst:sum3py},%
language=python]
#!/usr/bin/env python3
import sys
def search_two(A, sum2, i_skip):
i = 0
j = len(A) - 1
while i < j:
if i == i_skip:
i = i + 1
elif j == i_skip:
j = j - 1
else:
cs = A[i] + A[j]
if cs == sum2:
return True
elif cs > sum2:
j = j - 1
else:
i = i + 1
return False
def sum_of_three(A, sum3):
A.sort() # assume using mergesort for worst case of O(n*log(n))
l = len(A)
for i in range(l):
if search_two(A, sum3 - A[i], i):
return True
return False
if __name__ == "__main__":
args = [int(x) for x in sys.argv[1:]]
print(sum_of_three(args[1:], args[0]))
\end{lstlisting}
\end{document}