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, orsorted. - Output format must match the samples exactly.
Problems
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
First line: one integer n.Second line: n integers.Constraint: 1 ≤ n ≤ 100Output format
One line containing the sum of all elements.Sample 1
Input
510 20 30 40 50Output
150Sample 2
Input
43 -1 8 0Output
10Sample 3
Input
142Output
42Required structure
def sum_array(a): # TODO: return the sum of all elements passn = int(input())a = list(map(int, input().split()))print(sum_array(a))total = 0. Loop over every valid index and add a[i].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
First line: one integer n.Second line: n integers.Third line: one integer target.Constraint: 1 ≤ n ≤ 100Output format
One line containing the count.Sample 1
Input
580 91 76 88 9585Output
3Sample 2
Input
41 2 3 44Output
0Sample 3
Input
6-3 0 7 7 9 26Output
3Required structure
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))a[i] > target.You are given n integers. Find the maximum value and the first index where it appears. Indexes start from 0.
Input format
First line: one integer n.Second line: n integers.Constraint: 1 ≤ n ≤ 100Output format
Print one line:max=VALUE index=INDEXSample 1
Input
580 91 76 91 88Output
max=91 index=1Sample 2
Input
4-5 -2 -9 -2Output
max=-2 index=1Sample 3
Input
142Output
max=42 index=0Required structure
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}")You are given n integers. Replace every negative number with 0, then print the modified list.
Input format
First line: one integer n.Second line: n integers.Constraint: 1 ≤ n ≤ 100Output format
One line containing the modified values, separated by single spaces.Sample 1
Input
6-3 0 7 -1 9 -5Output
0 0 7 0 9 0Sample 2
Input
41 2 3 4Output
1 2 3 4Sample 3
Input
3-1 -2 -3Output
0 0 0Required structure
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)a[i] < 0, assign a[i] = 0.You are given n integers. Determine whether the list is sorted in non-decreasing order. Equal neighboring values are allowed.
Input format
First line: one integer n.Second line: n integers.Constraint: 1 ≤ n ≤ 100Output format
Print YES if the values are sorted in non-decreasing order. Otherwise print NO.Sample 1
Input
51 2 2 4 9Output
YESSample 2
Input
43 1 2 4Output
NOSample 3
Input
142Output
YESSample 4
Input
4-5 -5 -2 0Output
YESRequired structure
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")a[i - 1] and a[i]. If the previous value is greater, the array is not sorted.