Skip to content

Arrays #131

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

Merged
merged 23 commits into from
Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 1-js/05-data-types/04-array/1-item-value/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The result is `4`:
نتیجه `4` است:


```js run
Expand All @@ -13,5 +13,5 @@ alert( fruits.length ); // 4
*/!*
```

That's because arrays are objects. So both `shoppingCart` and `fruits` are the references to the same array.
به این دلیل که آرایه‌ها شیء هستند. پس هر دوی `shoppingCart` و `fruits` به یک آرایه رجوع می‌کنند.

8 changes: 4 additions & 4 deletions 1-js/05-data-types/04-array/1-item-value/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ importance: 3

---

# Is array copied?
# آیا آرایه کپی شده است؟

What is this code going to show?
این کد چه چیزی نشان خواهد داد؟

```js
let fruits = ["Apples", "Pear", "Orange"];

// push a new value into the "copy"
// یک مقدار جدید را به «کپی» اضافه کردیم
let shoppingCart = fruits;
shoppingCart.push("Banana");

// what's in fruits?
// چه چیزی وجود دارد؟ fruits در آرایه
alert( fruits.length ); // ?
```

44 changes: 22 additions & 22 deletions 1-js/05-data-types/04-array/10-maximal-subarray/solution.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
# The slow solution
# راه حل کُند

We can calculate all possible subsums.
ما می‌توانیم تمام جمع‌های ممکن را حساب کنیم.

The simplest way is to take every element and calculate sums of all subarrays starting from it.
ساده‌ترین راه حل این است که تمام المان‌ها را دریافت کنیم و از آن المان به بعد، حاصل جمع تمامی زیرآرایه‌ها را حساب کنیم.

For instance, for `[-1, 2, 3, -9, 11]`:
برای مثال، برای `[11 ,9- ,3 ,2 ,1-]`:

```js no-beautify
// Starting from -1:
// -1 شروع از:
-1
-1 + 2
-1 + 2 + 3
-1 + 2 + 3 + (-9)
-1 + 2 + 3 + (-9) + 11

// Starting from 2:
// 2 شروع از:
2
2 + 3
2 + 3 + (-9)
2 + 3 + (-9) + 11

// Starting from 3:
// 3 شروع از:
3
3 + (-9)
3 + (-9) + 11

// Starting from -9
// -9 شروع از:
-9
-9 + 11

// Starting from -11
// -11 شروع از:
-11
```

The code is actually a nested loop: the external loop over array elements, and the internal counts subsums starting with the current element.
در واقع کد یک حلقه تو در تو است: حلقه بیرونی در المان‌های آرایه حلقه می‌زند، و حلقه داخلی تمام جمع‌ها را حساب می‌کند که از المان کنونی شروع می‌شوند.

```js run
function getMaxSubSum(arr) {
let maxSum = 0; // if we take no elements, zero will be returned
let maxSum = 0; // اگر هیچ المانی نگیریم، صفر برگردانده می‌شود

for (let i = 0; i < arr.length; i++) {
let sumFixedStart = 0;
Expand All @@ -57,25 +57,25 @@ alert( getMaxSubSum([1, 2, 3]) ); // 6
alert( getMaxSubSum([100, -9, 2, -3, 5]) ); // 100
```

