RSS

Tag Archives: computer

New Coding Challenge – The Quincunx!

ImageThe Quincunx – A triangular pegboard that will create a nice normal distribution as balls are dropped from the top and bounce down randomly over the triangular array of pegs.

Society’s greatest achievement, The Price Is Right, demonstrates the use of a plinko board in this video with the most excited player ever.

The coding challenge is to design a quincunx that demonstrates each of the following four points… No animation is required, simply (1)show the board as an array of X’s with a (2) user-determined number of rows (1-20) and the (3)resulting bins filling with integers as a (4)user-determined number of balls (1-100,000) is dropped. This time, I’m awarding prizes to the cleanest, most clearly documented entries in each language represented on Codecademy (Ruby, JS, Python).

As always, the prizes are bragging rights, presentation of your code on my blog with full attribution to you and a promo copy of any of my eBooks on iPad for you to share with the youngster in your life (or keep yourself). Each of my books presents educational material  in the form of a story (Heracles and the Gas Laws, Sisyphus and the Laws of Motion, Zombies and Fractions).

Happy Coding!

(submit your entries as links in the comments below)

 
Leave a comment

Posted by on July 10, 2013 in Uncategorized

 

Tags: , , , , , , , , ,

My own Coding Problem

ImageI’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]

But, I’m stuck already!

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.

//  Copyright (c) 2013 Jack Treml. All rights reserved.

//

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

 

Tags: , , , , , , , ,

Winding down Coding Challenge I

ImageI’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.

 
Leave a comment

Posted by on June 26, 2013 in Uncategorized

 

Tags: , , , , , , , , , , , , ,

Coding Challenge II: Make Mine a MASTERmind

ImageDon’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.

 
Leave a comment

Posted by on June 22, 2013 in Uncategorized

 

Tags: , , , , , , , ,

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.

 

 
Leave a comment

Posted by on September 5, 2012 in Uncategorized

 

Tags: , , , , , ,

Alice

Today was day 3 of Programming Fundamentals, which uses the teaching program, Alice to introduce programming concepts to beginner students. It’s pretty easy overall – most of the complications I face have to do with just finding the right command or dealing with the program itself – not problems with the concepts taught.

Today we played with ‘Events’ – these are things that are done following some trigger. Technically, an event trigger occurs (press of a button, the world starts, etc) and this precipitates the event handler (the code that should play following the event). Doing this in Alice makes it very easy to manipulate objects in a world – even creating whole functional worlds with a lot going on.

I’m considering coming up with a project that this language can handle – something simple, like a zombie simulation. I’ll post my results.

If you have any experience coding in Alice, let me know if you ever did anything with it beyond playing around and learning intro stuff.

 

 

 
Leave a comment

Posted by on September 5, 2012 in Uncategorized

 

Tags: , , ,