\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{0llieC (0llieC)} \pdfinfo{ /Title (python-programming-competition.pdf) /Creator (Cheatography) /Author (0llieC (0llieC)) /Subject (Python Programming Competition 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}{2196F3} \definecolor{LightBackground}{HTML}{F1F8FE} \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{Python Programming Competition Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{0llieC (0llieC)} via \textcolor{DarkBackground}{\uline{cheatography.com/38321/cs/18889/}}} \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}0llieC (0llieC) \\ \uline{cheatography.com/0lliec} \\ \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 20th February, 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{tabularx}{17.67cm}{x{7.4261 cm} x{9.8439 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{File I/O}} \tn % Row 0 \SetRowColor{LightBackground} Open a File & `f = open("test.txt")` \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Open a File as Read-Only & `f = open("test.txt", 'r')` \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} Open a File to Write & `f = open("test.txt", 'w')` \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} Open a File to Append & `f = open("test.txt", 'a')` \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} Close a File & `f.close()` \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} Write to a File & `f.write("write this line\textbackslash{}n")` \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} Write a list of lines & `f.writelines(lines)` \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} Read a File & `f.read()` reads to the EOF\{\{nl\}\}`f.read(n)` reads n characters \tn % Row Count 15 (+ 3) % Row 8 \SetRowColor{LightBackground} Current File Position & `f.tell()` \tn % Row Count 17 (+ 2) % Row 9 \SetRowColor{white} Change the File Position & `f.seek(n)` where n is the new position. \tn % Row Count 19 (+ 2) % Row 10 \SetRowColor{LightBackground} Read a single line & `f.readline()` \tn % Row Count 21 (+ 2) % Row 11 \SetRowColor{white} Put all file lines into a list & `f.readlines()` \tn % Row Count 23 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.9721 cm} x{13.2979 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Tree Algorithms}} \tn % Row 0 \SetRowColor{LightBackground} Tree & `\{\{nobreak\}\}class~Node:\{\{nl\}\}\{\{nl\}\}~~~~def~\_\_init\_\_(self,~data):\{\{nl\}\}\{\{nl\}\}~~~~~~~~self.left~=~None\{\{nl\}\}~~~~~~~~self.right~=~None\{\{nl\}\}~~~~~~~~self.data~=~data\{\{nl\}\}\#~Insert~Node\{\{nl\}\}~~~~def~insert(self,~data):\{\{nl\}\}\{\{nl\}\}~~~~~~~~if~self.data:\{\{nl\}\}~~~~~~~~~~~~if~data~\textless{}~self.data:\{\{nl\}\}~~~~~~~~~~~~~~~~if~self.left~is~None:\{\{nl\}\}~~~~~~~~~~~~~~~~~~~~self.left~=~Node(data)\{\{nl\}\}~~~~~~~~~~~~~~~~else:\{\{nl\}\}~~~~~~~~~~~~~~~~~~~~self.left.insert(data)\{\{nl\}\}~~~~~~~~~~~~elif~data~\textgreater{}~self.data:\{\{nl\}\}~~~~~~~~~~~~~~~~if~self.right~is~None:\{\{nl\}\}~~~~~~~~~~~~~~~~~~~~self.right~=~Node(data)\{\{nl\}\}~~~~~~~~~~~~~~~~else:\{\{nl\}\}~~~~~~~~~~~~~~~~~~~~self.right.insert(data)\{\{nl\}\}~~~~~~~~else:\{\{nl\}\}~~~~~~~~~~~~self.data~=~data\{\{nl\}\}\{\{nl\}\}\#~Print~the~Tree\{\{nl\}\}~~~~def~PrintTree(self):\{\{nl\}\}~~~~~~~~if~self.left:\{\{nl\}\}~~~~~~~~~~~~self.left.PrintTree()\{\{nl\}\}~~~~~~~~print(~self.data),\{\{nl\}\}~~~~~~~~if~self.right:\{\{nl\}\}~~~~~~~~~~~~self.right.PrintTree()` \tn % Row Count 85 (+ 85) % Row 1 \SetRowColor{white} In-Order & `\{\{nobreak\}\}\#~Inorder~traversal\{\{nl\}\}\#~Left~-\textgreater{}~Root~-\textgreater{}~Right\{\{nl\}\}~~~~def~inorderTraversal(self,~root):\{\{nl\}\}~~~~~~~~res~=~{[}{]}\{\{nl\}\}~~~~~~~~if~root:\{\{nl\}\}~~~~~~~~~~~~res~=~self.inorderTraversal(root.left)\{\{nl\}\}~~~~~~~~~~~~res.append(root.data)\{\{nl\}\}~~~~~~~~~~~~res~=~res~+~self.inorderTraversal(root.right)\{\{nl\}\}~~~~~~~~return~res` \tn % Row Count 110 (+ 25) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.9721 cm} x{13.2979 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Tree Algorithms (cont)}} \tn % Row 2 \SetRowColor{LightBackground} \seqsplit{Pre-Order} & `\{\{nobreak\}\}\#~Preorder~traversal\{\{nl\}\}\#~Root~-\textgreater{}~Left~-\textgreater{}Right\{\{nl\}\}~~~~def~PreorderTraversal(self,~root):\{\{nl\}\}~~~~~~~~res~=~{[}{]}\{\{nl\}\}~~~~~~~~if~root:\{\{nl\}\}~~~~~~~~~~~~res.append(root.data)\{\{nl\}\}~~~~~~~~~~~~res~=~res~+~self.PreorderTraversal(root.left)\{\{nl\}\}~~~~~~~~~~~~res~=~res~+~self.PreorderTraversal(root.right)\{\{nl\}\}~~~~~~~~return~res` \tn % Row Count 26 (+ 26) % Row 3 \SetRowColor{white} \seqsplit{Post-Order} & `\{\{nobreak\}\}\#~Postorder~traversal\{\{nl\}\}\#~Left~-\textgreater{}Right~-\textgreater{}~Root\{\{nl\}\}~~~~def~PostorderTraversal(self,~root):\{\{nl\}\}~~~~~~~~res~=~{[}{]}\{\{nl\}\}~~~~~~~~if~root:\{\{nl\}\}~~~~~~~~~~~~res~=~self.PostorderTraversal(root.left)\{\{nl\}\}~~~~~~~~~~~~res~=~res~+~self.PostorderTraversal(root.right)\{\{nl\}\}~~~~~~~~~~~~res.append(root.data)\{\{nl\}\}~~~~~~~~return~res` \tn % Row Count 51 (+ 25) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.9721 cm} x{13.2979 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Tree Algorithms (cont)}} \tn % Row 4 \SetRowColor{LightBackground} Find a Value & `\{\{nobreak\}\}def~findval(self,~lkpval):\{\{nl\}\}~~~~~~~~if~lkpval~\textless{}~self.data:\{\{nl\}\}~~~~~~~~~~~~if~self.left~is~None:\{\{nl\}\}~~~~~~~~~~~~~~~~return~str(lkpval)+"~Not~Found"\{\{nl\}\}~~~~~~~~~~~~return~self.left.findval(lkpval)\{\{nl\}\}~~~~~~~~elif~lkpval~\textgreater{}~self.data:\{\{nl\}\}~~~~~~~~~~~~if~self.right~is~None:\{\{nl\}\}~~~~~~~~~~~~~~~~return~str(lkpval)+"~Not~Found"\{\{nl\}\}~~~~~~~~~~~~return~self.right.findval(lkpval)\{\{nl\}\}~~~~~~~~else:\{\{nl\}\}~~~~~~~~~~~~print(str(self.data)~+~'~is~found')` \tn % Row Count 40 (+ 40) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{p{1.8997 cm} x{15.3703 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Queues and Stack}} \tn % Row 0 \SetRowColor{LightBackground} Queue & `\{\{nobreak\}\}class~Queue:\{\{nl\}\}\{\{nl\}\}~~def~\_\_init\_\_(self):\{\{nl\}\}~~~~~~self.queue~=~list()\{\{nl\}\}\{\{nl\}\}~~def~addtoq(self,dataval):\{\{nl\}\}\#~Insert~method~to~add~element\{\{nl\}\}~~~~~~if~dataval~not~in~self.queue:\{\{nl\}\}~~~~~~~~~~self.queue.insert(0,dataval)\{\{nl\}\}~~~~~~~~~~return~True\{\{nl\}\}~~~~~~return~False\{\{nl\}\}\#~Pop~method~to~remove~element\{\{nl\}\}~~def~removefromq(self):\{\{nl\}\}~~~~~~if~len(self.queue)\textgreater{}0:\{\{nl\}\}~~~~~~~~~~return~self.queue.pop()\{\{nl\}\}~~~~~~return~("No~elements~in~Queue!")\{\{nl\}\}` \tn % Row Count 28 (+ 28) % Row 1 \SetRowColor{white} Stack & `\{\{nobreak\}\}class~Stack:\{\{nl\}\}\{\{nl\}\}~~~~def~\_\_init\_\_(self):\{\{nl\}\}~~~~~~~~self.stack~=~{[}{]}\{\{nl\}\}\{\{nl\}\}~~~~def~add(self,~dataval):\{\{nl\}\}\#~Use~list~append~method~to~add~element\{\{nl\}\}~~~~~~~~if~dataval~not~in~self.stack:\{\{nl\}\}~~~~~~~~~~~~self.stack.append(dataval)\{\{nl\}\}~~~~~~~~~~~~return~True\{\{nl\}\}~~~~~~~~else:\{\{nl\}\}~~~~~~~~~~~~return~False\{\{nl\}\}~~~~~~~~\{\{nl\}\}\#~Use~list~pop~method~to~remove~element\{\{nl\}\}~~~~def~remove(self):\{\{nl\}\}~~~~~~~~if~len(self.stack)~\textless{}=~0:\{\{nl\}\}~~~~~~~~~~~~return~("No~element~in~the~Stack")\{\{nl\}\}~~~~~~~~else:\{\{nl\}\}~~~~~~~~~~~~return~self.stack.pop()` \tn % Row Count 67 (+ 39) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Regex in Python}} \tn % Row 0 \SetRowColor{LightBackground} import module & `import re` \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Return a list of matches & `x = re.findall("ai", str)` \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} Split a string & `x = re.split("\textbackslash{}s", str)` \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} Replace & `x = re.sub("\textbackslash{}s", "9", str)` \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} Finds occurrences. Returns a Match object & `x = re.search("\textbackslash{}s", str)` \tn % Row Count 10 (+ 3) % Row 5 \SetRowColor{white} & `x.span()` returns a tuple of start and end of each match \tn % Row Count 13 (+ 3) % Row 6 \SetRowColor{LightBackground} & `x.string()` returns the string searched \tn % Row Count 15 (+ 2) % Row 7 \SetRowColor{white} & `x.group()` returns the part of the string matching \tn % Row Count 18 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.1086 cm} x{14.1614 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Regular Expressions}} \tn % Row 0 \SetRowColor{LightBackground} . & Any character except newline \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} a|b & a or b \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} a* & 0 or more a's \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} {[}ab-d{]} & One character of: a, b, c, d \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} {[}\textasciicircum{}ab-d{]} & One character except: a, b, c, d \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \textbackslash{}d & One digit \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \textbackslash{}D & One non-digit \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} \textbackslash{}s & One whitespace \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} \textbackslash{}S & One non-whitespace \tn % Row Count 9 (+ 1) % Row 9 \SetRowColor{white} \textasciicircum{} & Start of string \tn % Row Count 10 (+ 1) % Row 10 \SetRowColor{LightBackground} \$ & End of string \tn % Row Count 11 (+ 1) % Row 11 \SetRowColor{white} (...) & Capturing group \tn % Row Count 12 (+ 1) % Row 12 \SetRowColor{LightBackground} * & 0 or more \tn % Row Count 13 (+ 1) % Row 13 \SetRowColor{white} + & 1 or more \tn % Row Count 14 (+ 1) % Row 14 \SetRowColor{LightBackground} ? & 0 or 1 \tn % Row Count 15 (+ 1) % Row 15 \SetRowColor{white} \{2\} & Exactly 2 \tn % Row Count 16 (+ 1) % Row 16 \SetRowColor{LightBackground} \{2, 5\} & Between 2 and 5 \tn % Row Count 17 (+ 1) % Row 17 \SetRowColor{white} \{2,\} & 2 or more \tn % Row Count 18 (+ 1) % Row 18 \SetRowColor{LightBackground} (,5\} & Up to 5 \tn % Row Count 19 (+ 1) % Row 19 \SetRowColor{white} \textbackslash{}n & Newline \tn % Row Count 20 (+ 1) % Row 20 \SetRowColor{LightBackground} \textbackslash{}t & Tab \tn % Row Count 21 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{6.3899 cm} x{10.8801 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Graph Algorithms}} \tn % Row 0 \SetRowColor{LightBackground} Breadth First Traversal & `\{\{nobreak\}\}import~collections\{\{nl\}\} class~graph:\{\{nl\}\} \seqsplit{~~~~def~\_\_init\_\_(self},gdict=None):\{\{nl\}\} ~~~~~~~~if~gdict~is~None:\{\{nl\}\} ~~~~~~~~~~~~gdict~=~\{\}\{\{nl\}\} ~~~~~~~~self.gdict~=~gdict\{\{nl\}\} \{\{nl\}\} def~bfs(graph,~startnode):\{\{nl\}\} \#~Track~the~visited~and~unvisited~nodes~using~queue\{\{nl\}\} ~~~~~~~~seen,~queue~=~set({[}startnode{]}),~collections.deque({[}startnode{]})\{\{nl\}\} ~~~~~~~~while~queue:\{\{nl\}\} \seqsplit{~~~~~~~~~~~~vertex~=~queue}.popleft()\{\{nl\}\} ~~~~~~~~~~~~marked(vertex)\{\{nl\}\} ~~~~~~~~~~~~for~node~in~graph{[}vertex{]}:\{\{nl\}\} ~~~~~~~~~~~~~~~~if~node~not~in~seen:\{\{nl\}\} ~~~~~~~~~~~~~~~~~~~~seen.add(node)\{\{nl\}\} \seqsplit{~~~~~~~~~~~~~~~~~~~~queue}.append(node)\{\{nl\}\} \{\{nl\}\} def~marked(n):\{\{nl\}\} ~~~~print(n)\{\{nl\}\} \{\{nl\}\} \#~The~graph~dictionary\{\{nl\}\} gdict~=~\{~"a"~:~set({[}"b","c"{]}),\{\{nl\}\} ~~~~~~~~~~~~~~~~"b"~:~set({[}"a",~"d"{]}),\{\{nl\}\} ~~~~~~~~~~~~~~~~"c"~:~set({[}"a",~"d"{]}),\{\{nl\}\} ~~~~~~~~~~~~~~~~"d"~:~set({[}"e"{]}),\{\{nl\}\} ~~~~~~~~~~~~~~~~"e"~:~set({[}"a"{]})\{\{nl\}\} ~~~~~~~~~~~~~~~~\}\{\{nl\}\} \{\{nl\}\} bfs(gdict,~"a")` \tn % Row Count 95 (+ 95) % Row 1 \SetRowColor{white} Depth First Traversal & `\{\{nobreak\}\}class~graph:\{\{nl\}\}\{\{nl\}\}~~~~def~\_\_init\_\_(self,gdict=None):\{\{nl\}\}~~~~~~~~if~gdict~is~None:\{\{nl\}\}~~~~~~~~~~~~gdict~=~\{\}\{\{nl\}\}~~~~~~~~self.gdict~=~gdict\{\{nl\}\}\#~Check~for~the~visisted~and~unvisited~nodes\{\{nl\}\}def~dfs(graph,~start,~visited~=~None):\{\{nl\}\}~~~~if~visited~is~None:\{\{nl\}\}~~~~~~~~visited~=~set()\{\{nl\}\}~~~~visited.add(start)\{\{nl\}\}~~~~print(start)\{\{nl\}\}~~~~for~next~in~graph{[}start{]}~-~visited:\{\{nl\}\}~~~~~~~~dfs(graph,~next,~visited)\{\{nl\}\}~~~~return~visited\{\{nl\}\}\{\{nl\}\}gdict~=~\{~"a"~:~set({[}"b","c"{]}),\{\{nl\}\}~~~~~~~~~~~~~~~~"b"~:~set({[}"a",~"d"{]}),\{\{nl\}\}~~~~~~~~~~~~~~~~"c"~:~set({[}"a",~"d"{]}),\{\{nl\}\}~~~~~~~~~~~~~~~~"d"~:~set({[}"e"{]}),\{\{nl\}\}~~~~~~~~~~~~~~~~"e"~:~set({[}"a"{]})\{\{nl\}\}~~~~~~~~~~~~~~~~\}\{\{nl\}\}\{\{nl\}\}\{\{nl\}\}dfs(gdict,~'a')` \tn % Row Count 165 (+ 70) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.3175 cm} x{12.9525 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Searching Algorithms}} \tn % Row 0 \SetRowColor{LightBackground} Linear Search & `\{\{nobreak\}\}def~linear\_search(values,~search\_for):\{\{nl\}\}~~~~search\_at~=~0\{\{nl\}\}~~~~search\_res~=~False\{\{nl\}\}\{\{nl\}\}\#~Match~the~value~with~each~data~element \{\{nl\}\}~~~~while~search\_at~\textless{}~len(values)~and~search\_res~is~False:\{\{nl\}\}~~~~~~~~if~values{[}search\_at{]}~==~search\_for:\{\{nl\}\}~~~~~~~~~~~~search\_res~=~True\{\{nl\}\}~~~~~~~~else:\{\{nl\}\}~~~~~~~~~~~~search\_at~=~search\_at~+~1\{\{nl\}\}\{\{nl\}\}~~~~return~search\_res\{\{nl\}\}\{\{nl\}\}l~=~{[}64,~34,~25,~12,~22,~11,~90{]}\{\{nl\}\}print(linear\_search(l,~12))\{\{nl\}\}print(linear\_search(l,~91))` \tn % Row Count 33 (+ 33) % Row 1 \SetRowColor{white} Binary Search & `\{\{nobreak\}\}def~bsearch(list,~val):\{\{nl\}\}\{\{nl\}\}~~~~list\_size~=~len(list)~-~1\{\{nl\}\}\{\{nl\}\}~~~~idx0~=~0\{\{nl\}\}~~~~idxn~=~list\_size\{\{nl\}\}\#~Find~the~middle~most~value\{\{nl\}\}\{\{nl\}\}~~~~while~idx0~\textless{}=~idxn:\{\{nl\}\}~~~~~~~~midval~=~(idx0~+~idxn)//~2\{\{nl\}\}\{\{nl\}\}~~~~~~~~if~list{[}midval{]}~==~val:\{\{nl\}\}~~~~~~~~~~~~return~midval\{\{nl\}\}\#~Compare~the~value~the~middle~most~value\{\{nl\}\}~~~~~~~~if~val~\textgreater{}~list{[}midval{]}:\{\{nl\}\}~~~~~~~~~~~~idx0~=~midval~+~1\{\{nl\}\}~~~~~~~~else:\{\{nl\}\}~~~~~~~~~~~~idxn~=~midval~-~1\{\{nl\}\}\{\{nl\}\}~~~~if~idx0~\textgreater{}~idxn:\{\{nl\}\}~~~~~~~~return~None\{\{nl\}\}\#~Initialize~the~sorted~list\{\{nl\}\}list~=~{[}2,7,19,34,53,72{]}\{\{nl\}\}\{\{nl\}\}\#~Print~the~search~result\{\{nl\}\}print(bsearch(list,72))\{\{nl\}\}print(bsearch(list,11))` \tn % Row Count 83 (+ 50) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.4902 cm} x{12.7798 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Useful Functions}} \tn % Row 0 \SetRowColor{LightBackground} Counter & `from collections import Counter\{\{nl\}\}arr = {[}1, 3, 4, 1, 2, 1, 1, 3, 4, 3, 5, 1, 2, 5, 3, 4, 5{]} \{\{nl\}\}counter = Counter(arr) \{\{nl\}\} top\_three = counter.most\_common(3) \{\{nl\}\}print(top\_three) ` \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} Output & `{[}(1, 5), (3, 4), (4, 3){]}` \tn % Row Count 8 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{Top/Bottom} nth & `import heapq \{\{nl\}\}grades = {[}110, 25, 38, 49, 20, 95, 33, 87, 80, 90{]} \{\{nl\}\}print(heapq.nlargest(3, grades)) \{\{nl\}\}print(heapq.nsmallest(4, grades)) ` \tn % Row Count 14 (+ 6) % Row 3 \SetRowColor{white} Output & `{[}110, 95, 90{]} \{\{nl\}\}{[}20, 25, 33, 38{]}` \tn % Row Count 16 (+ 2) % Row 4 \SetRowColor{LightBackground} Map Function & ` \# Python code to apply a function on a list\{\{nl\}\} income = {[}10, 30, 75{]} \{\{nl\}\}def double\_money(dollars): \{\{nl\}\}~~return dollars * 2 \{\{nl\}\}\{\{nl\}\}new\_income = list(map(double\_money, income)) \{\{nl\}\}print(new\_income) ` \tn % Row Count 25 (+ 9) % Row 5 \SetRowColor{white} Output & `{[}20, 60, 150{]}` \tn % Row Count 26 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.7994 cm} x{13.4706 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Sorting Algorithms}} \tn % Row 0 \SetRowColor{LightBackground} Bubble Sort & `\{\{nobreak\}\}def~bubblesort(list):\{\{nl\}\} \{\{nl\}\} \#~Swap~the~elements~to~arrange~in~order\{\{nl\}\} \seqsplit{~~~~for~iter\_num~in~range(len(list)-1},0,-1):\{\{nl\}\} ~~~~~~~~for~idx~in~range(iter\_num):\{\{nl\}\} ~~~~~~~~~~~~if~list{[}idx{]}\textgreater{}list{[}idx+1{]}:\{\{nl\}\} ~~~~~~~~~~~~~~~~temp~=~list{[}idx{]}\{\{nl\}\} ~~~~~~~~~~~~~~~~list{[}idx{]}~=~list{[}idx+1{]}\{\{nl\}\} ~~~~~~~~~~~~~~~~list{[}idx+1{]}~=~temp\{\{nl\}\} \{\{nl\}\} \{\{nl\}\} list~=~{[}19,2,31,45,6,11,121,27{]}\{\{nl\}\} bubblesort(list)\{\{nl\}\} print(list)` \tn % Row Count 30 (+ 30) % Row 1 \SetRowColor{white} Merge Sort & `\{\{nobreak\}\}def~merge\_sort(unsorted\_list):\{\{nl\}\} ~~~~if~len(unsorted\_list)~\textless{}=~1:\{\{nl\}\} ~~~~~~~~return~unsorted\_list\{\{nl\}\} \#~Find~the~middle~point~and~devide~it\{\{nl\}\} ~~~~middle~=~len(unsorted\_list)~//~2\{\{nl\}\} ~~~~left\_list~=~unsorted\_list{[}:middle{]}\{\{nl\}\} ~~~~right\_list~=~unsorted\_list{[}middle:{]}\{\{nl\}\} \{\{nl\}\} ~~~~left\_list~=~merge\_sort(left\_list)\{\{nl\}\} ~~~~right\_list~=~merge\_sort(right\_list)\{\{nl\}\} \seqsplit{~~~~return~list(merge(left\_list},~right\_list))\{\{nl\}\} \{\{nl\}\} \#~Merge~the~sorted~halves\{\{nl\}\} \{\{nl\}\} def~merge(left\_half,right\_half):\{\{nl\}\} \{\{nl\}\} ~~~~res~=~{[}{]}\{\{nl\}\} \seqsplit{~~~~while~len(left\_half)~}!=~0~and~len(right\_half)~!=~0:\{\{nl\}\} ~~~~~~~~if~left\_half{[}0{]}~\textless{}~right\_half{[}0{]}:\{\{nl\}\} ~~~~~~~~~~~~res.append(left\_half{[}0{]})\{\{nl\}\} ~~~~~~~~~~~~left\_half.remove(left\_half{[}0{]})\{\{nl\}\} ~~~~~~~~else:\{\{nl\}\} ~~~~~~~~~~~~res.append(right\_half{[}0{]})\{\{nl\}\} ~~~~~~~~~~~~right\_half.remove(right\_half{[}0{]})\{\{nl\}\} ~~~~if~len(left\_half)~==~0:\{\{nl\}\} ~~~~~~~~res~=~res~+~right\_half\{\{nl\}\} ~~~~else:\{\{nl\}\} ~~~~~~~~res~=~res~+~left\_half\{\{nl\}\} ~~~~return~res\{\{nl\}\} \{\{nl\}\} unsorted\_list~=~{[}64,~34,~25,~12,~22,~11,~90{]}\{\{nl\}\} \{\{nl\}\} \seqsplit{print(merge\_sort(unsorted\_list))`} \tn % Row Count 99 (+ 69) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \end{document}