Skip to content

array translation #33

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 1 commit into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions 1-js/05-data-types/04-array/1-item-value/solution.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
The result is `4`:
`4`:الناتج


```js run
let fruits = ["Apples", "Pear", "Orange"];
let fruits = ["البرتقال", "الكمثري", "التفاح"];

let shoppingCart = fruits;

shoppingCart.push("Banana");
shoppingCart.push("الموز");

*!*
alert( fruits.length ); // 4
*/!*
```

That's because arrays are objects. So both `shoppingCart` and `fruits` are the references to the same array.
هذا لان المصفوفات تعتبر كائنات. لذا كلا من `shoppingCart` و `fruits` يعدوا كمرجع لنفس المصفوفه.

14 changes: 7 additions & 7 deletions 1-js/05-data-types/04-array/1-item-value/task.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
importance: 3
الاهميه: 3

---

# Is array copied?
# هل تم نسخ المصفوفه؟

What is this code going to show?
ماالذي سوف يعرضه الكود؟

```js
let fruits = ["Apples", "Pear", "Orange"];
let fruits = ["البرتقال", "الكمثري", "التفاح"];

// push a new value into the "copy"
// "ادفع قيمه جديده داخل"النسخ
let shoppingCart = fruits;
shoppingCart.push("Banana");
shoppingCart.push("الموز");

// what's in fruits?
//؟ fruits ماالذي داخل
alert( fruits.length ); // ?
```

45 changes: 22 additions & 23 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 @@
# 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]`:
علي سبيل المثال, for `[-1, 2, 3, -9, 11]`:

```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,24 @@ alert( getMaxSubSum([1, 2, 3]) ); // 6
alert( getMaxSubSum([100, -9, 2, -3, 5]) ); // 100
```

The solution has a time complexety 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://en.wikipedia.org/wiki/Big_O_notation). بمعنى آخر ، إذا قمنا بزيادة حجم المصفوفه مرتين ، فستعمل الخوارزمية لفترة أطول 4 مرات.
بالنسبة للمصفوفات الكبيرة (1000 أو 10000 أو أكثر من العناصر) ، يمكن أن تؤدي هذه الخوارزميات إلى بطء خطير.

For big arrays (1000, 10000 or more items) such algorithms can lead to a serious sluggishness.
# الحل الأسرع

# Fast solution
دعنا نسير في المصفوفة ونحتفظ بالمجموع الجزئي الحالي للعناصر في المتغير `s`. إذا أصبحت `s` سالبة في وقت ما ، قم بتعيين` s = 0`. سيكون الحد الأقصى لكل هذه الإجابات هو الإجابة.

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.

If the description is too vague, please see the code, it's short enough:
إذا كان الوصف غامضًا جدًا ، فيرجى الاطلاع على الكود ، فهو قصير بما يكفي:

```js run demo
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; // أضفه إلى مجموع الجزئي
maxSum = Math.max(maxSum, partialSum); // تذكر الحد الأقصى
if (partialSum < 0) partialSum = 0; // صفر إذا كانت سلبية
}

return maxSum;
Expand All @@ -89,6 +88,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).
تتطلب الخوارزمية تمريراً مصفوفه واحده ، لذا فإن تعقيد الوقت هو 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.
يمكنك العثور على مزيد من المعلومات التفصيلية حول الخوارزمية هنا: [Maximum subarray problem](http://en.wikipedia.org/wiki/Maximum_subarray_problem). إذا كان لا يزال من غير الواضح سبب نجاح ذلك ، فالرجاء تتبع الخوارزمية في الأمثلة أعلاه ، ومعرفة كيفية عملها ، وهذا أفضل من أي كلمات.
20 changes: 10 additions & 10 deletions 1-js/05-data-types/04-array/10-maximal-subarray/task.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
importance: 2
الأهميه: 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://en.wikipedia.org/wiki/Big_O_notation) أو حتى O (n) إذا استطعت.
8 changes: 4 additions & 4 deletions 1-js/05-data-types/04-array/2-create-array/solution.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@


```js run
let styles = ["Jazz", "Blues"];
styles.push("Rock-n-Roll");
styles[Math.floor((styles.length - 1) / 2)] = "Classics";
let styles = ["البلوز", "موسيقي الجاز"];
styles.push("موسيقى الروك آند رول");
styles[Math.floor((styles.length - 1) / 2)] = "كلاسيكيات";
alert( styles.shift() );
styles.unshift("Rap", "Reggae");
styles.unshift("موسيقي الريغي", "موسيقى الراب");
```

29 changes: 14 additions & 15 deletions 1-js/05-data-types/04-array/2-create-array/task.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
importance: 5
الاهميه: 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.

The array in the process:
1. قم بانشاء عده عناصر `styles` "موسيقي الجاز" و "البلوز" .
2. اضف في النهايه" موسيقي الروك آند رول"
3. استبدل القيمة في المنتصف بـ "كلاسيكيات". يجب ان يعمل الكود الخاص بك للعثور على القيمة الوسطى لأي مصفوفه ذات طول فردي.
4. تجريد القيمة الأولى من مصفوفه وإظهارها.
5. استعد `Rap` و `Reggae` الي المصفوفه .

المصفوفه في العمليه :
```js no-beautify
Jazz, Blues
Jazz, Blues, Rock-n-Roll
Jazz, Classics, Rock-n-Roll
Classics, Rock-n-Roll
Rap, Reggae, Classics, Rock-n-Roll
موسيقي الجاز, البلوز
موسيقي الجاز, البلوز, موسيقى الروك آند رول
موسيقي الجاز, كلاسيكيات, موسيقى الروك آند رول
كلاسيكيات,موسيقى الروك آند رول
موسيقى الراب, موسيقي الريغي, كلاسيكيات, موسيقى الروك آند رول
```

6 changes: 3 additions & 3 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]`, في دور `obj` نحن لدينا `arr`, وفي دور `method`نحن لدينا `2`.

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 @@ -12,4 +12,4 @@ arr.push(function() {
arr[2](); // a,b,function(){...}
```

The array has 3 values: initially it had two, plus the function.
المصفوفة لها 3 قيم: في البداية كانت تحتوي على قيمتين ، بالإضافة إلى function.
6 changes: 3 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
@@ -1,10 +1,10 @@
importance: 5
الاهميه: 5

---

# Calling in an array context
# استدعاء في سياق مصفوفه

What is the result? Why?
ماهي النتيجه؟ لماذا؟

```js
let arr = ["a", "b"];
Expand Down
6 changes: 3 additions & 3 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(" رقم من فضلك A Number Please", 0);

// should we cancel?
// يجب أن نلغي؟
if (value === "" || value === null || !isFinite(value)) break;

numbers.push(+value);
Expand Down
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
@@ -1,15 +1,15 @@
importance: 4
الاهميه: 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` وتخزين تلك القيم داخل المصفوفه.
- قم بإنهاء الاسئله عندما يدخل المستخدم قيمه غير رقمي او نص فارغ او بضغط علي "انهاء"
- احسب وقم بإعاده عمليه الجمع لعناصر المصفوفه.

P.S. A zero `0` is a valid number, please don't stop the input on zero.
ملاحظة. الصفر `0` هو رقم صالح ، يرجى عدم إيقاف الإدخال على الصفر.

[demo]
Loading