Thursday, April 28, 2011

Body for Life meal plan

Read about Body for Life Champion Joshua Sundquist

This was his daily meal plan:
6:00am Meal 1
Multivitamin
1.5 cups of whole grain rolled oats (old fashioned oatmeal) cooked with water and mixed with a scoop of 100% Whey Protein and a teaspoon of Flax Seed Oil (good source of Omega Oils).

7:00–8:00am Workout

8:30am Meal 2
Post-workout meal: Two servings of 100% Whey Protein mixed with a teaspoon of chocolate syrup (this is the only sugar I ate—I like to give my muscles some quick energy for recovery).

11:00am Meal 3
Sandwich made with two slices of 100% whole grain bread, natural peanut butter and two sliced bananas.

2:00pm Meal 4
Grilled chicken breast, brown rice, a serving of raw vegetables

6:00 pm Meal 5
Whole grain pasta, soy protein isolate shake, a serving of raw vegetables

9:00pm Meal 6
Bowl of whole grain cereal (Cheerios®*, Shredded Wheat®*, etc) and 100% Whey Protein shake

12:00pm Meal 7
Natural (unsweetened) applesauce mixed with low-fat cottage cheese, serving of Casein Protein shake

3:00am Bonus nutrition
100% Whey Protein shake



per day serving per week per month per challenge
flax Seed Oil 1 6 24 72
chocolate Syrup 1 6 24 72
100% Whey Protein 4 24 96 288
casein protein 1 6 24 72
unsweetend applesauce 1 6 24 72
chicken breasts 1 6 24 72
raw vegetables 2 12 48 144
100% whole grain bread 2 12 48 144
natural peanut butter 1 6 24 72
bananas 2 12 48 144
oatmeal 1.5 9 36 108
low fat cottage cheese 1 6 24 72
Cheerios 1 6 24 72
Skim Milk 1 6 24 72
Soy Protein Powder 1 6 24 72
Whole grain Pasta 1 6 24 72

Got all that? Great but let me tell you, I gave this plan a shot back in October of 2010, stuck it out for 12 weeks. I actually hated this toward the end, also did not get anywhere on it. I even tried to modify it by reducing portions or eliminating some of the late night meals after not seeing results for 6 weeks. I guess the lesson there is don't do something for 12 weeks if it does not work by 4 weeks. My current weight is under 215lb, I just finished a 12 week challenge at work a "biggest loser" competition, I lost about 20 lbs and 3% body fat. My sister is doing great on an HCG-style program, and so if I gain a single pound back, I am jumping on HCG so hard it might break something. She has lost more weight in 30 days than I lost in 90.

So far 12 Steps to Whole Foods Steps 1-3 are working out for me very consistently (well that along with 6-10 workouts per week.) I do feel great when I workout and eat better, now if I could just get enough sleep. I am surprised that adding two quarts of green smoothies and big salads for lunch and dinner is able to keep me from having hunger pains. I am not vegetarian but I have reduced my meat intake down to a 10th of what it was. Reading the book has made me avoid meat at almost every opportunity, I just see it crawling with diseases and squirming with parasites (or imagine it decaying inside my intestines) and order the vegetarian option.

I also took a running class from Ken Harper who owns the Runners Corner in Orem, that class was great and helped inspire me to make running outside a part of my weekly exercise. I read Born to Run and got the Vibrams Five Fingers which I think is helping to strengthen my weak feet and legs . I am also incorporating weight lifting with a partner from work, supposedly this is a great way to gain muscle and lose fat. The only things I am not doing that I may try are team sports, organized races, and swimming.

Sunday, January 2, 2011

Validating XML against a Schema (XSD)

Notepad++
Notepad++ has an optional plugin to allow you to validate an XML file against an XSD (this is good because it is a free "gratis" tool).

First, download and install Notepad ++ here

Download and add XML tools plugin for Notepad ++ here

Download the ExternalTools.zip as well from the sourceforge link above and extract the contents to the Notepad++ install folder.

Restart Notepad ++

Open XML file to validate, Click Plugins | XML Tools | Validate now

A window will pop up with errors or validation successfully completed message.


jEdit
jEdit also has optional plugin to validate XML against an XSD.

First download jEdit here

Open jEdit and click Plugins | Plugin Manager

Check the box for XML plugin, click install button

Open the XML file, validation errors appear as red underlines.

Also in jEdit you can open Plugins | XML | Parse as XML will open a window and show a tree navigation of the XML jumping to the first error found if any.

