This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function slasher(arr, howMany) { | |
// Return string after the amount chopped off. | |
return arr.slice(howMany); | |
} | |
slasher([1, 2, 3], 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function div(a, b) { | |
if (!b) //!b can be replaced with b===0 | |
{ return a; } | |
return div(b, a % b); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function repeat(str, num) { | |
return str.repeat(num); | |
} | |
repeat("abc ", 3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer