Code Monkey home page Code Monkey logo

cs50's People

Contributors

elisabethirgens avatar

Stargazers

 avatar

Watchers

 avatar

cs50's Issues

Figure out recursion for filter-helpers.c

There are an awful lot of cringy repetition in my functions, pretty sure that something more recursive would be better — just need to wrap my head around recursion.

Improve credit.c

This problem is from week 1, before we have gotten to for example arrays or functions, so I wanted to solve it using only the concepts that have been introduced so far in the program: data types, conditional statements and loops.

Even with these limitations, I think there are more potential improvements:

  • Can the checksum code be simplified with fewer variables? More resursive somehow?
  • Could the two while loops be combined to handle alternating digits?
  • Would a switch at the end be more readable than this weird nesting of if else?

Might also be interesting to see which other refactoring comes naturally after more weeks of programming with C.

Limit ascii range for caesar.c

Aaaaah 🤯 😁 after wild detours into multiple dead ends debugging competely different self-created problems and interesting misunderstandings (hello octal ascii) and dreaming about casting of characters while sleeping — I finally understand why I couldn't get the % 26 formula to work for https://cs50.harvard.edu/x/2020/psets/2/caesar/

string plain = get_string("plaintext: ");
int n = strlen(plain); // Get length of text provided as command-line argument
string cipher = plain; // Make a string copy to encrypt char by char in loop

for (int i = 0; i < n ; i++)
{

    if (islower(plain[i]) || isupper(plain[i]))
    {
        cipher[i] = (plain[i] + key); // Naive implementation to increment by key value
    }

}
printf("ciphertext: %s\n", cipher);

☝️ This will succesfully handle the strings, characters and loop logic. But it doesn't know how to wrap around the correct subset of characters in the ascii chart, so for example Z 90 becomes [ 91 instead of A 65

What I misunderstood

I thought implementing the formula: cᵢ = (pᵢ + k) % 26 would magically handle more than it does. I was trying to use the formula, thinking that the math in the formula itself would handle wrapping around for example Z to A. Lol nope. This misunderstanding caused a lot more confusion while debugging, but now all is clear.

What I understand now

In addition to using the formula, I need to add some code to limit the available characters to uppercase and lowercase letters in the ascii chart. A-Z are found at 65-90, a-z is 97-122.

The % 26 to find remainder when dividing by 26 will work when that math happens within the correct ranges.

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.