Write a program called no_vowels.pl that will count
how many words DO NOT have an 'a' or an 'A',
how many DO NOT have an 'e' or an 'E',
how many DO NOT have an 'i' or an
'I', and so on.
The output should look like the below.
Note that the letters must be listed in decreasing order of frequency
14000 words do not have an 'u'
11258 words do not have an 'o'
10123 words do not have an 'i'
11523 words do not have an 'a'
10111 words do not have an 'e'
Name:
Anonymous2011-06-04 16:06
#!/usr/bin/perl
print "14000 words do not have an \'u\'\n";
print "11258 words do not have an \'o\'\n";
print "10123 words do not have an \'i\'\n";
print "11523 words do not have an \'a\'\n";
print "10111 words do not have an \'e\'\n";
exit 0
Name:
Anonymous2011-06-04 16:12
$DO_NOT = grep !/a/i, /\w+/g;
Name:
Anonymous2011-06-04 16:19
>>1
Damn it, Tim, how often do I have to repeat this? It's not autism if you don't count them by hand.
#!/usr/bin/ruby
# Fuck your shit, I use awesome languages
def no_vowels(word_list) # word_list is expected to be an array
# Containing a string of every word to be counted
no_a, no_e, no_i, no_o, no_u = 0
word_list.each do |cap| cap.downcase! end
word_list.each do |word|
no_a += 1 unless word.include? "a"
no_e += 1 unless word.include? "e"
no_i += 1 unless word.include? "i"
no_o += 1 unless word.include? "o"
no_u += 1 unless word.include? "u"
end
[no_a, no_e, no_i, no_o, no_u] # can be sorted later
end
#lang racket
(require srfi/13)
(define (no-vowels words)
(define (display-summary no-A no-E no-I no-O no-U)
(display no-U) (displayln " words do not have an 'u'")
(display no-O) (displayln " words do not have an 'o'")
(display no-I) (displayln " words do not have an 'i'")
(display no-A) (displayln " words do not have an 'a'")
(display no-E) (displayln " words do not have an 'e'"))
with open('words.txt', 'r') as f:
for s in f:
total += 1
for c in set(s.strip().lower()):
if c in counter:
counter[c] += 1
ls = counter.items()
ls.sort(lambda x,y: cmp(x[1],y[1]))
for i in range(len(ls)):
print (total - ls[i][1]), 'words do not have \'' + ls[i][0] + '\''
except IOError as e:
print e
Name:
Anonymous2011-06-08 4:47
This actually does everything OP specified, right down to the sorting. I know this is OP's homework, but it helped me learn how to work with Perl hashes.
#!/usr/bin/perl
my %no_vowel = ('a', 0, 'e', 0, 'i', 0, 'o', 0, 'u', 0);
open my $read_file, '<', "test.txt";
my $text = <$read_file>;
close $read_file;
while ($text =~ /(\w+)/g) {
for my $key (keys %no_vowel) {
my $value = $no_vowel{$key};
$value++ if $1 !~ /$key/i;
delete $no_vowel{$key};
$no_vowel{$key} = $value; }}
for my $key (sort { $no_vowel{$b} cmp $no_vowel{$a} } keys %no_vowel) {
print "$no_vowel{$key} words do not have an '$key'\n"; }
my %no_vowel = ('a', 0, 'e', 0, 'i', 0, 'o', 0, 'u', 0);
open my $read_file, '<', "test.txt";
my $text = <$read_file>;
close $read_file;
while ($text =~ /(\w+)/g) {
my $word = $1;
for my $key (keys %no_vowel) {
my $value = $no_vowel{$key};
$value++ if $word !~ /$key/i;
delete $no_vowel{$key};
$no_vowel{$key} = $value; }}
for my $key (sort { $no_vowel{$b} cmp $no_vowel{$a} } keys %no_vowel) {
print "$no_vowel{$key} words do not have an '$key'\n"; }