RSS

Tag Archives: programming

Codecademy Problem

ImageI’ve mentioned many time on this blog how I am a big fan of codecademy. I think this is an excellent way to develop some coding skills in a simple hand-held way. I’ve been following the javascript track for some time and really enjoy it. Recently, Ruby and Python tracks have been introduced, although I have only given each one minimal attention so far. In the future, I am hoping to concentrate on the web programming elements in order to provide some context for the javascript work.

Presently, I have been stuck on a problem that should be easy, but the walkthrough does not seem to guide you in the right direction. The problem is intended to take a string of text and search for your name within this text. I think the difficulty arises from the fact that the walkthrough sounds like you should be solving the whole problem, but may actually be asking for less. 

Below, with a few extra details, I present a solution that effectively gets the job done. I’ve included some documentation so you can see what I was thinking as well as some debugging aids to visualize what the program is doing at each step.
ImageThis solution will get a ‘pass’ for the problem, but I am wondering how this might be simplified.
If you can simplify this solution, please leave a comment and I will update the code if significant advances are made.

/*jshint multistr:true */

var text = “hdajkslhgjalhjghjklhjackfghjdskghjkdfajlk”;  //the text we search through
var myName = “jack”;                                                  //what we are looking for
var hits = [];                                                                  //array storing match
var j=0;                                                                         //a counter for myName array

/////////first loop for match with myName[0]////////////
for (i=0;i< text.length;i++){
console.log(i); // enumerates position in text array
if (text[i] === myName[j]){

//////advances position in myName array, second loop continues search/////
for(var j=i;j<i +myName.length;j++){
console.log(“testing if”,text[j],” = “,myName[j-i]); //shows match test
if(text[j] != myName[j-i]){
hits.length = 0; //if no match, hits array is cleared
j=0; //array position in myName is reset
break; //exits inner loop
} //end ‘if no match’

if (text[j] === myName[j-i]){ //for each match, letter is pushed
hits.push(text[j]); //onto hits array
} //end ‘if match’
} //end inner loop

/////if reach size of myName array ////////
if (hits.length === myName.length){
console.log(“we found your name: “, hits); //output match
break;
} //end ‘size of array
}//end outer loop
}//end program

/* output
0
1
2
3
testing if j = j
testing if k = a
4
5
6
7
8
9
testing if j = j
testing if a = a
testing if l = c
10
11
12
13
testing if j = j
testing if g = a
14
15
16
testing if j = j
testing if k = a
17
18
19
20
testing if j = j
testing if a = a
testing if c = c
testing if k = k
we found your name: [ ‘j’, ‘a’, ‘c’, ‘k’ ]

*/

 
Leave a comment

Posted by on June 12, 2013 in Uncategorized

 

Tags: , , , ,

Code School Hall Pass

Code school is currently offering 48 hours of free access to their site. Code School has video classes covering Ruby, JavaScript, iOS and HTML. I just started mine tonight and I’m hoping to get a lot out of it and see if its something I can get more value from. If you’re interested in coding, I highly recommend looking into this site and also Codecademy

 
1 Comment

Posted by on March 18, 2013 in Uncategorized

 

Tags: , , , , ,

C++ coding in XCode

For some reason it took me forever to figure out how to use XCode as a C++ compiler. Recently, I figured it out and have worked on several projects without incident. 

I needed a compiler to use on my mac because I am currently taking a class on C++ algorithms that uses Microsoft Visual run on Windows. Not surprisingly, Visual doesn’t run on OSX (and it’s frankly too much effort and processing power to install windows on my mac and then run Visual through that. – possible, but unwieldy.) 

Frankly, once I started using Xcode for C++. I realized that it’s both simpler and better than Visual anyway. Especially for a new user: I’d rather spend my time and energy working in C++ than trying to figure out a complex compiler. So, without further ado…

Protocol:

1. Install XCode on your machine (it’s huge, so plan accordingly)

2. Open XCode and select File > New Project.

3. Select ‘Command Line Tool’ from OSX > Application

Image

4. Give your New Project a name

Image

5. Click Next and select a location to save your file (desktop / developer folder/ etc.)

Image

6.Your New Project will open and you just need to select main.cpp to get the familiar ‘hello world’ skeleton program.

Image

7. I erase the skeleton and replace it with a simple :

int main(){

return 0;

}

and start from there. 

8. In order to to compile and run your program, select Program > Build, correct any errors and Build again if necessary. Then hit the play button in the upper left corner of your window to run the program (intuitive is nice, isn’t it?)

Your program will run, giving you an output window at the bottom of the main window. You may need to click on that window in order to provide input if your program requires it.

I hope this helps you speed along to programming quicker and with less hunting than I had to do. I don’t know why I even tried to find something outside of XCode to start with.  

 
Leave a comment

Posted by on January 19, 2013 in Uncategorized

 

Tags: , , , , , , , ,

Two minutes into my latest coursera lecture….

Two minutes into my latest coursera lecture (introduction to interactive programming in python) the instructor indicated his frustration in javascript programming saying that it’s a terrible language. This may be the case… so far I don’t have a lot to compare against, but I have been enjoying learning JS in codecademy and I’m dying to know why he thinks so.

