Last active
May 6, 2020 19:01
-
-
Save johnifegwu/8d4ef16af3a721674995c9979ca53217 to your computer and use it in GitHub Desktop.
Answers to Questions 1, 2 and 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
import java.util.*; | |
import java.text.CharacterIterator; | |
//Author: John Ifegwu | |
public class Program | |
{ | |
// Determine if set2 is a subset of set1 with Computational Complexity of O(n^ 2) | |
public static boolean isSubset(char[] set1, char[] set2) | |
{ | |
int n = set1.length; | |
boolean result = false; | |
// Run a loop to find all 2^n subsets. | |
for (int i = 0; i < (1<<n); i++) | |
{ | |
ArrayList<Character> subSet1 = new ArrayList<Character>(); | |
// Get the subsets | |
for (int j = 0; j < n; j++) | |
{ | |
if ((i & (1 << j)) > 0) | |
{ | |
subSet1.add(set1[j]); | |
} | |
} | |
//Compare result to determine if set2 is a subset of set1 | |
char[] cR = toArray(subSet1); | |
result = compareSet(cR, set2); | |
//Check to see if a subset has been found. | |
if (result) | |
{ | |
//Stop searching | |
break; | |
} | |
} | |
return result; | |
} | |
public static char[] toArray(List<Character> list){ | |
char[] toReturn = new char[list.size()]; | |
int i = 0; | |
for(char c : list) | |
{ | |
toReturn[i ++] = c; | |
} | |
return toReturn; | |
} | |
public static boolean compareSet(char[] set1, char[] set2) | |
{ | |
// Equality count. | |
int eqCount = 0; | |
if (set1.length == set2.length) | |
{ | |
for (int x = 0; x < set1.length; x++) | |
{ | |
if (set1[x] == set2[x]) | |
{ | |
eqCount ++; | |
} | |
} | |
} | |
if ((eqCount > 0) && (eqCount == set1.length) && (eqCount == set2.length)) | |
{ | |
return true; | |
}else | |
{ | |
return false; | |
} | |
} | |
public static void main(String[] args) | |
{ | |
char[] set1 = new char[]{'a', 'b', 'c', 'd'}; | |
char[] set2 = new char[]{'b', 'c', 'd'}; | |
char[] set3 = new char[]{'a', 'x', 'c'}; | |
String str = ""; | |
str = Boolean.toString(isSubset(set1, set2)); | |
System.out.println(str); | |
str = Boolean.toString(isSubset(set1, set3)); | |
System.out.println(str); | |
} | |
} |
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.sql.Timestamp; | |
import java.util.*; | |
import java.lang.Math; | |
//Author: John Ifegwu | |
class Node { | |
int key; | |
int value; | |
Timestamp lastAccessTime; | |
double weight; | |
public Node(int key, int value, double weight) | |
{ | |
this.key = key; | |
this.value = value; | |
this.weight = weight; | |
// currentTimeStamp from system | |
setAccessTime(); | |
} | |
public void setAccessTime() | |
{ | |
//Date object | |
Date date= new Date(); | |
//getTime() returns current time in milliseconds | |
long time = date.getTime(); | |
//Passed the milliseconds to constructor of Timestamp class | |
Timestamp ts = new Timestamp(time); | |
// currentTimeStamp from system | |
this.lastAccessTime = ts; | |
} | |
public double getScore() | |
{ | |
//Date object | |
Date date= new Date(); | |
//getTime() returns current time in milliseconds | |
long time = date.getTime(); | |
//Passed the milliseconds to constructor of Timestamp class | |
Timestamp ts = new Timestamp(time); | |
if (ts != this.lastAccessTime) | |
{ | |
return this.weight / Math.log(this.lastAccessTime.getTime() - ts.getTime()); | |
}else | |
{ | |
return this.weight / -100; | |
} | |
} | |
} | |
class LRUCache { | |
private HashMap<Integer, Node> map; | |
private int capicity, count; | |
public LRUCache(int capacity) | |
{ | |
this.capicity = capacity; | |
map = new HashMap<>(); | |
count = 0; | |
} | |
//Removes excesses | |
public void deleteExcess() | |
{ | |
map.remove(getLeastAccessedKey()); | |
} | |
// Gets the least accessed key by score. | |
int getLeastAccessedKey() | |
{ | |
double tempScore = 0; | |
double score = 0; | |
int key = 0; | |
for (Node n : map.values()) | |
{ | |
tempScore = n.getScore(); | |
if (score == 0) | |
{ | |
score = tempScore; | |
key = n.key; | |
}else | |
{ | |
if (score > tempScore) | |
{ | |
score = tempScore; | |
key = n.key; | |
} | |
} | |
} | |
return key; | |
} | |
// This method works in O(1) | |
public int get(int key) | |
{ | |
if (map.get(key) != null) { | |
Node node = map.get(key); | |
//Update access time. | |
node.setAccessTime(); | |
int result = node.value; | |
System.out.println("Got the value : " + | |
result + " for the key: " + key); | |
return result; | |
} | |
else | |
{ | |
System.out.println(""); | |
System.out.println("Did not get any value" + | |
" for the key: " + key); | |
return -1; | |
} | |
} | |
// This method works in O(1) | |
public void put(int key, int value, double weight) | |
{ | |
//Update cache if key exist. | |
//Otherwise add new and delete excesses if needs be. | |
System.out.println(""); | |
System.out.println("Going to set the (key, "+ | |
"value, weight) : (" + key + ", " + value + ", " + weight + ")"); | |
if (map.get(key) != null) { | |
Node node = map.get(key); | |
node.value = value; | |
node.weight = weight; | |
//Update access time. | |
node.setAccessTime(); | |
} | |
else | |
{ | |
Node node = new Node(key, value, weight); | |
map.put(key, node); | |
if (count < capicity) { | |
count++; | |
} | |
else | |
{ | |
//Remove excesses | |
deleteExcess(); | |
} | |
} | |
} | |
} | |
public class lunchLRUCache { | |
public static void main(String[] args) | |
{ | |
System.out.println(""); | |
System.out.println("Testing the LRU Cache Implementation"); | |
LRUCache cache = new LRUCache(2); | |
// We will store a key (1) with value 100 in the cache. | |
cache.put(1, 100, 1000); | |
// We will store a key (2) with value 200 in the cache. | |
cache.put(2, 200, 1000); | |
System.out.println(""); | |
System.out.println("Value for the key: 1 is " + cache.get(1)); // returns 100 | |
// We will store a key (5) with value 500 in the cache. | |
// this will remove key 1 from the cache. | |
cache.put(5, 500, 1000); | |
System.out.println(""); | |
System.out.println("Value for the key: 1 is " + cache.get(1)); // returns -1 (not found) | |
// We will store a key (3) with value 300 in the cache. | |
// this will remove key 2 from the cache. | |
cache.put(3, 300, 1000); | |
System.out.println("Value for the key: 2 is " + cache.get(2)); // returns -1 (not found) | |
System.out.println(""); | |
System.out.println("Value for the key: 5 is " + cache.get(5)); // returns 500 | |
System.out.println("Value for the key: 3 is " + cache.get(3)); // return 300 | |
} | |
} |
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
//Author: John Ifegwu | |
function createArrayOfFunctions(y) { | |
var arr = []; | |
for(var i = 0; i<y; i++) { arr[i] = function(x) { return x + i; } } | |
return arr; | |
} | |
/*In Line 2, the variable arr was not properly declared, it should be declared as shown hereunder:*/ | |
// var arr = new Array(); | |
//Therefor the above function should be rewriten as stipulated within function createArrayOfFunctions2. | |
function createArrayOfyFunctions2(y) { | |
var arr = new Array(); | |
for(var i = 0; i<y; i++) { arr[i] = function(x) { return x + i; } } | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment