/python-csv-list-conversion-manipulation

Read String as CSV and do the manipulation using Python

Primary LanguagePythonMIT LicenseMIT

Problem Statement:

Write source code in Python that will do following: There are 2 strings S and C. String S represents a table in CSV format, where rows are separated by newline characters ('\n') and each row consists of one or more fields separated by commas (',').

The first row contains the names of columns and the following row contains the values.

For example, the table below is presented by the following string S: "id,name,age,gender,room,dep.\n1,Joe,54,M,12,8\n17,Betty,29,F,15,6".

+--+------+----+--------+------+------+
| id | name | age | gender | room | dep. |
+--+------+----+--------+------+------+
|   1 |     Joe | 54 |          M |     12 |       8 |
|   7 |   Betty | 29 |          F |     15 |       6 |
+--+------+----+--------+------+------+

String C is the name of the column described by S that contains only integers. The requirement is to find the maximum value in that column. In the example above, for C = "age" , the maximum value is 54.

I have written a function:
class Solution { public int solution(String S, String C); }
which, given 2 strings S and C consisting of N and M characters, respectively, returns the maximum value in column C of the table described by S.

Examples:

  1. Given S = "area,land,\n3722,CN\n6612,RU\n3855,CA\n3797,USA" and C="area"

+-------+-------+
|   area   |   land  |
+-------+-------+
|   3722  |   CN   |
|   6612  |   RU   |
|   3855  |   CA   |
|   3797  | USA   |
+-------+-------+

the function will return 6612

  1. Given S = "city,temp2,temp\nParis,7,-3\nKarachi,4,-4\nLahore,-1,-2" and C="temp"

+--------+---------+--------+
|   city     |   temp2  |   temp  |
+--------+---------+--------+
|   Paris     |     7       |     -3     |
|   Karachi   |     4     |     -4     |
|   Lahore     |   -1       |     -2     |
+--------+---------+---------+

the function with return -2

Keywords:

Python, Source Code, Algorithm, SpreadSheet, CSV Format to Array/List conversion, Find in List, Minium Value, Array Manipulation