├── README └── introduction-to-computer-science └── edx_cs50 ├── psets ├── pset0 │ ├── README │ └── pset0.pdf ├── pset1 │ ├── README │ ├── pset1.pdf │ ├── hacker1.pdf │ ├── hello.c │ ├── water.c │ ├── quiz │ ├── mario.c │ └── greedy.c ├── pset2 │ ├── pset2.pdf │ ├── hacker2.pdf │ ├── initials.c │ ├── quiz │ ├── caesar.c │ └── vigenere.c ├── pset3 │ ├── pset3.pdf │ ├── hacker3.pdf │ ├── fifteen │ │ ├── Makefile │ │ ├── 3x3.txt │ │ ├── questions.txt │ │ ├── 4x4.txt │ │ └── fifteen.c │ └── find │ │ ├── Makefile │ │ ├── helpers.h │ │ ├── find.c │ │ ├── generate.c │ │ └── helpers.c ├── pset4 │ ├── pset4.pdf │ └── hacker4.pdf ├── pset5 │ └── pset5.pdf ├── pset6 │ └── pset6.pdf ├── pset7 │ └── pset7.pdf ├── pset8 │ └── pset8.pdf ├── project │ └── project.pdf └── README └── README /README: -------------------------------------------------------------------------------- 1 | Open Source Society University 2 | https://github.com/open-source-society/computer-science 3 | https://ossu.firebaseapp.com/#/curriculum -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset0/README: -------------------------------------------------------------------------------- 1 | http://cdn.cs50.net/2016/x/psets/0/pset0/pset0.pdf 2 | 3 | https://x.cs50.net/2016/psets/0/ -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset1/README: -------------------------------------------------------------------------------- 1 | http://cdn.cs50.net/2016/x/psets/1/pset1/pset1.pdf 2 | 3 | https://x.cs50.net/2016/psets/1 -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset0/pset0.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/pset0/pset0.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset1/pset1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/pset1/pset1.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset2/pset2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/pset2/pset2.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset3/pset3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/pset3/pset3.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset4/pset4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/pset4/pset4.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset5/pset5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/pset5/pset5.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset6/pset6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/pset6/pset6.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset7/pset7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/pset7/pset7.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset8/pset8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/pset8/pset8.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset1/hacker1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/pset1/hacker1.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset2/hacker2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/pset2/hacker2.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset3/hacker3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/pset3/hacker3.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset4/hacker4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/pset4/hacker4.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/project/project.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jivoi/ossu/HEAD/introduction-to-computer-science/edx_cs50/psets/project/project.pdf -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset1/hello.c: -------------------------------------------------------------------------------- 1 | // check50 2015.fall.pset1.hello hello.c 2 | // :) hello.c exists 3 | // :) hello.c compiles 4 | // :) prints "hello, world\n" 5 | 6 | #include 7 | 8 | int main(void) 9 | { 10 | printf("hello, world\n"); 11 | } -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset3/fifteen/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile 3 | # 4 | # Computer Science 50 5 | # Problem Set 3 6 | # 7 | 8 | fifteen: fifteen.c 9 | clang -ggdb3 -O0 -std=c11 -Wall -Werror -o fifteen fifteen.c -lcs50 -lm 10 | 11 | clean: 12 | rm -f *.o a.out core fifteen log.txt -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/README: -------------------------------------------------------------------------------- 1 | https://www.edx.org/course/introduction-computer-science-harvardx-cs50x#! 2 | 3 | 4 | http://cdn.cs50.net/2016/x/references/instructions/instructions.html 5 | http://cdn.cs50.net/2016/x/references/syllabus/syllabus.html 6 | http://cs50.edx.org/2016/schedule 7 | 8 | https://manual.cs50.net/seminars/ -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/README: -------------------------------------------------------------------------------- 1 | http://cdn.cs50.net/2016/x/psets/ 2 | 3 | A schedule of problem sets appears below. 4 | Problem Set 0: Scratch 5 | Problem Set 1: C 6 | Problem Set 2: Crypto 7 | Problem Set 3: Game of Fifteen 8 | Problem Set 4: Forensics 9 | Problem Set 5: Mispellings 10 | Problem Set 6: Web Server 11 | Problem Set 7: C$50 Finance 12 | Problem Set 8: Mashup -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset3/fifteen/3x3.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 4 3 | 1 4 | 2 5 | 5 6 | 8 7 | 7 8 | 6 9 | 4 10 | 1 11 | 2 12 | 5 13 | 8 14 | 7 15 | 6 16 | 4 17 | 1 18 | 2 19 | 4 20 | 1 21 | 2 22 | 3 23 | 5 24 | 4 25 | 7 26 | 6 27 | 1 28 | 2 29 | 3 30 | 7 31 | 4 32 | 8 33 | 6 34 | 4 35 | 8 36 | 5 37 | 7 38 | 8 39 | 5 40 | 6 41 | 4 42 | 5 43 | 6 44 | 7 45 | 8 46 | 6 47 | 5 48 | 4 49 | 7 50 | 8 51 | -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset3/find/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile 3 | # 4 | # Computer Science 50 5 | # Problem Set 3 6 | # 7 | 8 | all: find generate 9 | 10 | find: find.c helpers.c helpers.h 11 | clang -ggdb3 -O0 -std=c11 -Wall -Werror -o find find.c helpers.c -lcs50 -lm 12 | 13 | generate: generate.c 14 | clang -ggdb3 -O0 -std=c11 -Wall -Werror -o generate generate.c 15 | 16 | clean: 17 | rm -f *.o a.out core find generate -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset3/find/helpers.h: -------------------------------------------------------------------------------- 1 | /** 2 | * helpers.h 3 | * 4 | * Computer Science 50 5 | * Problem Set 3 6 | * 7 | * Helper functions for Problem Set 3. 8 | */ 9 | 10 | #include 11 | 12 | /** 13 | * Returns true if value is in array of n values, else false. 14 | */ 15 | bool search(int value, int values[], int n); 16 | 17 | /** 18 | * Sorts array of n values. 19 | */ 20 | void sort(int values[], int n); -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset3/fifteen/questions.txt: -------------------------------------------------------------------------------- 1 | questions.txt 2 | 3 | Computer Science 50 4 | Problem Set 3 5 | 6 | 1. Besides 4 × 4 (which are Game of Fifteen’s dimensions), what other dimensions does 7 | the framework allow? 8 | 2. With what sort of data structure is the game’s board represented? 9 | 3. What function is called to greet the player at game’s start? 10 | 4. What functions do you apparently need to implement? 11 | 12 | 0. TODO 13 | 1. TODO 14 | 2. TODO 15 | 3. TODO 16 | -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset1/water.c: -------------------------------------------------------------------------------- 1 | // check50 2015.fall.pset1.water water.c 2 | // :) water.c exists 3 | // :) water.c compiles 4 | // :) 1 minute equals 12 bottles 5 | // :) 2 minutes equals 24 bottles 6 | // :) 5 minutes equals 60 bottles 7 | // :) 10 minutes equals 120 bottles 8 | // :) rejects "foo" minutes 9 | // :) rejects "" minutes 10 | // :) rejects "123abc" minute 11 | 12 | #include 13 | #include 14 | 15 | int main(void) 16 | { 17 | int i; 18 | int btls; 19 | 20 | printf("minutes: "); 21 | i = GetInt(); 22 | btls = 1.5 * i * 128 / 16; 23 | printf("bottles: %d\n", btls); 24 | return (0); 25 | } -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset1/quiz: -------------------------------------------------------------------------------- 1 | ===== 2 | Alright, should've seen this one coming! In just a few sentences, what's a library? * 3 | 4 | A library is a group of functions and declarations, exposed for use by other programs. 5 | Libraries can be used with the different programs and with the different processes running at the same time on the same computers. 6 | 7 | ===== 8 | In just a few sentences, what role does #include play when you 9 | write it atop some program? * 10 | 11 | This line give you access to different functions inside of CS50 library. 12 | For example functions like GetString(), GetInt, etc. 13 | 14 | ===== 15 | In just a few sentences, what role does -lcs50 play when you pass it as a 16 | "command-line argument" to clang? * 17 | 18 | With -l key you set a CS50 library as an arguments to the linker. 19 | So after compiling a program with this key you can use all functions and methods inside it. 20 | -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset2/initials.c: -------------------------------------------------------------------------------- 1 | // check50 2015.fall.pset2.initials initials.c 2 | // :) initials.c exists 3 | // :) initials.c compiles 4 | // :) outputs "MB" for "Milo Banana" 5 | // :) outputs "MB" for "milo banana" 6 | // :) outputs "RTB" for "Robert Thomas Bowden" 7 | // :) outputs "R" for "ROB" 8 | // :) outputs "RTB" for "Robert thomas Bowden 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | int main(void) 16 | { 17 | string name = GetString(); 18 | if (name != NULL) 19 | { 20 | printf("%c", toupper(name[0])); 21 | int name_len = strlen(name); 22 | int i; 23 | for (i = 0; i < name_len; i++) 24 | { 25 | if (name[i] == ' ' && name[i] != '\0') 26 | printf("%c", toupper(name[i + 1])); 27 | } 28 | printf("\n"); 29 | return 0; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset1/mario.c: -------------------------------------------------------------------------------- 1 | // check50 2015.fall.pset1.mario mario.c 2 | // :) mario.c exists 3 | // :) mario.c compiles 4 | // :) rejects a height of -1 5 | // :) handles a height of 0 correctly 6 | // :) handles a height of 1 correctly 7 | // :) handles a height of 2 correctly 8 | // :) handles a height of 23 correctly 9 | // :) rejects a height of 24, and then accepts a height of 2 10 | // :) rejects a non-numeric height of "foo" 11 | // :) rejects a non-numeric height of "" 12 | 13 | #include 14 | #include 15 | 16 | int main(void) 17 | { 18 | int h; 19 | printf("height: "); 20 | h = GetInt(); 21 | 22 | while (h < 0 || h > 23) 23 | { 24 | printf("Height: "); 25 | h = GetInt(); 26 | } 27 | 28 | for (int i = 0; i < h; i++) 29 | { 30 | for (int y = 0; y < (h - i - 1); y++) 31 | printf(" "); 32 | for (int n = 0; n < (i + 2); n++) 33 | printf("#"); 34 | 35 | printf("\n"); 36 | } 37 | } -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset3/fifteen/4x4.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 5 3 | 6 4 | 1 5 | 2 6 | 4 7 | 5 8 | 6 9 | 1 10 | 2 11 | 3 12 | 7 13 | 11 14 | 10 15 | 9 16 | 1 17 | 2 18 | 3 19 | 4 20 | 5 21 | 6 22 | 8 23 | 1 24 | 2 25 | 3 26 | 4 27 | 7 28 | 11 29 | 10 30 | 9 31 | 14 32 | 13 33 | 12 34 | 1 35 | 2 36 | 3 37 | 4 38 | 14 39 | 13 40 | 12 41 | 1 42 | 2 43 | 3 44 | 4 45 | 14 46 | 13 47 | 12 48 | 1 49 | 2 50 | 3 51 | 4 52 | 12 53 | 9 54 | 15 55 | 1 56 | 2 57 | 3 58 | 4 59 | 12 60 | 9 61 | 13 62 | 14 63 | 9 64 | 13 65 | 14 66 | 7 67 | 5 68 | 9 69 | 13 70 | 14 71 | 15 72 | 10 73 | 11 74 | 5 75 | 9 76 | 13 77 | 7 78 | 11 79 | 5 80 | 9 81 | 13 82 | 7 83 | 11 84 | 15 85 | 10 86 | 5 87 | 9 88 | 13 89 | 15 90 | 11 91 | 8 92 | 6 93 | 7 94 | 8 95 | 14 96 | 12 97 | 6 98 | 7 99 | 8 100 | 14 101 | 12 102 | 6 103 | 7 104 | 8 105 | 14 106 | 15 107 | 11 108 | 10 109 | 6 110 | 7 111 | 8 112 | 12 113 | 15 114 | 11 115 | 10 116 | 15 117 | 11 118 | 14 119 | 12 120 | 11 121 | 15 122 | 10 123 | 14 124 | 15 125 | 11 126 | 12 -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset1/greedy.c: -------------------------------------------------------------------------------- 1 | // check50 2015.fall.pset1.greedy greedy.c 2 | // :) greedy.c exists 3 | // :) greedy.c compiles 4 | // :) input of 0.41 yields output of 4 5 | // :) input of 0.01 yields output of 1 6 | // :) input of 0.15 yields output of 2 7 | // :) input of 1.6 yields output of 7 8 | // :) input of 23 yields output of 92 9 | // :) input of 4.2 yields output of 18 10 | // :) rejects a negative input like -.1 11 | // :) rejects a non-numeric input of "foo" 12 | // :) rejects a non-numeric input of "" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | int main (void) 19 | { 20 | double change; 21 | printf("Oh hai! How much change is owed?\n"); 22 | change = GetFloat(); 23 | 24 | while (change < 0) 25 | { 26 | printf("How much change is owed?\n"); 27 | change = GetFloat(); 28 | } 29 | 30 | double d = round(change * 100); 31 | int i = d; 32 | int quarters = i / 25; 33 | int dimes = (i % 25) / 10; 34 | int nickels = ((i % 25) % 10) / 5; 35 | int pennies = (((i % 25) % 10) % 5); 36 | int coins = quarters + dimes + nickels + pennies; 37 | 38 | printf("%d\n", coins); 39 | } -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset2/quiz: -------------------------------------------------------------------------------- 1 | ===== 2 | How does a while loop differ from a do-while loop? When is the latter particularly useful? 3 | 4 | while loop is a entry-controlled loop, it first check the condition then enter the body 5 | do-while loop first enter the body and then check the condition. 6 | 7 | the most important difference between while loop and do-while loop is that in do-while loop, the block of code is executed at least once. 8 | 9 | ===== 10 | What does "undeclared identifier" usually indicate if outputted by clang? 11 | 12 | the most often it indicate of forgetting to include the header file that contains the function declaration, for example, for getstring function. 13 | 14 | ===== 15 | Why is the Caesar Cipher not very secure? 16 | 17 | the key space is very small. 18 | using a brute force method, one could easily try all (25) possible combinations in order to decrypt the message without initially knowing the key. 19 | 20 | ===== 21 | What's a function? 22 | 23 | in programming, function a named section of a program that performs a specific task like GetString from 24 | cs50.h 25 | 26 | ===== 27 | Why bother writing functions when you can just copy and paste code as needed? 28 | 29 | if programming there is a principle don't repeat yourself (DRY), and functions help you with it. 30 | it means that you can write good function once and use it forever in different project and not to rewrite it every time. it help you to save a lot of time. 31 | -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset2/caesar.c: -------------------------------------------------------------------------------- 1 | // check50 2015.fall.pset2.caesar caesar.c 2 | // :) caesar.c exists 3 | // :) caesar.c compiles 4 | // :) encrypts "a" as "b" using 1 as key 5 | // :) encrypts "barfoo" as "yxocll" using 23 as key 6 | // :) encrypts "BARFOO" as "EDUIRR" using 3 as key 7 | // :) encrypts "BaRFoo" as "FeVJss" using 4 as key 8 | // :) encrypts "barfoo" as "onesbb" using 65 as key 9 | // :) encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key 10 | // :) handles lack of argv[1] 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | int main(int argc, string argv[]) 19 | { 20 | if (argc != 2 || atoi(argv[1]) <= 0) 21 | { 22 | printf("Enter one positive key?\n"); 23 | return 1; 24 | } 25 | 26 | int k = atoi(argv[1]); 27 | string text = GetString(); 28 | 29 | for (int i = 0, n = strlen(text); i < n; i++) 30 | { 31 | if (isalpha(text[i])) 32 | { 33 | if (isupper(text[i])) 34 | { 35 | int c = text[i]; 36 | int enc = (c - 65 + k) % 26; 37 | printf("%c", enc + 65); 38 | 39 | } 40 | else 41 | { 42 | int c = text[i]; 43 | int enc = (c - 97 + k) % 26; 44 | printf("%c", enc + 97); 45 | } 46 | 47 | } 48 | 49 | else 50 | { 51 | printf("%c", text[i]); 52 | } 53 | 54 | } 55 | printf("\n"); 56 | return 0; 57 | } -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset3/find/find.c: -------------------------------------------------------------------------------- 1 | /** 2 | * find.c 3 | * 4 | * Computer Science 50 5 | * Problem Set 3 6 | * 7 | * Prompts user for as many as MAX values until EOF is reached, 8 | * then proceeds to search that "haystack" of values for given needle. 9 | * 10 | * Usage: ./find needle 11 | * 12 | * where needle is the value to find in a haystack of values 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include "helpers.h" 20 | 21 | // maximum amount of hay 22 | const int MAX = 65536; 23 | 24 | int main(int argc, string argv[]) 25 | { 26 | // ensure proper usage 27 | if (argc != 2) 28 | { 29 | printf("Usage: ./find needle\n"); 30 | return -1; 31 | } 32 | 33 | // remember needle 34 | int needle = atoi(argv[1]); 35 | 36 | // fill haystack 37 | int size; 38 | int haystack[MAX]; 39 | for (size = 0; size < MAX; size++) 40 | { 41 | // wait for hay until EOF 42 | printf("\nhaystack[%i] = ", size); 43 | int straw = GetInt(); 44 | if (straw == INT_MAX) 45 | { 46 | break; 47 | } 48 | 49 | // add hay to stack 50 | haystack[size] = straw; 51 | } 52 | printf("\n"); 53 | 54 | // sort the haystack 55 | sort(haystack, size); 56 | 57 | // try to find needle in haystack 58 | if (search(needle, haystack, size)) 59 | { 60 | printf("\nFound needle in haystack!\n\n"); 61 | return 0; 62 | } 63 | else 64 | { 65 | printf("\nDidn't find needle in haystack.\n\n"); 66 | return 1; 67 | } 68 | } -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset3/find/generate.c: -------------------------------------------------------------------------------- 1 | /** 2 | * generate.c 3 | * 4 | * Computer Science 50 5 | * Problem Set 3 6 | * 7 | * Generates pseudorandom numbers in [0,LIMIT), one per line. 8 | * 9 | * Usage: generate n [s] 10 | * 11 | * where n is number of pseudorandom numbers to print 12 | * and s is an optional seed 13 | */ 14 | 15 | #define _XOPEN_SOURCE 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | // constant 23 | #define LIMIT 65536 24 | 25 | int main(int argc, string argv[]) 26 | { 27 | // TODO: comment me 28 | // check number of program agruments, need one or two 29 | // if it is not true, print usage message and exit with code 1 30 | if (argc != 2 && argc != 3) 31 | { 32 | printf("Usage: generate n [s]\n"); 33 | return 1; 34 | } 35 | 36 | // TODO: comment me 37 | // a integer variable to store number from first program agrument 38 | // using function atoi to convert string to int 39 | int n = atoi(argv[1]); 40 | 41 | // TODO: comment me 42 | // check that number of program agruments, if we set 2 argruments 43 | // we will convert second agrument from string to int and use it as 44 | // the seed of the sequence of pseudo-random integers in srand48() 45 | // if we set only one agrument we will use the number of seconds 46 | // since the start of Jan 1 1970 as a seed for srand48() 47 | if (argc == 3) 48 | { 49 | srand48((long int) atoi(argv[2])); 50 | } 51 | else 52 | { 53 | srand48((long int) time(NULL)); 54 | } 55 | 56 | // TODO: comment me 57 | // use for loop to print the sequence of n pseudo-random numbers 58 | // in range of 0 to LIMIT 59 | for (int i = 0; i < n; i++) 60 | { 61 | printf("%i\n", (int) (drand48() * LIMIT)); 62 | } 63 | 64 | // success 65 | return 0; 66 | } -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset3/find/helpers.c: -------------------------------------------------------------------------------- 1 | // check50 2015.fall.pset3.find helpers.c 2 | // :) helpers.c exists 3 | // :) helpers.c compiles 4 | // :) finds 42 in {42,43,44} 5 | // :) finds 42 in {41,42,43} 6 | // :) finds 42 in {40,41,42} 7 | // :) finds 42 in {41,42,43,44} 8 | // :) finds 42 in {40,41,42,43} 9 | // :) finds 42 in {39,40,41,42} 10 | // :) doesn't find 42 in {39,40,41} 11 | // :) doesn't find 42 in {39,40,41,43} 12 | 13 | /** 14 | * helpers.c 15 | * 16 | * Computer Science 50 17 | * Problem Set 3 18 | * 19 | * Helper functions for Problem Set 3. 20 | */ 21 | 22 | #include 23 | 24 | #include "helpers.h" 25 | 26 | /** 27 | * Returns true if value is in array of n values, else false. 28 | */ 29 | bool search(int value, int values[], int n) 30 | { 31 | // TODO: implement a searching algorithm 32 | // linear search 33 | // for (int i = 0; i < n; i++) 34 | // { 35 | // if (values[i] == value) 36 | // { 37 | // return true; 38 | // } 39 | // } 40 | 41 | // binary search 42 | int first = 0; 43 | int last = n - 1; 44 | 45 | while (last >= first) 46 | { 47 | int mid = (first + last) / 2; 48 | if (value == values[mid]) 49 | { 50 | return true; 51 | } 52 | else if (value < values[mid]) 53 | { 54 | last = mid - 1; 55 | } 56 | else if (value > values[mid]) 57 | { 58 | first = mid + 1; 59 | } 60 | } 61 | return false; 62 | } 63 | 64 | /** 65 | * Sorts array of n values. 66 | */ 67 | void sort(int values[], int n) 68 | { 69 | // TODO: implement an O(n^2) sorting algorithm 70 | for (int i = 1; i < n; i++) 71 | { 72 | for (int j = 0; j < n - i; j++) 73 | { 74 | if (values[j] > values[j + 1]) 75 | { 76 | int tmp = values[j]; 77 | values[j] = values[j + 1]; 78 | values[j + 1] = tmp; 79 | } 80 | } 81 | } 82 | return; 83 | } -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset2/vigenere.c: -------------------------------------------------------------------------------- 1 | // check50 2015.fall.pset2.vigenere vigenere.c 2 | // :) vigenere.c exists 3 | // :) vigenere.c compiles 4 | // :) encrypts "a" as "a" using "a" as keyword 5 | // :) encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword 6 | // :) encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword 7 | // :) encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword 8 | // :) handles lack of argv[1] 9 | // :) handles argc > 2 10 | // :) rejects "Hax0r2" as keyword 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | int main(int argc, string argv[]) 19 | { 20 | if (argc != 2) 21 | { 22 | printf("Enter letter key?\n"); 23 | return 1; 24 | } 25 | 26 | string k = argv[1]; 27 | int key_len = strlen(k); 28 | 29 | for (int i = 0; i < key_len; i++) 30 | { 31 | if (!isalpha(k[i])) 32 | { 33 | printf("There is a non-alphabetic character at %i\n", i); 34 | return 1; 35 | } 36 | else 37 | { 38 | if (isalpha(k[i])) 39 | { 40 | if (isupper(k[i])) 41 | { 42 | k[i] = k[i] - 65; 43 | } 44 | else 45 | { 46 | k[i] = k[i] - 97; 47 | } 48 | } 49 | } 50 | } 51 | 52 | string text = GetString(); 53 | int text_len = strlen(text); 54 | int c = 0; 55 | 56 | for (int i = 0; i < text_len; i++) 57 | { 58 | if (isalpha(text[i])) 59 | { 60 | if (isupper(text[i])) 61 | { 62 | printf("%c", (((text[i] - 65) + (k[c % key_len])) % 26) + 65); 63 | c++; 64 | } 65 | else 66 | { 67 | printf("%c", (((text[i] - 97) + (k[c % key_len])) % 26) + 97); 68 | c++; 69 | } 70 | } 71 | else 72 | { 73 | printf("%c", text[i]); 74 | } 75 | } 76 | printf("\n"); 77 | return 0; 78 | } -------------------------------------------------------------------------------- /introduction-to-computer-science/edx_cs50/psets/pset3/fifteen/fifteen.c: -------------------------------------------------------------------------------- 1 | /** 2 | * fifteen.c 3 | * 4 | * Computer Science 50 5 | * Problem Set 3 6 | * 7 | * Implements Game of Fifteen (generalized to d x d). 8 | * 9 | * Usage: fifteen d 10 | * 11 | * whereby the board's dimensions are to be d x d, 12 | * where d must be in [DIM_MIN,DIM_MAX] 13 | * 14 | * Note that usleep is obsolete, but it offers more granularity than 15 | * sleep and is simpler to use than nanosleep; `man usleep` for more. 16 | */ 17 | 18 | #define _XOPEN_SOURCE 500 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | // constants 26 | #define DIM_MIN 3 27 | #define DIM_MAX 9 28 | 29 | // board 30 | int board[DIM_MAX][DIM_MAX]; 31 | 32 | // dimensions 33 | int d; 34 | 35 | // prototypes 36 | void clear(void); 37 | void greet(void); 38 | void init(void); 39 | void draw(void); 40 | bool move(int tile); 41 | bool won(void); 42 | 43 | int main(int argc, string argv[]) 44 | { 45 | // ensure proper usage 46 | if (argc != 2) 47 | { 48 | printf("Usage: fifteen d\n"); 49 | return 1; 50 | } 51 | 52 | // ensure valid dimensions 53 | d = atoi(argv[1]); 54 | if (d < DIM_MIN || d > DIM_MAX) 55 | { 56 | printf("Board must be between %i x %i and %i x %i, inclusive.\n", 57 | DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX); 58 | return 2; 59 | } 60 | 61 | // open log 62 | FILE* file = fopen("log.txt", "w"); 63 | if (file == NULL) 64 | { 65 | return 3; 66 | } 67 | 68 | // greet user with instructions 69 | greet(); 70 | 71 | // initialize the board 72 | init(); 73 | 74 | // accept moves until game is won 75 | while (true) 76 | { 77 | // clear the screen 78 | clear(); 79 | 80 | // draw the current state of the board 81 | draw(); 82 | 83 | // log the current state of the board (for testing) 84 | for (int i = 0; i < d; i++) 85 | { 86 | for (int j = 0; j < d; j++) 87 | { 88 | fprintf(file, "%i", board[i][j]); 89 | if (j < d - 1) 90 | { 91 | fprintf(file, "|"); 92 | } 93 | } 94 | fprintf(file, "\n"); 95 | } 96 | fflush(file); 97 | 98 | // check for win 99 | if (won()) 100 | { 101 | printf("ftw!\n"); 102 | break; 103 | } 104 | 105 | // prompt for move 106 | printf("Tile to move: "); 107 | int tile = GetInt(); 108 | 109 | // quit if user inputs 0 (for testing) 110 | if (tile == 0) 111 | { 112 | break; 113 | } 114 | 115 | // log move (for testing) 116 | fprintf(file, "%i\n", tile); 117 | fflush(file); 118 | 119 | // move if possible, else report illegality 120 | if (!move(tile)) 121 | { 122 | printf("\nIllegal move.\n"); 123 | usleep(500000); 124 | } 125 | 126 | // sleep thread for animation's sake 127 | usleep(500000); 128 | } 129 | 130 | // close log 131 | fclose(file); 132 | 133 | // success 134 | return 0; 135 | } 136 | 137 | /** 138 | * Clears screen using ANSI escape sequences. 139 | */ 140 | void clear(void) 141 | { 142 | printf("\033[2J"); 143 | printf("\033[%d;%dH", 0, 0); 144 | } 145 | 146 | /** 147 | * Greets player. 148 | */ 149 | void greet(void) 150 | { 151 | clear(); 152 | printf("WELCOME TO GAME OF FIFTEEN\n"); 153 | usleep(2000000); 154 | } 155 | 156 | /** 157 | * Initializes the game's board with tiles numbered 1 through d*d - 1 158 | * (i.e., fills 2D array with values but does not actually print them). 159 | */ 160 | void init(void) 161 | { 162 | // TODO 163 | } 164 | 165 | /** 166 | * Prints the board in its current state. 167 | */ 168 | void draw(void) 169 | { 170 | // TODO 171 | } 172 | 173 | /** 174 | * If tile borders empty space, moves tile and returns true, else 175 | * returns false. 176 | */ 177 | bool move(int tile) 178 | { 179 | // TODO 180 | return false; 181 | } 182 | 183 | /** 184 | * Returns true if game is won (i.e., board is in winning configuration), 185 | * else false. 186 | */ 187 | bool won(void) 188 | { 189 | // TODO 190 | return false; 191 | } --------------------------------------------------------------------------------