6. Lists
To add to a list, sort it, and search it.
Explanation
Section titled “Explanation”- A list holds elements of one type between square brackets, and grows as you add to it:
[70, 95, 82]. اضف(ق, قيمة)(add) appends the value at the end.رتب(ق)(sort) sorts it ascending in place.طول(ق)(length) is the number of its elements.بحث(ق, قيمة)(search) returns the index of the first element equal to the value, orلاشيء(nothing) if it is not found (its type isعدد64؟), so writeبحث(ق, قيمة) والا -1if you always want a number. The index is the element’s position number, starting from 0.- A list is a reference: after
ب = ق,بandقare two names for the same list, and nothing is copied.
Example
Section titled “Example”درجات = [70, 95, 82]اضف(درجات, 60)رتب(درجات)اطبع(درجات, " ", طول(درجات), " ", بحث(درجات, 95))Common mistakes
Section titled “Common mistakes”An index outside the list:
ق = [1, 2, 3]اطبع(ق[3])Exit code: 3
In the error catalog: E0321
ق = [1, 2, 3]اطبع(ق[طول(ق) - 1])Another mistake is thinking that ب = ق copies the list: it does not, because a list is a reference.
Exercise
Section titled “Exercise”In the list [70, 95, 82, 64, 95], find the highest grade with a loop (without رتب), then its first position with بحث.
Show the solution
درجات = [70, 95, 82, 64, 95]اكبر = درجات[0]لكل د في درجات: اذا د > اكبر: اكبر = داطبع("أكبر درجة: ", اكبر)اطبع("موضعها الأول: ", بحث(درجات, اكبر))حرر(درجات)