Question: In Ruby write a function that takes a string which contains data in CSV format and it will return an array of strings in CSV

In Ruby write a function that takes a string which contains data in CSV format and it will return an array of strings in CSV format. The modifications to the fields are complete but I'm struggling to have the elements separated by a space.
Expected return value: ["Gender,FirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At","Male,Carl,Wilderman,carl,yahoo.com,21->40,Seattle,Safari iPhone,2,afternoon", "Male,Marvin,Lind,marvin,hotmail.com,66->99,Detroit,Chrome Android,2,afternoon", "Female,Shanelle,Marquardt,shanelle,hotmail.com,21->40,Las Vegas,Chrome,1,afternoon", "Female,Lavonne,Romaguera,lavonne,yahoo.com,66->99,Seattle,Chrome,2,morning", "Male,Derick,McLaughlin,derick,hotmail.com,41->65,Chicago,Chrome Android,1,afternoon"]
---------------------------------------------------------------
require 'date'
#[1->20]-[21->40]-[41->65]-[66->99]
def age_range(age)
myage =""
case age.to_i
when 1..20
myage ="1->20"
when 21..40
myage ="21->40"
when 41..65
myage ="41->65"
when 66..99
myage ="66->99"
else
myage = "NOAGE"
end
return myage
end
# [morning =>06:00am ->11:59am]-[afternoon =>12:00pm ->5:59pm]-[evening =>6:00pm ->11:59pm]
def time_of_day(datetime)
t = DateTime.parse(datetime,'%Y-%m-%d %H:%M:%S')
th = t.strftime("%H").to_i
if (th >=6 && th <12)
return "morning"
elsif (th >=12 && th <18)
return "afternoon"
elsif (th >=18 && th <00)
return 'evening'
else
return "NONE"
end
end
def my_data_transform(csv_content)
arr = csv_content.split("
")
index =0;
values =[]
while (index < arr.length)
values[index]= arr[index].split(",");
index +=1
end
values.map(&:join)
# puts values
# Gender,FirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At
# Male,Carl,Wilderman,carl,wilderman_carl@yahoo.com,29,Seattle,Safari iPhone,2,2020-03-0616:37:56
# Male,Marvin,Lind,marvin,marvin_lind@hotmail.com,77,Detroit,Chrome Android,2,2020-03-0213:55:51
# Female,Shanelle,Marquardt,shanelle,marquardt.shanelle@hotmail.com,21,Las Vegas,Chrome,1,2020-03-0517:53:05
# Female,Lavonne,Romaguera,lavonne,romaguera.lavonne@yahoo.com,81,Seattle,Chrome,2,2020-03-0410:33:53
# Male,Derick,McLaughlin,derick,mclaughlin.derick@hotmail.com,47,Chicago,Chrome Android,1,2020-03-0515:19:48
# puts arr[0][4]
# puts values[0].join(',')
index =1
while (index < values.length)
values[index][4]= values[index][4].split("@")[1] # remove username from email
values[index][5]= age_range(values[index][5]) # age -> range
values[index][9]= time_of_day(values[index][9]) # morning, afternoon, or evening
index +=1
end
nvals =[]
index =0
while (index < values.length)
nvals[index]= values[index].join(',')
index +=1
end
# puts nvals
return nvals
end
my_data_transform("Gender,FirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At
Male,Carl,Wilderman,carl,wilderman_carl@yahoo.com,29,Seattle,Safari iPhone,2,2020-03-0616:37:56
Male,Marvin,Lind,marvin,marvin_lind@hotmail.com,77,Detroit,Chrome Android,2,2020-03-0213:55:51
Female,Shanelle,Marquardt,shanelle,marquardt.shanelle@hotmail.com,21,Las Vegas,Chrome,1,2020-03-0517:53:05
Female,Lavonne,Romaguera,lavonne,romaguera.lavonne@yahoo.com,81,Seattle,Chrome,2,2020-03-0410:33:53
Male,Derick,McLaughlin,derick,mclaughlin.derick@hotmail.com,47,Chicago,Chrome Android,1,2020-03-0515:19:48
")
# input:
# Gender,FirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At
# Male,Carl,Wilderman,carl,wilderman_carl@yahoo.com,29,Seattle,Safari iPhone,2,2020-03-0616:37:56
# Male,Marvin,Lind,marvin,marvin_lind@hotmail.com,77,Detroit,Chrome Android,2,2020-03-0213:55:51
# Female,Shanelle,Marquardt,shanelle,marquardt.shanelle@hotmail.com,21,Las Vegas,Chrome,1,2020-03-0517:53:05
# Female,Lavonne,Romaguera,lavonne,romaguera.lavonne@yahoo.com,81,Seattle,Chrome,2,2020-03-0410:33:53
# Male,Derick,McLaughlin,derick,mclaughlin.derick@hotmail.com,47,Chicago,Chrome Android,1,2020-03-0515:19:48
")
# expected output (return, with spaces between rows):
# ["Gender,FirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At", "Male,Carl,Wilderman,carl,yahoo.com,21->40,Seattle,Safari iPhone,2,afternoon", "Male,Marvin,Lind,marvin,hotmail.com,

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!