RSS

Category Archives: Coding

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:

Goldbach_partitions_of_the_even_integers_from_4_to_28_300pxprime1 + 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?

zombie locker

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:

prime numbers       prime checker

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)

 
7 Comments

Posted by on June 19, 2013 in Codecademy, Coding

 

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:

this.suit = “spades”;

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);

 

Tags: , , , ,

Adding the Methods

Step5: Adding methods to interpret actual suit and value of cards

 

//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:

            this.suit = “spades”;

            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);

};

 

//Main Function

var myHand = new Hand();

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

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

console.log(“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(“card2 is a “+myHand.card2.suit+ ” of value “+myHand.card2.value);

 

 

In this section I added two methods (these are just functions that handle objects). Methods are placed inside of the objects they work on (is this right?Always?). In this program my methods interpret the suit and number variables as the suit that the number represents (clubs, diamonds, etc) and the value of the card (ace = 11, face cards are all worth 10 and numbered cards are worth their numerical value).

As usual, I have included a couple console.log statements so that I could track where problems cropped up along the way.

 

 

Tags: , , , , , ,

Next steps… making the card objects

Step4: Adding the Card Class (called from Deal function)

 

//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

function Card(s,n){

            this.number = n;

this.suit = s;

 

 

            getSuit (method to interpret suit numbers)

 

            getValue(method to interpret value unbers)

 

}

 

//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);

};

 

//Main Function

var myHand = new Hand();

 

 

New text is highlighted in red(Although I’m not sure this formatting will follow through to the post). Here we are adding a function call at the end of the ‘deal’ function in the form of a ‘return’ statement. This means that the deal function will generate two random numbers for us and then send these to the ‘card’ constructor to make our new card objects.

I’m leaving most of the card object unfinished at this point. We will come back later to add methods for interpreting the information associated with each card object.

 

 
 

Tags: , , , ,

Getting Started with rebuilding blackjack from scratch with notes (help requested)

Using the flowchart in the previous post, I’ve started walking through how to build this program.

Step 1: Outline:

 

Hand Constructor

 

Card Constructor

 

            getSuit (method to interpret suit numbers)

 

            getValue(method to interpret value unbers)

 

Deal function

 

Main Function

 

 

 

 

 

Step2: Fleshing out early program (make myHand)

 

//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

 

            getSuit (method to interpret suit numbers)

 

            getValue(method to interpret value unbers)

 

Deal function

 

//Main Function

var myHand = new Hand();

 

The main function immediately generates a hand “myHand” for the player.

Going to the Hand constructor, we know that we need two cards, and that they will be made by calling the deal function. I’ve included a console.log to immediately provide feedback showing that the cards have correct properties (simple numbers right now)

 

 

Step3: Generating cards with the ‘Deal’ function

 

//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

 

            getSuit (method to interpret suit numbers)

 

            getValue(method to interpret value unbers)

 

//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);

};

 

//Main Function

var myHand = new Hand();

 

Deal is called twice from the Hand Constructor. We should flesh out the deal function now. This function generates two random numbers (‘suit’ : 1-4) and (‘number’ : 1-13).

As before, I’ve added some console.log statements to make sure this is working correctly. I include a statement about being in the deal function as a check for how the program is walking through its steps.

Problem of Scope: am I making variables with appropriate scope in this function?

 

 
Leave a comment

Posted by on July 24, 2012 in Codecademy, Coding

 

Tags: , , , , , ,

Blackjack Flow

In order to make sense of the blackjack project, I made up this flow diagram to describe how the program creates ‘Hand’ objects consisting of two ‘card’ objects, and that these cards have properties that need to be generated and interpreted. The two properties are number (1-13, where 1 is Ace, 11 is Jack, 12, is Queen, etc) and suit (1-4, where each number is a given suit).

The properties of the cards can be set using a function, deal.

Once cards are made, they use two methods (because we are handling objects) to interpret the suit and value of each card.

Finally, we have two cards in a hand and can proceed to the next step…

Here’s a flow diagram of this process:Image

 
1 Comment

Posted by on July 24, 2012 in Codecademy, Coding

 

Tags: , , , ,

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){
//console.log(“I’m about to switch”);
switch (this.suit){
case 1:
this.suit = “clubs”;
break;
case 2:
this.suit = “diamonds”;
break;
case 3:
this.suit = “hearts”;
break;
case 4:
this.suit = “spades”;
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.

 
Leave a comment

Posted by on July 24, 2012 in Codecademy, Coding

 

Tags: , , , , , ,