Skip to content

Instantly share code, notes, and snippets.

View CodyEngel's full-sized avatar

Cody Engel CodyEngel

View GitHub Profile
@CodyEngel
CodyEngel / getParameters.cs
Created August 4, 2014 18:02
Return All Non-Null Parameters As Formatted String
private String getParameters<T>(T obj)
{
StringBuilder parameters = new StringBuilder();
var properties = typeof(T).GetProperties();
foreach(Property property in properties)
{
if(property.GetValue(obj, null) != null)
{
String propertyValue = property.GetValue(obj, null).ToString();
String propertyName = property.Name.ToString();
@CodyEngel
CodyEngel / GetChildNodes.sql
Created January 27, 2015 20:21
Get All Child Nodes Given A Parent
DECLARE @TopID_in int -- The top level we want to resolve children for
SELECT @TopID_in = -- ID of parent node
;WITH HierarchyCTE(ID, ParentID) AS
(
SELECT Id, ParentId
FROM Categories
WHERE Id = @TopID_in
UNION ALL
@CodyEngel
CodyEngel / GetParentNodes.sql
Created January 27, 2015 20:22
Find Parent Nodes Of Child
DECLARE @ID int
SET @ID = -- ID of child node
;WITH cte AS(
SELECT ParentId AS pid, 1 AS lvl
FROM Categories
WHERE Id = @ID
UNION ALL
SELECT a.ParentId as pid, lvl+1
FROM Categories a
@CodyEngel
CodyEngel / RandomDateTime.sql
Created January 28, 2015 19:24
Random date time with the year and month hard coded.
SELECT '2015-01-'
+ RIGHT('00' + CAST(
ROUND(((28 - 1 -1) * RAND() + 1), 0)
AS varchar), 2)
+ ' '
+ RIGHT('00' + CAST(
ROUND(((24 - 0 -0) * RAND() + 0), 0)
AS varchar), 2) -- Hour
+ ':'
+ RIGHT('00' + CAST(
@CodyEngel
CodyEngel / gist:3591132fff4998d904e6
Last active August 29, 2015 14:16
Set Duplicate Items to 0
int[] array = {2, 3, 5, 5, 7, 11, 11, 11, 13};
int originalNumber = array[0];
for (int i = 1; i < array.Length; i++)
{
if (array[i] == originalNumber)
{
array[i] = 0;
}
else
{
@CodyEngel
CodyEngel / gist:98f8c5ecb7e3f35871b0
Created March 7, 2015 23:21
Remove duplicate items from array, solution from Elements of Programming Interviews
public static int removeDuplicates(int[] A) {
if (A.length == 0) {
return 0;
}
int writeIndex = 0;
for (int i = 1; i < A.length; ++i) {
if (A[writeIndex] != A[i]) {
A[++writeIndex] = A[i];
}
@CodyEngel
CodyEngel / gist:13b01dbc9b91f67a5fe3
Created March 7, 2015 23:22
Remove duplicate items from array, final answer
public static int[] removeDuplicates(int[] A)
{
int writeIndex = 0;
for (int i = 1; i < A.Length; i++)
{
if (A[writeIndex] != A[i])
{
writeIndex++;
A[writeIndex] = A[i];
}
@CodyEngel
CodyEngel / generatePrimesFast.cs
Last active August 29, 2015 14:16
Generate Primes - Fast
public static List<int> returnPrimeNumbersToN(int n)
{
List<int> primeNumbers = new List<int>();
primeNumbers.Add(2);
primeNumbers.Add(3);
for (int i = 5; i <= n; i += 2)
{
bool test = true;
int sqrt = Convert.ToInt32(Math.Sqrt(i));
@CodyEngel
CodyEngel / generatePrimesBruteForce.cs
Last active August 29, 2015 14:16
Generate Primes - Brute Force
public static List<int> returnPrimeNumbersToN(int n)
{
List<int> primeNumbers = new List<int>();
// populate list
for (int i = 2; i <= n; i++)
{
primeNumbers.Add(i);
}
@CodyEngel
CodyEngel / NavigationItem.java
Created July 19, 2015 23:38
Android ListView Implementation Example
public class NavigationItem {
Drawable mIcon;
FragmentTransaction mFragmentTransaction;
String mTitle;
public NavigationItem(String title, Drawable icon, FragmentTransaction fragmentTransaction) {
mTitle = title;
mIcon = icon;
mFragmentTransaction = fragmentTransaction;