Tag Archives: code

The Book Scavenger

>Spoiler Alert! Or Trigger Warning, if you are emotionally tied to storytelling. This post will discuss some of the secret codes used in a book. If you haven’t yet read ‘The Book Scavenger,’ I suggest that you do so. Until that time, don’t read beyond the following paragraph!<

I picked up a copy of The Book Scavenger by Jennifer Chambliss Bertman from Denver’s Tattered Cover Book Store so that my wife would have something to read on the scant downtime she had during the AMVA Conference held there this past summer. It was advertised as the One Book One Denver selection for 2016.

Posted by on September 11, 2016 in Uncategorized

A really challenging puzzle – even with hints

I’ve been watching cryptography videos again on Khan Academy. In doing so, I created this puzzle. But don’t worry about it. It’s really hard, even with the hints I provide.

Posted by on November 23, 2014 in Uncategorized

Tags: , , , , ,

Cryptography

Today is the first day of the Coursera Cyrptography class taught by Stanford Professor, Dan Boneh. I follow courses like this every once in a while in order to learn a bit about topics that I would not otherwise get any exposure to. Boneh’s course is a little math-intense, but there is another more concept-driven course on the same topic being offered on Khan Academy. If you haven’t taken advantage of either of these two sites, you should look into them. Both are entirely free, and both are taught by excellent educators.

Here’s a video from the Khan Academy site introducing the Caesar Cipher, a simple cipher like those used on radio dramas of the past (get your secret decoder ring!).

If you want to crack a simple substitution cipher like this, you might want to start by using a frequency chart of letters used in the English language, like this one:

The Caesar cipher: Brit explains the Caesar cipher, the first popular substitution cipher, and shows how it was broken with “frequency analysis”

However, once you figure out the easiest letters (e,t,and a), things get a bit more difficult. At this point, you will probably have to start looking at letter pairings (Bigrams) to see if any useful patterns show up there. Here’s a listing of the most common Bigrams (again, in English).

It’s interesting that these kinds of codes might ever have been considered sophisticated enough to use in the real world. After all, it’s easy to find examples of these types of ciphers in daily newspapers around the world presented as cryptograms that people do for fun.

KT RPP EXRDJ PXZ’J KX BOXD EVDIXI,

KSZ DBZ JB BOXD EVDIXI ZQRZ BSA KARVDJ YRPP BSZ.

-AVWQRAI IRCNVDJ

Posted by on September 8, 2014 in Education

Codon Usage Bias – Part I

To the molecular biologists:

Optimize ye codons while ye may

For time is a-flying

And this clone you have in R & D today

Tomorrow will be … in manufacturing- and it’s just impossible to change anything at that point, so forget it.

I”m no rocket surgeon

Codon Usage Bias – Part I

I read an article yesterday about codon bias that has been stuck in my head ever since. The article, appeared as a ‘Perspectives’ piece in the 13 Dec 2014 issue of Science, with the title, ‘The Hidden Codes That Shape Protein Evolution.’

I spend a lot of time in my classes discussing the basic mechanism by which DNA –>RNA –> Protein, known as the Central Dogma. A lot gets left out of these lectures in order to keep it simple, which sometimes keeps the way I think about the flow of information pretty simplified as well.

Fortunately, this article rattled my cage enough to open my mind to the myriad influences that go into the stuff of life. Here, Weatheritt and Babu, look at how DNA sequences may be under selective pressures independent of just the proteins they encode.

I’ve done a fair amount of molecular biology in my life, including cloning genes and moving them into other organisms for expression as drugs or drug components. One example of this was in a lab where we used live-vectors as immunogens in order to take advantage of the uniquely broad immune response this single-cell pathogen elicits. The immune responses we wanted to trigger / amplify were typically against human tumor proteins or the products of human viruses (e.g. HIV, HPV), however the organism we were using as a vaccine was a bacteria.

As I said above, I usually teach the Central Dogma in a way that omits many of the complications seen in the real world. So, when we look at a codon chart, we see the redundancy (multiple DNA codons make the same amino acid) to illustrate how a change in the DNA sequence can often fail to change the protein sequence at all. These are called ‘silent mutations’.

The way these codon charts work is by triangulating a position in the middle of the chart using the bases depicted along three of the four edges. For example, the codon AUG is read by locating the ‘A’ on the left margin, the ‘U’ on the top margin, and the ‘G’ on the right margin. The location this identifies is an amino acid called Methionine (abbreviated as met) on this chart.

