Practice Coding Problems

1) Variable 'numbers' has been initialized.  The variable 'numbers' is an array of values.

    Example 1: numbers =  [2.3, 3.5, 4.1, 7, 9, 10]

    Example 2: numbers = [1, 2, 3, 4, 5]

   

Task: Initialize the value of 'sum' that represents the sum of the array.

2) Assume variables 'hoursWorked' and 'payRate' have been initialized.

hours worked represents the number of hours worked. 

payRate represents the amount of pay in dollars per hour.  



Task: Initialize the value of 'totalPay', which represents the amount to be paid to the employee.  However, if the worker works over 40 hours, then all hours over 40 hours are paid as “time and a half”.  This means the rate for these hours is 1.5 times as large as the normal pay rate.


Example 1: if hoursWorked = 30.0 and payRate = 10.50, then totalPay should be 315.

Example 2: If hours worked = 45.0 and payRate = 8.0, then totalPay should be 380.

3) The variables 'choices' and 'correct' have been initialized.   The first array consists of the answers given by a quiz taker and the second array consists of the correct answers (the quiz was all true/false questions).  You can assume the arrays have the same length.  


Task: Appropriately initialize the value of 'grade' which represents the proportion of questions answered correctly.


Example: 

choices = [true, false, true, true, false, false, false, true, false, true];

correct = [false, false, true, true, true, false, true, true, false, false];


grade should be .6 because the person answered 6 out of the 10 questions correctly (60%)

4) The variable 'data' has been initialized.  The variable 'data' is an array sorted from least to greatest.  

Task: Appropriately initialize 'range' that represents the range of the dataset (largest value – smallest value). 

 

Example:

data = [2.0, 4.3, 7.5, 9.1, 12, 15, 17.8];

range should be 15.8

5) The variable 'data' has been initialized.  The variable 'data' is an array (not sorted)

Task: Appropriately initialize 'max' that represents the largest value in the array.

 

Example:

data = [2.0, 4.3, 7.5, 18.3, 12, 15];

max should be 18.3