cygwin
An optional package to install in cygwin is called libXML2, this package includes a command line tool named xmllint which can also validate XML against an XSD
$ xmllint --schema /cygdrive/c/path/to/xsd/myschema.xsd /cygdrive/c/path/to/xml/myxml.xml

Friday, October 29, 2010

Four Steps

Debt is a form of enslavement, the victim is deceived into breaking a simple rule:
Spend less than you earn.

Spending less than you earn is very similar to another simple rule many struggle with when overweight:
Eat less than you burn.

Health and wealth and happiness are very inter-related. Getting control of your health can help you to get control of other areas of your life. Taking control of my eating habits helped me break away from other bad behaviors and get my financial situation under control.

With any change in life, you start by learning, then setting goals, making a plan to achieve them, and acting on your plan. The last step is critical, never wait to act. So learn what you should learn, get a plan for your goal, and GO!

1 Learn
You have to learn a better way, you can't keep doing the same thing and expect a different outcome. Learn what to do. For example, don't buy a gym membership and never learn how to safely exercise because you may hurt yourself and be worse off than before. Similarly, you should research budget plans or debt elimination strategies and pick one that you firmly believe will work for you, one that people like you have been successful with. This is also a time to get motivated and inspired by others who have been successful at making the change you are making. Finally, write a list of what benefits you will have from making the change (or start with this list), but keep it handy.

2 Goal != Dream
Good goals should be a specific and small, they should be steps on your journey toward your dreams and desires, their should be a deadline or target date for your goal. A dream to be a millionaire is not a goal. Good example of a goal would be to lose 10 lbs by December 2010, or pay off all credit card balances before July 2011.

3 Plan
Very important third step is to plan, be aware of your life and the obstacles you already have to accomplishing your goal, account for those in your plan. Some unexpected obstacles will blindside you, you may get pregnant, you may have a huge unexpected expense, or experience a job loss. Things happen that are out of your control and can impede your progress, expect them to happen but realize they happen to EVERYONE, and realize obstacles happened to people who were still successful with their goals. Decide to be one of those people, the ones that didn't give up or give in to challenges. Use the knowledge you gained to make a detailed plan. Keep in mind that your plan should not be unreasonable, your plan should be actually physically possible for you to do 100% of the time. Your plan should make sense to you, you need to believe that following the plan will work, not just hope that it will.

4 Act
Make a commitment to accomplish your goal with your plan. Think no-matter-what-I-am-doing-this, then as you act, no-matter-what, do it, stay committed. Decide now that you will always decide to go on, then never change your mind about it. You have to be a stubborn, hard-nosed jerk about it, because excess fat and debt are stubborn hard-nosed jerks too. Use the inspiration and motivation from other successful people to support you and help you to avoid falling backwards into addictive behaviors that take you away from your goal. If you look, you can find inspirational stories from people who had challenges accomplishing the very same thing you are trying to accomplish. Keep at it until you see movement, until you see a change you really are proud of, until you reach that goal, then immediately start over with step one and do it again. Don't bask in your awesomeness for a second, because that's a great time for an old behavior to creep back into your life. You achieved something, it felt great, it was great, don't ruin it by undoing that hard work.

Tuesday, September 14, 2010

Tynt programming assignment (Part 5)

Here is my implementation of a Hash collection for Integers:
package com.tynt.app.util;

public class IntegerHash {
public static final int BUFFER_SIZE = 100;

private IntegerList values;
private IntegerList valueCounts;

public IntegerHash() {
values = new IntegerList();
valueCounts = new IntegerList();
}

public boolean hasInteger(Integer value) {
if (value == null) {
return false;
}
for(int i=0;i < values.size();i++) {
if (values.get(i).compareTo(value) == 0) {
return true;
}
}
return false;
}

public Integer getIntegerCount(int index) {
return valueCounts.get(index);
}

public Integer getIntegerIndex(Integer value) {
for(int i=0;i < values.size();i++) {
if (values.get(i).compareTo(value) == 0) {
return i;
}
}
return -1;
}

public void incrementIntegerCount(Integer value) {
int integerIndex = getIntegerIndex(value);
valueCounts.put(integerIndex, getIntegerCount(integerIndex)+1);
}

public void addInteger(Integer value) {
if (!hasInteger(value)) {
values.concatenate(value);
valueCounts.concatenate(1);
} else {
incrementIntegerCount(value);
}
}

public IntegerList getIntegers() {
IntegerList vals = new IntegerList();
for(int i = 0; i < values.size(); i++) {
vals.concatenate(values.get(i));
}
return vals;
}

public Integer getIntegerFrequency(Integer value) {
if (hasInteger(value)) {
int index = getIntegerIndex(value);
return getIntegerCount(index);
} else {
return 0;
}
}
}

