HW7.24. Return nth integer from CSV string The function below takes two parameters: a string parameter: CSV_string and an integer index. This string parameter will hold a comma-separated collection of integers: '111,22,3333,4'. Complete the function to return the indexth value (counting from zero) from the comma-separated values as an integer. For example, your code should return 3333 (not '3333') if the example string is provided with an index value of 2. Hint: you should consider using the split() method and the int() function.

Respuesta :

Answer:

The solution code is written in Python 3.

  1. def processCSV(CSV_string, index):
  2.    data = CSV_string.split(",")
  3.    result = data[index]
  4.    return int(result)
  5. processCSV('111,22,3333,4', 2)

Explanation:

Firstly, create function and name it as processCSV() with two parameters, CSV_string and index (Line 1).

Since the CSV_string is a collection of integers separated by comma, we can make use of Python String built-in method split() to convert the CSV_string into a list of numbers by using "," as separator (Line 2).

Next, we can use the index to extract the target value from the list (Line 3).

Since the value in the list is still a string and therefore we shall use Python built-in method int() to convert the target value from string type to integer and return it as output of the function (Line 4)

We can test our function by using the sample test case (Line 6) and we shall see the expected output: 3333.

fichoh

The program which returns the nth term of a comma seperated value written in python 3 is given below :

def ret_ind(csv_string, index):

#initialize a function called ret_ind which takes in two parmaters ; a csv data and an integer index

csv_data = csv_string.split(',')

#since each Value is seperated by a comma, we can split using the string split method to make each value distinct.

nth_index = csv_data[index]

#using list slicing, we subset the required value using the passed in index parameter

return int(nth_index)

#return the nth index and convert to an integer

# A sample run of the program and the output is attached below.

print(ret_ind('111,22,3333,4', 3))

Learn more :https://brainly.com/question/14276852

Ver imagen fichoh