Skip to content

Instantly share code, notes, and snippets.

View bytao7mao's full-sized avatar
🐢
Being lazy

Marius Nicolae bytao7mao

🐢
Being lazy
View GitHub Profile
@bytao7mao
bytao7mao / Pacman.java
Created July 25, 2017 11:37 — forked from hanksudo/Pacman.java
[Java] Simple Pacman
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import javax.swing.ImageIcon;
function slasher(arr, howMany) {
// Return string after the amount chopped off.
return arr.slice(howMany);
}
slasher([1, 2, 3], 1);
1.
public means that the method is visible and can be called from other objects of other types. Other alternatives are private, protected, package and package-private. See here for more details.
static means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class.
void means that the method has no return value. If the method returned an int you would write int instead of void.
--------------------------------------------------------------------------
2.The precedence of each Boolean operator is as follows:
! is evaluated first
&& is evaluated second
|| is evaluated third
function palindrome(str) {
if (!isNaN(str)) { str = str.toString(); }
var re = /[\W_]/g;
var lowRegStr = str.toLowerCase().replace(re, '');
var reverseStr = lowRegStr.split('').reverse().join('');
return reverseStr === lowRegStr;
}
palindrome("11222211");
@bytao7mao
bytao7mao / common divisor
Last active August 7, 2017 04:30
JS how to find the greatest common divisor
function div(a, b) {
if (!b) //!b can be replaced with b===0
{ return a; }
return div(b, a % b);
};
@bytao7mao
bytao7mao / repeat function
Last active August 7, 2017 12:26
repeat function
function repeat(str, num) {
return str.repeat(num);
}
repeat("abc ", 3);
method #1:
public static void main(String[] args) {
int[] a = { 1, 4, 3, 5, 2 };
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
method #2:
public static int[] sortArray(int[] nonSortedArray) {
int[] sortedArray = new int[nonSortedArray.length];
@bytao7mao
bytao7mao / Insertion Sort
Created August 11, 2017 04:19
Insertion Sort
Insertion Sort
Suppose A is an array of N values. We want to sort A in ascending order.
Insertion Sort is an algorithm to do this as follows: We traverse the array and insert each element into the sorted part of the list where it belongs. This usually involves pushing down the larger elements in the sorted part.
For I = 1 to N-1
J = I
Do while (J > 0) and (A(J) < A(J - 1)
Temp = A(J)
@bytao7mao
bytao7mao / Bubble Sort
Created August 11, 2017 04:19
Bubble Sort
Bubble Sort
Suppose A is an array of N values. We want to sort A in ascending order.
Bubble Sort is a simple-minded algorithm based on the idea that we look at the list, and wherever we find two consecutive elements out of order, we swap them. We do this as follows: We repeatedly traverse the unsorted part of the array, comparing consecutive elements, and we interchange them when they are out of order. The name of the algorithm refers to the fact that the largest element "sinks" to the bottom and the smaller elements "float" to the top.
For I = 0 to N - 2
For J = 0 to N - 2
If (A(J) > A(J + 1)
Temp = A(J)
@bytao7mao
bytao7mao / pseudocod ascending
Created August 11, 2017 04:18
pseudocod ascending order
Selection Sort
Suppose A is an array of N values. We want to sort A in ascending order. That is, A[0] should be the smallest and A[N-1] should be the largest.
The idea of Selection Sort is that we repeatedly find the smallest element in the unsorted part of the array and swap it with the first element in the unsorted part of the array.
For I = 0 to N-1 do:
Smallsub = I
For J = I + 1 to N-1 do:
If A(J) < A(Smallsub)