If you have experience programming in javascript and python (or other languages…ruby?) let me know if you agree with the above statement and what makes you think so. As a new programmer I am interested in learning as much as possible – if I can understand what faults people see in these languages I think that would be very instructive.

Thanks

 
3 Comments

Posted by on October 25, 2012 in Uncategorized

 

Tags: , , , , , , , , ,

Codecademy – Recursion explanation

Image

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.

 
Leave a comment

Posted by on September 21, 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: , , ,

Fun with Arrays

In Codecademy this week I’m studying Arrays in Javascript. Arrays were introduced much earlier in this track, but in this unit the subject is expanded to include Multi-dimensional Arrays, how to create them, add to them, splice out of them and loop through them.

Amazingly, this is not my first experience with Multi-dimensional Arrays – but it is the first time that I meant to do so. I had a lot of trouble with an outside project I was working on early in the ‘course’ and determined that I could solve my problem by creating one of these cool Array things I had just learned about. I don’t know how I did it, but I somehow kept creating a Multi-dimensional Array by accident. 

So – this is not my first time working with these constructs, but it is the first time I’ve known what I’m doing. Well, sort of. I find the most difficult thing to come to terms with is the syntax, not the concept. I would like to come up with another outside project dealing with these kind of Arrays and I would love some input. 

Requirements:

1. Simple concept, nothing mind-bending to comprehend

2. Something hard enough that it requires making and handling one or more Multi-Dimensional Array to Solve

3. Something that requires interesting enough manipulation of the arrays that solving the problem will leave me feeling confident in dealing with all aspects of the topic

Suggestions???

Image

One Kind of Array

 
Leave a comment

Posted by on September 1, 2012 in Uncategorized

 

Tags: , , , ,

Codecademy Problem

I’m stuck. In working on a section in codecademy’s JavaScript class relating to methods and objects that inherit these methods, I’ve become a bit confused. The problem that I’m on involves an original Object that is a Car that contains the method ‘accelerate.’ – I’m OK with this:

Objects are things within a class, i.e. they are all contain similar property. In this example our Objects are Cars. There can be many different kinds of cars, each with different property such as (different colors, makes, speed, etc). But they are all in the same ‘class’ (i.e. same kind of thing: cars)

Methods are Functions that manipulate Objects. In this example, if we have made a Car Object that has the property Blue and Ford and 10 mph, we can now do something with this object using a method. The codecademy project has us write an ‘accelerate’ Method that adds 10 to the speed. OK – so every time we send our car object to that method, it looks for the speed property and increases it by 10.

Inheritance is a way of making subsets of an Object so that they automatically get the properties that the ‘parent’ Object has, but can also be given new properties that the ‘parent’ Object does not have. In the present codecademy example, we are making an ElectricCar Object that inherits from the Car Object. No problem, this is a good example because we can easily see that it is still a kind of car and that means it makes sense that it should have all the same properties as other cars (color, make, speed – perhaps Blue, Chevy Volt, 20Mph) But we also want to give it properties that other cars do not have, like ‘electricity’ (meaning battery charge).

 

OK, here’s what I have with some notes to follow along an to indicate where I am having problems. Help is greatly appreciated.

 

function Car( listedPrice ) {
var price = listedPrice;
this.speed = 0;
this.numWheels = 4;

this.getPrice = function() {
return price;
};
}

Car.prototype.accelerate = function() {
this.speed += 10;
};

function ElectricCar( listedPrice ) {
var price = listedPrice;
this.electricity = 100;
}
ElectricCar.prototype = new Car();

// Write the accelerate method for ElectricCar here
myElectricCar.accelerate = function() {
this.speed += 20;
};

// Write the decelerate method for ElectricCar here
myElectricCar.decelerate = function(secondsStepped) {
this.speed -= (5*secondsStepped);
};

myElectricCar = new ElectricCar(500);

myElectricCar.accelerate();
console.log(“myElectricCar has speed ” + myElectricCar.speed);
myElectricCar.decelerate(3);
console.log(“myElectricCar has speed ” + myElectricCar.speed);

 

 

 
Leave a comment

Posted by on August 7, 2012 in Uncategorized

 

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

Traveling again

Harry and I have set out traveling again.

This time we’re visiting family on the East coast. We flew out on Friday – after waiting more than five hours in the airport for a late arrival of the previous flight, then a maintenance problem, then a needed part and finally for an entirely new plane. 

Once that was through, we had not further problems and got to BWI quickly. I rented a car and took the little guy to visit his grandparents (my in-laws)  outside of Washington DC while I visited friends and family in the Philly area. I have to say, I miss him very much already. He’s become more than a very small child without opinions and observations of his own and I enjoy having conversation with him through the day.

That said, I will be going to fetch him and bring him up to see my family later in the week. I think he’ll especially enjoy seeing his newest cousin, Nora, whom I met for the first time just today. Like her older sister, she’s completely adorable.

Until I bring him up, I have nothing but time to work on programming, DownHouse or school prep. I’ll see if I can’t return to getting the Mastermind game moved onto the DHS website and maybe even make some progress in getting it re-worked for an app as well.Image

 
Leave a comment

Posted by on July 23, 2012 in Personal Life

 

Tags: , , , ,