4: 1D Arrays

4.1 Calculate the sum

Coding Exercise: Calculate the sum
Given a sequence of natural numbers terminated by -1 (minus one). Calculate and output the sum of sequence members. Use lists (arrays) to store the sequence.
You may enter input for the program in the box below.

4.2 Find the value

Coding Exercise: Find the value
This task corresponds worked example 11.11 (page 145).
Take a number as an input. Take the sequence consisting of 5 numbers as an input. Search this number in the given sequence and output either "Value not found" or "Value found at location Index". If the number is encountered in the sequence several times, output the smallest index. Use lists (arrays) to solve this task.
You may enter input for the program in the box below.

4.3 Reverse the sequence

Coding Exercise: Reverse the sequence
Given a sequence of natural numbers terminated by -1. Write a program to output the reverse sequence.
For example, if the given data were: 1 8 12 64 -1 then the output should be: 64 12 8 1.
To reverse the stored sequence you can use reverse function (read more: Lists in Python). Search for list.reverse().

4.4 Most frequent number

Coding Exercise: Most frequent number
Given the length of the sequence n. Given the sequence containing n integers from 0 to 10. Write a program to find the number that was repeated in the given sequence most frequently.
Output the most frequent number and the number of its repetitions. If there are several such numbers, output the smaller one.
For example, if n = 7 and the sequence is the following: 5 10 6 8 10 4 6, then the output should be 6 2, because two sequence members (10 and 6) are repeated twice in the sequence. Out of these two numbers 6 is smaller than 10.

4.5 Cut the Sequence

Coding Exercise: Cut the Sequence
Given the length of the sequence n (n is even) and the sequence itself (n integer numbers).
Is it possible to find a number, such that exactly half of sequence members are larger than the number and the other half of the sequence members is smaller?
If so, output such smallest possible number. If such number does not exist, output 'No'.
For example, if n = 6 and the sequence is 8 7 4 8 2 0, then the output should be 5. If if n = 6 and the sequence is 5 7 5 8 2 0, the output should be 'No'.
You may enter input for the program in the box below.

4.6 Binary Search

Coding Exercise: Binary Search
Given the length of the sequence n and the sequence itself (n integer numbers) and an integer number m. The given sequence is sorted in increasing order.
Write a program to test if the number m is among the elements of the sequence. If so, output its index, if not - output 'No'. If number m is met in the sequence several times, output the smallest index.
For example, if n = 7, the given sequence is 1 3 5 9 12 14 15 and m = 14, then the output should be 6 (because 14 is the 6'th member of the given sequence).
Solve the problem by using the binary search. You can read more about it: Binary search
You may enter input for the program in the box below.

4.7 Bubble sort

Coding Exercise: Bubble sort
Given a sequence of non-negative integers that terminates with -1 (minus one).
Implement Bubble-sort algorithm (look at worked example 11.12, pages 146-149) to sort the given sequence.
Output the sorted sequence.
You may enter input for the program in the box below.