r/programmingchallenges • • Nov 27 '18
Just failed a coding challenge, and I'd love to learn more about the solution (Rewriting a perl script as golang)
I just totally bombed at a phone screen - I am not much of a Perl developer, or a GoLang developer, I was under the impression the job was java based! But in the spirit of the hacker ethic, I'd absolutely LOVE to see a working solution!
So - Here is the perl script I was provided with :
#!/usr/bin/perl -w
use strict;
use Encode;
my $tiff_flag = 1;
my $count = 0;
open(FILE,'<',$ARGV[0]) or die 'Error opening input file';
binmode(FILE) or die 'Error setting binary mode on input file';
while (read (FILE,$_,4)) {
my $rec_len = unpack("N",$_);
die "Bad record length: $rec_len" unless ($rec_len > 0);
read (FILE,$_,$rec_len);
if (substr($_,0,2) eq "\xF5\xF2") {
if ($tiff_flag) {
$count++;
open (TIFF, '>', $ARGV[0] . '_img' . sprintf("%04d",$count) . '.tiff') or die "Can't create image file";
binmode(TIFF) or die 'Error setting binary mode on image file';
print TIFF substr($_,117);
close TIFF;
}
$_ = substr($_,0,117);
}
print decode ('cp1047', $_) . "\n";
}
close FILE;Now, I'll first explain what it going on here - this thing parses x9.37 files - which are a kind of binary file for storing EBCDIC encoded strings, and also tiff images, for use with files that represent checks (for banks)
Now, the challenge was to rewrite this in golang - 100% score was to get the whole thing, but the company in question would be happy with not getting the TIFF files if they were too complicated... which would essentially mean you could get away with translating the code below instead :
#!/usr/bin/perl -w
use strict;
use Encode;
my $count = 0;
open(FILE,'<',$ARGV[0]) or die 'Error opening input file';
binmode(FILE) or die 'Error setting binary mode on input file';
while (read (FILE,$_,4)) {
my $rec_len = unpack("N",$_);
die "Bad record length: $rec_len" unless ($rec_len > 0);
read (FILE,$_,$rec_len);
print decode ('cp1047', $_) . "\n";
}
close FILE;I have no idea how to even read the PERL side of this, I looked up some stackoverflow afterwards, and it seems as though other people have been handed the same snippet as me in the past, but I haven't seen a perfect working solution yet in any of my searches!
EDIT : One last thing - The solution doesn't NEED to worry about the models, just being able to pull out each record as a string would even be fine!
1
u/FerociousBiscuit Nov 27 '18 edited Nov 27 '18
So here is the original: https://www.perlmonks.org/?node_id=784982
To handle passing command line parameters in Go use the "Args" variable of the "os" package os.Args. Args hold the command-line arguments, starting with the program name. https://golang.org/pkg/os/
On the Golang side we have some options for reading files. We can use the os, bufio, or io/ioutil packages. This stackoverflow goes over these three methods in greater detail. https://stackoverflow.com/questions/1821811/how-to-read-write-from-to-file-using-go
Then to handle decoding you'll use the text/encoding/charmap package. https://godoc.org/golang.org/x/text/encoding/charmap
Then to print everything out we use fmt. https://golang.org/pkg/fmt/
Here is an oversimplified example of how to use those components. I didn't bother checking for a non empty file.
EDIT: I've not done much with TIFF files before but for the more complex example the Tiff package will probably help you out.
https://godoc.org/golang.org/x/image/tiff
EDIT 2: If you wanted to do more error handling without assigning a variable when closing the file you can do something like this:
This will omit the results of file.Close and assign the error to err if there was one. Esentially in Goland error on right, return on left.. So our reader could also be error handled with
1
u/sd522527 Nov 27 '18
Here's one way. This hasn't been tested at all:
https://play.golang.org/p/edLhG6i50Js