Professional Documents
Culture Documents
Lec3:
List Exercises:
1- Sum all the items in a list using function
def sum_list(items):
sum_numbers = 0
for x in items:
sum_numbers += x
return sum_numbers
print(sum_list([1,2,-8]))
Data Structure Lab Ahmed Eskander Mezher
string length is 2 or more and the first and last character are same
def match_words(words):
ctr = 0
ctr += 1
return ctr
a = [10,20,30,20,10,50,60,40,80,50,40]
uniq_items = []
for x in a:
if x not in dup_items:
uniq_items.append(x)
dup_items.add(x)
print(dup_items)
Data Structure Lab Ahmed Eskander Mezher
Arrays in Python
1- Array creation:
This determines the type of the array during creation. Commonly used type codes are listed
as follows:
Data Structure Lab Ahmed Eskander Mezher
print ("\r")
# using index() to print index of 1st occurrenece of 2
print ("The index of 1st occurrence of 2 is : ", end ="")
print (arr.index(2))
5- Linear Search
# Function call
result = search(arr, n, x)
if(result == -1):
print("Element is not present in array")
else:
print("Element is present at index", result)