Skip to content

Instantly share code, notes, and snippets.

View davidvthecoder's full-sized avatar

David davidvthecoder

View GitHub Profile
@davidvthecoder
davidvthecoder / gist:5bdd6bfacce20de6134ae6301a754c9d
Created December 3, 2018 22:51
simple inline logger for magento 2 to do simple testing on things that cant dump to page.
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/logfile.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('message/var to log');
@davidvthecoder
davidvthecoder / codechallenge.go
Created July 8, 2016 20:53
code challenge fun
package main
import "log"
func main() {
numbers := []int{23, 2, 4, 7, 2, 11}
find := 20 // should return true
// numbers := []int{1, 3, 5, 23, 2}
// find := 8 // should return true
//numbers := []int{1, 3, 5, 23, 2}
@davidvthecoder
davidvthecoder / round.go
Created April 9, 2014 19:58
Arggh Golang does not include a round function in the standard math package. So I wrote a quick one.
package main
import (
"log"
"math"
)
func Round(val float64, roundOn float64, places int ) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
@davidvthecoder
davidvthecoder / FinderExample.Go
Created November 13, 2013 21:36
Find the first element in an array that matches the search criteria.
package main
import (
"reflect"
"log"
)
type Part struct {
Id int
UPC string
@davidvthecoder
davidvthecoder / sortBasedOnStructsProperty.go
Created October 15, 2013 15:27
Sorting a list of structs/objects based on the object's properties.
package main
import (
"fmt"
"sort"
)
type Person struct {
Name string
Weight int
@davidvthecoder
davidvthecoder / CSVwriterExample
Created October 9, 2013 18:32
Golang CSV Writer Example for a Web Application - Warning No error handling, just the basic concept.
import (
"../../web"
"bytes"
"encoding/csv"
}
func GenerateCSV(ctx *web.Context, args ...string) {
record := []string{"test1", "test2", "test3"} // just some test data to use for the wr.Writer() method below.
@davidvthecoder
davidvthecoder / FindRanges.cs
Created September 9, 2013 19:28
Handy Non-recursive way to find the contiguous natural number ranges given a list of numbers. For example: A list of numbers 1,2,3,6,7,8,9,14,15,22 the contiguous ranges would be: 1-3, 6-9, 14-15, 22-22 Potential issues: since it finds all the missing Numbers, ranges with huge gaps of missing numbers can hinder performance.
public List<Range> findRanges(List<int> numbers) {
List<Range> ranges = new List<Range>(); // create a list of ranges that will eventually be populated.
numbers = numbers.OrderBy(x=>x).ToList<int>(); // order the list of numbers coming in.
int min = numbers.Min(); // get the smallest number
int max = numbers.Max(); // get the largest number
List<int> missingNumbers = new List<int>(); // create an empty list of missing numbers
int i = min; // create a counter for stepping through all the numbers starting with the smallest number
while (i < max) { // step through each possible number from min to max.
if (!numbers.Contains(i)) { // if the number doesnt exist from our list, its a missing number
// found a gap in the number (missing number)
@davidvthecoder
davidvthecoder / Happpi_Getting_Started.php
Created November 1, 2012 17:20 — forked from curt-labs/Happpi_Getting_Started.php
Happpi PHP Library Examples - Examples on how to use the CURT PHP Library known as Happpi.
<?php
require_once('libraries/happpi/LoadAll.php'); // points to the LoadAll.php file for loading the library.
?>
<!doctype html>
<html>
<head>
</head>
<body>
@davidvthecoder
davidvthecoder / Happpi_Getting_Started.php
Created November 1, 2012 17:19 — forked from curt-labs/Happpi_Getting_Started.php
Happpi PHP Library Examples - Examples on how to use the CURT PHP Library known as Happpi.
<?php
require_once('libraries/happpi/LoadAll.php'); // points to the LoadAll.php file for loading the library.
?>
<!doctype html>
<html>
<head>
</head>
<body>
@davidvthecoder
davidvthecoder / example output
Created October 19, 2012 16:46
Xelement with example output
<Root xmlns="http://www.adventure-works.com">
<Child3>1</Child3>
<Child3>2</Child3>
<Child3>3</Child3>
<Child4>4</Child4>
<Child5>5</Child5>
<Child5>6</Child5>
</Root>