# Monthly Archives: June 2013

## My own Coding Problem

I’ve been considering a new classroom exercise to describe genetics and the inheritance of alleles within a population. The idea is that every student draws two coins randomly to start – these represent two alleles for a single gene in that organism. Then each student has the opportunity to ‘breed’ with another student to make two offspring. The genetics of the offspring will be determined randomly from that of the parents (something like each parent flips their coins until they get one heads and one tails – the heads from each parent goes to the offspring).

We’ll do this for several generations and track the frequency of alleles through time.

OK – now to coding:

I thought this sounded like a simple project that could be simulated easily, so that’s a coding challenge for myself. To start with, I thought I would build the program up bit by bit doing unit testing along the way. At this point I am trying to just set up the parental generation. To do this, I:

1. get input from the user: how many people are playing?

how many different alleles for the gene exist?

2. loop through making ‘User’ objects for each person playing. Each User object has an id number for tracking (idNum) and two alleles determined randomly(alleles[0,1]). I was thinking of keeping my list of users as an array called players[0…10max]

I am getting and using my userData with no problem. I think I am even setting up my objects alright and assigning them two randomly determined elements. But instead of those elements being coins (representing alleles), I’m getting the address of a char within the first denomination….

Any help at all is greatly appreciated, but remember – I’m doing this to learn, so I need to be able to understand the solution, not just get it solved.

Here’s the code with some debugging output statements:

//

//  main.cpp

//  CoinsLabSimulation

//

//  Created by Jack on 6/27/13.

//

//A simulation game to test how my coins lab idea will work

#include <iostream>

#include <cmath>

#include <time.h>

using namespace::std;

class User{

private:

int idNum;

string alleles[2];

public:

User(int A){idNum=A;};//int myArray[] = new int[idNum];

char getAlleles();

void setAlleles(string, int[]);

};

void getInput(int userInput[]);

void randomAssignment(int userInput[], string denominations[], int players[]);

int main()

{

srand(time(NULL));

int userInput[2];

char p[10] = {“pennies”};   //I hate character arrays / strings in C++

char n[10] = {“nickels”};   //I want an array of the coins we will use as alleles in this game

char d[10] = {“dimes”};

char q[10] = {“quarters”};

char s[15] = {“SusanBAnthonys”};

string denominations[5] = {p,n,d,q,s}; //this needs to be fixed so I can call on this array for coin types

int players[10];

getInput(userInput);

cout<<“We will be playing with “<<userInput[0]<<” players and “<<userInput[1]<<” alleles”<<endl;

cout<<“Alleles used will be:”<<endl;

for (int i =0; i<userInput[1]; i++) {

cout<<denominations[i]<<endl;

}

randomAssignment(userInput, denominations, players);

return 0;

} //end main

//*****************non-member functions

void getInput(int usersChoices[]){  //fx to retreive user settings –unit test passed

cout<<“How many players will be participating(10 max)?”<<endl;

cin>>usersChoices[0];

cout<<“How many denominations would you like to use?”<<endl;

cin>>usersChoices[1];

}// end getInput

void randomAssignment(int userNumbers[], string allelesUsed[], int player[]){ //will create user objects and assign each alleles

//————-   unit test:

cout<<endl<<endl<<“Unit Test”<<endl;

cout<<allelesUsed[0]<<”    “<<allelesUsed[3]<<endl<<endl;

//————-   pass

User *a[10];

for (int i=0; i<userNumbers[0]; i++) {

a[i] = new User(i);

a[i]->setAlleles(*allelesUsed,userNumbers);  ///trace this

}

}// end randomAssignment

//******************User Member Methods

void User::setAlleles(string allelesUsed,int userNum[]){

int max = userNum[1];

int randomNumber = floor(1 + (rand() % max));

cout<<“Random number: “<<randomNumber<<endl;

string allele1 = &allelesUsed[randomNumber];

randomNumber = floor(1 + (rand() % max));

cout<<“Random number: “<<randomNumber<<endl;

string allele2 = &allelesUsed[randomNumber];

alleles[0] = allele1;

alleles[1] = allele2;

//———–unit test2

cout<<“UNIT TEST2::::Alleles for this player are: “<<allele1<<” and “<<allele2<<endl;

//———–fail – it’s using the random number as an address within ‘pennies’

cout<<“Alleles for this player are: “<<alleles[0]<<” and “<<alleles[1]<<endl;

}

char User::getAlleles(){}

————————————————————-

my output looks like this:

How many players will be participating(10 max)?

2

How many denominations would you like to use?

4

We will be playing with 2 players and 4 alleles

Alleles used will be:

pennies

nickels

dimes

quarters

Unit Test

pennies    quarters

Random number: 4

Random number: 2

UNIT TEST2::::Alleles for this player are: ies and nnies

Alleles for this player are: ies and nnies

Random number: 1

Random number: 2

UNIT TEST2::::Alleles for this player are: ennies and nnies

Alleles for this player are: ennies and nnies

1 Comment

Posted by on June 29, 2013 in Uncategorized

