forked from wdi-sg/js-control-flow
-
Notifications
You must be signed in to change notification settings - Fork 11
Uploaded solutions to all exercises #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ckkok
wants to merge
3
commits into
awongh:master
Choose a base branch
from
ckkok:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,10 @@ | ||
| const exampleData = ["super", "hyper", "mathematical", "thisisalongword", "thisisalongerword", "standard", "forvalhalla"]; | ||
| const maxLength = 9; | ||
|
|
||
| function filterLongWords(arr, maxLength) { | ||
| if (typeof(maxLength) != "number" || parseInt(maxLength) != maxLength || maxLength < 0) {console.error("Need a positive number for max length!"); return}; | ||
| console.log(maxLength); | ||
| return arr.filter(word => word.length > maxLength) | ||
| } | ||
|
|
||
| filterLongWords(exampleData, maxLength); |
This file contains hidden or 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,93 @@ | ||
| // The concept of this solution is that the residues of integers | ||
| // modulo 15 are periodic (with period 15). The implementation can | ||
| // be done in a few ways, but since machine language deals with binary | ||
| // numbers, it's possible that the fastest implementation uses | ||
| // binary numbers as a filter (because division is expensive!). | ||
| // Nevertheless, I'll write here (in 15 min?) | ||
| // both the binary and the human-readable version. | ||
|
|
||
| // Note: 810092018 in binary is 110000010010010000011000010000 | ||
| // Split into pairs, 11 00 00 01 00 10 01 00 00 01 10 00 01 00 00 | ||
| // Notice that there are 15 pairs of bits, which represent: | ||
| // 11 - divisible by 15 | ||
| // 01 - divisible by 3 | ||
| // 10 - divisible by 5 | ||
|
|
||
| // To effectively use that large binary string as a filter, we use bitwise | ||
| // comparison operators, e.g. the & comparison operator, and bitshift operators | ||
| // e.g. the >> and << shift operators. | ||
|
|
||
| // The reason why that binary string is written like this "in reverse" is that | ||
| // the digits with smaller values are on the right, e.g. in decimal, the digit 7 | ||
| // has the smallest value in the number 1234127. | ||
| 'use strict'; | ||
|
|
||
| const end = 100; | ||
| const start = 1; | ||
|
|
||
| function bitFizzBuzz(start, end) { | ||
| var bFilter = 810092048; | ||
| let counter = bFilter & 3; | ||
|
|
||
| for (let i = 1; i < start; i++) { | ||
| bFilter = bFilter >> 2 | counter << 28; | ||
| } | ||
|
|
||
| for (let num = start; num <= end; num++){ | ||
| let counter = bFilter & 3; | ||
| let result = 'FizzBuzz'; | ||
| // if the binary string returns 0 when bitwise compared to 3 (11), then it's neither | ||
| // divisible by 3 (represented by 01) nor 5 (represented by 10). | ||
| // if the binary string returns 1 when bitwise compared to 3 (11), then it's divisible by 3 (01). | ||
| // if the binary string returns 2 when bitwise compared to 3 (11), then it's divisible by 5 | ||
| // (remember, 10 indicates divisibility by 5). | ||
|
|
||
| if (counter === 0) { | ||
| result = num; | ||
| } else if (counter === 1) { | ||
| result = 'Fizz'; | ||
| } else if (counter === 2) { | ||
| result = 'Buzz'; | ||
| } | ||
|
|
||
| // Sadly this algorithm relies on its own iterative nature to seek the correct position in the | ||
| // binary filter, and so doing bitFizzBuzz(15) in the console will not work. | ||
| // We will need to do something like "for (let i=1;i<upperLimit;i++) {console.log(bitFizzBuzz(i))}" | ||
| // to actually use this. Still need to benchmark this vs the normal implementation below though! | ||
| // We can of course shift the binary string beforehand to account for this, but *lazy*. Edit: Ah | ||
| // heck it, I wrote that thing above. | ||
|
|
||
| // Need to either shift the binary filter to the right by 2 digits to prepare for the next iteration, or to reset it if it's done. | ||
| bFilter = bFilter >> 2 | counter << 28; | ||
|
|
||
| console.log(result); | ||
| } | ||
| console.log("Bitwise implementation"); | ||
| } | ||
|
|
||
|
|
||
| // And now for the normal implementations... | ||
| function normalFizzBuzz(start, end) { | ||
| let nFilter = [false, false, 'Fizz', false, 'Buzz', 'Fizz', false, false, 'Fizz', 'Buzz', false, 'Fizz', false, false, 'FizzBuzz']; | ||
| for (let i = start; i <= end; i++) { | ||
| let result = nFilter[(i - 1) % 15]; //This is an expensive operation, but how much more expensive is it in JS compared to lower level languages, and is it expensive enough to merit the bit comparison implementation above?? If we wanted to cheat like in the bit comparison implementation, we wouldn't do this and just increment the index each time, subtracting 15 each time we hit 16 for the fastest possible runtime :D | ||
| console.log(((result) ? result : i)); | ||
| } | ||
| console.log("Normal implementation via modulo check"); | ||
| } | ||
|
|
||
| function nFizzBuzz(start, end) { //This is the cheaty implementation described above :D | ||
| let nFilter = [false, false, 'Fizz', false, 'Buzz', 'Fizz', false, false, 'Fizz', 'Buzz', false, 'Fizz', false, false, 'FizzBuzz']; | ||
| var r = (start % 16) - 1; | ||
| for (let i = start; i <= end; i++){ | ||
| if (r > 14) {r-=15}; | ||
| console.log((nFilter[r]) ? nFilter[r] : i); | ||
| r++; | ||
| } | ||
| console.log("Normal implementation with only one modulo check :D"); | ||
| } | ||
|
|
||
| // Pick your poison! :D | ||
| //bitFizzBuzz(start, end); | ||
| //normalFizzBuzz(start, end); | ||
| nFizzBuzz(start, end); | ||
This file contains hidden or 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,18 @@ | ||
| const score = 56; | ||
|
|
||
| function grade(testScore) { | ||
| if (typeof(testScore) != "number" || testScore < 0 || testScore > 100) {console.error("Need a valid test score between 0 and 100!"); return} | ||
| let result = "A"; | ||
| switch (true) { | ||
| case (testScore < 45): {result = "F"; break}; | ||
| case (testScore < 50): {result = "E"; break}; | ||
| case (testScore < 55): {result = "D"; break}; | ||
| case (testScore < 60): {result = "C"; break}; | ||
| case (testScore < 70): {result = "B"; break}; | ||
| default: | ||
| break; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| console.log("A score of " + score + " gives you a grade of " + grade(score) + "."); |
This file contains hidden or 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,38 @@ | ||
| var phoneBook = { | ||
| "Abe": "111-111-1111", | ||
| "Bob": "222-222-2222", | ||
| "Cam": "333-333-3333", | ||
| "Dan": "444-444-4444", | ||
| "Ern": "555-555-5555", | ||
| "Fry": "111-111-1111", | ||
| "Gil": "222-222-2222", | ||
| "Hal": "333-333-3333", | ||
| "Ike": "444-444-4444", | ||
| "Jim": "555-555-5555", | ||
| "Kip": "111-111-1111", | ||
| "Liv": "222-222-2222", | ||
| "Mia": "333-333-3333", | ||
| "Nik": "444-444-4444", | ||
| "Oli": "555-555-5555", | ||
| "Pam": "111-111-1111", | ||
| "Qiq": "222-222-2222", | ||
| "Rob": "333-333-3333", | ||
| "Stu": "444-444-4444", | ||
| "Tad": "555-555-5555", | ||
| "Uwe": "111-111-1111", | ||
| "Val": "222-222-2222", | ||
| "Wil": "333-333-3333", | ||
| "Xiu": "444-444-4444", | ||
| "Yam": "555-555-5555", | ||
| "Zed": "111-111-1111" | ||
| }; | ||
|
|
||
| function phoneBookLookUp (phonebook, number) { | ||
| for (let name in phonebook) { | ||
| if (phonebook[name] == number) { | ||
| console.log(name) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| console.log(phoneBookLookUp(phoneBook, "333-333-3333")); |
This file contains hidden or 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,23 @@ | ||
| const inputString = 'building'; | ||
|
|
||
| function reverseString(num) { | ||
| // Iteratively concatenate a result string with the character popped off at the end of the given string | ||
| let result = ""; | ||
| let tmp = num.split(''); | ||
| while (tmp.length) { | ||
| result += tmp.pop(); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function reverseStringAlt(num) { | ||
| // Can't decide if this implementation would be faster. What happens if we want to reverse an entire novel? :D | ||
| let result = ""; | ||
| for (let i = num.length - 1; i >= 0; i--) { | ||
| result += num.charAt(i); | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| console.log(reverseString(inputString)); | ||
| console.log(reverseStringAlt(inputString)); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I'm checking out this discussion function of GitHub... I've never used this before. Seems a bit messy D:)
Note: This explanation needs to be better phrased. The | isn't conventionally referred to by the "or reset the ..." phrase. What it's doing is 3 bitwise operators, shift to the right by 2, then bitwise-OR with the previous result shifted left by 28 (effectively returning the current 2-digit binary filter to all the way to the left for use later).