As you can see, I re-used the IntegerList collection in the Hash collection. Since I am not sorting the IntegerList, I have to iterate through the entire key IntegerList to check for a key in the hash, with a sorted key IntegerList, I could short circuit after the key was larger than the sorted key it was compared with. I am also not implementing a hash algorithm at all, so the collection was not named very well. Implementing a true hash collection was not part of the assignment so I didn't worry about it.

Here is assignment5 using the above collection:
package com.tynt.app;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.tynt.app.util.*;

public class Assignment5 implements InputReader {

public String readFile(InputStream is) {
BufferedReader d = new BufferedReader(new InputStreamReader(is));
String line;
IntegerHash unsortedHash = new IntegerHash();
try {
line = d.readLine();
while (line != null) {
Integer value = null;
try {
value = new Integer(Integer.parseInt(line));
} catch (NumberFormatException e) {
throw new RuntimeException(e);
}
unsortedHash.addInteger(value);
line = d.readLine();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
StringBuilder sb = new StringBuilder();
IntegerList sortedList = IntegerList.quicksort(unsortedHash.getIntegers());
if (sortedList == null) {
sortedList = new IntegerList();
}
for (int i=0;i < sortedList.size();i++) {
sb.append(sortedList.get(i));
sb.append(" - ");
sb.append(unsortedHash.getIntegerFrequency(sortedList.get(i)));
sb.append(NEW_LINE);
}
return sb.toString();
}

}

Finally, with all of the Assignment objects implemented and the tests passing, I can implement the Driver for the project:
package com.tynt.app;

import java.io.*;
import com.tynt.app.util.*;

/**
* Tynt programming challenge
*
* @author Four Gables Guy
*
*/
public class App {
public static final String header = "********************";

public static void main(String[] args) throws IOException {
String choice = displayChoices();
while (choice.equals("1") || choice.equals("2") || choice.equals("ALL")) {
parseChoice(choice);
if (choice.equals("ALL")) {
break;
}
choice = displayChoices();
}
}

public static void parseChoice(String choice) throws IOException {
if (choice.equals("1")) {
doAssignment2();
} else if (choice.equals("2")) {
doAssignment3();
} else if (choice.equals("ALL")) {
doAssignment2();
doAssignment3();
}
}

public static String displayChoices() {
System.out.println(header);
System.out.println("Enter 1 to parse " + InputReader.INPUT_FILE
+ " and print numbers sorted ascending.");
System.out.println("Enter 2 to parse " + InputReader.INPUT_FILE
+ " and print numbers sorted ascending.");
System.out.println("Enter any other characters to quit.");
System.out.println(header);
Console console = System.console();
if (console == null) {
return "ALL";
} else {
String line = console.readLine();
line = line.trim();
return line;
}
}

public static void doAssignment2() throws IOException {
Assignment4 assignment4 = new Assignment4();
FileInputStream is = new FileInputStream(new File(
InputReader.INPUT_FILE));
System.out.println(assignment4.readFile(is));
}

public static void doAssignment3() throws IOException {
Assignment5 assignment5 = new Assignment5();
FileInputStream is = new FileInputStream(new File(
InputReader.INPUT_FILE));
FileOutputStream os = new FileOutputStream(new File(
InputReader.OUTPUT_FILE));
String output = assignment5.readFile(is);
System.out.println(output);
os.write(output.getBytes());
os.flush();
os.close();
}
}

It is a bit confusing that I named the method's doAssignment2 and doAssignment3 but use Assignment4 and Assignment5 objects. I should have named these assignment classes better. Again it was a small programming assignment, and an easy refactor. Overall, I do think I did a decent job with this assignment and benefitted from the effort.

I created a handy batch file to execute the code called tynt.bat which does this:
java -cp target\program_assignment-1.0-SNAPSHOT.jar com.tynt.app.App

Monday, September 13, 2010

Tynt programming assignment (Part 4)

Finally, here is where I have to implement the sorting algorithm and collection. I created a List collection I called IntegerList:
package com.tynt.app.util;

public class IntegerList {
public static final int BUFFER_SIZE = 100;
private Integer[] internalArray;
private int size;

public IntegerList() {
internalArray = new Integer[BUFFER_SIZE];
size = 0;
}

public IntegerList(int size) {
if (size < 0) {
throw new RuntimeException("Cannot create IntegerList with negative size");
}
internalArray = new Integer[size];
size = 0;
}

public int size() {
return size;
}

public void put(int index, Integer value) {
if (index > -1 && index < size) {
internalArray[index] = value;
} else {
throw new RuntimeException("Cannot put value "+value+" at index "+index+" IntegerList has size "+size);
}
}

public void concatenate(Integer value) {
if (value == null) {
throw new RuntimeException("Cannot concatenate null values");
}
if (size + 1 == internalArray.length) {
Integer[] temp = new Integer[internalArray.length + BUFFER_SIZE];
System.arraycopy(internalArray, 0, temp, 0,
internalArray.length);
internalArray = temp;
}
internalArray[size++] = value;
}

public void concatenate(IntegerList values) {
for (int i = 0 ; i < values.size(); i++) {
concatenate(values.get(i));
}
}

public Integer get(int index) {
if (index > -1 && index < size) {
return internalArray[index];
} else {
throw new RuntimeException("Cannot access value at "+index+" in IntegerList size "+size);
}
}

public Integer remove(int index) {
if (index > -1 && index < size) {
Integer result = get(index);
IntegerList integerList = new IntegerList();
for(int i = 0; i < size; i++) {
if (i != index) {
integerList.concatenate(get(i));
}
}
internalArray = integerList.internalArray;
size = size - 1;
return result;
} else {
throw new RuntimeException("Cannot access value at "+index+" in IntegerList size "+size);
}
}

public static IntegerList quicksort(IntegerList unsorted) {
IntegerList less = new IntegerList();
IntegerList greater = new IntegerList();
if (unsorted.size() <= 1) {
return unsorted;
}
Integer pivot = selectPivot(unsorted);
for(int i = 0; i < unsorted.size(); i++) {
Integer value = unsorted.get(i);
if (value <= pivot) {
less.concatenate(value);
} else {
greater.concatenate(value);
}
}
return concatenate(quicksort(less), pivot, quicksort(greater));
}

public static Integer selectPivot(IntegerList unsorted) {
if (unsorted == null || unsorted.size() < 2) {
throw new RuntimeException("Cannot select pivot on empty or single element list");
}
int pivotIndex = unsorted.size() / 2;
return unsorted.remove(pivotIndex);
}

public static IntegerList concatenate(IntegerList less, Integer pivot, IntegerList greater) {
if (less == null | pivot == null || greater == null) {
throw new RuntimeException("Cannot concatenate null values");
}
IntegerList finalList = new IntegerList(less.size()+1+greater.size());
finalList.concatenate(less);
finalList.concatenate(pivot);
finalList.concatenate(greater);
return finalList;
}
}

IntegerList implements a quicksort method which returns the IntegerList as a sorted IntegerList. My implementation of quicksort has several drawbacks:

  • I create additional collections (less,greater,etc...) for each invocation of quicksort and concatenate, so lots of memory could be used up with a long list to sort. It would be less memory to sort the list in-place without recursion/extra collections.
  • Speaking of recursion, too much recursion could cause a stack overflow as well.
With IntegerList implemented, I can use it to implement Assignment4:
package com.tynt.app;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.tynt.app.util.*;

public class Assignment4 implements InputReader {

public String readFile(InputStream is) {
BufferedReader d = new BufferedReader(new InputStreamReader(is));
String line;
IntegerList unsortedList = new IntegerList();
try {
line = d.readLine();
while (line != null) {
Integer value = null;
try {
value = new Integer(Integer.parseInt(line));
} catch (NumberFormatException e) {
throw new RuntimeException(e);
}
unsortedList.concatenate(value);
line = d.readLine();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
StringBuilder sb = new StringBuilder();
IntegerList sortedList = IntegerList.quicksort(unsortedList);
if (sortedList == null) {
sortedList = new IntegerList();
}
for (int i=0;i < sortedList.size();i++) {
sb.append(sortedList.get(i));
sb.append(NEW_LINE);
}
return sb.toString();
}

}

Still need to implement the Hash collection and driver.

Sunday, September 12, 2010

Tynt programming assignment (Part 3)

I glossed over creating the Assignment1-5 java files in the last post, this was done by first right clicking in eclipse and creating a package under this folder:
src\main\java\com\tynt\app
called
com.tynt.app
Then right clicking the empty package and creating new Class (Assignment1) adding the InputReader interface to the class and stubbing out the interface method:
public String readFile(InputStream is) {
return null;
}

Just so you know.

For Assignment1 I just wanted to get the code for reading and creating a string from the input stream working:

package com.tynt.app;

import java.io.*;

import com.tynt.app.util.InputReader;

public class Assignment1 implements InputReader {

/**
* @return contents of the InputStream as a String
*/
public String readFile(InputStream is) {
BufferedReader d = new BufferedReader(new InputStreamReader(is));
String line;
StringBuilder sb = new StringBuilder();
try {
line = d.readLine();
while (line != null) {
sb.append(line);
sb.append(NEW_LINE);
line = d.readLine();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return sb.toString();
}
}
For Assignment2 I decided to use the built-in Java collections classes to test what the output of my custom collection should look like:
package com.tynt.app;

import java.io.*;
import java.util.*;

import com.tynt.app.util.InputReader;

public class Assignment2 implements InputReader {

/**
* @return is list of numbers sorted in ascending order
*/
public String readFile(InputStream is) {
BufferedReader d = new BufferedReader(new InputStreamReader(is));
String line;
SortedSet sortedList = Collections.synchronizedSortedSet(new TreeSet());
try {
line = d.readLine();
while (line != null) {
Integer value = null;
try {
value = new Integer(Integer.parseInt(line));
} catch (NumberFormatException e) {
throw new RuntimeException(e);
}
sortedList.add(value);
line = d.readLine();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
StringBuilder sb = new StringBuilder();
for(Integer i: sortedList) {
sb.append(i);
sb.append(NEW_LINE);
}
return sb.toString();
}

}
Again with Assignment3, I used built-in collections to check what the output should be for the histogram:
package com.tynt.app;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

import com.tynt.app.util.InputReader;

public class Assignment3 implements InputReader {

/**
* @return sorted list of numbers and their frequency
*/
public String readFile(InputStream is) {
BufferedReader d = new BufferedReader(new InputStreamReader(is));
String line;
SortedMap sortedMap = Collections.synchronizedSortedMap(new TreeMap());
try {
line = d.readLine();
while (line != null) {
Integer value = null;
try {
value = new Integer(Integer.parseInt(line));
} catch (NumberFormatException e) {
throw new RuntimeException(e);
}
Integer frequency = 1;
if (sortedMap.containsKey(value)) {
frequency = sortedMap.get(value);
frequency += 1;
}
sortedMap.put(value, frequency);
line = d.readLine();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
StringBuilder sb = new StringBuilder();
for(Integer key: sortedMap.keySet()) {
Integer frequency = sortedMap.get(key);
sb.append(key);
sb.append(" - ");
sb.append(frequency);
sb.append(NEW_LINE);
}
return sb.toString();
}

}
Assignment4 and Assignment5 actually adhered to the restriction of no built-in collections.

Saturday, September 11, 2010

Tynt programming assignment (Part 2)

Next I generated an eclipse project file for the maven project with this command:

mvn eclipse:eclipse

I opened the projet in eclipse. For the Junit tests I created some test input files here:
src\test\resources\TestData.txt
src\test\resources\BadInput.txt

I then created the unit test java file here:
src\test\java\com\tynt\app\AppTest.java

Here is the source for my unit test:
package com.tynt.app;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.io.*;
import java.util.*;

import com.tynt.app.util.*;

/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}

/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}

/**
* Test each assignment with input files
*/
public void testApp()
{
List readers = new ArrayList();
Assignment1 assignment1 = new Assignment1();
Assignment2 assignment2 = new Assignment2();
Assignment3 assignment3 = new Assignment3();
Assignment4 assignment4 = new Assignment4();
Assignment5 assignment5 = new Assignment5();
readers.add(assignment1);
readers.add(assignment2);
readers.add(assignment3);
readers.add(assignment4);
readers.add(assignment5);
readerTest(readers,"TestData.txt");
try {
readerTest(readers,"BadInput.txt");
fail("Bad input should throw RuntimeException");
} catch (RuntimeException e) {
assertTrue(true);
}
}

public void readerTest(List readers, String resource) {
for (InputReader inputReader : readers) {
InputStream is = getClass().getClassLoader().getResourceAsStream(resource);
String result = inputReader.readFile(is);
System.out.println(result);
}
}
}
I created an interface I called InputReader to help me test the different assignments against the same input files.

package com.tynt.app.util;

import java.io.*;

public interface InputReader {

public static final String INPUT_FILE = "THire_input.txt";
public static final String OUTPUT_FILE = "THire_histogram.txt";
public static final String NEW_LINE = System.getProperty("line.separator");

/**
* Reads the InputStream parameter and returns a string of the contents
*
* @param is
* InputStream to read from
* @return String of the processed contents of InputStream
*/
public String readFile(InputStream is);
}
I then stubbed out the Assignment1-5 java classes which implement the InputReader interface and ran the package goal for maven using a maven eclipse plugin to fix any typos and get a baseline for beginning implementation.

About Me

My photo
Lead Java Developer Husband and Father

Tags