Notice that if the first two bases in a codon are CU, then it does not matter what the third base is, no matter what, this codon will call for a Leucine (leu).. This means is the sequence of RNA is CUU originally, but mutates to CUC, there will be no change in the protein.

What codon optimization addresses is the fact that different organisms tend to prefer some codons over others, even if they encode the same amino acid. This has been appreciated for many years now so when a molecular biologist takes a protein (e.g. from a human tumor) that they want made by bacteria and they redesign the DNA sequence in a way that codon preferences are maximized in the organism that will express the protein.

This figure examines the percentage of times a gene uses a particular codon to make Leucine. In the bacteria, E. coli, CTG is used nearly 50% of the time. Meanwhile, in the yeast, S. cerevisea, TTA and TTG are preferred.

(Note that T in DNA = U in RNA)

————

So, what does this mean? Consider a simplified example…

I want to clone this protein from a yeast and grow it up in bacteria:

Met – Leu – Leu   [stop]

ATG- TTA – TTG – TAG

We would take this DNA from the yeast and then modify the sequence by changing the two Leucine codons into the preferred sequence in bacteria (CTG):

Met – Leu – Leu   [stop]

ATG- CTG – CTG – TAG

The result should be a sequence of DNA that the bacteria will be able to optimally translate into protein.

———

This has worked out to be much longer and more technical than I intended – and I haven’t even addressed the new ideas brought up in the Science article.

Therefore, I’m going to stop here and continue tomorrow with part II

Posted by on February 9, 2014 in Uncategorized

Stack Overflow hates me

I tend to ask a lot of questions that stack overflow considers useless. I understand the format of the site – it’s a place where people can ask questions that can be answered accurately and with references. An easy way to get your question flagged is to ask for a recommendation for something – a question that can only be answered by opinion and therefore does not have a discrete, ‘provable’ answer.

However, I still want my questions answered. I really do need help and I’m not sure where else to turn to get it (to reach such a well experienced audience).

What I am dealing with is:

I have a small C++ program that I have developed in Xcode. Things are pretty close to working, but I would like to take my program and wrap it in a more attractive user  interface. Somehow, I thought I was just needing to learn some more C++. I thought that was all in ‘book 2’ or something. But I am getting the impression that I need another program and I have no idea where to start.

(The layers of programming are amazingly deep)

So, please write if anyone out there knows of

#1 a good program to do this with -or- a way to approach it by linking my C++ programs in XCode to an objective-C interface (blahblah, these are words I can repeat, but I don’t understand how to do it)

#2 know a website / book / youtube channel that embodies a walkthrough approach to doing this

Posted by on August 2, 2013 in Uncategorized

Tags: , , , , , , , , ,

Russian Nesting Dolls

This post is primarily for those readers who are working through the Codecademy.com course on javascript. I was having trouble with mastering the recursion problem for solving factorials there and noticed that many others were in the same boat.

This code was really blowing my mind. I was having the worst time understanding it. I was OK with the idea of recursion, but I don’t think I was really understanding it.

I was following the code and could see it is working, but didn’t see why it actually returned the correct value. Additionally, I had a conceptual block – i.e. how does the code store the value of each recursion without a variable?

Here’s the problem:

Write a recursing code that solves for the factorial of any given value.

Recall that factorials are the product of all numbers from 1 to n, where n is the value you are solving for. For example, the factorial of 3 : In mathematical terms this is written, 3!, so

3! = 1*2*3 = 6

4! = 1*2*3*4 = 12

The problem suggests that you set a ‘base case’ as returning 1 if n === 0, and that you use a terminator to prevent crash if negative numbers are entered (because they will otherwise never get to the terminator)

Here’s my code:

function factorial(n) {

if (n<0){

console.log(“We can only compute factorials from positive integers”);

return;     // the catch-all in case a negative number is entered

}

if (n ==0){

return 1;    // the base case

}

else{

return n* factorial (n-1);    // the nested function call

}

}

factorial(4);    // function call to test program

If we start at 4 (as an example), then we go through the code and get to the nested function call that says

return n * factorial(n-1)

if we sub in the actual values for n, it would read:

return (4) * factorial(3)

But factorial(3) is not a number – it is a function call, so before (4) is multiplied by anything, it sends a new value to factorial. This keeps happening until we get to n=0. At that point we’re at the base case and no further recursion will happen – the code simply returns 1.