## RubyMonk

I mention codecademy all the time here and rant about how great a way it is to learn a variety of languages and markups online, on your own time and free. I’ve been following both the web programming track and the Ruby tracks aggressively lately (I’m on a 20 day streak presently). However, I have to also mention another free site that does much the same thing. RubyMonk offers free online courses in Ruby (and Python, under PythonMonk). The Monk websites are clean, well structured and provide an element of atmosphere as well.

Unfortunately, RubyMonk does not provide a forum where I can pitch my project challenges – er, I mean Koans. But if you are learning Ruby or Python with the Monk, please feel free to come here from time to time to see if there are any simple programming challenges open.

Posted by on June 28, 2013 in Uncategorized

## Getting Oxygen Where It’s Needed

Oxygen is required by many organisms for survival, luckily it is plentiful in the air, but how does it get into all the tiny cells all over the body?

First, Oxygen is a highly electronegative atom. This means that it attracts electrons very well and can pull them away from other molecules. Only one other atom is more electronegative and that’s the most reactive element in the periodic table, Fluorine. Electronegativity becomes useful biologically because electrons are capable to storing energy that can be passed along from one molecule to the next. But , to do this, each molecule must be more electronegative than the last. Therefore, it is not surprising that Oxygen is used as the final electron acceptor in the electron transport chain of cellular respiration. This reaction is required by many organisms, and can be highly beneficial even to some organisms that can live without Oxygen. An electron transport chain is a process by which molecules in a membrane pass en electron down the line using its energy in a controlled way to extract even more energy from sugar.

But how does Oxygen get to the cells that need it? Two molecules account for much of this action, myoglobin and hemoglobin, both illustrated below:

Hb- hemoglobin, Mb-myoglobin.

What this image also elegantly portrays is the amazing similarity between the molecules that belies their evolutionary relationship.

Each of these molecules is capable of binding Oxygen, but each occurs in a different tissue. Hemoglobin is present in Red Blood Cells, making them capable of transporting oxygen from the lungs to other tissues. Myoglobin occurs in these ‘other’ tissues, particularly muscle cells.

Despite the fact that they both bind oxygen, they do not bind it equally well under all conditions. While hemoglobin is just as good at binding Oxygen in high concentration environments (like the lung after inhalation), it is not as good at retaining oxygen when found in less Oxygen-rich environments (such as tissues like muscle).  Under these conditions, myoglobin is much better at binding Oxygen and can pull the molecules away from hemoglobin.

Recently, several groups have published new data about how subtle differences amongst proteins involved in Oxygen transport through blood and muscle result in different binding properties. In turn, the variability in these properties underlie the amazing diversity of lifestyles found in nature, from humans to birds to giant whales capable of holding their breath for up to an hour.

That brings us back to the electron transport chain and Oxygen’s electronegativity, where Ois used as a ‘magnet’ for the electron traveling down the pathway from one molecule to the next. As it goes it loses some of its energy, which is converted into a new form that the cell can use. Once the electron, now at a lower energy state, gets to O2, the Oxygen splits  and takes up a Hydrogen ion to form water.

So, electronegativity and binding affinity are the forces that both transport Oxygen through the body and pulls electrons from one molecule to another. Together, the movement of electrons, like that of water through a mill, powers processes that lead to the synthesis of ATP, the energy currency of the cell (see below).

Note the electron traveling down the chain (in pink)

Given what we’ve discussed here, how do you think a baby ever gets to pull the Oxygen away from its mother’s blood / hemoglobin?

Posted by on June 26, 2013 in Uncategorized

## Winding down Coding Challenge I

I’ve received several entries answering my coding challenge to demonstrate / test Goldbach’s Conjecture that all even numbers > 4 are the sum of two primes. So far python has been the language of choice for entries.

I will be closing down this challenge as of June 30 at 11:59pm.

Once I take a look at the entries, I’ll award the prize, a copy of my iBook, In Parts to the winner and post the code here with a walkthrough to show how the problem was tackled as well as any interesting comparisons between entrants.

Posted by on June 26, 2013 in Uncategorized

## A cute video about the beginning of time

From the Ted Ed site:

“How did the universe begin — and how is it expanding? CERN physicist Tom Whyntie shows how cosmologists and particle physicists explore these questions by replicating the heat, energy, and activity of the first few seconds of our universe, from right after the Big Bang.”

Lesson by Tom Whyntie, animation by Hornet Inc

Posted by on June 24, 2013 in Uncategorized

## Old Aviary at KC Zoo

In the middle of a rapidly growing zoo sits this unused, overgrown structure. Reminds me of Logan’s Run.

Posted by on June 24, 2013 in Uncategorized

## Coding Challenge II: Make Mine a MASTERmind

Don’t worry, Coding Challenge I is still open, but someone was writing about games to develop in the codecademy discussion groups.

A while ago, when I was first following the JavaScript pathway, there, I decided to write a MasterMind program. Many of the versions I saw prohibited players from using the same number/color more than once, but I felt that was a cop-out. My solution works, but as usual for me, had some tortuous logic.

