Skip to content

Commit c022449

Browse files
authored
Merge pull request #131 from mahdiHashemi14/master
Arrays
2 parents ebc4e51 + e68ea17 commit c022449

File tree

10 files changed

+223
-226
lines changed

10 files changed

+223
-226
lines changed

1-js/05-data-types/04-array/1-item-value/solution.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The result is `4`:
1+
نتیجه `4` است:
22

33

44
```js run
@@ -13,5 +13,5 @@ alert( fruits.length ); // 4
1313
*/!*
1414
```
1515

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

1-js/05-data-types/04-array/1-item-value/task.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ importance: 3
22

33
---
44

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

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

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

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

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
# The slow solution
1+
# راه حل کُند
22

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

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

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

99
```js no-beautify
10-
// Starting from -1:
10+
// -1 شروع از:
1111
-1
1212
-1 + 2
1313
-1 + 2 + 3
1414
-1 + 2 + 3 + (-9)
1515
-1 + 2 + 3 + (-9) + 11
1616

17-
// Starting from 2:
17+
// 2 شروع از:
1818
2
1919
2 + 3
2020
2 + 3 + (-9)
2121
2 + 3 + (-9) + 11
2222

23-
// Starting from 3:
23+
// 3 شروع از:
2424
3
2525
3 + (-9)
2626
3 + (-9) + 11
2727

28-
// Starting from -9
28+
// -9 شروع از:
2929
-9
3030
-9 + 11
3131

32-
// Starting from -11
32+
// -11 شروع از:
3333
-11
3434
```
3535

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

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

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

60-
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.
60+
این راه حل یک پیچیدگی زمانی [O(n<sup>2</sup>)](https://fa.wikipedia.org/wiki/نماد_O_بزرگ) دارد، به عبارتی دیگر، اگر ما اندازه آرایه را دو برابر کنیم، الگوریتم 4 برابر بیشتر زمان می‌برد.
6161

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

64-
# Fast solution
64+
# راه حل سریع
6565

66-
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.
66+
بیایید در آرایه پیش برویم و حاصل جمع کنونی المان‌ها را در متغیر `s` نگه داریم. اگر `s` در جایی منفی شود، سپس `s=0` را مقداردهی می‌کنیم. بیشترین مقدار چنین `s`هایی جواب خواهد بود.
6767

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

7070
```js run
7171
function getMaxSubSum(arr) {
7272
let maxSum = 0;
7373
let partialSum = 0;
7474

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

8181
return maxSum;
@@ -89,6 +89,6 @@ alert( getMaxSubSum([1, 2, 3]) ); // 6
8989
alert( getMaxSubSum([-1, -2, -3]) ); // 0
9090
```
9191

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

94-
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.
94+
شما می‌توانید اطلاعات بیشتری درباره الگوریتم را اینجا پیدا کنید: [مسئله زیرآرایه بیشینه](http://en.wikipedia.org/wiki/Maximum_subarray_problem). اگر هنوز هم برای شما مشخص نیست که چرا کار می‌کند، لطفا الگوریتم را در مثال بالا دنبال کنید، ببینید چگونه کار می‌کند، این کار بهتر از حرفی است.

1-js/05-data-types/04-array/10-maximal-subarray/task.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@ importance: 2
22

33
---
44

5-
# A maximal subarray
5+
# بزرگ‌ترین زیرآرایه
66

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

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

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

13-
For instance:
13+
برای مثال:
1414

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

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

2626
```js
2727
getMaxSubSum([-1, -2, -3]) = 0
2828
```
2929

30-
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.
30+
لطفا سعی کنید یک راه حل سریع پیدا کنید: [O(n<sup>2</sup>)](https://fa.wikipedia.org/wiki/نماد_O_بزرگ) یا حتی O(n) اگر می‌توانید.

1-js/05-data-types/04-array/2-create-array/task.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ importance: 5
22

33
---
44

5-
# Array operations.
5+
# عملیات‌های آرایه.
66

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

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

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

1717
```js no-beautify
1818
Jazz, Blues
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
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`.
1+
صدازدن `arr[2]()` از لحاظ سینتکس همان `obj[method]()` قدیم است، ما `arr` را در نقش `obj` داریم و `2` در نقش `method`.
22

3-
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:
3+
پس صدازدن تابع `arr[2]` مانند متد یک شیء است. به طور طبیعی، این تابع `this` را درحالی که به شیء `arr` رجوع می‌کند می‌گیرد و آرایه را نمایش می‌دهد:
44

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

1212
arr[2](); // a,b,function(){...}
1313
```
14-
15-
The array has 3 values: initially it had two, plus the function.
14+
آرایه 3 مقدار دارد: در ابتدا 2 مقدار داشت سپس تابع اضافه شد.

1-js/05-data-types/04-array/3-call-array-this/task.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 5
22

33
---
44

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

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

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

1616
arr[2](); // ?
1717
```
18-

1-js/05-data-types/04-array/5-array-input-sum/solution.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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.
1+
لطفا جزئیات کوچک اما مهم راه حل را در نظر داشته باشید. ما `value` را درست بعد از `prompt` به عدد تبدیل نمی‌کنیم، چون بعد از `value = +value` ما نمی‌توانیم بین یک رشته خالی (نشان‌دهنده توقف) و صفر (مقداری معتبر) فرقی قائل شویم. در عوض آن را بعدا انجام می‌دهیم.
22

33

44
```js run demo
@@ -8,9 +8,9 @@ function sumInput() {
88

99
while (true) {
1010

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

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

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

2626
alert( sumInput() );
2727
```
28-

1-js/05-data-types/04-array/5-array-input-sum/task.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ importance: 4
22

33
---
44

5-
# Sum input numbers
5+
# اعداد ورودی را جمع بزنید
66

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

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

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

15-
[demo]
15+
[دمو]

0 commit comments

Comments
 (0)