So, we have to start moving our way back out of the loop. We set n=1 here, so the innermost recursion actually looks like this:

return 1* factorial(0)

because factorial(0) returns 1, we are actually doing this:

return 1*1     (*   = 1    *)

then we back out to the next step:

return 2* 1     (*   = 2   *)

then we get to…

return 3*2…   (*    = 6     *)

return 4*6      (*    = 24     *)

Or, if you can see this more clearly, this is how the computer sees the code:

return 4 * (return 3 * (return 2 * (return 1 * (1))));

work backwards doing the math from the inside out:

1*1 = 1

2*1 = 2

3*2 = 6

4*6 = 24

Q.E.D.

postscript: I just received a reply to my post on codecademy.com from a moderator, Alex. He directed me towards an article he had written explaining the same topic using a very elegant flow diagram. You can find that article here.  – I had to write this as a postscript, because his article is so clear that if you read it first, you would have no need to read mine.

Posted by on September 21, 2012 in Uncategorized

How to program ‘ZombieCatchAlice’

OK, I’ve thrown together a quick program with a Zombie chasing Alice through the streets of a little city. He says ‘Brains!’, she yells ‘Oh, Help!’, runs a couple of paces, turns, yells for help again and runs a few paces. All the while, the zombie is following her around the screen and the camera follows the action.

What I don’t know how to do is program an event that is triggered by the zombie ‘catching’ alice. I’d like to see the zombie get her, turn her into a zombie and the two can go after other characters on the screen.

Help is greatly appreciated.

Posted by on September 5, 2012 in Uncategorized

Tags: , , , , , ,

Finishing Touches

Step 6: You get a hand, Determining scores and judging winner

Here are the last touches that make this code into the bare-bones structure of the game. Cards are dealt out to two hands (myHand and yourHand), each card is given a suit and a value and the two hands are compared to determine a winner. (I haven’t added anything about ‘Hitting’ or ‘Holding’, and since you start with only two cards, you can’t go over 21. I’m assuming that these things are all part of the final project).

//Hand Constructor

var Hand = function(){

this.card1 = deal();

//console.log(“card1 is “+this.card1.number+” of ” + this.card1.suit);

this.card2 = deal();

//console.log(“card2 is “+this.card2.number+” of ” + this.card2.suit);

};

//Card Constructor

var Card = function(s,n){

this.number = n;

this.suit = s;

this.getSuit = function(s){

//console.log(“I’m here in getSuit”);

switch(this.suit){

case 1:

this.suit = “clubs”;

break;

case 2:

this.suit = “diamonds”;

break;

case 3:

this.suit = “hearts”;

break;

case 4:

break;

}

};

this.getValue = function(n){

// console.log(“I’m here in getValue”);

if (n === 1){

this.value = 11;

}

else if (n >= 11){

this.value = 10;

}

else{

this.value = n;

}

};

};

//Deal function

var deal = function(){

var suit = Math.floor(Math.random()*4)+1;

//console.log(“deal function suit: “+suit);

var number = Math.floor(Math.random()*13)+1;

//console.log(“deal function number: “+number);

return new Card(suit,number);

};

//Score function

var score = function(one, two){

return one+two;

};

//comparison of hands

var judgement = function(mine,yours){

if (mine>yours){

console.log(“I win “+mine+ ” vs “+yours);

}

else if (yours>mine){

console.log(“You win “+mine+ ” vs “+yours);

}

else{

console.log(“We tied “+mine+ ” vs “+yours);

}

};

//Main Function

var myHand = new Hand();

myHand.card1.getSuit(myHand.card1.suit);

myHand.card1.getValue(myHand.card1.number);

console.log(“my card1 is a “+myHand.card1.suit+” of value “+myHand.card1.value);

myHand.card2.getSuit(myHand.card2.suit);

myHand.card2.getValue(myHand.card2.number);

console.log(“my card2 is a “+myHand.card2.suit+ ” of value “+myHand.card2.value);

var yourHand = new Hand();

yourHand.card1.getSuit(yourHand.card1.suit);

yourHand.card1.getValue(yourHand.card1.number);

console.log(“your card1 is a “+yourHand.card1.suit+” of value “+yourHand.card1.value);

yourHand.card2.getSuit(yourHand.card2.suit);

yourHand.card2.getValue(yourHand.card2.number);