So, here’s the challenge:

In any language, write a MasterMind game where the computer chooses the numbers and the user deduces them.

1. use numbers (four of them,#s1-4, randomly chosen by the computer), rather than colors

2.  that allows multiple uses of the same numbers, i.e. ‘1122’

3. provides appropriate feedback to the user to help them close in on the correct sequence.

4. keeps track of the number of turns taken

5. (optional) can also be played with 2 users -or- user sets the code and computer guesses

6. (optional) allow user to select # of positions and range of numbers used.

simple code trumps tangly code. I prefer languages I can read (C++, javascript, python) but all are welcome.

Posted by on June 22, 2013 in Uncategorized

## Project One of my Game Development Class made my head explode

I’m taking an intro Game Development course online (it’s well known that I hate online courses in general) and here I am on what amounts to day three and I’m struggling.

Do this Stuff. Don’t worry, it’s easy.

Instructor:”So, you just need to make up this data tree thing with nodes and identifiers and stuff… look, just do it and get back to me. Here’s an outline:

1. Each node has an ID property which is unique number identifying the node
2. Each node as a Report() method that will print to the console it’s ID and if its a leaf or not
3. When created, the tree has a single node, the root
4. The tree has a SplitLeafs() method which will cause all leafs to create two children
5. The tree has a VisitAll() method which will visit every node and call the node’s Report() method
6. The tree has a LeafReport() method which call Report() on just the leaf nodes
7. In your main() method in Program.cs/Main.cs, you have the following:
1. Create a tree
2. Call VisitAll() on the tree
3. Call SplitLeafs() on the tree
4. Call LeafReport() on the tree
5. Call SplitLeafs() on the tree
6. Call VisitAll() on the tree”

OK…

Me: “Um, OK. I don’t really know what this is, but if you say it’s easy, I’m sure that I can figure it out.”

Instructor:”Got it yet?”

Arghh!!!

Me: “Arghh… Let’s see: Tree class and Node class… How do I instantiate these things?

The root has no parent, but has children…”

Instructor:”Yeah. You totally have it. Let’s talk about the completed project tomorrow.”

Me:”Oh crap. So, the tree just gets made once, but then it makes the nodes…? Each node will hold some data: let’s keep that simple. Make it an integer.

What the hell’s a leaf?

Ughh. Simple isn’t simple enough.”

Feeling the heat now?

Instructor:” A leaf if just a node. It has a parent, but no children.”

Me:  “And, how to we do this splitLeaf thing?”

Cripes! instantiate two children from each leaf….how… to…?The pain!

Instructor:” Look, it’s just a couple methods within the class. write up a couple setters and a couple getters and then one or two other methods to do the work.”

Look, I don’t mean to tighten the screws or anything, but this needs to be done and uploaded onto the Google+ document space…

I’m not kidding. I went from dominating my into C++ class to being a joke in this next class. I’ll try to  deconstruct the problem and post a walkthrough of the general idea if anyone’s interested.

Posted by on June 20, 2013 in Uncategorized

Tags: , , , ,

## Take a nice close look at the surface of Mars

What is this rock made of?

Jerry Coyne’s Why Evolution is True webiste uncovers a lot of gems on the internet. Recently, he posted a link to Nasa’s billion-pixel panoramic photo (composite) from the Curiosity rover. You can  take a close look at a great deal of Martian landscape using their viewer. Just below the panorama are links that will drag you to come intriguing places in the image (a shiny white object in the sand, what seriously looks like a Martian bird – I can’t stand not knowing what this really is, etc.) I also found a rock that the rover had broken open that exposed a shiny, light-colored interior (above).

Posted by on June 20, 2013 in Uncategorized

## Coding Challenge

Recently, there have been a couple new revelations about number theory published in Science Within the article was a pair of theories about prime numbers that I had never heard before, one of which was:

Goldbach’s conjecture, [which] makes two assertions: that every even number greater than 2 is the sum of two primes, and that every odd number greater than 5 is the sum of three primes.

I thought it would be fun to start with the first part of this problem and write a program to accept user input in the form of an even integer > 2  and then look for the two primes whose sum is equal to the user provided:

prime1 + prime2 = user input

where prime1 and prime2 may be any prime number (even the same number twice)

I could easily see this escaping the processing power of my machine if the numbers get high, but I think it shouldn’t be too hard to at least write a code that could look for them and demonstrate whether this worked with known input.

Are you up for a quick challenge?

Learn Fractions with Zombies

If so, submit your documented answer here as a comment. Feel free to use any language you would like (I just did it in C++, but I’m eager to see better answers than my own). My favorite submissions will win a free copy of  my iBook, In Parts, Tales of Fractional Zombies, which you can enjoy yourself or regift to a youngster in your life who wants a fun way to learn the concept of fractions.

You can use these links as resources to help check your work:

If you are new to coding and are looking for a coding environment to work in, check out this posting for help setting up a C++ coding environment using Xcode (on your mac)