-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f434fbf
Showing
175 changed files
with
1,082,074 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Pset1 - cash | ||
// A program that prompts users for a value in dollars | ||
// and returns a value that represents the minimal amount of coins this dollar value can be reached | ||
|
||
#include <math.h> // round() | ||
#include <cs50.h> // get_float | ||
#include <stdio.h> | ||
|
||
int main(void) | ||
{ | ||
// 1 - prompt user for value in dollars (d) | ||
float d; | ||
do | ||
{ | ||
d = get_float("Change owed: "); | ||
} | ||
while (d < 0); // reprompt if value is negative | ||
|
||
// 2 - convert dollars to cents (c) | ||
int c = round(d * 100); | ||
|
||
// 3 - algorithm to determine least amount of coins | ||
|
||
// 3 - 1 25c quarter (q) | ||
int q = c / 25; | ||
int x = c - q * 25; | ||
|
||
// 3 - 2 10c dime (m) | ||
int m = (x / 10); | ||
x = x - m * 10; | ||
|
||
// 3 - 3 5c nickel (n) | ||
int n = (x / 5); | ||
x = x - n * 5; | ||
|
||
// 3 - 4 1c penny (p) | ||
int p = (x / 1); | ||
x = x - p; | ||
|
||
// 4 total (t) the number of each coin and print this value as minimal number of coins | ||
int t = q + m + n + p; | ||
printf("%i\n", t); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Pset1 - credit | ||
// design a program to prompt user for a credit card number then report whether it is American Express, Visa, Mastercard, or invalid. | ||
|
||
// 1 - prompt user | ||
// 2 - checksum | ||
// 3 - count number of digits | ||
// 4 - if and else conditions to filter for VISA, AMEX, or MASTERCARD | ||
|
||
#include <cs50.h> // get_long_long | ||
#include <stdio.h> | ||
|
||
int main(void) | ||
{ | ||
// 1.0 Prompt user for a credit card number (cc) | ||
long long cc = get_long_long("Number: "); | ||
|
||
// 2.0 Checksum | ||
// 2.1 Define new variables | ||
long long cc1 = cc; // copy cc so it remains unchanged as cc1 will be updated | ||
int checksum = 0; // starting value of checksum | ||
|
||
// 2.2 while() loop to perform checksum arithmetic | ||
while (cc1 > 0) | ||
{ | ||
int a = cc1 % 10; // get first digit (to be added to checksum, +checksum) | ||
cc1 = cc1 / 10; | ||
|
||
int b = cc1 % 10 * 2; // get second-to-last digit and mulitply by 2 (may result in 2 digits) | ||
int c = b % 10; // get last digit of a (+checksum) | ||
int d = b / 10 % 10; // get first digit of a (+checksum) | ||
|
||
checksum = checksum + a + c + d; | ||
cc1 = cc1 / 10; | ||
|
||
} | ||
|
||
// 3.0 Count number of digits using while loop | ||
long long cc2 = cc; // same principle as cc1 | ||
int digit = 0; // digit counter | ||
|
||
while (cc2 > 0) | ||
{ | ||
cc2 = cc2 / 10; | ||
digit++; | ||
} | ||
|
||
// 4.0 VISA, AMEX, or MASTERCARD? | ||
if (checksum % 10 == 0) // Select for numbers that pass checksum | ||
{ | ||
// Select for numbers with digits 13, 15, OR 16 then sort further | ||
if (digit == 13) // Select for 13-digit number | ||
{ | ||
if (cc / 1000000000000 == 4) | ||
{ | ||
printf("VISA\n"); | ||
} | ||
else | ||
{ | ||
printf("INVALID\n"); | ||
} | ||
} | ||
else if (digit == 15) // Select for 15-digit number | ||
{ | ||
if (cc / 10000000000000 == 34 || cc / 10000000000000 == 37) | ||
{ | ||
printf("AMEX\n"); | ||
} | ||
else | ||
{ | ||
printf("INVALID\n"); | ||
} | ||
} | ||
else if (digit == 16) // Select for 16-digit number | ||
{ | ||
int mc = cc / 100000000000000; | ||
if (mc == 51 || mc == 52 || mc == 53 || mc == 54 || mc == 55) | ||
{ | ||
printf("MASTERCARD\n"); | ||
} | ||
else if (cc / 1000000000000000 == 4) | ||
{ | ||
printf("VISA\n"); | ||
} | ||
else | ||
{ | ||
printf("INVALID\n"); | ||
} | ||
} | ||
else | ||
{ | ||
printf("INVALID\n"); // If number is not 13, 15, or 16 digits-long, INVALID | ||
} | ||
} | ||
else | ||
{ | ||
printf("INVALID\n"); // If it doesn't pass checksum, INVALID | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// pset1: Mario (less) | ||
// create a program, utilising printf(), that prints out a half-pyramid of a user-defined height, displayed as # | ||
// e.g. | ||
// # | ||
// ## | ||
// ### | ||
// #### | ||
// ##### | ||
|
||
// Specifications: integer between 1 and 8 | ||
|
||
#include <cs50.h> // for get_int() | ||
#include <stdio.h> | ||
|
||
int main(void) | ||
{ | ||
// do-while loop | ||
int h; | ||
do | ||
{ | ||
// prompt once for integer h | ||
h = get_int("Height: "); | ||
} | ||
// reprompt if h is not between 1 and 8 | ||
while (h < 1 || h > 8); | ||
|
||
// define y, which will be used, as a separate decreasing counter with an initial value h, to determine number of spaces | ||
int y = h; | ||
|
||
// print pyramid using for loop | ||
// define number of lines (pyramid height) based on h | ||
for (int i = 0; i < h; i++) | ||
{ | ||
// print spaces | ||
for (int a = 1; a < y; a++) | ||
{ | ||
printf(" "); | ||
} | ||
//after exiting loop (or effectively, one pyramid row), decrease value of y by 1 | ||
y--; | ||
|
||
// print hashtags | ||
for (int b = 0; b < i + 1; b++) | ||
{ | ||
printf("#"); | ||
} | ||
|
||
// next line | ||
printf("\n"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// pset1: Mario | ||
// create a program, utilising printf(), that prints out a pyramid of a user-defined height, displayed as # | ||
// e.g. | ||
// # # | ||
// ## ## | ||
// ### ### | ||
// #### #### | ||
// ##### ##### | ||
|
||
// Specifications: accepts integer between 0 and 8 | ||
|
||
#include <cs50.h> // for get_int() | ||
#include <stdio.h> | ||
|
||
int main(void) | ||
{ | ||
// do-while loop | ||
int h; | ||
do | ||
{ | ||
// prompt once for integer h | ||
h = get_int("Height: "); | ||
} | ||
// reprompt if h is not between 0 and 8 | ||
while (h < 1 || h > 8); | ||
|
||
// define y, which will be used, as a separate decreasing counter with an initial value h, to determine number of spaces | ||
int y = h; | ||
|
||
// print pyramid using for loop | ||
// define number of lines (pyramid height) based on h | ||
for (int i = 0; i < h; i++) | ||
{ | ||
// this block of code codes for one row of the pyramid | ||
// print spaces | ||
for (int a = 1; a < y; a++) | ||
{ | ||
printf(" "); | ||
} | ||
//after exiting loop (or almost effectively, one pyramid row), decrease value of y by 1 | ||
y--; | ||
|
||
// print hashtags (left) | ||
for (int b = 0; b < i + 1; b++) | ||
{ | ||
printf("#"); | ||
} | ||
|
||
// 2 space gap | ||
printf(" "); | ||
|
||
// print hashtag (right) | ||
for (int c = 0; c < i + 1; c++) | ||
{ | ||
printf("#"); | ||
} | ||
|
||
// next line | ||
printf("\n"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
https://docs.cs50.net/2018/x/psets/2/caesar/caesar.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Pset2: Caesar | ||
// Implement program that encrypts messages using Caesar's ciper | ||
// idea is that the ciper (k), a non-negative integer, added to a message will return the cipher text | ||
|
||
#include <cs50.h> // get_string | ||
#include <stdio.h> | ||
#include <stdlib.h> // atoi | ||
#include <string.h> // strlen() | ||
#include <ctype.h> // isalpha, islower, isupper | ||
|
||
#define index_upper 65 // + to convert ASCII to indexed alphabet, - to conver indexed alphabet to ASCII | ||
#define index_lower 97 // " " | ||
#define alpha_n 26 // number of alphabet letters | ||
|
||
int main(int argc, string argv[]) | ||
{ | ||
// if more than one command-line argument is typed, print error message and return main value of 1 | ||
if (argc >= 3) | ||
{ | ||
printf("error: please input a single secret key\n"); | ||
return 1; | ||
} | ||
else if (argc < 2) | ||
{ | ||
printf("error: please enter secret key\n"); | ||
return 1; | ||
} | ||
else | ||
{ | ||
// store user-input plaintext as string | ||
string txt = get_string("plaintext: "); | ||
|
||
// convert k from string to int for cipher algorithm | ||
int k = atoi(argv[1]); | ||
|
||
// for loop to scan for alphabetical characters and replace with ciphered text | ||
for (int i = 0; i < strlen(txt); i++) | ||
{ | ||
char p = txt[i]; | ||
if (isalpha(p)) | ||
{ | ||
if (isupper(p)) | ||
{ | ||
// cipher algorithm + replace i-th position of text | ||
txt[i] = (p + k - index_upper) % alpha_n + index_upper; | ||
} | ||
else if (islower(p)) | ||
{ | ||
// cipher algorithm + replace i-th position of text | ||
txt[i] = (p + k - index_lower) % alpha_n + index_lower; | ||
} | ||
} | ||
} | ||
// print ciphertext output | ||
printf("ciphertext: %s\n", txt); | ||
|
||
return 0; | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
https://docs.cs50.net/2018/x/psets/2/crack/crack.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Pset 2: Crack | ||
// Implement a program that cracks passwords encrypted using a "one-way hash function" | ||
|
||
#define _XOPEN_SOURCE // crypt() | ||
|
||
#include <cs50.h> // string | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <unistd.h> // crypt() | ||
|
||
int main(int argc, string argv[]) | ||
{ | ||
// 1.0a Select for valid hashed password input | ||
if (argc == 2) | ||
{ | ||
// 2.0 Extract salt from hash input | ||
string hash = argv[1]; | ||
char b[3] = {hash[0], hash[1], '\0'}; // extract first two characters from hash input and store in array | ||
string salt = b; // combine first two characters from hash input into a string | ||
|
||
// 3.0 generate random plaintext to feed into crypt() | ||
// 3.1 declare alphabet and empty array to store random plaintext | ||
char alphabet[] = {"\0ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}; | ||
char p[6] = {'\0', '\0', '\0', '\0', '\0', '\0'}; | ||
|
||
// 3.2 counter for 5th letter | ||
for (int letter_five = 0; letter_five < 53; letter_five++) // 52 is the length of variable alphabet including the initial '\0' | ||
{ | ||
// 3.3 counter for 4th letter | ||
for (int letter_four = 0; letter_four < 53; letter_four++) | ||
{ | ||
// 3.4 counter for 3rd letter | ||
for (int letter_three = 0; letter_three < 53; letter_three++) | ||
{ | ||
// 3.5 counter for 2nd letter | ||
for (int letter_two = 0; letter_two < 53; letter_two++) | ||
{ | ||
// 3.6 counter for 1st letter | ||
for (int letter_one = 1; letter_one < 53; letter_one++) | ||
{ | ||
p[0] = alphabet[letter_one]; | ||
p[1] = alphabet[letter_two]; | ||
p[2] = alphabet[letter_three]; | ||
p[3] = alphabet[letter_four]; | ||
p[4] = alphabet[letter_five]; | ||
|
||
if (strcmp(hash, crypt(p, salt)) == 0) | ||
{ | ||
printf("%s\n", p); | ||
return 0; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
printf("hash: %s\n", hash); | ||
return 0; | ||
} | ||
// 1.0b If invalid input, print error message and return 1 | ||
else | ||
{ | ||
printf("error: input valid hashed password\n"); | ||
return 1; | ||
} | ||
} |
Oops, something went wrong.