diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..38e7ff3 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "HW0/GreatestCommonDivisor"] + path = HW0/GreatestCommonDivisor + url = https://github.com/stevengogogo/GreatestCommonDivisor +[submodule "HW0/NonogramSolver"] + path = HW0/NonogramSolver + url = https://github.com/stevengogogo/NonogramSolver diff --git a/HW0/GreatestCommonDivisor b/HW0/GreatestCommonDivisor new file mode 160000 index 0000000..e51b82d --- /dev/null +++ b/HW0/GreatestCommonDivisor @@ -0,0 +1 @@ +Subproject commit e51b82d033a761138315dc0a971f85cf64c982d8 diff --git a/HW0/NonogramSolver b/HW0/NonogramSolver new file mode 160000 index 0000000..99ab2ed --- /dev/null +++ b/HW0/NonogramSolver @@ -0,0 +1 @@ +Subproject commit 99ab2ed0f24dafbccff7c41aaf7b9c9f35a4bf07 diff --git a/HW0/Pointers/README.md b/HW0/Pointers/README.md new file mode 100644 index 0000000..9d7db65 --- /dev/null +++ b/HW0/Pointers/README.md @@ -0,0 +1,258 @@ +# Prolem 3 - *(Human Compiler)(Hand-written) + +## Problem 3a: Swaps two arrays using pointers + +![Screen Shot 2021-03-07 at 8 34 25 AM](https://user-images.githubusercontent.com/29009898/110225352-efdec580-7f1f-11eb-9675-516acb605e42.png) + + +```c +int fake_a[] = {1,3}; +int fake_b[] = {2,4}; +int *real_a = fake_a; +int *real_b = fake_b; + +for(int i=0;i<2;i++) + printf("%d", *(real_a + i)); +for(int i=0;i<2;i++) + printf("%d", *(real_b + i)); + +int *tmp = real_a; //tmp is a pointer to pointer +real_b = fake_a; // fill the blanks +real_a = fake_b; // fil the blanks + +for(int i=0;i<2;i++) + printf("%d", *(real_a + i)); +for(int i=0;i<2;i++) + printf("%d", *(real_b + i)); +``` + +Test at: https://onlinegdb.com/SJ4JFi-7_ + +![](img/Problem3a.png) + +--- + +## Problem 3b: An array supporting negative indices + +![Screen Shot 2021-03-07 at 9 17 08 AM](https://user-images.githubusercontent.com/29009898/110226009-f7a16880-7f25-11eb-81db-7c27eb264c33.png) + +```c +#include +#define MINN -50 +#define MAXN 50 + +int main(){ + int storage[MAXN - MINN + 1] = {0}; // 101 + int *ary = storage - MINN ; //fill the blank + + for(int i=MINN;i<=MAXN;i++) + ary[i] = i; + for(int i=MINN;i<=MAXN;i++) + printf("%d", ary[i]); + return 0; +} +``` +Verified at: https://onlinegdb.com/rydhl3W7u + +![](img/Problem3b.png) + +--- + +## Problem 3C: Tranverses data nodes in a linked list. + +![Screen Shot 2021-03-07 at 12 03 34 PM](https://user-images.githubusercontent.com/29009898/110228580-38f14280-7f3d-11eb-9891-65aeaeb849e0.png) + +```c +#include +#include //malloc / free +#include //memset + +//use typedef to define "struct node" as "node" +typedef struct node{ + int data; + struct node *nxt; +} node; + +node* alloc(int data, node* nxt){ + node *tmp = (node*)malloc(sizeof(node)); + tmp->data=data; + tmp->nxt = nxt; + return tmp; +} + +void destroy(node *head){ + if(head != NULL){ //FIll the blank + destroy(head->nxt); + //clean sensitive data; + memset(head,0,sizeof(head)); + free(head); + } +} + +int main(){ + // create nodes [0,1,2,4] + node* head = alloc(0, alloc(1,alloc(2,alloc(4,NULL)))); + node* tmp = head; + //print the nodes subsequently + while(tmp!=NULL){ + printf("%d -> ", *tmp-> data ); //FIll the blank + tmp = (*tmp)->nxt; //FIll the blank + } + printf("NULL"); + + //free the nodes subsequently to avoid memory leak + destroy(head); + return 0; +} +``` + +### Curate + +Use `temp->data` not `(*tmp)->data` + +#### Copy a pointer to another pointer + +![Screen Shot 2021-03-07 at 12 31 17 PM](https://user-images.githubusercontent.com/29009898/110229016-09dcd000-7f41-11eb-9414-77bad043e4c8.png) +> From [Basics of C Programmin](https://computer.howstuffworks.com/c24.htm) + +### Question + +Wrong version: https://www.onlinegdb.com/ +Corrected version: https://onlinegdb.com/BkReV0bXd + +--- + +## Problem 3D: Binary Tree +![Screen Shot 2021-03-07 at 11 52 38 AM](https://user-images.githubusercontent.com/29009898/110228385-9f756100-7f3b-11eb-92e7-76ee4bf3413f.png) +![Screen Shot 2021-03-07 at 11 52 48 AM](https://user-images.githubusercontent.com/29009898/110228386-a603d880-7f3b-11eb-9108-fb2e0b11d480.png) + +**問題**: `printf` 應該在 `if` 前面 + +```c +#include +#include //malloc /free +#include //memset + +//use typedef to substitute "struct node with "node"" +typedef struct node { + int data; + struct node *left, *right; +} node; + +//creates a node filledwith predefined values +node* alloc(int data, node *left, node *right){ + node *tmp = (node*)malloc(sizeof(node)); + tmp->data = data, + tmp->left = left; + tmp->right = right; + return tmp; +} + +//traverses (遍歷) the nodes recursively +void traverse(node* root){ + printf("%d", root->data); + if ((root->left != NULL) & (root->right != NULL)){ + traverse(root->left); + traverse(root->right); + } +} + +//frees the nodes recursively +void destroy(node *root){ + if ((root->left != NULL) & (root->right!=NULL)){ + destroy(root->left); + destroy(root->right); + //clean sensitive data + memset(root, 0, sizeof(root)); + free(root); + } +} + +int main(){ + // creates a hierarchical data structure + node *root = \ + alloc(0, + alloc(3, + alloc(7, NULL, NULL), + alloc(4, NULL, NULL) + ), + alloc(2, + alloc(1, NULL, NULL), + alloc(9, NULL, NULL) + ) + ); + + traverse(root); + destroy(root); +} +``` +Verify at: https://onlinegdb.com/r1VxlA-7d +![](img/Problem3c.png) + +--- + +## Notes + +### Null Pointer and `Segmentation fault` + +- Segmentation fault: + > Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you +- NULL Pointer + > People assign NULL to pointer to indicate that it points to nothing. + +### Read Only String and its Dynamical type + +- `char* str` Read only: able to share among functions + ```c + char *str = "gfg"; + ``` + Read only. `str[0]=a` leads to segmentation fault. Noted that read-only string can be passed among functions: + ```c + char *getString(){ + char *str = "GfG"; /* Stored in read only part of shared segment */ + + /* No problem: remains at address str after getString() returns*/ + return str; + } + + int main(){ + printf("%s", getString()); + getchar(); + return 0; + } + + ``` +- `char str[]` Mutable string: unable to share among functions. + ```c + int main(){ + char str[]; + str = "gfg"; + } + ``` +- Store in heap: allow to share and modify + ```c + char *getString() + { + int size = 4; + char *str = (char *)malloc(sizeof(char)*size); /*Stored in heap segment*/ + *(str+0) = 'G'; + *(str+1) = 'f'; + *(str+2) = 'G'; + *(str+3) = '\0'; + + /* No problem: string remains at str after getString() returns */ + return str; + } + int main() + { + printf("%s", getString()); + getchar(); + return 0; + } + ``` + +--- +## Reference +1. Segmentation fault and pointer. [[stackoverflow](https://stackoverflow.com/questions/17873561/pointer-initialisation-gives-segmentation-fault)] +2. String and storage. [[GreekforGeek](https://www.geeksforgeeks.org/storage-for-strings-in-c/)] +3. Pointing to the same address. [[eBook-Basics of C Programming](https://computer.howstuffworks.com/c24.htm)] \ No newline at end of file diff --git a/HW0/Pointers/img/Problem3a.png b/HW0/Pointers/img/Problem3a.png new file mode 100644 index 0000000..479e301 Binary files /dev/null and b/HW0/Pointers/img/Problem3a.png differ diff --git a/HW0/Pointers/img/Problem3b.png b/HW0/Pointers/img/Problem3b.png new file mode 100644 index 0000000..341fb80 Binary files /dev/null and b/HW0/Pointers/img/Problem3b.png differ diff --git a/HW0/Pointers/img/Problem3c.png b/HW0/Pointers/img/Problem3c.png new file mode 100644 index 0000000..2681881 Binary files /dev/null and b/HW0/Pointers/img/Problem3c.png differ diff --git a/HW0/Problem_1/README.md b/HW0/Problem_1/README.md deleted file mode 100644 index f1e3da6..0000000 --- a/HW0/Problem_1/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# Problem 1: Greatest Common Divisor of Big Integers (Programming) - - - -## Reference -1. BigInt in C [[gist](https://gist.github.com/bloopletech/338338)] diff --git a/HW0/Problem_1/bigint.c b/HW0/Problem_1/bigint.c deleted file mode 100644 index 479f545..0000000 --- a/HW0/Problem_1/bigint.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include -#include "bigint.h" - - -bigint* newnumc(char* numb) -{ - //allocate storage for the bigint - bigint* num = (bigint*)malloc(sizeof(bigint)); - - int numbl = strlen(numb); - int numbll = numbl-1; - -} \ No newline at end of file diff --git a/HW0/Problem_1/bigint.h b/HW0/Problem_1/bigint.h deleted file mode 100644 index 10db9c1..0000000 --- a/HW0/Problem_1/bigint.h +++ /dev/null @@ -1,10 +0,0 @@ -# define CHARZERO_ABOVEINTZERO 48 - -typedef unsigned char digit; - -typedef struct -{ - digit* number; - int length; -} bigint; - diff --git a/HW0/Problem_1/main.c b/HW0/Problem_1/main.c deleted file mode 100644 index 53c5fdf..0000000 --- a/HW0/Problem_1/main.c +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/README.md b/README.md index 60cfe4d..23ca385 100644 --- a/README.md +++ b/README.md @@ -1 +1,40 @@ # Data Structures 2021 Spring + +## Course Website + + +## How to use `gitsubmodule` + + +Show status +----------- + +```bash +git submodule status --recursive +``` + +Update all submodule +-------------------- + +#### Add project + +```bash +# Clone to the current folder: +git submodule add +git submodule init + +``` + +#### Fetch +```bash +git submodule update --recursive +``` + +#### Pull +```bash +git pull --recurse-submodules +``` + +## Plotting with [diagrams](https://app.diagrams.net/) + +[Diagram](https://app.diagrams.net/) features uploading to Github repository, making the drawing up-to-date easily! \ No newline at end of file diff --git a/assets/Screen_Shot_2021-03-02_at_1.53.11_PM.png b/assets/Screen_Shot_2021-03-02_at_1.53.11_PM.png new file mode 100644 index 0000000..59e8d6e Binary files /dev/null and b/assets/Screen_Shot_2021-03-02_at_1.53.11_PM.png differ diff --git a/docs/.nojekyll b/docs/.nojekyll new file mode 100644 index 0000000..e69de29 diff --git a/docs/assets/css/custom.css b/docs/assets/css/custom.css new file mode 100644 index 0000000..e15ae2c --- /dev/null +++ b/docs/assets/css/custom.css @@ -0,0 +1,24 @@ +@import url("https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,700;1,400&display=swap"); + +:root { + --mono-hue: 222; + --mono-saturation: 14%; + --base-background-color: #282c35; + --theme-color: #66e0ff; + --link-color: var(--theme-color); + + --base-font-family: Roboto, sans-serif; + --strong-font-weight: 700; + + --search-flex-order: 0; + --search-margin: 1rem 0; + --docsify-example-panels-left-panel-width : 50%; + --docsify-example-panels-right-panel-width : 50%; +} + +@media screen and (min-width: 1230px) { + :root { + --sidebar-width: 22rem; + --content-max-width : 66em; + } +} diff --git a/docs/favicon.ico b/docs/favicon.ico new file mode 100644 index 0000000..6244dd6 Binary files /dev/null and b/docs/favicon.ico differ diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 0000000..c191797 --- /dev/null +++ b/docs/index.html @@ -0,0 +1,72 @@ + + + + + My notebooks + + + + + + + + + + + + + + + + +
Loading content...
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/mdfiles/C.md b/docs/mdfiles/C.md new file mode 100644 index 0000000..95824c5 --- /dev/null +++ b/docs/mdfiles/C.md @@ -0,0 +1,9 @@ +# Programming with C + +## Pointers + + +## Function + + +[cartpole](https://raw.githubusercontent.com/stevengogogo/GreatestCommonDivisor/main/src/main.c ':include') \ No newline at end of file diff --git a/docs/mdfiles/Lecture.md b/docs/mdfiles/Lecture.md new file mode 100644 index 0000000..0d0980c --- /dev/null +++ b/docs/mdfiles/Lecture.md @@ -0,0 +1,160 @@ +## Motivations of Data Structures and Algorithms + + + + + +### Debug + +![image](https://user-images.githubusercontent.com/29009898/110299183-581ccc80-8030-11eb-9f18-192f57d6922d.png) + +- integer `m` undefined +- 會重複算到 +- `i + +--- + +## 如何證明演算法正確? + +### Loop invariance + +After `i-th` iteration of the `for` loop, `arr[m]` is always the smallest among `arr[0]`,...., `arr[i]` +- true when `i=0` +- true when `i=k` → true when `i=k+1` + +#### 證明 + +- 先寫要證明什麼 +- 看看離散數學的內容 + +### Efficiency of Algorithm + +- Sequential search +- Binary search + +### Pseudo code for `getMinIndex` + +#### Linear serach + +```c +int getMinIndex(int arr[], int len){ + int i; + int m=0; + for(i=0;i arr[i]){ + m=i; + } + } +} + +``` + +```c +GET-MIN-INDEX(A) + +m = 1 +for i = 2 to A.length + if A[m] > A[i] + m = i + +return m + +``` + +#### Selection sort + +![image](https://user-images.githubusercontent.com/29009898/110298600-b9906b80-802f-11eb-9d9e-55b1f2f927eb.png) + + +### What is data structure? + +- Scheme of organizing data within computer +- 快: 常常用的東西取的快 +- Good data structure needs proper maintenance algorithms: + - `Construct` / `Update` / `remove` + +### Why data structures and algorithms? + +- Data structures + - Storage + - Network, bandwidth +- Algorithms + - Computation + +### Proper use: Tradeoff of different factors + +## C Programming versus DSA + +- Test + - 怎麼測試程式碼 + - 設計關鍵情形 +- Modualize + - 模組化 +- Tradeoff + - Theory + - Practice + +## What is Array? + +- Index +- Memory is array +- Address + - `arr[index]` , `memory[address]` +- C programming + - Pointer + +### Array as memory block in C + +- Access: `arr[index]` → `memory[arr + index * sizeof(data)]` + - Array 可以快速 update 和拿出來 +- Maintenance: `malloc(sizeof(data)*length)` + - construct (length) + +![image](https://user-images.githubusercontent.com/29009898/110298769-ee9cbe00-802f-11eb-9461-cf2c5cac1088.png) + + +- Garbage collection + +## Orderred array + +![image](https://user-images.githubusercontent.com/29009898/110298847-0116f780-8030-11eb-8efe-de5e00896c29.png) + +### 可以試試 實做 orderred array + +- Insertion +- remove + +## Sorting + +### Insertion Sort + +$O(n^2)$ + +![](https://user-images.githubusercontent.com/29009898/110297582-a335e000-802e-11eb-94dd-322eb7caccb2.png) + +### Selection Sort + +$O(n^2)$ + +![Untitled 1](https://user-images.githubusercontent.com/29009898/110297584-a3ce7680-802e-11eb-8400-65c32a2ae1ca.png) + +## Sequential search algorithm + +--- + +## 作業0 + +![Untitled](https://user-images.githubusercontent.com/29009898/110297587-a4670d00-802e-11eb-9c69-6fe3de90e824.png) + +- 會走到沒有 define 的地區 +- 執行錯誤 +- `ary[-4]` +- `segmentation fault` 每次呼叫都是系統給空間,如果取用非法空間就是 `segmentation fault` + + diff --git a/docs/mdfiles/README.md b/docs/mdfiles/README.md new file mode 100644 index 0000000..b34dbb3 --- /dev/null +++ b/docs/mdfiles/README.md @@ -0,0 +1,7 @@ +# Data Structure and Algorithm + +## Course Website +https://cool.ntu.edu.tw/courses/4621 + +## DSA Judge +https://dsa-2021.csie.org/#!/ diff --git a/docs/mdfiles/_sidebar.md b/docs/mdfiles/_sidebar.md new file mode 100644 index 0000000..6145f67 --- /dev/null +++ b/docs/mdfiles/_sidebar.md @@ -0,0 +1,5 @@ +- [🏠 **Home**](README.md) +- [🤹 **Lecture Note**](lec/) +- [📒 **Assignments**](assignment.md) +- [🤖️ **C Programming**](C.md) +- [**Markdown**](how-to-use-docify.md) \ No newline at end of file diff --git a/docs/mdfiles/assignment.md b/docs/mdfiles/assignment.md new file mode 100644 index 0000000..01197a4 --- /dev/null +++ b/docs/mdfiles/assignment.md @@ -0,0 +1,8 @@ +# Assignment + +## Homework 0 + + +|Porblem|Status| +|---|---| +|rgrg|grg| \ No newline at end of file diff --git a/docs/mdfiles/how-to-use-docify.md b/docs/mdfiles/how-to-use-docify.md new file mode 100644 index 0000000..6a6d3f2 --- /dev/null +++ b/docs/mdfiles/how-to-use-docify.md @@ -0,0 +1,633 @@ +# Markdown syntax + +Sourced from [Docsify themable](https://jhildenbiddle.github.io/docsify-themeable/). + + + + +## Headings + + + +# Heading 1 {docsify-ignore} + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse luctus +nulla eu ex varius, a varius elit tincidunt. Aenean arcu magna, gravida id purus +a, interdum convallis turpis. Aenean id ipsum eu tortor sollicitudin scelerisque +in quis elit. + +## Heading 2 {docsify-ignore} + +Vestibulum lobortis laoreet nunc vel vulputate. In et augue non lectus +pellentesque molestie et ac justo. Sed sed turpis ut diam gravida sagittis nec +at neque. Vivamus id tellus est. Nam ac dignissim mi. Vestibulum nec sem +convallis, condimentum augue at, commodo diam. + +### Heading 3 {docsify-ignore} + +Suspendisse sit amet tincidunt nibh, ac interdum velit. Ut orci diam, dignissim +at enim sit amet, placerat rutrum magna. Mauris consectetur nibh eget sem +feugiat, sit amet congue quam laoreet. Curabitur sed massa metus. + +#### Heading 4 {docsify-ignore} + +Donec odio orci, facilisis ac vehicula in, vestibulum ut urna. Ut bibendum +ullamcorper risus, ac euismod leo maximus sed. In pulvinar sagittis rutrum. +Morbi quis cursus diam. Cras ac laoreet nulla, rhoncus sodales dui. + +##### Heading 5 {docsify-ignore} + +Commodo sit veniam nulla cillum labore ullamco aliquip quis. Consequat nulla +fugiat consequat ex duis proident. Adipisicing excepteur tempor exercitation ad. +Consectetur voluptate Lorem sint elit exercitation ullamco dolor. + +###### Heading 6 {docsify-ignore} + +Ipsum ea amet dolore mollit incididunt fugiat nulla laboris est sint voluptate. +Ex culpa id amet ipsum amet pariatur ipsum officia sit laborum irure ullamco +deserunt. Consequat qui tempor occaecat nostrud proident. + + + +```markdown +# Heading 1 + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse luctus +nulla eu ex varius, a varius elit tincidunt. Aenean arcu magna, gravida id purus +a, interdum convallis turpis. Aenean id ipsum eu tortor sollicitudin scelerisque +in quis elit. + +## Heading 2 + +Vestibulum lobortis laoreet nunc vel vulputate. In et augue non lectus +pellentesque molestie et ac justo. Sed sed turpis ut diam gravida sagittis nec +at neque. Vivamus id tellus est. Nam ac dignissim mi. Vestibulum nec sem +convallis, condimentum augue at, commodo diam. + +### Heading 3 + +Suspendisse sit amet tincidunt nibh, ac interdum velit. Ut orci diam, dignissim +at enim sit amet, placerat rutrum magna. Mauris consectetur nibh eget sem +feugiat, sit amet congue quam laoreet. Curabitur sed massa metus. + +#### Heading 4 + +Donec odio orci, facilisis ac vehicula in, vestibulum ut urna. Ut bibendum +ullamcorper risus, ac euismod leo maximus sed. In pulvinar sagittis rutrum. +Morbi quis cursus diam. Cras ac laoreet nulla, rhoncus sodales dui. + +##### Heading 5 + +Commodo sit veniam nulla cillum labore ullamco aliquip quis. Consequat nulla +fugiat consequat ex duis proident. Adipisicing excepteur tempor exercitation ad. +Consectetur voluptate Lorem sint elit exercitation ullamco dolor. + +###### Heading 6 + +Ipsum ea amet dolore mollit incididunt fugiat nulla laboris est sint voluptate. +Ex culpa id amet ipsum amet pariatur ipsum officia sit laborum irure ullamco +deserunt. Consequat qui tempor occaecat nostrud proident. +``` + + + + + + +## Text + + + +Body text + +**Bold text** + +*Italic text* + +~~Strikethrough~~ + +Marked text + +
Preformatted text
+ +Small Text + +This is subscript + +This is superscript + +WHO + + + + +```markdown +Body text + +**Bold text** + +*Italic text* + +~~Strikethrough~~ + +Marked text + +
Preformatted text
+ +Small Text + +This is subscript + +This is superscript + +WHO +``` + + + + + + +## Links + + + +[Inline link](https://google.com) + +[Inline link with title](https://google.com "Google") + +[Reference link by name][link1] + +[Reference link by number][1] + +[Reference link by self] + +[link1]: https://google.com +[1]: https://google.com +[Reference link by self]: https://google.com + + + +```markdown +[Inline link](https://google.com) + +[Inline link with title](https://google.com "Google") + +[Reference link by name][link1] + +[Reference link by number][1] + +[Reference link by self] + +[link1]: https://google.com +[1]: https://google.com +[Reference link by self]: https://google.com +``` + + + + + + +## Lists + + + +**Ordered Lists** + +1. Ordered 1 +1. Ordered 2 + 1. Ordered 2a + 1. Ordered 2b + 1. Ordered 2c +1. Ordered 3 + +**Unordered Lists** + +- Unordered 1 +- Unordered 2 + - Unordered 2a + - Unordered 2b + - Unordered 2c +- Unordered 3 + +**Task Lists** + +- [x] Task 1 +- [ ] Task 2 + - [x] Subtask A + - [ ] Subtask B +- [ ] Task 3 + + + +```markdown +**Ordered Lists** + +1. Ordered 1 +1. Ordered 2 + 1. Ordered 2a + 1. Ordered 2b + 1. Ordered 2c +1. Ordered 3 + +**Unordered Lists** + +- Unordered 1 +- Unordered 2 + - Unordered 2a + - Unordered 2b + - Unordered 2c +- Unordered 3 + +**Task Lists** + +- [x] Task 1 +- [ ] Task 2 + - [x] Subtask A + - [ ] Subtask B +- [ ] Task 3 +``` + + + + + + +## Blockquotes + + + +> Cras aliquet nulla quis metus tincidunt, sed placerat enim cursus. Etiam +> turpis nisl, posuere eu condimentum ut, interdum a risus. Sed non luctus mi. +> Quisque malesuada risus sit amet tortor aliquet, a posuere ex iaculis. Vivamus +> ultrices enim dui, eleifend porttitor elit aliquet sed. +> +> *- Quote Source* + + + +```markdown +> Cras aliquet nulla quis metus tincidunt, sed placerat enim cursus. Etiam +> turpis nisl, posuere eu condimentum ut, interdum a risus. Sed non luctus mi. +> Quisque malesuada risus sit amet tortor aliquet, a posuere ex iaculis. Vivamus +> ultrices enim dui, eleifend porttitor elit aliquet sed. +> +> *- Quote Source* +``` + + + + + + +## Code + + + +This is `inline code` + +```javascript +const add = (num1, num2) => num1 + num2; +const total = add(1, 2); + +console.log(total); // 3 +``` + +```html + +

