\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{cp2300} \pdfinfo{ /Title (comp204-final.pdf) /Creator (Cheatography) /Author (cp2300) /Subject (COMP204 Final Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{B5101B} \definecolor{LightBackground}{HTML}{FAF0F0} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{COMP204 Final Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{cp2300} via \textcolor{DarkBackground}{\uline{cheatography.com/103341/cs/21267/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}cp2300 \\ \uline{cheatography.com/cp2300} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 5th December, 2019.\\ Updated 5th December, 2019.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Binary Numbers}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/cp2300_1575513556_Screen Shot 2019-12-04 at 18.05.30.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.74195 cm} x{3.23505 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Binary Numbers}} \tn % Row 0 \SetRowColor{LightBackground} 8 bits & 1 byte \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} 16 bits & word \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} 32 bits & double word \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} always at least 1 byte & add one to first number in sequence to make negative, add zeros to get full byte \tn % Row Count 7 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Machine Learning}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{-program adjusts itself automatically to fit data, end result is a program trained to achieve a given task} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Supervised learning-given examples of input and desired outputs, predict outputs on future unseen inputs(classification, regression, time series)} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Unsupervised learning-creates a new representation of the input} \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Reinforcement learning-learning action to maximize payoff} \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Types of supervised learning tasks} \tn % Row Count 11 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{1. classification- predict which predefined set of classes and example belongs to} \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{2. regression - predict a real value} \tn % Row Count 14 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{2. probability estimation - estimate probability of an event} \tn % Row Count 16 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Sensitivity = fraction of positive examples predicted to be positive} \tn % Row Count 18 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{TP/(TP+FN)} \tn % Row Count 19 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Specificity= proportion of negative examples predicted negative} \tn % Row Count 21 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{TN/(FP+TN)} \tn % Row Count 22 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{False-positive rate(FPR)= negatives predicted to be positive} \tn % Row Count 24 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{FP/(FP+TN)} \tn % Row Count 25 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Sets}} \tn % Row 0 \SetRowColor{LightBackground} Set & stores an unordered set of objects, CANT BE INDEXED, no duplicates, only immutable objects contained \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} myset = set({[}{]}) & creates a set from a list \tn % Row Count 7 (+ 2) % Row 2 \SetRowColor{LightBackground} len(myset) & length of the set \tn % Row Count 8 (+ 1) % Row 3 \SetRowColor{white} if x in myset: & evaluates boolean condition \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} for element in myset: & iterate through set \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} for set1 = A and set2 = B, A\&B = intersection & elements in common to A and B \tn % Row Count 15 (+ 3) % Row 6 \SetRowColor{LightBackground} A | B or A.union(B) & union of two sets \tn % Row Count 16 (+ 1) % Row 7 \SetRowColor{white} A-B & difference of sets, elements in A that are not in B \tn % Row Count 19 (+ 3) % Row 8 \SetRowColor{LightBackground} & all these rules can be applied to multiple sets \tn % Row Count 22 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Integer Sequences}} \tn % Row 0 \SetRowColor{LightBackground} range(start, end, step) & (start default is zero, step default 1) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} range(5) & 0 1 2 3 4 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} range(3,8) & 3 4 5 6 7 \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \seqsplit{range(len(seq))} & sequence of index of values in seq \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} range(2,12,3) & 2 5 8 11 \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} def functionname( arguments): & defines a function of given name with given arguments \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} return & only returns a certain value or string generated by the function, doesn't print, exits at this value \tn % Row Count 8 (+ 5) % Row 2 \SetRowColor{LightBackground} list = {[}{[}0 for i in range(ncols){]} for j in range(nrowns){]} & creates a two dimensional list of nrows and ncols filled with zeros \tn % Row Count 12 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Operations on Lists}} \tn % Row 0 \SetRowColor{LightBackground} lst = {[}{]} & creates empty list \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \seqsplit{lst.append(val)} & adds item to list at end \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{lst.extend(seq)} & add sequence of items at end \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} lst.insert(idx, val) & inserts item at index \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{lst.remove(val)} & remove first item in the list with value val \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} lst.pop(idx) & remove and return item at index \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{lst.sort()/lst.reverse()} & sort/reverse list in place \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} \seqsplit{lst.min()/.max()} & finds the min/max \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.54287 cm} x{3.43413 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{matplotlib.pyplot}} \tn % Row 0 \SetRowColor{LightBackground} .plot(x,y, color) & plots the x values to x values in a certain color \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} .show() & shows the graph \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{.ylabel(name)} & names y axis \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{.xlabel(name)} & names x axis \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} .savefig( figname) & saves image under figname \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.33919 cm} x{2.63781 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{OOP}} \tn % Row 0 \SetRowColor{LightBackground} class & defines \seqsplit{attributes(information} we want to keep together) and methods( operations want to be able to perform on that data) \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} class \seqsplit{some\_class\_name:} & defines a class of some\_class\_name \tn % Row Count 8 (+ 2) % Row 2 \SetRowColor{LightBackground} def \_\_int\_\_(self, other attributes): & defines and initializes attributes \tn % Row Count 10 (+ 2) % Row 3 \SetRowColor{white} object.method() & calls method on an object of that class \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.18988 cm} x{2.78712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Bugs}} \tn % Row 0 \SetRowColor{LightBackground} Sytax errors & violation of a writing rule \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{Exceptions(runtime)} & syntax is fine, some other thing is occasionally \seqsplit{flawed:ZeroDivisionError(can't} divide by zero), NameError(can't access the variable), IndexError(When the index you are attempting to access does not exist), TypeError( operation on non-compatible type) \tn % Row Count 14 (+ 12) % Row 2 \SetRowColor{LightBackground} Logical (semantic) errors & code runs, but doesn't do what its supposed to \tn % Row Count 17 (+ 3) % Row 3 \SetRowColor{white} try: & does something that may cause an exception \tn % Row Count 19 (+ 2) % Row 4 \SetRowColor{LightBackground} except \textless{}SomeExceptionType\textgreater{}: & do something to handle the exception \tn % Row Count 21 (+ 2) % Row 5 \SetRowColor{white} raise{[} exception object {]} & raises a certain, defined type of exception \tn % Row Count 23 (+ 2) % Row 6 \SetRowColor{LightBackground} try, except, else & try something, that could cause the exception, if its fine, go to else \tn % Row Count 27 (+ 4) % Row 7 \SetRowColor{white} finally: & adds statement that happens no matter what \tn % Row Count 29 (+ 2) % Row 8 \SetRowColor{LightBackground} for a, b in zip(list 1, list 2): & iterates over elements of two lists in parallel, yields a tuple with both iterations \tn % Row Count 33 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.69218 cm} x{3.28482 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{File Input/Output}} \tn % Row 0 \SetRowColor{LightBackground} f = open(myfile, 'x') & opens the file, x=r for reading only, x=w for writing only, x=a for appending, x=b for file in binary format, x=wr+ reading and writing and so on \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} .read(size) & reads the entire file, returns a single string, size is optional number of characters \tn % Row Count 10 (+ 4) % Row 2 \SetRowColor{LightBackground} \seqsplit{.readlines(size)} & reads all lines and returns them as a list of strings \tn % Row Count 13 (+ 3) % Row 3 \SetRowColor{white} .rstrip() & returns a string with the end-of-line character(\textbackslash{}n) removed from the end of the string \tn % Row Count 17 (+ 4) % Row 4 \SetRowColor{LightBackground} .readline() & reads single line from file, returns empty string if end of file \tn % Row Count 20 (+ 3) % Row 5 \SetRowColor{white} .close() & closes file \tn % Row Count 21 (+ 1) % Row 6 \SetRowColor{LightBackground} gzip.open() & interface to read/write compressed files, .gz extension \tn % Row Count 24 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Operations on Dictionaries}} \tn % Row 0 \SetRowColor{LightBackground} dict = \{\} & sets up an empty dictionary \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} dict{[}key{]} = value & sets value of said at that key to value \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} dict{[}key{]} & calls the value at that key \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} del dict{[}key{]} & deletes a key from dict \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} dict.clear() & clears the dict of all keys \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} dict.update(dict2) & can update/add associations from another \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{dict.keys()/dict.items()/dict.values()} & looks at either all keys, values, or all items in the dict \tn % Row Count 15 (+ 3) % Row 7 \SetRowColor{white} dict.pop(key) & removes element associated to that key \tn % Row Count 17 (+ 2) % Row 8 \SetRowColor{LightBackground} dict.popitem() & removes key, value pair and returns it as a tuple \tn % Row Count 20 (+ 3) % Row 9 \SetRowColor{white} dict.get(key) & returns value at that key \tn % Row Count 22 (+ 2) % Row 10 \SetRowColor{LightBackground} \seqsplit{dict.setdefault(key)} & either returns value of the key or creates the given key if not previously existent \tn % Row Count 27 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}