CSCI127 Cheat Sheet
CSCI127 Cheat Sheet
● pwd - prints the directory (ex. pwd -> ● import matplotlib.pyplot as plt
/Users/login/CSci127) ● import pandas as pd
● ls - lists what is in the directory (ex. ls -> thing.png ● pd.read_csv() - reads in the csv file (ex. pop = pd.read_csv(“”))
thing2.py thing3.csv) ● pop.plot() - creates a graph with whatever you want on the x and y (ex.
● ls -l - lists more information about the directory (ex. ls pop.plot(x = “Year”, y = “Bronx”)) Change plot to bar or scatter to change
-l -> drwxr-xr-x 2 stjohn wheel 68 Jan 7 21:00 the look
thomasH/) ● length = len(df) - returns the length of a df
● ls * - lists files with specific features (ex. ls *.py -> ● pop[“”].max() - prints the maximum value
lists anything ending in .py) ● pop[“”].min() - prints the min value
● ls -l | grep “” - searches for patterns (ex. ls -l | grep ● pop[“”].mean() - prints the average value
“Oct” -> lists all files modified in October) ● pop[“”].std() - prints the standard deviation value
● wc -m - counts characters in files ● pop[“”].count() - prints the number of non-NaN values
● wc -w - counts words ● plt.gcf() - gets the graph/image showing (ex. file = plt.gcf)
● wc -1 - counts lines (ex. ls *.html | wc -l -> 3) ● file.savefig(“”) - saves the file
● mkdir - makes a new directory (ex. mkdir cookies) ● groupby(“”) - groups the rows in a specified column (ex. grouped =
● cd - changes directory (ex. cd cookies, pwd -> rain.groupby(“Location”) (In order to get a specific group do get_group(‘’)
/Users/login/CSci127/cookies) ● value_counts()[#] - prints the top/bottom from a column (ex. tickets["Plate
● cd ../, cd .. - goes up one directory ID"].value_counts()[:10 -> prints the 10 highest tickets)
● cd ~ - returns to home directory Turtle
● cp - makes a copy of a file (ex. cp p2.py p3.py) ● import turtle
● mv - renames a file (ex. mv p1.py prog1.py) ● Turtle points to the right without any adjustments
● #!/bin/bash - “shebang” line that tells program we’re ● turtle.Turtle() - initializes a turtle with whatever name
using bash ● turtle.color(“any color”)
● echo - similar to print in Python ● turtle.Screen() - allows for editing the screen [set it to a variable]
● chmod +x name - changes file to be executable ● forward() - move forward a certain value (ex.
● ./name - runs the file in terminal joe.forward(100) -> moves forward 100 steps,
● git clone link - calls programs from the github website applies for the below commands) right() - rotates
● git fetch link - updates the program with any changes clockwise a certain value
Numpy/Pyplot ● left() - rotates counterclockwise a certain value
● import matplotlib.pyplot as plt ● backward() - moves back a certain value
● import numpy as np ● colormode(255) - allows numbers to be adjusted between 0 and 255
● plt.imread() - reads in the image ● color(#,#,#) - edits the color in the corresponding RGB spot
● plt.imshow(img) - loads image into pyplot ● pensize() - changes size of pen
● plt.show() - shows the image ● penup() - lifts the pen so moving the turtle does not draw
● img = np.ones((size, size, 3)) ● pendown() - places the pen down so you can draw
● plt.copy() - copies image (ex. Img2 = img.copy() Random
● img[r,c,chan] - r is row, c is column, chan is color ● random.randrange(start, end, step) - choose a random between your start and
channel end (ex. randrange(10, 101, 10) -> prints one number randomly from
● img2[:,:,0] = 0 - sets red channel of entire image to 0 10,20,30…100)
● img2[:,:,1] = 0 - sets green channel of entire image to 0 ● random.random() - returns a random float value between 0 and 1
● img2[:,:,2] = 0 - sets blue channel of entire image to 0 Misc.
● imsave(file, name) - saves the image ● Formal parameters = def example (x, y):
● np.loadtxt() - loads txt file into array ● Actual parameters = example(3, 10)
● np.zeros(#, #, 3) - creates an all black image of MIPS
whatever size, can also be used on a existing shape ● Printing characters
● np.ones(#, #, 3) - creates an all white image ○ ADDI $sp, $sp, x - Adds x to the stack (including null)
● [r::[], c::[], 3] -> Skips rows and columns by the end ○ ADDI $t0, $zero, x - Adds a value, x, to t0 for storage
amount ○ SB $t0, x($sp) - Stores byte, and offsets by x (Increment for each new
● height = img.shape[0] - Compute the height of the img value)
● width = img.shape[1] - Compute the width of the img ○ ADDI $v0, $zero, 4 - 4 is for print string, prints t0
● leftQuarter = img[ : , : width//4 ] - Select left quarter of ○ ADDI $a0, $sp, 0 - Set a0 to stack pointer for printing
the image store in leftQuarter ○ syscall - End of code
● botLeft = img[height//2 :, : width//2] ○ Don’t forget to add null to the end of t0 when done with adding ASCII
● botRight = img[height//2 :, width//2: ] values
● topRight = img[: height//2, width//2 :] ● Loops
● topLeft = img[: height//2, :width//2] ○ ADDI $s0, $zero, x - Sets s0 to value x (Starting value)
● mapShape = elevations.shape + (3,) - Adds 3 channels ○ ADDI $s1, $zero, y - Sets s1 to value y (increment value)
of color to the mapShape ○ ADDI $s2, $zero, z - Sets s2 to value z (end value)
Folium/Pandas ○ AGAIN: - Label for loop
● import folium ■ SUB $s0, $s0, $s1 - Subtracts s1 from s0
● import pandas as pd ■ ADD $s0, $s0, $s1 - Adds s1 to s0
● folium.Map() - creates a world map (ex. ○ BEQ $s0, $zero OR $s2, DONE - Checks if s0 has reached zero or s2
mapWorld = folium.Map(location = [0,0], ○ J AGAIN - Comes after the branch, checks if valid, if not, runs loop
zoom_start=3) -> creates world map again
centered at 0,0 and zoomed) ○ DONE: - Label for end of code
save(outfile=”.html”) - saves the world ● ADDI $sp, $sp, 1 - Think of this as i+=1 in Python
map ● ADDI $t0, $t0, 1 - Adds 1 to the ASCII value of t0
● folium.Marker(location = [], popup = “”) - creates a ● Examples
marker on the map, popup gives the marker a name ○ ADDI $sp, $sp, -16 # Set up stack
● add_to(mapWorld) - adds the marker to the map ○ ADDI $s3, $zero, 1 # Store 1 in a register
● for index,row in cuny.iterrows(): ○ ADDI $t0, $zero, 90 # Set $t0 at 90 (Z)
○ lat = row["Latitude"] ○ ADDI $s2, $zero, 15 # Use to test when you reach 15
○ lon = row["Longitude"] ○ SETUP: SB $t0, 0($sp) # Next letter in $t0
○ name = row["Campus"] ○ ADDI $sp, $sp, 1 # Increment the stack
○ SUB $s2, $s2, $s3 # Decrement the counter by 1
○ newMarker = folium.Marker([lat, lon], ○ SUB $t0, $t0, $s3 # Decrement the letter by 1
popup=name) ○ BEQ $s2, $zero, DONE # Jump to DONE if s2 == 0
○ newMarker.add_to(mapCUNY) ○ J SETUP # Else, jump back to SETUP
The code above goes through each row and sets the ○ DONE: ADDI $t0, $zero, 0 # Null (0) to terminate string
corresponding number to lat, lon, and name. They use these ○ SB $t0, 0($sp) # Add null to stack
variables with .Marker to add it to the map. ○ ADDI $sp, $sp, -15 # Set up stack to print
Strings ○ ADDI $v0, $zero, 4 # 4 is for print string
● split(“”) - splits the string at the character and turns it ○ ADDI $a0, $sp, 0 # Set $a0 to stack pointer
into a list ○ syscall # Print to the log
● upper() - makes the entire string capital ord(“”) - gives
the ASCII value of character
● lower() - makes the entire string lowercase chr(#) -
gives the character corresponding to the ASCII value
● len(“”) - returns length of the string
● count(“”) - counts the number of times a character
appears in a string
● print(x.split()[0]) - in a for loop, print the 0th character
of x while splitting the iterated variable
● print(x, end = “ “) - prints a space at the end of the line
instead of going to a new line (in a for loop)
Hexadecimal
● Hexadecimal is represented by a # followed by 6 digits
(ex. #000000)
● First two digits/letters represent red (ex. FF0000 is
only red)
● Second two digits/letters represent red (ex. 00FF00 is
only green)
● Third two digits/letters represent red (ex. 0000FF is
only blue)
● Hexadecimal goes from 0-9 and right after A-F (ex.
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F Think of A-F as 10-16)
● #000000 is black, #FFFFFF is white
● If 1F, F base = 16^0 * 15, 1 base = 16^1 * 1 Flood Map Example:
Combine F base and 1 base which is 15 + 16 for row in range(mapShape[0]):
● for col in range(mapShape[1]):
if elevations[row,col] <= 20.0 and elevations[row,col] > 5.0:
If all 6 digits are the same regardless if it's a number or floodMap[row,col,:] = 0.5 # Sets to grey
letter, the color is gray. (ex. #AAAAAA, #777777,#808080) elif elevations[row,col] <= 0:
floodMap[row,col,0] = 1.0 # Sets red and green channels to 100%
C++ floodMap[row,col,1] = 1.0
● #include <iostream> - imports libraries to use cout, cin elif elevations[row,col] <= 6:
● using namespace std; - allows to use the functions # Below the storm surge of Hurricane Sandy (flooding likely)
● int main () {} - every program requires this to operate # Set red to 100%
● cout << - prints to the terminal floodMap[row,col,0] = 1.0
● cin >> - takes the users input else:
● for (int i = #; i < #; i++) {} - standard definite loop #Above the 6 foot storm surge and didn't flood
function floodMap[row,col,1] = 1.0 #Set the green channel to 100%
● if (condition) {} - standard if statements Snow Count Example:
● else if (condition) {} for i in range(caOne.shape[0]):
● else {} for j in range(caOne.shape[1]):
● while (condition) {} - standard indefinite loop #Check if red, green, and blue are > t:
● end1 == \n if (caOne[i,j,0] > t) and (caOne[i,j,1] > t) and (caOne[i,j,2] > t):
All lines of code end with ; (ex. cout << “Hello World!”;) countSnowOne = countSnowOne + 1
Both end1 and “\n” signify a new line (ex. cout << “Hello
World” << end1; is the same as cout << “Hello World!\n”;)