\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{sercand} \pdfinfo{ /Title (python-midterm-question-answers.pdf) /Creator (Cheatography) /Author (sercand) /Subject (python Midterm Question Answers 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{python Midterm Question Answers Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{sercand} via \textcolor{DarkBackground}{\uline{cheatography.com/32560/cs/10034/}}} \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}sercand \\ \uline{cheatography.com/sercand} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 2nd December, 2016.\\ Updated 2nd December, 2016.\\ 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*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Zigzag Matrix}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Mat: \newline \newline def \_\_init\_\_(self, n): \newline self.\_m = {[}{]} \newline for i in range(n): \newline self.\_m.append({[}0{]} * n) \newline \newline def get(self, r, c): \newline return self.\_m{[}r{]}{[}c{]} \newline \newline def \_\_iter\_\_(self): \newline self.\_x, self.\_y, self.\_incr = (0, -1, 1) \newline self.\_n = len(self.\_m) \newline return self \newline \newline def next(self): \newline self.\_y += self.\_incr \newline if self.\_y \textgreater{}= self.\_n: \newline self.\_x += 1 \newline self.\_y = self.\_n - 1 \newline self.\_incr = -1 \newline elif self.\_y \textless{} 0: \newline self.\_x += 1 \newline self.\_y = 0 \newline self.\_incr = 1 \newline if self.\_x \textgreater{}= self.\_n: \newline raise StopIteration \newline return self.\_m{[}self.\_x{]}{[}self.\_y{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{chaincomms}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{from subprocess import * \newline def chaincomms(cmds): \newline c = cmds{[}0{]}.split(' ') \newline p = Popen(c, stdout=PIPE, stdin=PIPE) \newline i = p.stdin \newline for k in cmds{[}1:{]}: \newline c = k.split(' ') \newline p = Popen(c, stdin=p.stdout, stdout=PIPE, close\_fds=True) \newline return (i, p.stdout)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{forks}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Forks: \newline \_l = Lock() \newline \newline def \_\_init\_\_(self, nforks): \newline self.\_c = Condition(self.\_l) \newline self.\_st = {[}True{]} * nforks \newline self.\_n = nforks \newline \newline def getforks(self): \newline self.\_l.acquire() \newline t = self.testforks() \newline while not t: \newline self.\_c.wait() \newline t = self.testforks() \newline for i in t: \newline self.\_st{[}i{]} = False \newline self.\_l.release() \newline return (t{[}0{]}, t{[}1{]}, t{[}2{]}) \newline \newline def releaseforks(self, trip): \newline self.\_l.acquire() \newline for i in trip: \newline self.\_st{[}i{]} = True \newline self.\_c.notify() \newline self.\_l.release()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{cache}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class CacheServ: \newline \_vars = \{\} \newline \newline def \_\_init\_\_(self, ipport): \newline self.\_l = Lock() \newline self.sock = socket(AF\_INET, SOCK\_STREAM) \newline self.sock.bind(('', ipport)) \newline self.sock.listen(1) \newline \newline def set(self, var, value): \# set var in dict to value \newline self.\_l.acquire() \newline self.\_vars{[}var{]} = value \newline self.\_l.release() \newline \newline def get(self, var): \# get var from dict \newline self.\_l.acquire() \newline v = self.\_vars{[}var{]} \newline self.\_l.release() \newline return v \newline \newline def varl(self): \newline self.\_l.acquire() \newline v = ' \seqsplit{'.join(self.\_vars.keys())} \newline self.\_l.release() \newline return v \newline \newline def start(self): \# start accepting connections \newline c = self.sock.accept() \newline while c: \# main loop, create a thread here \newline a = Agent(self, c) \newline a.start() \newline c = self.sock.accept()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{zigzag}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def zigzag(a): \newline (x, y, incr) = (0, -1, 1) \newline n = len(a.\_m) \newline while True: \newline y += incr \newline if y \textgreater{}= n: \newline x += 1 \newline y = n - 1 \newline incr = -1 \newline elif y \textless{} 0: \newline x += 1 \newline y = 0 \newline incr = 1 \newline if x \textgreater{}= n: \newline break \newline yield a.get(x, y)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{regex}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{from re import {\emph{ \newline s = '144::122::71::2::3' \newline print findall('{[}0-9{]}+', s) \newline \newline s = '144::122::71::2' \newline print sub('({[}0-9{]}+)::({[}0-9{]}+)::({[}0-9{]}+)::({[}0-9{]}+)', '\textbackslash{}g\textless{}4\textgreater{}.\textbackslash{}g\textless{}3\textgreater{}.\textbackslash{}g\textless{}2\textgreater{}.\textbackslash{}g\textless{}1\textgreater{}', s) \newline \newline s = \seqsplit{'/home/onur/498/2011/prj/project}.py' \newline print sub('/{[}\textasciicircum{}/{]}}}\$', '', s).split('/'){[}1:{]} \newline print s.split('/'){[}1:-1{]} \newline print findall('{[}\textasciicircum{}/{]}+', sub('/{[}\textasciicircum{}/{]}*\$', '', s)) \newline print sub('\textasciicircum{}.*/({[}\textasciicircum{}/{]}+)\$', '\textbackslash{}g\textless{}1\textgreater{}', s) \newline \newline s = '78.165.170.147 - - {[}19/Apr/2011:12:36:52 +0300{]} "GET remindersheet.pdf HTTP/1.1" 304 181' \newline \newline \newline print sub('\textasciicircum{}({[}\textasciicircum{} {]}+) {[}\textasciicircum{}:{]}+:({[}0-9{]}+:{[}0-9{]}+:{[}0-9{]}+) .*GET ({[}\textasciicircum{} {]}+) .*\$', \newline 'IP=\textbackslash{}g\textless{}1\textgreater{} F=\textbackslash{}g\textless{}3\textgreater{} T=\textbackslash{}g\textless{}2\textgreater{}', s)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{agent}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Agent(Thread): \newline def \_\_init\_\_(self, serv, sock): \newline \seqsplit{Thread.\_\_init\_\_(self)} \newline self.\_s = serv \newline self.sock = sock{[}0{]} \newline def run(self): \newline print self.sock \newline l = self.sock.recv(1024) \newline while l: \newline c = l.strip(' \textbackslash{}n').split(' ') \newline if c{[}0{]} == 'get': \newline self.sock.send(serv.get(c{[}1{]}) + '\textbackslash{}n') \newline elif c{[}0{]} == 'set': \newline serv.set(c{[}1{]}, c{[}2{]}) \newline elif c{[}0{]} == 'vars': \newline \seqsplit{self.sock.send(serv.varl()} + '\textbackslash{}n') \newline l = self.sock.recv(1024)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{proxy cache}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class ProxyCache: \newline \newline def \_\_init\_\_(self, addr): \newline self.sock = socket(AF\_INET, SOCK\_STREAM) \newline self.sock.connect(addr) \newline self.\_obs = {[}{]} \newline \newline def get(self, var): \newline self.sock.send('get ' + var + '\textbackslash{}n') \newline return self.sock.recv(1024).strip('\textbackslash{}n') \newline \newline def set(self, var, val): \newline self.sock.send(' '.join({[}'set', var, val{]}) + '\textbackslash{}n') \newline self.notify() \newline \newline def varl(self): \newline self.sock.send('vars\textbackslash{}n') \newline return self.sock.recv(1024).strip('\textbackslash{}n').split(' ') \newline \newline def notify(self): \newline for obj in self.\_obs: \newline obj.update(self) \newline \newline def register(self, obj): \newline if not (obj in self.\_obs): \newline self.\_obs.append(obj) \newline \newline def unregister(self, obj): \newline self.\_obs.delete(obj)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}