-
Notifications
You must be signed in to change notification settings - Fork 6.7k
[ WDPT SAO 05-2021 ] Thiago Henrique Silva de Campos #2364
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
Closed
Closed
Changes from all commits
Commits
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 |
---|---|---|
@@ -1,19 +1,95 @@ | ||
// Iteration #1: Find the maximum | ||
|
||
function maxOfTwoNumbers(num1, num2){ | ||
if(num1 > num2){ | ||
return num1; | ||
} | ||
return num2; | ||
} | ||
|
||
// Iteration #2: Find longest word | ||
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; | ||
const words = ['aaaaaaaaaaaaa','mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot','aaaa']; | ||
const wordsNull = []; | ||
function findLongestWord(arrOfWords){ | ||
let len = arrOfWords.length; | ||
let longest = '' | ||
switch(len){ | ||
case 0: | ||
return null; | ||
case 1: | ||
return arrOfWords[0]; | ||
default: | ||
for (let i = 0; i < arrOfWords.length; i++) { | ||
if (longest.length < arrOfWords[i].length) { | ||
longest = arrOfWords[i]; | ||
} | ||
} | ||
return longest; | ||
} | ||
} | ||
|
||
// Iteration #3: Calculate the sum | ||
|
||
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; | ||
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; | ||
|
||
function sumNumbers(arrOfNumbers){ | ||
let len = arrOfNumbers.length; | ||
switch(len){ | ||
case 0: | ||
return 0 | ||
case 1: | ||
return arrOfNumbers[0] | ||
default: | ||
return arrOfNumbers.reduce((va1, va2) => va1 + va2, 0) | ||
} | ||
} | ||
function sum(arr){ | ||
let sum = 0; | ||
if(arr.length === 0){ | ||
return null | ||
} | ||
for(let i = 0; i < arr.length; i+=1){ | ||
switch(typeof arr[i]){ | ||
case 'boolean': | ||
if(arr[i]){sum +=1} | ||
else{sum -=1} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E no caso, "false" pode ser considerado como 0 para a soma ;) |
||
break; | ||
case 'string': | ||
sum += arr[i].length; | ||
break; | ||
case 'number': | ||
sum += arr[i]; | ||
break; | ||
default: | ||
throw new Error(`Unsupported data type sir or ma'am`) | ||
} | ||
} | ||
return sum | ||
} | ||
sum(mixedArr) | ||
|
||
// Iteration #4: Calculate the average | ||
// Level 1: Array of numbers | ||
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; | ||
function averageNumbers(arr){ | ||
sum = sum(arr); | ||
if(sum === null){ | ||
return null | ||
} | ||
return sum / arr.length; | ||
} | ||
|
||
// Level 2: Array of strings | ||
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; | ||
const wordsArr = ['correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; | ||
function averageWordLength(arr){ | ||
return sum(arr) / arr.length | ||
} | ||
averageWordLength(wordsArr) | ||
|
||
//A generic avg() function | ||
function avg(arr){ | ||
return sum(arr) / arr.length | ||
} | ||
avg(mixedArr) | ||
|
||
// Iteration #5: Unique arrays | ||
const wordsUnique = [ | ||
|
@@ -27,11 +103,28 @@ const wordsUnique = [ | |
'poison', | ||
'communion', | ||
'simple', | ||
'bring', | ||
'bring', | ||
'bring', | ||
'bring', | ||
'bring' | ||
]; | ||
function uniquifyArray(arr){ | ||
return arr.filter( (cb, i) => { return arr.indexOf(cb) === i}) | ||
} | ||
uniquifyArray(wordsUnique) | ||
|
||
// Iteration #6: Find elements | ||
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; | ||
function doesWordExist(arr, word){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aqui faltou só a condição de borda, array vazio. |
||
for(let i = 0; i < arr.length; i += 1){ | ||
if(arr[i] === word){ | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
doesWordExist(wordsFind, 'subset') | ||
|
||
// Iteration #7: Count repetition | ||
const wordsCount = [ | ||
|
@@ -47,8 +140,16 @@ const wordsCount = [ | |
'disobedience', | ||
'matter' | ||
]; | ||
function howManyTimes(arr, word){ | ||
let repetition = 0; | ||
for(let i = 0; i < arr.length; i += 1){ | ||
if(arr[i] === word) { repetition +=1 } | ||
} | ||
return repetition | ||
} | ||
howManyTimes(wordsCount,'machine') | ||
|
||
// Iteration #8: Bonus | ||
// Iteration #8/8.1: Bonus | ||
|
||
const matrix = [ | ||
[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8], | ||
|
@@ -72,3 +173,55 @@ const matrix = [ | |
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54], | ||
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] | ||
]; | ||
|
||
const matrix2 = [ | ||
[2,2,2,2], | ||
[2,2,2,2], | ||
[2,2,2,2], | ||
[2,2,2,2], | ||
] | ||
|
||
|
||
function greatestProduct(matrix){ | ||
let largestProduct = 0; | ||
for(let row = 0; row < matrix.length; row ++){ | ||
for(let column = 0; column < matrix[0].length; column ++){ | ||
let cell = matrix[row][column] | ||
//busca horizontal | ||
let product = cell | ||
for(let i = 1; i <= 3; i += 1){ | ||
if(matrix[row] && matrix[row][column+1]){ | ||
product *= matrix[row][column + 1] | ||
}else { break } | ||
} | ||
if(product > largestProduct) { largestProduct = product } | ||
//busca horizontal | ||
product = cell | ||
for(i = 1; i <= 3; i += 1 ){ | ||
if(matrix[row + i] && matrix[row + i][column]){ | ||
product = product * matrix[row + i][column] | ||
}else{ break } | ||
} | ||
if(product > largestProduct) { largestProduct = product } | ||
//busca diagonal(direit) | ||
product = cell | ||
for(i = 1; i <= 3; i += 1 ){ | ||
if(matrix[row + i] && matrix[row + i][column+1]){ | ||
product = product * matrix[row + i][column+1] | ||
}else{ break } | ||
} | ||
if(product > largestProduct) { largestProduct = product } | ||
//busca diagonal(esquerda) | ||
product = cell | ||
for(i = 1; i <= 3; i += 1 ){ | ||
if(matrix[row - i] && matrix[row - i][column + i]){ | ||
product = product * matrix[row - i][column + i] | ||
}else{ break } | ||
} | ||
if(product > largestProduct) { largestProduct = product } | ||
} | ||
} | ||
|
||
return largestProduct | ||
} | ||
console.log(greatestProduct(matrix)) |
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.
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.
Olha a pegadinha do Jasmine!
Nesse aqui ele pede para que retorne 0, se o array vier vazio!