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"; }
Name:
Anonymous2011-06-08 4:55
>>39
what does it say here:
$no_vowel{$b} cmp $no_vowel{$a}
i tried sorting:
sort{$a cmp $b} %values_sort...
but it throws a exception saying "a" is not a number. granted.
but doesnt the aboce code say. compare value with string? i dont get it
what does it mean?
how i understood "keys". keys gives you a pair of values from the array-hash. correct?
so what does the $no_vowel($b) cmp $no_vowel($a) say?
>>43
yes that is clear to me that it sorts in a specific way.
but it is a hash. and a hash is key:value
so how do i say: pls use only the values for the comparsion and not the key.
(define (print-result result)
(for-each
(lambda (pair) (printf "~a words do not have an ~a\n" (cdr pair) (car pair)))
(sort (hash->list result) > #:key cdr)))
words := FS.open("/usr/share/dict/words").read().split("\n");
Map
->c[{(
words;
Map \[c !in \w;] $;
Len($);
),c};]
"aeiou".to_list();
Sort($);
Map ->p["%d words do not have a '%s'" % p;] $;
"\n".join($);
Print($);
my ($no_a, $no_e, $no_i, $no_o, $no_u) = (0, 0, 0, 0, 0);
open my $read_file, '<', "test.txt";
my $text = <$read_file>;
close $read_file;
while ($text =~ /(\w+)/g) {
my $word = $1;
$no_a++ if $word !~ /a/i;
$no_e++ if $word !~ /e/i;
$no_i++ if $word !~ /i/i;
$no_o++ if $word !~ /o/i;
$no_u++ if $word !~ /u/i; }
my @no_vowel = ("$no_a a", "$no_e e", "$no_i i", "$no_o o", "$no_u u");
for (sort {$b <=> $a} @no_vowel) {
/^([0-9]+) ([a-z])$/;
print "$1 words do not have an '$2'\n"; }
Hash table at the end for sorting:
#!/usr/bin/perl
my ($no_a, $no_e, $no_i, $no_o, $no_u) = (0, 0, 0, 0, 0);
open my $read_file, '<', "test.txt";
my $text = <$read_file>;
close $read_file;
while ($text =~ /(\w+)/g) {
my $word = $1;
$no_a++ if $word !~ /a/i;
$no_e++ if $word !~ /e/i;
$no_i++ if $word !~ /i/i;
$no_o++ if $word !~ /o/i;
$no_u++ if $word !~ /u/i; }
my %no_vowel = ('a', $no_a, 'e', $no_e, 'i', $no_i, 'o', $no_o, 'u', $no_u);
for my $key (sort { $no_vowel{$b} <=> $no_vowel{$a} } keys %no_vowel) {
print "$no_vowel{$key} words do not have an '$key'\n"; }
Name:
Anonymous2011-06-08 22:42
>>50
You're a fucking moron. Do society a favor and go back to playing with your sister's barbie dolls.
">Face it. It's just PASCAL with proper pointers and memory management tacked on. "
With that, maybe I'm not the C language master like you, but the only memory management that I'm aware of for ANSI/ISO C are for local variables with automatic duration.
Name:
!!kCq+A64Losi56ze2011-06-09 15:48
>>68
Fuck, I still can't get the quoting correct on this fucking site.
for(size_t i = 0; i < ncores; ++i)
total += totals[i];
for(std::set< std::pair<size_t, char> >::iterator it = ms.begin(); it != ms.end(); ++it)
std::cout << (total - it->first) << " words do not have '" << it->second << "'\n";
}
>Someone please optimize this, I suck at programming.
Yeah, don't use C++ io streams, they are slow as fuck!, there's like several virtual calls per fucking character read from a file plus 2 or 3 fucking buffers things get copied into. The solution is either to use C style fopen/fread/etc. which is better, but the optimal solution is to use your operating system's file IO facilities, like the POSIX open/read/write calls or Windows OpenFile, ReadFile, etc. and just wrap it in a thin C++ class with no virtual functions.
Also, boost's threads implementation is slow and unoptimized because boost programmers are lazy as fuck like you. Use a C++0x/C++11 threads implementation, which has the same interfaces, but is more optimized.
>>60 It's actually a fairly functional language. But yeah, it's probably going nowhere, like most of my petty projects ;_;
Well, today it's hard to advance new language, without backing at Microsoft (Haskell) or Google (Python).
>>81
Hmm I just realized you can pass extra parameters directly to boost.thread without using boost.bind.
>>83
Yes I just discovered how slow they are. I wrote the LineReader class after trying to do the whole thing with streams and wondering why it was so butt-fucking-slow.
Name:
Anonymous2011-06-10 22:15
</code>
sub print_vowels {
my %vowels = @_;
foreach (sort { $vowels{$a} <=> $vowels{$b} } keys %vowels) {
print "$_ was not in $vowels{$_} words\n";
}
}
my %vowels;
while (<>) {
chomp;
my @words = split " ", $_;
foreach (@words) {
$vowels{a}++ if (/^[^Aa]*$/);
$vowels{e}++ if (/^[^Ee]*$/);
$vowels{i}++ if (/^[^Ii]*$/);
$vowels{o}++ if (/^[^Oo]*$/);
$vowels{u}++ if (/^[^Uu]*$/);
}
print_vowels %vowels;
}
</code>