145k views
0 votes
Complete the getLastValue() function to take the following three int values: 1. a starting number 2. a ending number 3. an increment size Return the last value in the series that starts with the first value, continues up to, but not including the second value, by incrementing by the third value. If the relationship between the starting and ending values does not make sense given the increment size, then return the built-in constant) None. Use a while loop. Do not use any for loops nor calls to range().

Example 1: getLastValue( 10, 22, 3 ) returns 19

User Cathal
by
8.7k points

1 Answer

2 votes

Final answer:

To find the last value in a series using a starting number, ending number, and increment size, we can use a while loop. The getLastValue() function generates a series of numbers beginning from a start number, incrementing by a given size, and stopping before it reaches an end number. It uses a while loop and should return None if the increment does not allow a sequence to be created.

Step-by-step explanation:

To solve this problem, we can use a while loop. We will start with the starting number and continuously increment it by the given increment size until it becomes greater than or equal to the ending number. Using the given example, if the starting number is 10, ending number is 22, and increment size is 3:

  1. Initialize a variable 'last _value' as None.
  2. Initialize a variable 'current _value' with the starting number (10 in this example).
  3. While 'current _value' is less than the ending number (22 in this example):
  4. Return 'last _value'.

Applying this approach, the last value in the series that starts with 10, continues up to, but not including 22, and increments by 3 is 19.

The get Last Value() function generates a series of numbers beginning from a start number, incrementing by a given size, and stopping before it reaches an end number. It uses a while loop and should return None if the increment does not allow a sequence to be created.

To complete the get Last Value() function, you will need to use a while loop to generate a series of numbers. The function starts with a given number and increments by a specified size until it approaches but does not include the ending number. If the series of numbers cannot be properly generated given the increment size, the function should return None. Below is an example implementation in Python:

def get Last Value(start _num, end _num, increment):
if (increment <= 0) or (start _num >= end _num and increment > 0) or (start _num <= end _num and increment < 0):
return None
last _value = start _num
while (last _value + increment) < end _num:
last _value += increment
return last _value

For example, get Last Value(10, 22, 3) returns 19 because it starts the sequence with 10, increments by 3 each step (13, 16, 19), and stops before reaching 22.

User WindChaser
by
8.1k points