# Learning to Code: Week 6 – Codecademy

**6/15/16 – Continued**

Read chapter 6 of Grokking Algorithms

- Breadth-first Search
- What are graphs
- Directed graphs vs Undirected graphs
- Queues
- Queues (FIFO) vs Stack (LIFO)
- Implementing the algorithm in Python
- Topological sort
- Trees

**6/16/16**

- Website discovered: http://c.learncodethehardway.org/book/index.html (free resource)
- He says:
*“LCTHW will not be for beginners, but for people who have at least read LPTHW or know one other programming language.”* - Hopefully I can get something out of it regardless.
- LCTHW Exercise 1: Dust Off That Compiler

- He says:
- Didn’t get much done today. Pretty bummed about it. 😕

**6/17/16**

- Since I have the Javascript meetup coming up, I started on the Codecademy Javascript course just to mess around.
- I don’t like to jump around too much though so I don’t think I’ll go too deep into it, only up until the workshop. CS50 is my main focus.
- Is console.log same as the cs50 printf function?

CS50 walkthrough videos:

- Sigma function
- Swapping
- Copying
- Compare
- Structs
- Pointers

- Coursera is changing platforms and getting rid of 400+ courses so I downloaded a few good ones before they are gone:
- Algorithms Part I by Kevin Wayne and Robert Sedgewick (Princeton University)
- Algorithms Part II by Kevin Wayne and Robert Sedgewick (Princeton University)
- Compilers by Alex Aiken (Stanford University)
- Introduction to Mathematical Thinking by Dr. Keith Devlin (Stanford University)

**6/18/16**

**Finished entire Codecademy Javascript Unit #1 – INTRODUCTION TO JAVASCRIPT**- Breezing through it since it’s all a refresher from CS50 just with different style/words.

**Worked on Reddit Daily Programmer problem “CRITICAL HIT”. It is classified as easy. Also it can be solved with recursion.**- You can see the problem here: https://www.reddit.com/r/dailyprogrammer/comments/4nvrnx/20160613_challenge_271_easy_critical_hit/

Critical hits work a bit differently in this RPG. If you roll the maximum value on a die, you get to roll the die again and add both dice rolls to get your final score. Critical hits can stack indefinitely — a second max value means you get a third roll, and so on. With enough luck, any number of points is possible.

- My only hangup is figuring out the probability formula. I feel like it would be easy to code once I know what the correct formula is to get the correct output all the time.
- Finished my solution to CRITICAL. You can find it HERE with all of my notes along with it

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include <stdio.h> #include <cs50.h> #include <math.h> /* [2016-06-13] Challenge #271 [Easy] Critical Hit * http://bit.ly/1sLH0bn * * Critical hits work a bit differently in this RPG. If you roll the maximum value on a die, you * get to roll the die again and add both dice rolls to get your final score. Critical hits can * stack indefinitely -- a second max value means you get a third roll, and so on. With enough * luck, any number of points is possible. * */ double p = 0; // probability of a favorable outcome double probability(double d, double h) { if (d < h) { return ((1/d) * probability(d,(h-d))); // recursive case. } if (d >= h) { return ((d-h)+1)/d; // base case of a favorable outcome in the number of sides is >= hp left } return 0; } int main(void) { // Disable stdout buffering setvbuf(stdout, NULL, _IONBF, 0); // Put the die numbers and hp numbers into arrays int arrd[] = {4,4,4,4,1,100,8}; int arrh[] = {1,4,5,6,10,200,20}; // Need to loop through arrays to be able to print out the outputs for (int i = 0; i < 7; i++) { // d and h just look better to me than using the arrd and arrh double d = arrd[i]; double h = arrh[i]; p = probability(d,h); printf("d = %i, h = %i, output = %.9f\n", arrd[i], arrh[i], p); //print to 9 decimal places } } |

I am using CS50’s library as that is the only programming class I’m taking at the moment and am only on week 4. I am sure there is a way to simplify the probability formula as well.

Biggest hurdle was thinking about what the recursive case and base case would be. The twist is that there is always a chance if you roll the largest number on the die so (1/d) * (the next probability) and so on and so forth.

I am not so good with understanding how to use the data types so I chose double for everything since a lot of division was involved. Also I don’t know a good way to format the output. I don’t know how to put it into a nice grid or anything like that.

Also I should be dealing with memory allocation and clearing, but I only just learned how in CS50 and haven’t implemented it over there yet.

- It was actually a little bit easier to conceptualize what to do then to actually implement it.
- I am not comfortable with the various data types, memory allocation (I didn’t use at all), or really if I structured my recursive function correctly.
- I am proud that I didn’t look at anyone’s answers and figured this one out on my own.

- Scratch notes here.
- I brushed up on my probability here: http://www.dummies.com/how-to/content/how-to-determine-probable-outcomes-with-coins-and-.html
- Then I looked up how to limit the number of decimal places printed here: http://stackoverflow.com/questions/1343890/rounding-number-to-2-decimal-places-in-c
- Overall I had fun doing the challenge. I think I will try this one next! https://www.reddit.com/r/dailyprogrammer/comments/4lpygb/20160530_challenge_269_easy_basic_formatting/

**Coding in Real Life vs Coding in the Movies (I find this to be true)**

**6/19/16**

- Read Chapters 4-8 in CODE book.
- Part of chapter 7 in Grokking Alg: Dijkstra’s Algorithm
- Seems simple in theory, but I am having a hard time wrapping my head around doing it step by step (fastest path/cheapest path through a weighted graph).
- I’ll need to reread I think.

**6/20/16**

Week 4 – Section Videos

- Hexadecimal (10min)
- Pointers (Long but important – 30min)

**6/21/16**

Didn’t get anything in today! 😓

**6/22/16**

**Was able to get up at 6:30 AM this morning for once.**

Continuing JS course on Codecademy to prepare a little more for the workshop tomorrow evening:

- An entire function ends in a ;
- No mention of data types yet
- DRY principle (Don’t Repeat Yourself) = Shawn mentioned this, but any time you find yourself typing the same thing, but modifying only one small part, you can probably just use a function.
- Scope = global vs local variables

*Finished Unit 2: Introduction to Functions in JS*

Simple “Rock, Paper, Scissors” game here: https://gist.github.com/CraigRodrigues/067ffb27ae2cd18c1c9676286ea8502e

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); if (computerChoice < 0.34) { computerChoice = "rock"; } else if (computerChoice <= 0.67) { computerChoice = "paper"; } else { computerChoice = "scissors"; } console.log("Computer: " + computerChoice); var compare = function(choice1,choice2){ if (choice1 === choice2) return ("The result is a tie!"); if (choice1 === "rock") { if (choice2 === "scissors") return ("Rock wins!"); else return ("Paper wins!"); } if (choice1 === "paper") { if (choice2 === "scissors") return ("Scissors wins!"); else return ("Paper wins!"); } if (choice1 === "scissors") { if (choice2 === "rock") return ("Rock wins!"); else return ("Scissors wins!"); } }; console.log(compare(userChoice,computerChoice)); |