console.log(“your card2 is a “+yourHand.card2.suit+ ” of value “+yourHand.card2.value);

var myScore = score(myHand.card1.value,myHand.card2.value);

var yourScore = score(yourHand.card1.value,yourHand.card2.value);

judgement(myScore,yourScore);

Blackjack game is driving me crazy

I’ve been working on the blackjack name for codecademy all day today without luck.  The challenge / puzzle / assignment is to have a hand of cards generated and scored.

The hand is an object that includes two ‘card’ objects. each card is generated randomly with two features (number and suit*).The suit is defined by a random number 1-4 and defined by a method for matching this numeral to a suit. The number is defined as 1-13 and set to values such that 1 (ace) = 11, 1-10 =face value and 11-13 are face cards worth 10pts each.

I simply can’t do it. My code looks reasonable to me (but I don’t really feel comfortable with it at all), but I’m getting crazy results. Here’s what I have, try copying it into a javascript field and see if you can make heads or tails of it:

// Make your card constructor again here, but make sure to use private
// variables!
function Card(s,n){
this.number = n;
this.suit = s;

this.getSuit = function(suit){
switch (this.suit){
case 1:
this.suit = “clubs”;
break;
case 2:
this.suit = “diamonds”;
break;
case 3:
this.suit = “hearts”;
break;
case 4:
break;
}
}

this.getValue = function(number){
if(this.number === 1){
//console.log(“value = 11”);
this.value = 11;
return 11;
}
else if (this.number >=11){
//console.log(“value = 10”);
this.value = 10;
return 10;
}
else{
//console.log(“value = “+ this.number);
this.value = number;
return this.number;
}
};
};

// Make a deal function here. It should return a new card with a suit
// that is a random number from 1 to 4, and a number that is a random
// number between 1 and 13
var deal = function(){
var suit = Math.floor(Math.random()*4)+1;
console.log(suit);
var number = Math.floor(Math.random()*13)+1;
console.log(number);
return new Card(suit,number);
};

var Hand = function(){
this.card1 = deal();
console.log(“card1 is “+card1.number+ ” of “+card1.suit);
this.card2 = deal();
console.log(“card2 is “+card2.number+ ” of “+card2.suit);
};

Hand();
console.log(Hand);

*by the way, it is clear that there is nothing that prevents the same card from being drawn twice from what presumably is a single deck.

Posted by on July 24, 2012 in Codecademy, Coding

function: AcceptGuess

The next function I had to work out was how to accept the player’s guess in a way that would set up an array in a way that each digit of the player’s guess is an entry in the array. This would give me an array containing the secretCode generated by the computer and an array containing the guess generated by the user. All I’d have to do then, in cycle through the arrays to make comparisons (If only it was so easy).

So, this function proved to be quite simple:

//accepts guess from user and parses it into an array of ‘code’ length

acceptGuess = function(code,numbers){

console.log(“***acceptGuess function reached***”);

var guess = prompt(“Make a guess at the secret code (“+code+”digits/1-“+numbers+”)”);

//need to add something to ensure guess is within parameters

console.log(“guesslength is “+guess.length);

console.log(“code is “+code);

var codeNum = code * 1;

console.log(typeof guess.length);

console.log(typeof codeNum);

if (guess.length === codeNum){

console.log(“got inside the loop”);

for (i=0;i>code; i++){

console.log(“guess[i] = “+guess[i]);

guessDigit[i] =guess.substring(i,i+1);

console.log(guessDigit[i]);

}

return guessDigit;

} else {

console.log(“invalid guess”);

}

};

The only problem I had with this was that the parameter ‘code’ comes through as a string. Note the two lines of code:

console.log(typeof guess.length);

console.log(typeof codeNum);

I’ve realized that the best way to debug is to have console.log lines everywhere that write out what is going on step by step. ‘typeof’ has been a very valuable code that I use whenever it looks like math isn’t working – so far it has uncovered the problem nearly 100% of the time.

Javascript will convert it to a ‘number’ if you do any mathematical function with the variable. An easy way to do this without messing anything up is to multiply by 1. To be even more careful, I created a new variable, ‘codeNum’, to hold this value as a number. The following line did that:

var codeNum = code * 1;

Last piece in my next post: comparing the values of the guess with the secret code. I just finished this 5 minutes ago after running myself ragged for several days (it was the reason I was dilly dallying about this posting)