From f6a192f9dc4588a4768c34539dd0cf0773c0a0e7 Mon Sep 17 00:00:00 2001 From: PurityGwaro Date: Wed, 16 Aug 2023 20:28:44 +0300 Subject: [PATCH 1/3] answer big o notation exercises --- Notes/Exercise/Questions/Exercise-01.md | 4 +++- Notes/Exercise/Questions/Exercise-02.md | 4 +++- Notes/Exercise/Questions/Exercise-05.md | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Notes/Exercise/Questions/Exercise-01.md b/Notes/Exercise/Questions/Exercise-01.md index 33ae964..b55dca5 100644 --- a/Notes/Exercise/Questions/Exercise-01.md +++ b/Notes/Exercise/Questions/Exercise-01.md @@ -6,4 +6,6 @@ function isLeapYear(year){ return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0); } -```` \ No newline at end of file +```` + +The time complexity for `isLeapYear` is: `O(1)`. Regardless of the input the algorithm will still go through the same number of operations during operation. \ No newline at end of file diff --git a/Notes/Exercise/Questions/Exercise-02.md b/Notes/Exercise/Questions/Exercise-02.md index fcc949f..129809d 100644 --- a/Notes/Exercise/Questions/Exercise-02.md +++ b/Notes/Exercise/Questions/Exercise-02.md @@ -11,4 +11,6 @@ const sumOfArray = (array) => { return sum; } -``` \ No newline at end of file +``` + +The time complexity for `sumOfArray` is: `O(n)`. The number of operations that will be used in the for loop will increase as the input increases. \ No newline at end of file diff --git a/Notes/Exercise/Questions/Exercise-05.md b/Notes/Exercise/Questions/Exercise-05.md index 08f91eb..429455c 100644 --- a/Notes/Exercise/Questions/Exercise-05.md +++ b/Notes/Exercise/Questions/Exercise-05.md @@ -11,4 +11,6 @@ const median (array) => { return array[middle]; } -``` \ No newline at end of file +``` + +The time complexity for `median` is: `O(1)`. Regardless of the length of the input array accessing the middle elements will take constant time. \ No newline at end of file From 9b3b5ac88767a0d5acef72bdf9c0d2ba3ffbe4c7 Mon Sep 17 00:00:00 2001 From: PurityGwaro Date: Wed, 16 Aug 2023 21:56:06 +0300 Subject: [PATCH 2/3] answer execise 4 --- Notes/Exercise/Questions/Exercise-04.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Notes/Exercise/Questions/Exercise-04.md b/Notes/Exercise/Questions/Exercise-04.md index b02ecfc..a4a011f 100644 --- a/Notes/Exercise/Questions/Exercise-04.md +++ b/Notes/Exercise/Questions/Exercise-04.md @@ -1 +1,8 @@ -**Write a function that accepts an array of strings and return a new array that only contains the strings that start with the character 'd'. Use Big O Notation to describe the time complexity of the function.** \ No newline at end of file +**Write a function that accepts an array of strings and return a new array that only contains the strings that start with the character 'd'. Use Big O Notation to describe the time complexity of the function.** + +```js +const returnString = (arr) => { + return arr.filter(string => string.toLowerCase().startsWith('d')); +} +``` +The above function will have a time complexity of `O(n)`. This is because the filteration operations will increase as the input increases. \ No newline at end of file From 994d52783d0044127930609fecb3e470f6e6b7e6 Mon Sep 17 00:00:00 2001 From: PurityGwaro Date: Sun, 20 Aug 2023 23:40:06 +0300 Subject: [PATCH 3/3] place the solutions in a solutions folder --- Notes/Exercise/Questions/Exercise-01.md | 4 +--- Notes/Exercise/Questions/Exercise-02.md | 4 +--- Notes/Exercise/Questions/Exercise-04.md | 7 ------- Notes/Exercise/Questions/Exercise-05.md | 4 +--- Notes/Exercise/Solutions/Solution-01.md | 11 +++++++++++ Notes/Exercise/Solutions/Solution-02.md | 16 ++++++++++++++++ Notes/Exercise/Solutions/Solution-03.md | 3 +++ Notes/Exercise/Solutions/Solution-04.md | 8 ++++++++ Notes/Exercise/Solutions/Solution-05.md | 16 ++++++++++++++++ 9 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 Notes/Exercise/Solutions/Solution-01.md create mode 100644 Notes/Exercise/Solutions/Solution-02.md create mode 100644 Notes/Exercise/Solutions/Solution-03.md create mode 100644 Notes/Exercise/Solutions/Solution-04.md create mode 100644 Notes/Exercise/Solutions/Solution-05.md diff --git a/Notes/Exercise/Questions/Exercise-01.md b/Notes/Exercise/Questions/Exercise-01.md index b55dca5..33ae964 100644 --- a/Notes/Exercise/Questions/Exercise-01.md +++ b/Notes/Exercise/Questions/Exercise-01.md @@ -6,6 +6,4 @@ function isLeapYear(year){ return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0); } -```` - -The time complexity for `isLeapYear` is: `O(1)`. Regardless of the input the algorithm will still go through the same number of operations during operation. \ No newline at end of file +```` \ No newline at end of file diff --git a/Notes/Exercise/Questions/Exercise-02.md b/Notes/Exercise/Questions/Exercise-02.md index 129809d..fcc949f 100644 --- a/Notes/Exercise/Questions/Exercise-02.md +++ b/Notes/Exercise/Questions/Exercise-02.md @@ -11,6 +11,4 @@ const sumOfArray = (array) => { return sum; } -``` - -The time complexity for `sumOfArray` is: `O(n)`. The number of operations that will be used in the for loop will increase as the input increases. \ No newline at end of file +``` \ No newline at end of file diff --git a/Notes/Exercise/Questions/Exercise-04.md b/Notes/Exercise/Questions/Exercise-04.md index a4a011f..c598d96 100644 --- a/Notes/Exercise/Questions/Exercise-04.md +++ b/Notes/Exercise/Questions/Exercise-04.md @@ -1,8 +1 @@ **Write a function that accepts an array of strings and return a new array that only contains the strings that start with the character 'd'. Use Big O Notation to describe the time complexity of the function.** - -```js -const returnString = (arr) => { - return arr.filter(string => string.toLowerCase().startsWith('d')); -} -``` -The above function will have a time complexity of `O(n)`. This is because the filteration operations will increase as the input increases. \ No newline at end of file diff --git a/Notes/Exercise/Questions/Exercise-05.md b/Notes/Exercise/Questions/Exercise-05.md index 429455c..08f91eb 100644 --- a/Notes/Exercise/Questions/Exercise-05.md +++ b/Notes/Exercise/Questions/Exercise-05.md @@ -11,6 +11,4 @@ const median (array) => { return array[middle]; } -``` - -The time complexity for `median` is: `O(1)`. Regardless of the length of the input array accessing the middle elements will take constant time. \ No newline at end of file +``` \ No newline at end of file diff --git a/Notes/Exercise/Solutions/Solution-01.md b/Notes/Exercise/Solutions/Solution-01.md new file mode 100644 index 0000000..b55dca5 --- /dev/null +++ b/Notes/Exercise/Solutions/Solution-01.md @@ -0,0 +1,11 @@ +**Use Big O Notation to describe the time complexity of the following function that determines whether a given yaer is a leap year:** + +```js + +function isLeapYear(year){ + return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0); +} + +```` + +The time complexity for `isLeapYear` is: `O(1)`. Regardless of the input the algorithm will still go through the same number of operations during operation. \ No newline at end of file diff --git a/Notes/Exercise/Solutions/Solution-02.md b/Notes/Exercise/Solutions/Solution-02.md new file mode 100644 index 0000000..129809d --- /dev/null +++ b/Notes/Exercise/Solutions/Solution-02.md @@ -0,0 +1,16 @@ +**Use Big O Notation to describe the time complexity of the following function that sums up all the numbers from a given array:** + +```js + +const sumOfArray = (array) => { + let sum = 0; + + for(let i = 0; i < array.length; i++){ + sum += array[i]; + } + return sum; +} + +``` + +The time complexity for `sumOfArray` is: `O(n)`. The number of operations that will be used in the for loop will increase as the input increases. \ No newline at end of file diff --git a/Notes/Exercise/Solutions/Solution-03.md b/Notes/Exercise/Solutions/Solution-03.md new file mode 100644 index 0000000..6556506 --- /dev/null +++ b/Notes/Exercise/Solutions/Solution-03.md @@ -0,0 +1,3 @@ +**Imagine you have a chessboard and put a single grain of rice on one square. On the second square, you put 2 grains of rice, since that is double the amount of rice on the previous square. On the third square, you put 4 grains and on the fourth square you put 8 grains. The pattern continues like that.** + +**Write a function calculates which square you will need to place a certain number of rice grains. For example, for 16 grains, the function will return 5. Use Big O Notation to describe the time complexity of that function.** \ No newline at end of file diff --git a/Notes/Exercise/Solutions/Solution-04.md b/Notes/Exercise/Solutions/Solution-04.md new file mode 100644 index 0000000..a4a011f --- /dev/null +++ b/Notes/Exercise/Solutions/Solution-04.md @@ -0,0 +1,8 @@ +**Write a function that accepts an array of strings and return a new array that only contains the strings that start with the character 'd'. Use Big O Notation to describe the time complexity of the function.** + +```js +const returnString = (arr) => { + return arr.filter(string => string.toLowerCase().startsWith('d')); +} +``` +The above function will have a time complexity of `O(n)`. This is because the filteration operations will increase as the input increases. \ No newline at end of file diff --git a/Notes/Exercise/Solutions/Solution-05.md b/Notes/Exercise/Solutions/Solution-05.md new file mode 100644 index 0000000..429455c --- /dev/null +++ b/Notes/Exercise/Solutions/Solution-05.md @@ -0,0 +1,16 @@ +**The following function calculates the median from an ordered array. Describe its time complexity in terms of Big O Notation:** + +```js + +const median (array) => { + const middle = Math.floor(array.length / 2); + + if(array.length % 2 === 0) { + return (array[middle - 1] + array[middle]) / 2; + } + return array[middle]; +} + +``` + +The time complexity for `median` is: `O(1)`. Regardless of the length of the input array accessing the middle elements will take constant time. \ No newline at end of file