Craig Rodrigues!

Learning to Code: Week 9 - Python?

7/8/16

CS50 Week 5

  • Watched lecture 1 (56m)

Found this website that indexes the popularity of various programming languages: http://www.tiobe.com/tiobe_index

Trying Daily Programmer Challenge #273 - Degree

  • Learned about scanf and its capabilities. Very cool. http://beej.us/guide/bgc/output/html/multipage/scanf.html
  • Wrote up the program on scratch paper while on break at work.
  • Everything seems to be working okay so far except when I use strcmp on “rd” it still thinks “dr” is acceptable?
  • Well it seems I should’ve read more about strcmp
// this is a little weird...but if the strings are the same, it'll
// return zero, which can also be thought of as "false".  Not-false
// is "true", so (!strcmp()) will be true if the strings are the
// same.  yes, it's odd, but you see this all the time in the wild
// so you might as well get used to it:

if (!strcmp(s1, s3))
   printf("The strings are the same!")

if (!strncmp(s1, s2, 6))
   printf("The first 6 characters of s1 and s2 are the same");
  • Program is working now for degree and radians. I may try to tackle the bonus as well a little bit later as it would only require adding in more formulas.
  • A function would probably help that just used all that stuff and returned the conversion just to look cleaner?
  • I am not sure how to deal with decimals or rounding here. The output for rd is just to a whole number, but if I do %.2f I’ll always get a trailing 180.00.
  • Google to rescue again for my question above. I can use %g! http://stackoverflow.com/questions/9628645/use-printf-to-format-floats-without-decimal-places-if-only-trailing-0s
  • Still not sure how well %g will work well since precision is impaired.
  • http://stackoverflow.com/questions/277772/avoid-trailing-zeroes-in-printf WTF O_o
  • For the bonus attempt I’m going to write a function to convert any of the provided inputs. For converting Kelvin I’ll just have the function call itself to convert F to C or C to F.
  • Also that way you can add other units down the line like inches to feet or inches to cm.
  • http://www.almanac.com/content/temperature-conversion
  • Kept getting 0 as an output in my program, then remembered my functions have to return something. Doh.
  • My program works for all bonuses except I can’t seem to do a recursive call on my own function. Perhaps since there isn’t a base case...maybe it goes on forever somehow?
  • Debugging my function continuously calls itself instead of calling the cf one and returning a value…
  • Here is my code so far with the recursive call causing a segmentation fault :(

/**
 * degree.c
 *
 * Reddit Daily Programmer Challenge #273 [EASY]
 * Getting a degree
 *
 * https://www.reddit.com/r/dailyprogrammer/comments/4q35ip/20160627_challenge_273_easy_getting_a_degree/
 * 
 */

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define pi 3.141592

char units[2];
float num;
float conversion;

float convert(float* numbers, char* conversion_units);

int main(void)
{
    while (scanf("%f%s", &num, units) == 2)
    {
        float answer = convert(&num, units);
        if (answer != -1)
            printf("%2g%c\n", answer, units[1]);
    }
    return 0;
}

float convert(float* numbers, char* conversion_units)
{
    if (!strcmp(units,"dr")) // degrees to radians
    {
        return *numbers * (pi / 180.0);
    }
    else if (!strcmp(units,"rd")) // radians to degrees
    {
        return *numbers * (180.0 / pi);
    }
    else if (!strcmp(units,"cf")) // celsius to farenheit
    {
        return *numbers * 1.8 + 32;
    }
    else if (!strcmp(units,"fc")) // farenheit to celsius
    {
        return (*numbers - 32) * 0.5556;
    }
    else if (!strcmp(units,"ck")) // celsius to kelvin
    {
        return *numbers + 273.15;
    }
    else if (!strcmp(units,"kc")) // kelvin to celsius
    {
        return *numbers - 273.15;
    }
    else if (!strcmp(units,"fk")) // farenheit to kelvin
    {
        return ((convert(numbers, "fc")) + 273.15);
    }
    else if (!strcmp(units,"kf")) // kelvin to farenheit
    {
        return *numbers * 1.8 - 459.67;
    }
    else
        printf("No candidate for conversion\n");
        return -1;

    return 0;
}

 

7/9/16

image16

  • Also I’m not sure, but I probably should be reversing the ACTUAL string and not putting it into another variable? All the stuff I’ve seen on StackOverflow so far that deal with the same question have been doing that.
  • So from reading a better way to do this is to take the first index and last index and swap. Then go to the next two letters and swap until you hit a middle letter or your indexes swap using pointers to swap the letters.
  • Ok I finally got a functional reverse function in C and it only took 2 hours!
  • That problem was a bitch and half to solve, but I did learn quite a few things especially a little bit more clarification about pointers.
  • While I didn’t use it in my FOR loop I also learned you can decrement/increment more than one variable at a time. Pretty baller imo.
  • Bitwise video: https://www.youtube.com/watch?v=d0AwjSpNXR0
  • Reverse function in C posted here
  • There is no check for a null string.
  • Did another really quick and simple leet code problem here: Nim Game
  • Solution is:
bool canWinNim(int n) {
   return n % 4 != 0;
}

7/10/16

Today I didn’t do much coding, but I did do a little more research into this 4 week intensive online Python class/workshop.

This course seems really compelling to me especially if I can finish CS50 and run through a bit of Python before I start it. It looks intense and it gives me access to mentors almost anytime I want to ask questions.

It doesn’t seem that expensive either if I didn’t get a scholarship thing. It would require 4 mandatory days a week, but if I want to get more serious and get more time in I feel that is what is required.

Then again it could just be some cash grab BS course, which I don’t want. It’s hard to tell who is a legit good developer when I don’t know shit. :(

7/11/16

  • Didn’t get anything done today, but in the future I would LOVE to take this master’s degree program at GA Tech online. http://www.omscs.gatech.edu/explore-oms-cs
  • Maybe just to prove to myself that I can do it. Do anything I set my mind to.

7/12/16

7/13/16

image19

Learning to Code: Week 10 - Understand the Whole Problem

Learning to Code: Week 8 - Recover