Hello

+ +``` + + + +````markdown +This is `inline code` + +```javascript +const add = (num1, num2) => num1 + num2; +const total = add(1, 2); + +console.log(total); // 3 +``` + +```html + +

Hello

+ +``` +```` + + + + + + +## Notices + +Note, tip, warning, and attention notices are provided by [Flexible alerts](https://github.com/fzankl/docsify-plugin-flexible-alerts) + + + +!> **Important** notice with `inline code` and additional placeholder text used +to force the content to wrap and span multiple lines. + +?> **Tip** notice with `inline code` and additional placeholder text used to +force the content to wrap and span multiple lines. + +> [!NOTE] +> An alert of type 'note' using global style 'callout'. + +> [!TIP] +> An alert of type 'tip' using global style 'callout'. + +> [!WARNING] +> An alert of type 'warning' using global style 'callout'. + +> [!ATTENTION] +> An alert of type 'attention' using global style 'callout'. + + + +```markdown +!> **Important** notice with `inline code` and additional placeholder text used +to force the content to wrap and span multiple lines. + +?> **Tip** notice with `inline code` and additional placeholder text used to +force the content to wrap and span multiple lines. + +> [!NOTE] +> An alert of type 'note' using global style 'callout'. + +> [!TIP] +> An alert of type 'tip' using global style 'callout'. + +> [!WARNING] +> An alert of type 'warning' using global style 'callout'. + +> [!ATTENTION] +> An alert of type 'attention' using global style 'callout'. +``` + + + + + + +## Tabs + +Provided by [docsify-tabs](https://jhildenbiddle.github.io/docsify-tabs). + + + + + +#### **English** + +Hello! + +#### **French** + +Bonjour! + +#### **Italian** + +Ciao! + + + + + +```markdown + + +#### **English** + +Hello! + +#### **French** + +Bonjour! + +#### **Italian** + +Ciao! + + +``` + + + + + + +## Tables + +Checkout [Markdown table generator](https://www.tablesgenerator.com/markdown_tables) to create tables in Markdown. + + + +| Left Align | Center Align | Right Align | Non-Breaking Header | +| ---------- |:------------:| -----------:| ------------------------------ | +| A1 | A2 | A3 | A4 | +| B1 | B2 | B3 | B4 | +| C1 | C2 | C3 | C4 | + + + +```markdown +| Left Align | Center Align | Right Align | Non-Breaking Header | +| ---------- |:------------:| -----------:| ------------------------------ | +| A1 | A2 | A3 | A4 | +| B1 | B2 | B3 | B4 | +| C1 | C2 | C3 | C4 | +``` + + + + + + +## Keyboard + +Using HTML `key`. + + + + Arrow Up + + Arrow Down + + Arrow Left + + Arrow Right + + Caps Lock + + Command + + Control + + Delete + + Delete (Forward) + + End + + Enter + + Escape + + Home + + Page Up + + Page Down + + Option, Alt + + Return + + Shift + + Space + + Tab + + Tab + Shift + + + +```markdown + Arrow Up + + Arrow Down + + Arrow Left + + Arrow Right + + Caps Lock + + Command + + Control + + Delete + + Delete (Forward) + + End + + Enter + + Escape + + Home + + Page Up + + Page Down + + Option, Alt + + Return + + Shift + + Space + + Tab + + Tab + Shift +``` + + + + +## Horizontal Rule + + + +#### ** Rendered ** + +--- + +#### ** Markdown ** + +```markdown +--- +``` + + + + + + +## Images + + + +Inline-style + +![alt text](//source.unsplash.com/daily "Provided by unsplash.com") + +Reference-style + +![alt text][logo] + +[logo]: //source.unsplash.com/collection/881815 "Provided by unsplash.com" + + + +```markdown +**Inline** + +![alt text](//source.unsplash.com/daily "Provided by unsplash.com") + +**Reference** + +![alt text][logo] + +[logo]: //source.unsplash.com/collection/881815 "Provided by unsplash.com" +``` + + + +## Emoji + +A complete list is available here: [Emoji Cheat Sheet](https://www.webpagefx.com/tools/emoji-cheat-sheet/). I personally just copy-paste from [Emojipedia](https://emojipedia.org). + +## Include + +Format to include other files: + +```markdown +[LABLE](url ':include') + +[LABLE](url ':include :type=code') +``` + +### Gist (or files outside) + +```markdown +[cartpole](https://gist.githubusercontent.com/sosiristseng/b57eb8c05b2b9126d48e763206a1667a/raw/cartpole.py ':include') +``` + +[cartpole](https://gist.githubusercontent.com/sosiristseng/b57eb8c05b2b9126d48e763206a1667a/raw/cartpole.py ':include') + + +### Files inside + +```markdown +[GitlabCI](_media/gitlab-ci.yaml ':include') +``` + +```markdown +[example](_media/example.md ':include') +``` + +```markdown +[bird](../_media/birb.mp4 ':include :type=video autoplay loop muted playsinline') +``` diff --git a/docs/mdfiles/lec/00_Introduction.md b/docs/mdfiles/lec/00_Introduction.md new file mode 100644 index 0000000..5535ad7 --- /dev/null +++ b/docs/mdfiles/lec/00_Introduction.md @@ -0,0 +1,14 @@ +# Motivations of Data Structures and Algorithms + + + + + +## Debug + +![image](https://user-images.githubusercontent.com/29009898/110299183-581ccc80-8030-11eb-9f18-192f57d6922d.png) + +- integer `m` undefined +- 會重複算到 +- `i + +--- + +# 如何證明演算法正確? + +## Loop invariance + +After `i-th` iteration of the `for` loop, `arr[m]` is always the smallest among `arr[0]`,...., `arr[i]` +- true when `i=0` +- true when `i=k` → true when `i=k+1` + +### 證明 + +- 先寫要證明什麼 +- 看看離散數學的內容 + +## Efficiency of Algorithm + +- Sequential search +- Binary search + +## Pseudo code for `getMinIndex` + +### Linear serach + +```c +int getMinIndex(int arr[], int len){ + int i; + int m=0; + for(i=0;i arr[i]){ + m=i; + } + } +} + +``` + +```c +GET-MIN-INDEX(A) + +m = 1 +for i = 2 to A.length + if A[m] > A[i] + m = i + +return m + +``` + +### Selection sort + +![image](https://user-images.githubusercontent.com/29009898/110298600-b9906b80-802f-11eb-9d9e-55b1f2f927eb.png) + + +## What is data structure? + +- Scheme of organizing data within computer +- 快: 常常用的東西取的快 +- Good data structure needs proper maintenance algorithms: + - `Construct` / `Update` / `remove` + +## Why data structures and algorithms? + +- Data structures + - Storage + - Network, bandwidth +- Algorithms + - Computation + +## Proper use: Tradeoff of different factors + +# C Programming versus DSA + +- Test + - 怎麼測試程式碼 + - 設計關鍵情形 +- Modualize + - 模組化 +- Tradeoff + - Theory + - Practice + +# What is Array? + +- Index +- Memory is array +- Address + - `arr[index]` , `memory[address]` +- C programming + - Pointer + +## Array as memory block in C + +- Access: `arr[index]` → `memory[arr + index * sizeof(data)]` + - Array 可以快速 update 和拿出來 +- Maintenance: `malloc(sizeof(data)*length)` + - construct (length) + +![image](https://user-images.githubusercontent.com/29009898/110298769-ee9cbe00-802f-11eb-9461-cf2c5cac1088.png) + + +- Garbage collection + +# Orderred array + +![image](https://user-images.githubusercontent.com/29009898/110298847-0116f780-8030-11eb-8efe-de5e00896c29.png) + +## 可以試試 實做 orderred array + +- Insertion +- remove + +# Sorting + +## Insertion Sort + +$O(n^2)$ + +![](https://user-images.githubusercontent.com/29009898/110297582-a335e000-802e-11eb-94dd-322eb7caccb2.png) + +## Selection Sort + +$O(n^2)$ + +![Untitled 1](https://user-images.githubusercontent.com/29009898/110297584-a3ce7680-802e-11eb-8400-65c32a2ae1ca.png) + +# Sequential search algorithm + +--- + +# 作業0 + +![Untitled](https://user-images.githubusercontent.com/29009898/110297587-a4670d00-802e-11eb-9c69-6fe3de90e824.png) + +- 會走到沒有 define 的地區 +- 執行錯誤 +- `ary[-4]` +- `segmentation fault` 每次呼叫都是系統給空間,如果取用非法空間就是 `segmentation fault` \ No newline at end of file diff --git a/docs/mdfiles/lec/README.md b/docs/mdfiles/lec/README.md new file mode 100644 index 0000000..db04fad --- /dev/null +++ b/docs/mdfiles/lec/README.md @@ -0,0 +1,73 @@ +# Julia tutorial + +1. [Think Julia](https://benlauwens.github.io/ThinkJulia.jl/latest/book.html) +2. [From zero to Julia](https://techytok.com/from-zero-to-julia/) +3. [Julia cheat sheet](https://juliadocs.github.io/Julia-Cheat-Sheet/) if you are familiar with Python and/or MATLAB. +4. [Official Julia Manual](https://docs.julialang.org/) for the API. +5. [Julia for Pythonistas](https://colab.research.google.com/github/ageron/julia_notebooks/blob/master/Julia_for_Pythonistas.ipynb) +6. [Learn Julia](https://julialang.org/learning/) from the official website. + +# Why Julia? + +[John F. Gibson's talk](https://github.com/johnfgibson/whyjulia/blob/master/1-whyjulia.ipynb) + +- Python/MATLAB-like syntax + natural math presentation +- C-like speed (!) + built-in parallelism +- Composable and extensible (e.g. DiffEq + ANN) +- Right tool for this course to solve differential equations and stochastic simulations + +## Why not Julia? + +- Time to first plot (TTFF). Code needs to be compiled first so it feels less responsive than Python. [Somebody just gave up](https://www.zverovich.net/2016/05/13/giving-up-on-julia.html). +- Less libraries in some domains. (e.g. metabolic networks) +- [Arrays start at one](https://i.imgur.com/VRSkSGd.jpg) + +# Install and run Julia + +[Download Julia](https://julialang.org/downloads/) and install. You could give version `1.6-rc1` a try because it loads much fater than previous versions. + +## REPL workflow + +Run the julia REPL (terminal) and start typing. + +## Jupyter workflow + +This workflow suits demonstration + +Open the Julia terminal and enter the following commands to install IJulia, the Julia kernel for Jupyter notebooks. + +```julia +using Pkg + +pkg"add IJulia" +``` + +And then run Jupyter Lab either from the Julia terminal. + +```julia +using IJulia + +# It will prompt you to install jupyter lab, enter y to proceed +# Will open at ${HOME}, thatby default it is "C:\Users\yourname\" in Windows systems. +IJulia.jupyterlab() +``` + +Or from Anaconda's Jupyter lab interface if you have installed Anaconda. + +## VS Code workflow + +[VS Code](https://code.visualstudio.com/) with [Julia extension](https://www.julia-vscode.org/) works better for structured code. See this [Youtube video](https://www.youtube.com/watch?v=IdhnP00Y1Ks). + +!> Juno, the original IDE for Julia, is [in maintenance mode](https://www.youtube.com/watch?v=rQ7D1lXt3GM). + +## My computer cannot run Julia + +### Google collab + +Open this [Julia for Pythonistas](https://colab.research.google.com/github/ageron/julia_notebooks/blob/master/Julia_for_Pythonistas.ipynb) notebook, save a copy to your Google drive, and run it. + +### Nextjournal + +[Nextjournal](https://nextjournal.com/) provides a notebook-like UI for reproducible research. + +You could use [Tmy template for Julia 1.6 + Differential Equations](https://nextjournal.com/bebi5009/julia-template). diff --git a/docs/mdfiles/lec/_sidebar.md b/docs/mdfiles/lec/_sidebar.md new file mode 100644 index 0000000..bd62b9e --- /dev/null +++ b/docs/mdfiles/lec/_sidebar.md @@ -0,0 +1,3 @@ +- [🤹 **Lecture Note**](README.md) +- [Introduction](00_Introduction.md) +- [Arrays and Linked Lists](01_linked_list.md) \ No newline at end of file