\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{anglimin} \pdfinfo{ /Title (cs1010s-mid-terms.pdf) /Creator (Cheatography) /Author (anglimin) /Subject (CS1010S Mid terms 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}{43A390} \definecolor{LightBackground}{HTML}{F3F9F8} \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{CS1010S Mid terms Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{anglimin} via \textcolor{DarkBackground}{\uline{cheatography.com/77601/cs/19011/}}} \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}anglimin \\ \uline{cheatography.com/anglimin} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 3rd March, 2019.\\ Updated 6th March, 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{1.54287 cm} x{3.43413 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Types}} \tn % Row 0 \SetRowColor{LightBackground} str & 'abcd' \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} int & 1,2,3,4 \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} float & 1.23 \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} bool & True/False \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} None & None \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} type('abc') & shows the type of the object \tn % Row Count 7 (+ 2) % Row 6 \SetRowColor{LightBackground} type conversion & int('123') -{}-\textgreater{} 123 \tn % Row Count 9 (+ 2) % Row 7 \SetRowColor{white} bool(x) & False for x values that are null or empty x. True for otherwise \tn % Row Count 12 (+ 3) % Row 8 \SetRowColor{LightBackground} Naming conventions & lowercase and underscore (\_HAPPYday, happyday) \tn % Row Count 14 (+ 2) \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}{Order of growth}} \tn % Row 0 \SetRowColor{LightBackground} def factorial(n): if n \textless{} 2: return 1 return n * factorial(n - 1) & O(n) \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} def fib(n): if n \textless{} 2: return 1 return fib(n - 1) + fib(n - 2) & O(2\textasciicircum{}n) (each branch produces another 2 branches) \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} def foo(n): while n \textgreater{} 1: print(n) n = n // 2 & O(log n) \tn % Row Count 12 (+ 4) % Row 3 \SetRowColor{white} def bar(n): while n \textgreater{} 1: x = n while x \textgreater{} 1: print(n, x) x -= 1 n -= 1 & O(n\textasciicircum{}2) (loop in a loop) \tn % Row Count 19 (+ 7) % Row 4 \SetRowColor{LightBackground} def baz(n): while n \textgreater{} 1: x = n while x \textgreater{} 1: print(n, x) x = x // 2 n -= 1 & O(n log n) (log n in a loop) \tn % Row Count 26 (+ 7) % Row 5 \SetRowColor{white} String and tuple slicing & Slicing is an O(n) operation as you {\bf{ need to create another string/tuple when you slice}}. \tn % Row Count 31 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Order of growth (cont)}} \tn % Row 6 \SetRowColor{LightBackground} def count\_me(n): if n \textless{} 10: return n else: return count\_me(n + 1) & O(infinity) \tn % Row Count 5 (+ 5) % Row 7 \SetRowColor{white} String and tuple concatenation & O(n) \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Time complexity-{}-\textgreater{} looking at the branches of the tree \newline Space complexity-{}-\textgreater{} looking at the depth of the tree (depends on the pending operations the function incurred) \newline \newline And also for analysing OOG questions: \newline for \textless{}loop\textgreater{}: \newline \textless{}do work\textgreater{} \newline Time \newline = work(1) + work(2)+...+work(n) \newline If work is fixed (let's say work = k) regardless of n, Time = n * k \newline Otherwise, \newline Time = sum of work \newline Remember to state what the n means} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Recursive and Iteration}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Recursion}} \newline def recusive\_fn(n): \newline if \textless{}base case\textgreater{}: \newline return \textless{}base case value\textgreater{} \newline else: \newline return \textless{}manipulate recursive\_fn(n-1) such that it becomes nth case\textgreater{} \newline \newline {\bf{Iteration (for or while loop)}} \newline def iterative\_fn(n): \newline temp = \textless{}some basic value\textgreater{} \newline for i in \textless{}range\textgreater{}: \newline \textless{}update temp\textgreater{} \newline return temp \newline \newline def factorial (n): \newline product , counter = 1,1 \newline while counter \textless{}= n: \newline product *= counter \newline counter += 1 \newline return product} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Tuples comparison}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Tuples are compared {\bf{position by position}}: the {\bf{first item of the first tuple is compared to the first item of the second tuple}} (same as string); if they are not equal (i.e. the first is greater or smaller than the second) then that's the result of the comparison, else the second item is considered, then the third and so on.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Truth Tables}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/anglimin_1551592578_Capture.PNG}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Truth values revisited}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Truth and False will be represented by 1 and 0 respectively. \newline not 'abc' -{}-\textgreater{} False \newline 1 or 0 -{}-\textgreater{} 0 \newline '' is same as False is same as 0 is same as {[}{]} \newline \newline while True: infinite looping \newline while False: will not loop at all} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{UnboundLocalError}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/anglimin_1551597465_Capture.PNG}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{String and tuple slicing}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{s{[}start:stop({\bf{noninclusive}}):step{]} \newline \newline eg) s = "12345", d ='6789' \newline \newline concatenate-{}-\textgreater{} s + d-{}-\textgreater{} '123456789' \newline \newline s{[}0{]} = "1", s{[}1:{]}= "2345", s{[}:2{]}= "12", s{[}1:5:2{]} = "13" \newline \newline s{[}-1{]} = "5", s{[}-1:-3{]} = "", s{[}-3:-1{]}= '34', s{[}-5:-1:2{]}= '13', s{[}-5:-1:-2{]}= ''', s{[}-1:-5:-2{]} = '53'} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Variable (Global and Local)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/anglimin_1551597308_Capture.PNG}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Variable is local if assigned to in function \newline Namespaces match function and variable names to their definitions and assignments} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.38896 cm} x{2.58804 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Random module}} \tn % Row 0 \SetRowColor{LightBackground} random.randint(a, b) & Return a random integer N such that a \textless{}= N \textless{}= b. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{random.randrange(start}, stop{[}, step{]}) & Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn't actually build a range object \tn % Row Count 12 (+ 9) % Row 2 \SetRowColor{LightBackground} random.random() & Return the next random floating point number in the range {[}0.0, 1.0). \tn % Row Count 16 (+ 4) % Row 3 \SetRowColor{white} random.random() can be scaled & int(random.random() * 9999) → 3658 \tn % Row Count 18 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{break}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{for val in "string": \newline if val == "i": \newline break \newline print(val) \newline \newline print("The end") \newline {\bf{Output}}: \newline s \newline t \newline r \newline The end} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop. \newline \newline If break statement is inside a nested loop (loop inside another loop), break will terminate the innermost loop.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Hiding 'something'}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Input an empty {\bf{(lambda : "Prof is really cool")}} \newline In that way what is printed or sliced/accessed will be the function itself. \newline To recover the secret message, can input () at the back.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def \textless{}name\textgreater{}(formal parameters): \newline body \newline {\bf{return}} (something if not it is going to return to zero) \newline \newline Anonymous function→ {\bf{lambda}} \newline lambda x: x+ 1 \newline \newline A function need not use every parameter passed into it. (def foo(a, b): return a + a) \newline A function without any parameters or return value is still valid. (def foo():return ) \newline Functions do not necessarily need a return value.(def foo(a, b, c):a + b + c)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Conditionals}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{if}} \textless{}statement\textgreater{}: \newline do something \newline {\bf{elif}} \textless{}statement\textgreater{}: \newline do something \newline {\bf{else}} \textless{}statement\textgreater{}: \newline do something \newline \newline {\bf{while}} \textless{}statement\textgreater{}: \newline do something} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} x{4.4793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Equality/ in function}} \tn % Row 0 \SetRowColor{LightBackground} is & This means the same object. (3 is 3)-{}-\textgreater{} True \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} == & This mean the same content. (3.00 == 3) -{}-\textgreater{} True \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} in & check the elements inside the other group. Eg) x = (1,2,3). 1 in x-{}-\textgreater{} True \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{OOG Time and Speed}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/anglimin_1551595789_Capture.PNG}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.4931 cm} x{3.4839 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Different Types of Error}} \tn % Row 0 \SetRowColor{LightBackground} NameError & Raised when a variable is not found in local or global scope. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} IndexError & Raised when index of a sequence is out of range. \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{RuntimeError} & Raised when an error does not fall under any other category. \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} SyntaxError & Raised by parser when syntax error is encountered \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{ZeroDivisionError} & Raised when second operand of division or modulo operation is zero. \tn % Row Count 13 (+ 3) % Row 5 \SetRowColor{white} \seqsplit{UnboundLocalError} & Raised when a reference is made to a local variable in a function or method, but no value has been bound to that variable \tn % Row Count 18 (+ 5) % Row 6 \SetRowColor{LightBackground} \seqsplit{RecursionError} & Infinite looping \tn % Row Count 20 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.74655 cm} x{4.23045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Tuple and string functions}} \tn % Row 0 \SetRowColor{LightBackground} len() & Return the length (the number of items) in the tuple. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} max() & Return the largest item in the tuple. \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} min() & Return the smallest item in the tuple \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} sum() & Retrun the sum of all elements in the tuple. \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{tuple()} & Convert an iterable (list, string, set, dictionary) to a tuple. \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{count()} & count the occurrences of an element in a tuple \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{index()} & Searches the string for a specified value and returns the position of where it was found \tn % Row Count 15 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{continue}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{for val in "string": \newline if val == "i": \newline continue \newline print(val) \newline \newline print("The end") \newline {\bf{Output}}: \newline s \newline t \newline r \newline n \newline g \newline The end} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}