\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{hasansheikh1} \pdfinfo{ /Title (cosc121.pdf) /Creator (Cheatography) /Author (hasansheikh1) /Subject (Cosc121 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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{Cosc121 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{hasansheikh1} via \textcolor{DarkBackground}{\uline{cheatography.com/214069/cs/46619/}}} \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}hasansheikh1 \\ \uline{cheatography.com/hasansheikh1} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 18th June, 2025.\\ 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*}{4} \begin{tabularx}{3.833cm}{p{0.3433 cm} p{0.3433 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{plot\_squared() - NumPy + Matplotlib Plotting}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{import numpy as np \newline import matplotlib.pyplot as plt \newline \newline def plot\_squared(n, x0, x1): \newline xs = np.linspace(x0, x1, n) \newline ys = xs {\emph{ xs \newline plt.plot(xs, ys) \newline plt.title("y = x }} x") \newline plt.xlabel("x") \newline plt.ylabel("y") \newline plt.grid(True) \newline plt.show()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.3433 cm} p{0.3433 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{afunction() - Nested While Loops}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{def afunction(value1, value2): \newline op1 = 0 \newline while op1 \textless{} value1: \newline op2 = value1 \newline while op2 \textgreater{} value2: \newline result = op1 * op2 \newline print(f"\{op1:3\} × \{op2:3\} = \{result:3\}") \newline op2 -= 1 \newline op1 += 1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.3433 cm} p{0.3433 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{File Max Finder}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{'ye' \newline def main(): \newline 'ye' \newline filename = input("Filename? ") \newline with open(filename, "r") as file: \newline max\_int = None \newline for line in file: \newline line = int(line) \newline if max\_int == None or line \textgreater{} max\_int: \newline max\_int = line \newline print(f"Largest number found: \{max\_int\}") \newline main()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.3433 cm} p{0.3433 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Files}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{def case\_fixer(input\_name, output\_name): \newline "ye" \newline with open(input\_name, 'r') as infile, open(output\_name, 'w') as outfile: \newline for line in infile: \newline cleaned\_line = line.strip().lower() \newline \seqsplit{outfile.write(cleaned\_line} + '\textbackslash{}n') \newline \newline line.strip() \# Remove leading/trailing whitespace \newline line.split() \# Split into list of words \newline line.split(',') \# Split by comma \newline d.lower(), .upper(), .title() \# Case transforms} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.3433 cm} p{0.3433 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{outputs}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{def trongo(x): \newline print(len(x)) \newline s = set(x) \newline y = \{'fo', 'to', 'fun'\} \newline if len(s.intersection(y)) == 1: \newline s = s.union(y) \newline print(", ".join(sorted(list(s)))) \newline \newline \# Example usage: \newline trongo({[}'fo', 'fi', 'hasan', 'fi', 'hasan'{]}) \newline \# Output: \newline \# 5 \newline \# fi, fo, fun, hasan, to} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.3433 cm} p{0.3433 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Student Class 1}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{class Student: \newline def \_\_init\_\_(self, student\_id, usercode, first\_name, family\_name): \newline self.student\_id = int(student\_id) \newline self.usercode = str(usercode) \newline self.first\_name = str(first\_name) \newline self.family\_name = str(family\_name) \newline self.courses = set() \newline \newline def \_\_str\_\_(self): \newline return f"\{self.student\_id\} \{self.family\_name.upper()\}, \{self.first\_name.capitalize()\}" \newline \newline def \seqsplit{get\_email\_address(self):} \newline return f"\{self.usercode\}@uclive.ac.nz"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Dicts}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{def \seqsplit{inverted\_word\_counts(word\_count\_dict):} \newline % Row Count 1 (+ 1) "ye" \newline % Row Count 2 (+ 1) inverted = \{\} \newline % Row Count 3 (+ 1) for word, word\_count in \seqsplit{word\_count\_dict.items():} \newline % Row Count 5 (+ 2) if word\_count in inverted: \newline % Row Count 6 (+ 1) inverted{[}word\_count{]}.append(word) \newline % Row Count 7 (+ 1) else: \newline % Row Count 8 (+ 1) inverted{[}word\_count{]} = {[}word{]} \newline % Row Count 9 (+ 1) for words in inverted.values(): \newline % Row Count 10 (+ 1) words.sort() \newline % Row Count 11 (+ 1) return inverted \newline % Row Count 12 (+ 1) def find\_key(input\_dict, value): \newline % Row Count 13 (+ 1) "ye" \newline % Row Count 14 (+ 1) for key, val in input\_dict.items(): \newline % Row Count 15 (+ 1) if val == value: \newline % Row Count 16 (+ 1) return key \newline % Row Count 17 (+ 1) return None% Row Count 18 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.3433 cm} p{0.3433 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Bar Graph}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{import numpy as np \newline import matplotlib.pyplot as plt \newline \newline def main(): \newline rainfalls = \seqsplit{np.loadtxt('laketaylorstation2005.txt'}, delimiter=',', skiprows=9, usecols=3) \newline days = np.arange(1, len(rainfalls) + 1) \newline axes = plt.axes() \newline axes.bar(days, rainfalls) \newline axes.set\_title("Lake Taylor Station Rainfalls, 2005") \newline axes.set\_xlabel("Day number") \newline \seqsplit{axes.set\_ylabel("Rainfall} (mm)") \newline axes.grid(True) \newline plt.show() \newline \newline main()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.3433 cm} p{0.3433 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Student Class 2}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{def enrol\_course(self, course): \newline self.courses.add(course) \newline \newline def is\_enrolled\_in(self, course): \newline return course in self.courses \newline \newline def main(): \newline astudent = Student(1234567, "ffn127", "first NAMES", "Family name") \newline print(type(astudent)) \newline print(astudent) \newline \seqsplit{astudent.enrol\_course("COSC121")} \newline \seqsplit{print(astudent.is\_enrolled\_in("COSC121"))} \newline \seqsplit{print(astudent.is\_enrolled\_in("COSC122"))} \newline \seqsplit{print(astudent.get\_email\_address())} \newline \newline main()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.3433 cm} p{0.3433 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{dict}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{def frequencies(input\_str): \newline "ye" \newline count\_dict = \{\} \newline for char in input\_str: \newline if char in count\_dict: \newline count\_dict{[}char{]} += 1 \newline else: \newline count\_dict{[}char{]} = 1 \newline return count\_dict} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.3433 cm} p{0.3433 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Classes}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{class Whatsit: \newline def \_\_init\_\_(self, grade, name, health): \newline allowed = \{'Sooglet', 'Throve', 'Plaguelet'\} \newline if grade not in allowed: \newline raise ValueError('Invalid Whatsit grade') \newline self.grade = grade \newline self.name = name \newline self.health = max(float(health), 0) \newline \newline def \_\_str\_\_(self): \newline return f"\{self.name\} (\{self.grade\}), health = \{self.health:.1f\}" \newline \newline def damage(self, amount): \newline self.health = max(self.health - amount, 0)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}