\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{bigocb} \pdfinfo{ /Title (coding-python.pdf) /Creator (Cheatography) /Author (bigocb) /Subject (Coding (Python) 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}{A36560} \definecolor{LightBackground}{HTML}{F9F5F5} \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{Coding (Python) Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{bigocb} via \textcolor{DarkBackground}{\uline{cheatography.com/97523/cs/20901/}}} \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}bigocb \\ \uline{cheatography.com/bigocb} \\ \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 25th October, 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*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Loops}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{FOR}} \newline list = {[}"geeks", "for", "geeks"{]} \newline for index in range(len(list)): \newline print list{[}index{]} \newline \newline list = {[}"geeks", "for", "geeks"{]} \newline for index in range(len(list)): \newline print list{[}index{]} \newline else: \newline print "Inside Else Block" \newline \newline {\bf{WHILE}} \newline while (count \textless{} 3): \newline count = count + 1 \newline print("Hello Geek")} \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}{Strings}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Count number of occurrences of x in str}} \newline {\emph{Use counter to create a collection of letters. Pull number where str matches. Also useful in anagram}} \newline \newline (Py) count = collections.Counter(str) \newline \newline {\bf{Reverse str}} \newline {\emph{Useful in many problems including valid palindrome and plain reverse string}} \newline \newline (Py) revs = str{[}::-1{]} \newline (Py) revs = str.reverse \newline \newline {\bf{Find first unique character}} \newline {\emph{First create a collection of letters. THEN use enumerate() in for loop to find index of character with first 1}} \newline \newline (Py) count = collections.Counter(str) \newline (Py) for idx,i in enumerate(str)} \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}{Lists}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{List = {[}{]} \newline \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline \# Addition of Elements \newline \# in the List \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline List.append(1) \newline List.append(2) \newline List.append(4) \newline \newline for i in range(1, 4): \newline List.append(i) \newline \newline List2 = {[}'For', 'Geeks'{]} \newline List.append(List2) \newline \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline \# accessing a element from the \newline \# list using index number \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline print(List{[}0{]}) \newline print(List{[}2{]}) \newline \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline \# Removing elements from List \newline \# using Remove() method \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline for i in range(1, 5): \newline List.remove(i) \newline \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline \# Removing element at a \newline \# specific location from the \newline \# Set using the pop() method \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline List.pop(2) \newline \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline \# Print elements from a \newline \# pre-defined point to end \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline Sliced\_List = List{[}5:{]}} \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}{Intersection}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution(object): \newline def intersect(self, nums1, nums2): \newline from collections import Counter \newline c1 = Counter(nums1) \newline c2 = Counter(nums2) \newline return \seqsplit{list((c1\&c2).elements())}} \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}{Valid Anagram}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution(object): \newline def isAnagram(self, s, t): \newline \newline sColl = collections.Counter(s) \newline tColl = collections.Counter(t) \newline \newline if (sColl == tColl): \newline return True \newline else: \newline return False} \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}{Valid Palendrone}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution(object): \newline def isPalindrome(self, s): \newline cleans = re.sub(r'{[}\textasciicircum{}\textbackslash{}w\textbackslash{}s{]}','',s).replace(" ","").lower() \newline \newline revs = cleans{[}::-1{]} \newline \newline cleanr = re.sub(r'{[}\textasciicircum{}\textbackslash{}w\textbackslash{}s{]}','',revs).replace(" ","").lower() \newline \newline if(cleanr == cleans): \newline return True \newline else: \newline return False} \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}{Max depth of Tree}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution: \newline def maxDepth(self, root: TreeNode) -\textgreater{} int: \newline if root == None: \newline return 0 \newline \newline left = self.maxDepth(root.left) \newline right = \seqsplit{self.maxDepth(root.right)} \newline return 1 + max(left, right)} \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}{Shuffle an Array}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution: \newline def \_\_init\_\_(self, nums): \newline self.array = nums \newline self.original = list(nums) \newline \newline def reset(self): \newline self.array = self.original \newline self.original = list(self.original) \newline return self.array \newline \newline def shuffle(self): \newline aux = list(self.array) \newline \newline for idx in range(len(self.array)): \newline remove\_idx = \seqsplit{random.randrange(len(aux))} \newline self.array{[}idx{]} = aux.pop(remove\_idx) \newline \newline return self.array} \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}{Shuffle an Array}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution: \newline def \_\_init\_\_(self, nums): \newline self.array = nums \newline self.original = list(nums) \newline \newline def reset(self): \newline self.array = self.original \newline self.original = list(self.original) \newline return self.array \newline \newline def shuffle(self): \newline aux = list(self.array) \newline \newline for idx in range(len(self.array)): \newline remove\_idx = \seqsplit{random.randrange(len(aux))} \newline self.array{[}idx{]} = aux.pop(remove\_idx) \newline \newline return self.array} \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}{Dicts}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{dict = \{'Spammer' : 1\} \newline \newline \# Deleting a key-value pair \newline del dict{[}'Spammer'{]} \newline \newline \# Adding a key-value pair \newline ab{[}'Guido'{]} = 'guido@python.org'} \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}{import from file}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{with open('rules.json') as file: \newline data = json.load(file)} \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}{Write to file}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{f= open("guru99.txt","w+") \newline f.write("This is line") \newline f.close} \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}{Linked List}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Node: \newline def \_\_init\_\_(self,val): \newline self.val = val \newline self.next = None \newline self.prev = None \newline \newline def traverse(self): \newline node = self \newline while node != None: \newline print node.val \newline node = node.next \newline \newline n1 = Node(12) \newline n2 = Node(3) \newline \newline n1.next = n2 \newline n2.prev = n1} \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}{Remove Dupes from Array}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def removeDuplicates(self, nums): \newline for x in range(len(nums)-1,0,-1): \newline if nums{[}x{]} == nums{[}x-1{]}: \newline nums.remove(nums{[}x{]}) \newline print nums} \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}{Best time to Buy and Sell}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution(object): \newline def maxProfit(self, prices): \newline \newline n = len(prices)-1 \newline if n \textless{}= 1: \newline print 0 \newline maxprofit = 0 \newline \newline for i in range(0, n): \newline t = prices{[}i{]} \newline m = prices{[}i+1{]} \newline if t \textless{} m: \newline maxprofit += m-t \newline i++1 \newline return maxprofit} \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}{Contains Dupe}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution(object): \newline def containsDuplicate(self, nums): \newline nSorted = sorted(nums) \newline ans = False \newline l = len(nSorted) \newline \newline if len(nSorted) \textgreater{} 1: \newline for i in range(0,len(nums)): \newline if nSorted{[}i{]} == nSorted{[}i-1{]}: \newline ans = True \newline return ans} \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}{Find non dup number}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution(object): \newline def singleNumber(self, nums): \newline \newline nSort = sorted(nums) \newline \newline if(len(nums) \textgreater{} 1): \newline \newline for i in range(0,len(nSort)): \newline if nSort{[}i{]} == nSort{[}i-1{]}: \newline nums.remove(nSort{[}i{]}) \newline nums.remove(nSort{[}i-1{]}) \newline \newline return nums{[}0{]}} \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}{Tree}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\# Definition for a binary tree node. \newline class TreeNode(object): \newline def \_\_init\_\_(self, x): \newline self.val = x \newline self.left = None \newline self.right = None} \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}{Shuffle an Array}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution: \newline def \_\_init\_\_(self, nums): \newline self.array = nums \newline self.original = list(nums) \newline \newline def reset(self): \newline self.array = self.original \newline self.original = list(self.original) \newline return self.array \newline \newline def shuffle(self): \newline aux = list(self.array) \newline \newline for idx in range(len(self.array)): \newline remove\_idx = \seqsplit{random.randrange(len(aux))} \newline self.array{[}idx{]} = aux.pop(remove\_idx) \newline \newline return self.array} \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}{Merge Sorted Arrays}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution(object): \newline def merge(self, nums1, m, nums2, n): \newline n1len = len(nums1) \newline n2len = len(nums2) \newline \newline if n1len \textgreater{} n2len: \newline \newline while n1len != m: \newline t = nums1{[}m{]} \newline nums1.remove(t) \newline n1len -=1 \newline \newline elif n2len \textgreater{} n1len: \newline \newline while n2len != m: \newline t = nums2{[}m{]} \newline nums2.remove(t) \newline n2len -=1 \newline \newline num1j = nums1 + nums2 \newline \newline nums1 = sorted(num1j) \newline \newline return nums1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}