Interview Question

Perl Developer Interview

-

Booking.com

You have the file with word at a single line. #input sample file abactor abaculus abacus Abadite . . Zyrenian #Output ******************a **********b ************************c a) you have to count the character and create a histogram in alphabetical order. b) now you have to produce a histogram with max 80 character in line in reference to max count c) now same out based histrogram based on the character count

AnswerAdd Tags

Interview Answers

6 Answers

2

#!/usr/bin/env perl use strict; use warnings; &main(); sub main { my %hist; open (my $fh, ") { my $line = lc($_); @arr = split("", $line); foreach my $ch (@arr) { $hist{$ch}++; $char_counter++; $most = $hist{$ch} if $most = $most-$a) { print "x "; } else { print " "; } } print "\n"; } print " a b c d e f g h i j k l m n o p q r s t u v w x y z\n"; }

Manoj Shekhawat on

1

#!/bin/perl # # # # my %tab; while() { chomp; map { ++$tab{$_} } split // ; } foreach my $key (sort keys %tab) { print '*' x $tab{$key}; print "$key\n"; } __DATA__ abactor abadite abaculus abacus

zeus on

0

#!/bin/perl # # All three conditions a,b,c is here. # use strict; use warnings; open my $fh, 'input.txt' or die('Cannot open file.'); my %counter; my $max = 0; while () { map { $max = ++$counter{$_} > $max ? $counter{$_} : $max } grep { $_ =~ /[a-z]/ } split //, lc $_; } foreach my $k ( reverse sort { $counter{$a} $counter{$b} } keys %counter ) { print ( ('*' x int( 79 / $max * $counter{$k} ) ) . $k . ($counter{$k} == $max ? '' : "\n")); } print "\n"; close($fh);

EvilCartman on

0

$line) { for ($i = 0; $i $n) { for ($i = 0; $i < $n; $i++) { } $output .= $c . "\t" . $n . "\n"; } $output .= (microtime(true) - $time) . "\n"; echo $output;

Anonymous on

0

//Correction: $line) { for ($i = 0; $i $n) { for ($i = 0; $i < $n; $i++) { echo '*'; } $output .= $c . "\n"; } $output .= (microtime(true) - $time) . "\n"; echo $output;

Anonymous on

0

$line) { for ($i = 0; $i $n) { for ($i = 0; $i < $n; $i++) { echo '*'; } echo $c . "\n"; }

Anonymous on

Add Answers or Comments

To comment on this, Sign In or Sign Up.