Skip to content

Instantly share code, notes, and snippets.

View vicsstar's full-sized avatar

Victor Igbokwe vicsstar

View GitHub Profile
@vicsstar
vicsstar / StringFinder.scala
Last active November 6, 2015 00:13
DZone :: Code Challenge Series: Substring Search
object StringFinder {
/**
* Finds the first occurrence of a substring (`query`) within a `text`.
* @param text - the text to search in.
* @param query - the substring to find in `text`.
* @param pos - position in text to start searching from.
*/
def find(text: String, query: String)(pos: Int): Unit = {
/*
* make sure `pos` hasn't gone out
using System.Collections;
using System.IO;
class FileListings {
public void ListFiles() {
var directory = new System.IO.DriveInfo(@"C:\").RootDirectory;
var folders = new Stack<DirectoryInfo>();
var files = new Stack<FileInfo>();
@vicsstar
vicsstar / ordering-and-rankings.js
Last active June 23, 2018 23:28
Ordering and Rankings
function inRankingOrder(arr) {
return arr.sort((obj1, obj2) => {
const ranking1 = obj1.ranking,
ranking2 = obj2.ranking;
return ranking1 < ranking2 ? -1 : (ranking1 > ranking2 ? 1 : 0);
});
}
function averageRanking(arr) {
const sum = arr.map(obj => obj.ranking).
@vicsstar
vicsstar / Depreciation.java
Created February 8, 2018 00:56
Calculates depreciation value based on a cost and depreciation year.
import java.util.Scanner;
public class Depreciation {
private static Scanner scanner = new Scanner(System.in);
private static Column[] columns = {
new Column("Year", " "),
new Column("Start Value", " "),
new Column("Amt. Depreciated", " "),
new Column("Total Depreciation", "")
};
@vicsstar
vicsstar / ListyMain.scala
Last active December 5, 2018 00:10
Basic scala list-like implementation.
object Main {
type T = Any
trait Listy[T] {
val value: T
def isEmpty: Boolean
def head: Option[T]
def tail: Listy[T]
def filter(predicate: T => Boolean): Listy[T] = {
if (predicate(value)) ListyImpl(value, tail.filter(predicate)) else tail
}
@vicsstar
vicsstar / add_underscore_rule.js
Last active June 23, 2018 23:26
Tiny script that suffixes an underscore and number to a list of numbers, based on their index in their own group of equal numbers.
/*
* Replace the contents of the "string" variable with the actual input numbers you have.
* Make sure you space them, with each number on its own line.
*/
const numbers = `
2
1
2
3
1
@vicsstar
vicsstar / ReverseSentence.scala
Created June 23, 2018 23:15
Reverses a phrases/sentences without reversing the characters in the words.
object ReverseSentence extends App {
if (args == null || args.length == 0) {
printf("%d arguments found; 2 arguments required.", Option(args).map(_.length).getOrElse(0))
sys.exit(0);
}
if (args(0) == args(1)) {
println("The string parameters are equal.")
sys.exit(0)
}
@vicsstar
vicsstar / simple-tree.js
Created December 4, 2018 23:51
Implementation of a simple Tree data structure - tested with the common DOM structure
function createNode(key) {
const children = [];
const attributes = {};
return {
key,
children,
attributes,
addChild(key) {
const child = createNode(key);
@vicsstar
vicsstar / QuickSortNoSideEffect.scala
Last active January 4, 2019 14:04
Simple QuickSort functions without side-effects in JavaScript and Scala #just-for-fun The tricks to make it "stateless", makes it slower - the algorithm becomes useless, almost, in the context of "quick sort" for large to really large lists. Little optimisation (with filtering & zipping). Then again, it's just for fun....
object QuickSort {
def sort(list: Seq[Int]): Seq[Int] = {
if (list.length < 2) list
else {
val midIndex = list.length / 2
val mid = list(midIndex)
val result = list.zipWithIndex.filter(_._2 != midIndex)
.partition(n => n._1 < mid)
result match {
@vicsstar
vicsstar / flames.js
Last active May 21, 2022 13:40
FLAMES game, written in JavaScript - I liked the logic behind the game and wanted to represent it in code
// flames.js
// FLAMES game. Algorithm can be found here: http://flamesgame.appspot.com/algorithm
const getFlame = (name1, name2) =>
name1.toLowerCase().split(''). // take all characters in "name1", to compare
filter(c => name2.toLowerCase().includes(c)). // keep only characters (in "name1") also found in "name2"
reduce((result, c) => [
result[0].replace(new RegExp(`${c}`, 'g'), ''), // replace common characters in "name1"
result[1].replace(new RegExp(`${c}`, 'g'), '') // replace common characters in "name2"
], [name1.toLowerCase(), name2.toLowerCase()]).