homework.wenqian.dev
← Back to index
Homework 12026-06-22

Array Practice

Python lists · Use indexes · Write list functions · Print exact output

Requirements

  • 5 required problems. Estimated time: 2 – 3 hours.
  • Only submit a Python version (.py) for each problem.
  • These are array/list problems. Read the input values into a Python list before calling the required function.
  • Use loops yourself. In Python, do not use sum, max, min, count, index, or sorted.
  • Output format must match the samples exactly.

Problems

1Array Sum

You are given n integers. Store them in a Python list, then write a function sum_array that returns the sum of all elements.

Input format

text
First line: one integer n.Second line: n integers.Constraint: 1 ≤ n ≤ 100

Output format

text
One line containing the sum of all elements.

Sample 1

Input

text
510 20 30 40 50

Output

text
150

Sample 2

Input

text
43 -1 8 0

Output

text
10

Sample 3

Input

text
142

Output

text
42

Required structure

array_sum.py
def sum_array(a):    # TODO: return the sum of all elements    passn = int(input())a = list(map(int, input().split()))print(sum_array(a))
Hint: Start with total = 0. Loop over every valid index and add a[i].
2Count Greater Than Target

You are given n integers and a target value. Store the integers in a Python list, then count how many elements are greater than the target.

Input format

text
First line: one integer n.Second line: n integers.Third line: one integer target.Constraint: 1 ≤ n ≤ 100

Output format

text
One line containing the count.

Sample 1

Input

text
580 91 76 88 9585

Output

text
3

Sample 2

Input

text
41 2 3 44

Output

text
0

Sample 3

Input

text
6-3 0 7 7 9 26

Output

text
3

Required structure

count_greater.py
def count_greater(a, target):    # TODO: count elements greater than target    passn = int(input())a = list(map(int, input().split()))target = int(input())print(count_greater(a, target))
Hint: This is a counting loop. Increase the counter only when a[i] > target.
3First Maximum Index

You are given n integers. Find the maximum value and the first index where it appears. Indexes start from 0.

Input format

text
First line: one integer n.Second line: n integers.Constraint: 1 ≤ n ≤ 100

Output format

text
Print one line:max=VALUE index=INDEX

Sample 1

Input

text
580 91 76 91 88

Output

text
max=91 index=1

Sample 2

Input

text
4-5 -2 -9 -2

Output

text
max=-2 index=1

Sample 3

Input

text
142

Output

text
max=42 index=0

Required structure

max_index.py
def index_of_first_max(a):    # TODO: return the first index of the maximum value    passn = int(input())a = list(map(int, input().split()))index = index_of_first_max(a)print(f"max={a[index]} index={index}")
Hint: Start with index 0 as the best index. Only update it when you find a strictly larger value.
4Replace Negatives with Zero

You are given n integers. Replace every negative number with 0, then print the modified list.

Input format

text
First line: one integer n.Second line: n integers.Constraint: 1 ≤ n ≤ 100

Output format

text
One line containing the modified values, separated by single spaces.

Sample 1

Input

text
6-3 0 7 -1 9 -5

Output

text
0 0 7 0 9 0

Sample 2

Input

text
41 2 3 4

Output

text
1 2 3 4

Sample 3

Input

text
3-1 -2 -3

Output

text
0 0 0

Required structure

replace_negative.py
def replace_negative(a):    # TODO: change every negative element to 0    passdef print_array(a):    # TODO: print all elements on one line    passn = int(input())a = list(map(int, input().split()))replace_negative(a)print_array(a)
Hint: This function changes the list in place. If a[i] < 0, assign a[i] = 0.
5Is Sorted

You are given n integers. Determine whether the list is sorted in non-decreasing order. Equal neighboring values are allowed.

Input format

text
First line: one integer n.Second line: n integers.Constraint: 1 ≤ n ≤ 100

Output format

text
Print YES if the values are sorted in non-decreasing order. Otherwise print NO.

Sample 1

Input

text
51 2 2 4 9

Output

text
YES

Sample 2

Input

text
43 1 2 4

Output

text
NO

Sample 3

Input

text
142

Output

text
YES

Sample 4

Input

text
4-5 -5 -2 0

Output

text
YES

Required structure

is_sorted.py
def is_sorted(a):    # TODO: return True if sorted, False otherwise    passn = int(input())a = list(map(int, input().split()))print("YES" if is_sorted(a) else "NO")
Hint: Compare neighboring values: a[i - 1] and a[i]. If the previous value is greater, the array is not sorted.
Check: For every problem, read the values into a Python list first. Still write the loop yourself even though list helpers exist.
— Homework 1 End —