Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Arrays#154
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Arrays#154
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
12 changes: 6 additions & 6 deletions1-js/05-data-types/04-array/1-item-value/solution.md
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,17 +1,17 @@ | ||
Výsledek je `4`: | ||
```js run | ||
letovoce = ["Jablko", "Hruška", "Pomeranč"]; | ||
letnákupníKošík =ovoce; | ||
nákupníKošík.push("Banán"); | ||
*!* | ||
alert(ovoce.length ); // 4 | ||
*/!* | ||
``` | ||
Je to tím, že pole jsou objekty. Proto jsou `nákupníKošík` i `ovoce` odkazy na totéž pole. | ||
16 changes: 8 additions & 8 deletions1-js/05-data-types/04-array/1-item-value/task.md
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
16 changes: 8 additions & 8 deletions1-js/05-data-types/04-array/10-maximal-subarray/_js.view/solution.js
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,11 +1,11 @@ | ||
functionvraťMaxSoučetPodpole(arr) { | ||
letmaxSoučet = 0; | ||
letčástečnýSoučet = 0; | ||
for (letprvek ofpole) { | ||
částečnýSoučet +=prvek; | ||
maxSoučet = Math.max(maxSoučet, částečnýSoučet); | ||
if (částečnýSoučet < 0)částečnýSoučet = 0; | ||
} | ||
returnmaxSoučet; | ||
} |
38 changes: 19 additions & 19 deletions1-js/05-data-types/04-array/10-maximal-subarray/_js.view/test.js
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,37 +1,37 @@ | ||
describe("vraťMaxSoučetPodpole", function() { | ||
it("maximální podsoučet[1, 2, 3]se rovná 6", function() { | ||
assert.equal(vraťMaxSoučetPodpole([1, 2, 3]), 6); | ||
}); | ||
it("maximální podsoučet[-1, 2, 3, -9]se rovná 5", function() { | ||
assert.equal(vraťMaxSoučetPodpole([-1, 2, 3, -9]), 5); | ||
}); | ||
it("maximální podsoučet[-1, 2, 3, -9, 11]se rovná 11", function() { | ||
assert.equal(vraťMaxSoučetPodpole([-1, 2, 3, -9, 11]), 11); | ||
}); | ||
it("maximální podsoučet[-2, -1, 1, 2]se rovná 3", function() { | ||
assert.equal(vraťMaxSoučetPodpole([-2, -1, 1, 2]), 3); | ||
}); | ||
it("maximální podsoučet[100, -9, 2, -3, 5]se rovná 100", function() { | ||
assert.equal(vraťMaxSoučetPodpole([100, -9, 2, -3, 5]), 100); | ||
}); | ||
it("maximální podsoučet[]se rovná 0", function() { | ||
assert.equal(vraťMaxSoučetPodpole([]), 0); | ||
}); | ||
it("maximální podsoučet[-1]se rovná 0", function() { | ||
assert.equal(vraťMaxSoučetPodpole([-1]), 0); | ||
}); | ||
it("maximální podsoučet[-1, -2]se rovná 0", function() { | ||
assert.equal(vraťMaxSoučetPodpole([-1, -2]), 0); | ||
}); | ||
it("maximální podsoučet[2, -8, 5, -1, 2, -3, 2]se rovná 6", function() { | ||
assert.equal(vraťMaxSoučetPodpole([2, -8, 5, -1, 2, -3, 2]), 6); | ||
}); | ||
}); |
92 changes: 46 additions & 46 deletions1-js/05-data-types/04-array/10-maximal-subarray/solution.md
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,94 +1,94 @@ | ||
#Pomalé řešení | ||
Můžeme spočítat všechny možné podsoučty. | ||
Nejjednodušším způsobem je vzít každý prvek a počítat součty všech podpolí, která jím začínají. | ||
Například pro `[-1, 2, 3, -9, 11]`: | ||
```js no-beautify | ||
//Začínající -1: | ||
-1 | ||
-1 + 2 | ||
-1 + 2 + 3 | ||
-1 + 2 + 3 + (-9) | ||
-1 + 2 + 3 + (-9) + 11 | ||
//Začínající 2: | ||
2 | ||
2 + 3 | ||
2 + 3 + (-9) | ||
2 + 3 + (-9) + 11 | ||
//Začínající 3: | ||
3 | ||
3 + (-9) | ||
3 + (-9) + 11 | ||
//Začínající -9: | ||
-9 | ||
-9 + 11 | ||
//Začínající 11: | ||
11 | ||
``` | ||
Kód je ve skutečnosti vnořený cyklus: vnější cyklus prochází prvky pole, vnitřní počítá podsoučty počínaje aktuálním prvkem. | ||
```js run | ||
functionvraťMaxSoučetPodpole(pole) { | ||
letmaxSoučet = 0; //nevezmeme-li žádné prvky, vrátí se nula | ||
for (let i = 0; i <pole.length; i++) { | ||
letsoučetPevnýZačátek = 0; | ||
for (let j = i; j <pole.length; j++) { | ||
součetPevnýZačátek +=pole[j]; | ||
maxSoučet = Math.max(maxSoučet, součetPevnýZačátek); | ||
} | ||
} | ||
returnmaxSoučet; | ||
} | ||
alert(vraťMaxSoučetPodpole([-1, 2, 3, -9]) ); // 5 | ||
alert(vraťMaxSoučetPodpole([-1, 2, 3, -9, 11]) ); // 11 | ||
alert(vraťMaxSoučetPodpole([-2, -1, 1, 2]) ); // 3 | ||
alert(vraťMaxSoučetPodpole([1, 2, 3]) ); // 6 | ||
alert(vraťMaxSoučetPodpole([100, -9, 2, -3, 5]) ); // 100 | ||
``` | ||
Toto řešení má časovou složitost[O(n<sup>2</sup>)](https://cs.wikipedia.org/wiki/Landauova_notace).Jinými slovy, když zvětšíme pole dvojnásobně, algoritmus bude pracovat čtyřikrát déle. | ||
Pro velká pole (1000, 10000nebo více prvků) mohou takové algoritmy vést k vážnému zpomalení. | ||
#Rychlé řešení | ||
Budeme procházet prvky pole a pamatovat si aktuální částečný součet prvků v proměnné`s`.Bude-li `s`v některém bodě záporné, přiřadíme`s=0`.Odpovědí budemaximumvšech takových`s`. | ||
Pokud je popis příliš vágní, prosíme nahlédněte do kódu, je dosti krátký: | ||
```js run demo | ||
functionvraťMaxSoučetPodpole(pole) { | ||
letmaxSoučet = 0; | ||
letčástečnýSoučet = 0; | ||
for (letprvek ofpole) { //pro každý prvek pole | ||
částečnýSoučet +=prvek; //přičteme jej do částečnýSoučet | ||
maxSoučet = Math.max(maxSoučet, částečnýSoučet); //zapamatujeme si maximum | ||
if (částečnýSoučet < 0)částečnýSoučet = 0; //je-li součet záporný, vynulujeme ho | ||
} | ||
returnmaxSoučet; | ||
} | ||
alert(vraťMaxSoučetPodpole([-1, 2, 3, -9]) ); // 5 | ||
alert(vraťMaxSoučetPodpole([-1, 2, 3, -9, 11]) ); // 11 | ||
alert(vraťMaxSoučetPodpole([-2, -1, 1, 2]) ); // 3 | ||
alert(vraťMaxSoučetPodpole([100, -9, 2, -3, 5]) ); // 100 | ||
alert(vraťMaxSoučetPodpole([1, 2, 3]) ); // 6 | ||
alert(vraťMaxSoučetPodpole([-1, -2, -3]) ); // 0 | ||
``` | ||
Tento algoritmus vyžaduje přesně 1průchod polem, takže jeho časová složitost je O(n). | ||
Podrobnější informace o algoritmu můžete najít zde: [Maximum subarray problem (Problém maximálního podpole)](http://en.wikipedia.org/wiki/Maximum_subarray_problem).Není-li vám stále jasné, proč to funguje, potom si prosíme projděte algoritmus na výše uvedených příkladech a podívejte se, jak funguje. Je to lepší než jakákoli slova. |
28 changes: 14 additions & 14 deletions1-js/05-data-types/04-array/10-maximal-subarray/task.md
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
10 changes: 5 additions & 5 deletions1-js/05-data-types/04-array/2-create-array/solution.md
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,10 +1,10 @@ | ||
```js run | ||
letstyly = ["Jazz", "Blues"]; | ||
styly.push("Rock-n-Roll"); | ||
styly[Math.floor((styly.length - 1) / 2)] = "Klasika"; | ||
alert(styly.shift() ); | ||
styly.unshift("Rap", "Reggae"); | ||
``` | ||
22 changes: 11 additions & 11 deletions1-js/05-data-types/04-array/2-create-array/task.md
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
12 changes: 6 additions & 6 deletions1-js/05-data-types/04-array/3-call-array-this/solution.md
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,15 +1,15 @@ | ||
Volání `pole[2]()`je syntakticky stará dobrá`obj[metoda]()`,v roli`obj`máme `pole` a v roli `metoda` máme `2`. | ||
Zavolali jsme tedy funkci `pole[2]`jako objektovou metodu. Ta přirozeně obdrží `this`odkazující se na objekt `pole` a vypíše toto pole: | ||
```js run | ||
letpole = ["a", "b"]; | ||
pole.push(function() { | ||
alert( this ); | ||
}) | ||
pole[2](); // a,b,function(){...} | ||
``` | ||
Toto pole má 3hodnoty: na začátku mělo dvě, plusfunkce. |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.