0% found this document useful (0 votes)
3 views

Lesson 01 - Array - Student

This document provides an introduction to arrays in Python, covering basic and advanced operations such as adding, removing, and searching for elements. It includes example problems and algorithms, such as finding the second largest number in an array and practice problems for further learning. Mastery of arrays is emphasized as essential for solving programming challenges in Olympiad contests.

Uploaded by

Văn Tú Phạm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lesson 01 - Array - Student

This document provides an introduction to arrays in Python, covering basic and advanced operations such as adding, removing, and searching for elements. It includes example problems and algorithms, such as finding the second largest number in an array and practice problems for further learning. Mastery of arrays is emphasized as essential for solving programming challenges in Olympiad contests.

Uploaded by

Văn Tú Phạm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Olympiad Programming 1

Lesson 01: Arrays in Python for Olympiad Programming

Learning Objectives

 Understand the concept of arrays in Python.


 Master basic and advanced array operations.
 Apply arrays to solve typical problems in Olympiad programming contests.

Lesson Content

1. Introduction to Arrays

In Python, arrays can be represented using lists. An array is a data structure that contains elements of the
same type, accessible via indices.

Example:

# Declare an array
arr = [1, 2, 3, 4, 5]

# Access elements of the array


print(arr[0]) # Output: 1
print(arr[3]) # Output: 4

2. Basic Operations on Arrays

a. Adding and Removing Elements

 Add an element to the end of the array: append()


 Insert an element at a specific position: insert()
 Remove an element at a specific position: pop()
 Remove a specific element by value: remove()

Example:

arr = [1, 2, 3, 4, 5]
arr.append(6) # [1, 2, 3, 4, 5, 6]
arr.insert(2, 2.5) # [1, 2, 2.5, 3, 4, 5, 6]
arr.pop(3) # [1, 2, 2.5, 4, 5, 6]
arr.remove(2.5) # [1, 2, 4, 5, 6]

b. Traversing an Array

Traverse an array using a for loop.

Example:

for element in arr:


print(element)

c. Searching in an Array

 Search for an element using in or index()

Example:

Phạm Văn Tú 1|
Olympiad Programming 2

if 3 in arr:
print("3 is in the array")
print(arr.index(3)) # Returns the index of element 3

3. Basic Algorithms Using Arrays

a. Sorting

 Use sort() or sorted()

Example:

arr = [3, 1, 4, 1, 5, 9, 2]
arr.sort() # [1, 1, 2, 3, 4, 5, 9]
sorted_arr = sorted(arr, reverse=True) # [9, 5, 4, 3, 2, 1, 1]

b. Searching

 Linear search

Example:

def linear_search(arr, x):


for i in range(len(arr)):
if arr[i] == x:
return i
return -1

index = linear_search(arr, 4)
print(index) # Returns the index of element 4

4. Example Problems

Problem: Find the Second Largest Number in an Array

Problem Statement: Given an array of integers, find the second largest number in the array.

Solution:

def second_largest(arr):
if len(arr) < 2:
return None

first, second = float('-inf'), float('-inf')


for number in arr:
if number > first:
second = first
first = number
elif first > number > second:
second = number

return second if second != float('-inf') else None

arr = [10, 5, 8, 12, 7, 15, 6]


print(second_largest(arr)) # Output: 12

5. Practice Problems

Phạm Văn Tú 2|
Olympiad Programming 3

1. Write a function to reverse an array without using built-in functions.


2. Write a function to find all elements that appear more than once in an array.
3. Write a function to find the largest and smallest elements in an array by traversing it only once.

Hints:

# Reverse array
def reverse_array(arr):
start, end = 0, len(arr) - 1
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1
return arr

# Find duplicates
def find_duplicates(arr):
count = {}
duplicates = []
for num in arr:
if num in count:
count[num] += 1
else:
count[num] = 1

for key, value in count.items():


if value > 1:
duplicates.append(key)

return duplicates

# Find max and min


def find_max_min(arr):
if not arr:
return None, None

max_val, min_val = arr[0], arr[0]


for num in arr:
if num > max_val:
max_val = num
if num < min_val:
min_val = num

return max_val, min_val

Conclusion

In this lesson, we covered arrays in Python, basic and advanced operations on arrays, and how to use
arrays to solve real-world problems. Mastering arrays and their related operations is a crucial step in
solving programming problems in Olympiad contests.

Phạm Văn Tú 3|

You might also like