Skip to content

Commit ccdd65a

Browse files
author
Allen Maxwell
committed
checkin files after meeting...
1 parent d47ea78 commit ccdd65a

9 files changed

+164
-3
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ Basic things to discuss and watch out for:
3939
* uploading csv files to server in rails apps - timeout issues. I've had to deal with this using the delayed_jobs gem because rails will time out if the csv file is too large.
4040

4141
# Coding Exercise - let's build a csv parser! (oh boy!!!)
42+
43+
###### the code can be published for other users by adding it to this gist: https://gist.github.com/aamax/3fdf85c50407bad9c630
44+
4245
The challenge this month is to build a parser to read csv files. The parameters are as follows:
4346
* You may NOT use any external libraries for reading csv files (like faster csv) to solve the challenge.
4447
* You must account for quoted strings (quotes will always be double quote chars: "") and allow the file delimiter to be embedded within the quotes

import_files/final_transactions.csv

-1
This file was deleted.

import_files/prodfile.csv

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
product_id,name
2+
1,MacBook Pro
3+
2,Dell PC Laptop
4+
3,HP Desktop PC
5+
4,BlueTooth Keyboard and Mouse
6+
5,"21"" flat screen LCD Monitor"
7+
6,"""All in One Printer, Scanner, Fax"""
8+
7,"""Office Furniture: chair, desk, lamp"""
9+
8,business card holder

import_files/product_file1.csv

-1
This file was deleted.

import_files/trans.csv

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
product_id,date,quantity,price
2+
1,1/1/13,3,2400
3+
1,1/15/13,1,900
4+
2,2/12/13,1,1000
5+
3,4/1/13,1,1250
6+
4,5/2/14,15,65
7+
2,6/16/14,3,750
8+
8,6/30/14,5,12.5
9+
6,9/3/14,3,225
10+
6,9/4/14,2,235
11+
7,9/15/14,25,1200
12+
5,9/16/14,25,175
13+
5,9/20/14,3,195
14+
7,9/21/14,3,1300

import_files/transaction_master.csv

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
product_id,date,price,quantity
2+
1,1/1/13,2400,"""3"""
3+
1,1/15/13,900,1
4+
2,2/12/13,1000,1
5+
3,4/1/13,1250,1
6+
4,5/2/14,65,15
7+
2,6/16/14,750,3
8+
8,6/30/14,12.5,5
9+
6,9/3/14,225,3
10+
6,9/4/14,235,2
11+
7,9/15/14,1200,25
12+
5,9/16/14,175,25
13+
5,9/20/14,195,3
14+
7,9/21/14,1300,3
15+
1,1/1/13,2400,3
16+
1,1/15/13,900,1
17+
2,2/12/13,1000,1
18+
3,4/1/13,1250,1
19+
4,5/2/14,65,"""15"""
20+
2,6/16/14,750,3
21+
8,6/30/14,12.5,5
22+
6,9/3/14,225,3
23+
6,9/4/14,235,2
24+
7,9/15/14,1200,25
25+
5,9/16/14,175,25
26+
5,9/20/14,195,3
27+
7,9/21/14,"""1300""",3
28+
1,1/1/13,2400,3
29+
1,1/15/13,900,1
30+
2,2/12/13,1000,1
31+
3,4/1/13,1250,1
32+
4,5/2/14,65,15
33+
2,6/16/14,750,3
34+
8,6/30/14,12.5,5
35+
6,9/3/14,225,3
36+
6,9/4/14,235,2
37+
7,9/15/14,1200,25
38+
5,9/16/14,175,25
39+
5,9/20/14,195,3
40+
7,9/21/14,1300,3
41+
1,1/1/13,2400,3
42+
1,1/15/13,"""900""",1
43+
2,2/12/13,1000,1
44+
3,4/1/13,1250,1
45+
4,5/2/14,65,15
46+
2,6/16/14,750,3
47+
"""8""",6/30/14,12.5,5
48+
6,9/3/14,225,3
49+
6,9/4/14,235,2
50+
7,9/15/14,1200,25
51+
5,9/16/14,175,25
52+
5,9/20/14,195,3
53+
"""7""",9/21/14,1300,3

import_files/transactions.csv

-1
This file was deleted.
0 Bytes
Binary file not shown.

solution_aamax_dan_hacket.rb

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
class Parser
2+
def initialize filename, delimiter=","
3+
@fhandle = File.open(filename, 'r')
4+
@delim = delimiter
5+
@header = []
6+
@arr_val = []
7+
line_str = @fhandle.readline
8+
@header = parse_row(line_str)
9+
end
10+
11+
def header
12+
@header
13+
end
14+
15+
def eof?
16+
@fhandle.eof?
17+
end
18+
19+
def read
20+
line_str = @fhandle.readline
21+
parse_row(line_str)
22+
end
23+
24+
def parse_row(line)
25+
ret_val = []
26+
while !line.nil? && line.length > 0
27+
result = parse_field(line)
28+
line = result[1]
29+
ret_val << result[0]
30+
end
31+
ret_val
32+
end
33+
34+
def parse_field(str)
35+
str.chomp!
36+
ret_val = []
37+
if str[0] != '"'
38+
field = str.split(@delim)[0]
39+
ret_val << field
40+
ret_val << str[field.length + 1, str.length]
41+
else
42+
idx = 1
43+
44+
puts " idx: #{idx} quoted field: #{str}" if str[0] == '"'
45+
46+
47+
while true
48+
if str[idx] == '"'
49+
# read to quote followed by delim or eol
50+
if (idx == str.length - 1) || (str[idx + 1] == @delim)
51+
fld = str[1..idx]
52+
str = str[idx + 2, str.length]
53+
ret_val = [fld, str]
54+
break
55+
end
56+
idx += 1
57+
else
58+
# read to next delim or eol
59+
if (idx == str.length - 1) || (str[idx] == @delim)
60+
fld = str[0..idx - 1]
61+
str = str[idx + 1, str.length]
62+
ret_val = [fld, str]
63+
break
64+
end
65+
idx += 1
66+
end
67+
break if str.empty?
68+
end
69+
70+
end
71+
ret_val
72+
end
73+
74+
def close
75+
@fhandle.close if @fhandle
76+
end
77+
78+
end
79+
80+
p = Parser.new('./import_files/prodfile.csv')
81+
puts "Header: #{p.header}"
82+
until p.eof?
83+
puts p.read.map {|v| v}.join(',')
84+
end
85+
p.close

0 commit comments

Comments
 (0)