Learning to Code: Week 4 – Algorithms

Algorithms Summary

6/1/16 – Continued

6/2/16

  • Shawn helped me look into making a function for the water.c program. I worked on it for a bit.
  • Error 1 – 0 arguments instead of 2.

image08

  • Error 2 – variable bottles but nothing there. Call the function INTO that variable?

image28

  • Boom, got it to work. That was pretty cool.
    • Now I can account for variable shower heads.
    • I think the input question could be better to explain what the metric is for waterflow etc.
  • I didn’t think of rounding errors.

Code Here: https://gist.github.com/CraigRodrigues/aeff70c00abf8fd4628f19e57c837a55

I put a function in Caesar.c!

Code Here: https://gist.github.com/CraigRodrigues/6e3724fbe3bd5fed9d05d2cafcedbc5c

  • Not sure about the names of the variables though. I think it works okay.
  • Also not sure if functions should be in printf or on a separate line going into another variable?
    • Example: printf(“%c”, encryptCode(code[i], k));

6/3/16

  • Got stuck in a few places on Credit but eventually solved it after working on it for an hour this morning. I need to extract a few functions to make the program really the way I want it, but right now it functionally is correct!
  • That one was tough. I decided to use arrays, but I’m not sure if that is ideal. I feel like there are easier ways to manipulate digits of a number without putting each digit into a slot in an array.
  • So far I am using a massive number of printf statements inside my program to be able to check if things are working. Don’t know of a better way at the moment.
  • See below for what I did.

image07

STEPS:

  1. Prompt user for credit card number. (long long)
  2. Check length of the card #. If not valid print INVALID.
  3. Function: cardLength to get the number of digits in a card number using Log10 + 1.
  4. Put the digits into an array by using % and a loop and /10 etc.
  5. Loop from the back to the front of the new array of digits and take every other digit.
  6. Multiply the results of 5 by 2.
  7. Add the individual digits together (what do you do about double digits?)
  8. Add to #7 the sum of the remaining digits.
  9. If answer to #8’s final digit is a zero then the card number is legit.
  10. Check the card’s first digit(s) to get the card company.
  11. If #9 & #10 are true then print out what card company it is and return 0.

NOTES/THOUGHTS:

  • Had to do a bit of research for the questions I had such as how to find out the number of digits in an integer and how to put the individual digits of a number into an array. Once I found those it was just a matter of piecing the puzzle together doing down the list of steps above.
  • One loop I had initialized the counter i inside the loop itself so it actually never incremented. That took a while to see, yet it was such a simple problem!
  • I had to figure out how to reverse a loop to put the digits into it back to front instead of front to back.
  • Put “return 1” in a couple of places when the program really wanted “return 0” regardless.
  • Need to remember that using the data type int only takes the integer portion not anything after the decimal. Useful to remember for this problem set.
  • Bought the Grokking Algorithms book as next lecture begins to talk about them and big O notation.

6/4/16

Nothing today 😢

6/5/16

Week 3 – Lecture 1

  • Bubble sort: Taking two things and comparing them then moving through the array. If not sorted after that doing it again. Pretty damn slow.
  • Selection sort: going through the list and selecting the next smallest element and moving it to the front.
  • Insertion sort: Go through the list and insert the smallest number where it should be, pushing everything else forward. Don’t have to go back through the list more than once.
  • Big O describes the upper bound on an algorithm. Bubble Sort, selection and insertion sort big O is n2.
  • Big O = worst case scenario.
  • Binary Search has a Big O of n.
  • Binary search: Divide and conquer (phone book example) is O = log n since the list gets half as small every time.
  • Ω (omega) is the lower bound on an algorithm. Best case scenario.
  • Video: What Different Sorting Algorithms Sound Like
  • Video: Why Study Algorithms?

Week 3 – Lecture 2

image12

6/6/16

Very cool blog posts from someone who successfully followed the path I want to (also very intimidating):

If you’re struggling to understand a concept in a book or on Wikipedia, look up Youtube videos of people explaining it in different ways until you get it. I’ve found this strategy amazingly effective.

SECTION VIDEOS & SHORTS

  • GDB
  • Computational Complexity
  • Selection Sort
  • Insertion Sort
  • Merge Sort
  • Linear Search
  • Binary Search
  • Algorithms Summary
  • Asymptotic Notation
  • Quicksort

Click Here for my Handwritten Notes

6/7/16

  • Worked on pset 3 Game of Fifteen.
  • Binary Search Trees
  • Another video on debugging with GDB (23 min). Incredibly useful video.
  • An unsigned int is just an int that cannot be negative.
  • Finished generate.c by adding comments to distribution code.
  • https://reference.cs50.net/stdlib.h/drand48 was a useful resource.

6/8/16

  • Worked on implementing Insertion sort – pseudocode from wikipedia really helped me visualize how to get it to work.

image09

If you somehow stumbled upon my blog and read this far and have questions just hit me up on Twitter or just email me!