elisabethirgens / cs50 Goto Github PK
View Code? Open in Web Editor NEWTrack my progress, keep some of the code and gather notes from CS50's Introduction to Computer Science
Track my progress, keep some of the code and gather notes from CS50's Introduction to Computer Science
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
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.
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.
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:
while
loops be combined to handle alternating digits?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.
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.