The solution has a time complexity of [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation). In other words, if we increase the array size 2 times, the algorithm will work 4 times longer.
این راه حل یک پیچیدگی زمانی [O(n<sup>2</sup>)](https://fa.wikipedia.org/wiki/نماد_O_بزرگ) دارد، به عبارتی دیگر، اگر ما اندازه آرایه را دو برابر کنیم، الگوریتم 4 برابر بیشتر زمان می‌برد.

For big arrays (1000, 10000 or more items) such algorithms can lead to a serious sluggishness.
برای آرایه‌های بزرگ (1000، 10000 یا المان‌های بیشتر) چنین الگوریتمی می‌تواند باعث سستی جدی شود.

# Fast solution
# راه حل سریع

Let's walk the array and keep the current partial sum of elements in the variable `s`. If `s` becomes negative at some point, then assign `s=0`. The maximum of all such `s` will be the answer.
بیایید در آرایه پیش برویم و حاصل جمع کنونی المان‌ها را در متغیر `s` نگه داریم. اگر `s` در جایی منفی شود، سپس `s=0` را مقداردهی می‌کنیم. بیشترین مقدار چنین `s`هایی جواب خواهد بود.

If the description is too vague, please see the code, it's short enough:
اگر توضیحات خیلی مبهم است، لطفا به کد نگاه کنید، به اندازه کافی کوتاه است:

```js run
function getMaxSubSum(arr) {
let maxSum = 0;
let partialSum = 0;

for (let item of arr) { // for each item of arr
partialSum += item; // add it to partialSum
maxSum = Math.max(maxSum, partialSum); // remember the maximum
if (partialSum < 0) partialSum = 0; // zero if negative
for (let item of arr) { // برای هر المان آرایه
partialSum += item; // اضافه کن partialSum آن را به
maxSum = Math.max(maxSum, partialSum); // بیشترین مقدار را یه یاد بسپر
if (partialSum < 0) partialSum = 0; // اگر منفی بود برابر با منفی شود
}

return maxSum;
Expand All @@ -89,6 +89,6 @@ alert( getMaxSubSum([1, 2, 3]) ); // 6
alert( getMaxSubSum([-1, -2, -3]) ); // 0
```

The algorithm requires exactly 1 array pass, so the time complexity is O(n).
الگوریتم دقیقا به 1 آرایه نیاز دارد، پس پیچیدگی زمان O(n) است.

You can find more detail information about the algorithm here: [Maximum subarray problem](http://en.wikipedia.org/wiki/Maximum_subarray_problem). If it's still not obvious why that works, then please trace the algorithm on the examples above, see how it works, that's better than any words.
شما می‌توانید اطلاعات بیشتری درباره الگوریتم را اینجا پیدا کنید: [مسئله زیرآرایه بیشینه](http://en.wikipedia.org/wiki/Maximum_subarray_problem). اگر هنوز هم برای شما مشخص نیست که چرا کار می‌کند، لطفا الگوریتم را در مثال بالا دنبال کنید، ببینید چگونه کار می‌کند، این کار بهتر از حرفی است.
18 changes: 9 additions & 9 deletions 1-js/05-data-types/04-array/10-maximal-subarray/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ importance: 2

---

# A maximal subarray
# بزرگ‌ترین زیرآرایه

The input is an array of numbers, e.g. `arr = [1, -2, 3, 4, -9, 6]`.
ورودی یک آرایه از اعداد است، برای مثال `arr = [1, -2, 3, 4, -9, 6]`.

The task is: find the contiguous subarray of `arr` with the maximal sum of items.
کاری که باید انجام شود: زیرآرایه متوالی از `arr` را پیدا کنید که بیشترین مقدار جمع المان‌ها را دارد.

Write the function `getMaxSubSum(arr)` that will return that sum.
تابع `getMaxSubSum(arr)` را بنویسید که مقدار جمع را برگرداند.

For instance:
برای مثال:

```js
getMaxSubSum([-1, *!*2, 3*/!*, -9]) == 5 (the sum of highlighted items)
getMaxSubSum([-1, *!*2, 3*/!*, -9]) == 5 (جمع المان‌های برجسته)
getMaxSubSum([*!*2, -1, 2, 3*/!*, -9]) == 6
getMaxSubSum([-1, 2, 3, -9, *!*11*/!*]) == 11
getMaxSubSum([-2, -1, *!*1, 2*/!*]) == 3
getMaxSubSum([*!*100*/!*, -9, 2, -3, 5]) == 100
getMaxSubSum([*!*1, 2, 3*/!*]) == 6 (take all)
getMaxSubSum([*!*1, 2, 3*/!*]) == 6 (همه را انتخاب می‌کنیم)
```

If all items are negative, it means that we take none (the subarray is empty), so the sum is zero:
اگر تمام المان‌ها منفی باشند، به این معنی است که هیچ کدام را انتخاب نمی‌کنیم (زیرآرایه خالی است)، پس جمع برابر با صفر است:

```js
getMaxSubSum([-1, -2, -3]) = 0
```

Please try to think of a fast solution: [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation) or even O(n) if you can.
لطفا سعی کنید یک راه حل سریع پیدا کنید: [O(n<sup>2</sup>)](https://fa.wikipedia.org/wiki/نماد_O_بزرگ) یا حتی O(n) اگر می‌توانید.
16 changes: 8 additions & 8 deletions 1-js/05-data-types/04-array/2-create-array/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 5

---

# Array operations.
# عملیات‌های آرایه.

Let's try 5 array operations.
بیایید 5 عملیات آرایه را امتحان کنیم.

1. Create an array `styles` with items "Jazz" and "Blues".
2. Append "Rock-n-Roll" to the end.
3. Replace the value in the middle by "Classics". Your code for finding the middle value should work for any arrays with odd length.
4. Strip off the first value of the array and show it.
5. Prepend `Rap` and `Reggae` to the array.
1. یک آرایه `styles` با المان‌های "Jazz" و "Blues" ایجاد کنید.
2. "Rock-n-Roll" را به انتهای آن اضافه کنید.
3. مقدار وسطی را با "Classics" جایگزین کنید. کد شما برای پیدا کردن مقدار وسط باید برای هر آرایه‌ای با طول فرد کار کند.
4. مقدار اول آرایه را بیرون بیاورید و آن را نمایش دهید.
5. دو مقدار `Rap` و `Reggae` را به اول آرایه اضافه کنید.

The array in the process:
آرایه در حین فرایند اینگونه خواهد بود:

```js no-beautify
Jazz, Blues
Expand Down
7 changes: 3 additions & 4 deletions 1-js/05-data-types/04-array/3-call-array-this/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The call `arr[2]()` is syntactically the good old `obj[method]()`, in the role of `obj` we have `arr`, and in the role of `method` we have `2`.
صدازدن `arr[2]()` از لحاظ سینتکس همان `obj[method]()` قدیم است، ما `arr` را در نقش `obj` داریم و `2` در نقش `method`.

So we have a call of the function `arr[2]` as an object method. Naturally, it receives `this` referencing the object `arr` and outputs the array:
پس صدازدن تابع `arr[2]` مانند متد یک شیء است. به طور طبیعی، این تابع `this` را درحالی که به شیء `arr` رجوع می‌کند می‌گیرد و آرایه را نمایش می‌دهد:

```js run
let arr = ["a", "b"];
Expand All @@ -11,5 +11,4 @@ arr.push(function() {

arr[2](); // a,b,function(){...}
```

The array has 3 values: initially it had two, plus the function.
آرایه 3 مقدار دارد: در ابتدا 2 مقدار داشت سپس تابع اضافه شد.
5 changes: 2 additions & 3 deletions 1-js/05-data-types/04-array/3-call-array-this/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# Calling in an array context
# فراخوانی محتوای یک آرایه

What is the result? Why?
نتیجه چه خواهد بود؟ چرا؟

```js
let arr = ["a", "b"];
Expand All @@ -15,4 +15,3 @@ arr.push(function() {

arr[2](); // ?
```

7 changes: 3 additions & 4 deletions 1-js/05-data-types/04-array/5-array-input-sum/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Please note the subtle, but important detail of the solution. We don't convert `value` to number instantly after `prompt`, because after `value = +value` we would not be able to tell an empty string (stop sign) from the zero (valid number). We do it later instead.
لطفا جزئیات کوچک اما مهم راه حل را در نظر داشته باشید. ما `value` را درست بعد از `prompt` به عدد تبدیل نمی‌کنیم، چون بعد از `value = +value` ما نمی‌توانیم بین یک رشته خالی (نشان‌دهنده توقف) و صفر (مقداری معتبر) فرقی قائل شویم. در عوض آن را بعدا انجام می‌دهیم.


```js run demo
Expand All @@ -8,9 +8,9 @@ function sumInput() {

while (true) {

let value = prompt("A number please?", 0);
let value = prompt("لطفا یک عدد وارد کنید.", 0);

// should we cancel?
// باید لغو کنیم؟
if (value === "" || value === null || !isFinite(value)) break;

numbers.push(+value);
Expand All @@ -25,4 +25,3 @@ function sumInput() {

alert( sumInput() );
```

14 changes: 7 additions & 7 deletions 1-js/05-data-types/04-array/5-array-input-sum/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ importance: 4

---

# Sum input numbers
# اعداد ورودی را جمع بزنید

Write the function `sumInput()` that:
تابع `sumInput()` را بنویسید که:

- Asks the user for values using `prompt` and stores the values in the array.
- Finishes asking when the user enters a non-numeric value, an empty string, or presses "Cancel".
- Calculates and returns the sum of array items.
- از کاربر با استفاده از `prompt` درخواست مقدار می‌کند و مقدارها را در آرایه ذخیره می‌کند.
- زمانی که کاربر یک مقدار غیر عددی یا رشته خالی وارد کند یا "Cancel" را فشار دهد، درخواست کردن را متوقف می‌کند.
- جمع المان‌های آرایه یا محاسبه و آن را برگردانید.

P.S. A zero `0` is a valid number, please don't stop the input on zero.
پی‌نوشت: صفر `0` یک مقدار معتبر است، لطفا درخواست ورودی را با صفر متوقف نکنید.

[demo]
[دمو]
Loading