Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

PERL counting words that are missing a vowel

Name: Anonymous 2011-06-04 16:02

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: Anonymous 2011-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: Anonymous 2011-06-04 16:12

$DO_NOT = grep !/a/i, /\w+/g;

Name: Anonymous 2011-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.

Name: VIPPER 2011-06-04 16:29

Do your own homework.

Name: Anonymous 2011-06-04 17:51

>>2 YES

Name: Anonymous 2011-06-04 20:10

boyz its done

Name: Anonymous 2011-06-04 20:41

>>1
IF AND ELSE CLAUSES, SAVIOUR OF THE FLESH!

Name: Anonymous 2011-06-05 9:39

"uoiae" >>= \f → [(f, length . filter (not.(f `elem`)) $ words file)]

Name: Anonymous 2011-06-05 9:57

>>9
Not completely point-free, it's shit.

Name: Anonymous 2011-06-06 11:30

Name: Anonymous 2011-06-06 17:35

bampu

Name: Anonymous 2011-06-06 17:47

Name: Anonymous 2011-06-07 1:43

#!/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

Name: Anonymous 2011-06-07 8:58

>>14

Clearly not ready for enterprise.

Name: Anonymous 2011-06-07 9:14

>>14
SLOW AS FUCK

Name: Anonymous 2011-06-07 9:16

>>14
oh_shit_nigger_what_are_you_doing.png

Name: Anonymous 2011-06-07 9:18

>>17
What part of textboard did you not get?

Name: chris 2011-06-07 10:14

#!/usr/bin/perl -w

my $a_count = 0;
my $e_count = 0;
my $i_count = 0;
my $o_count = 0;
my $u_count = 0;
my $i;
my @splitted_i;
open(FILE,"/no_vowl/test");


while(defined($i = <FILE>)){
        print "i:".$i."\n";
   @splitted_i = split(/ /,$i);
        foreach(@splitted_i){
        print "splitted_i: ".$_."\n";
        if($_ =~ m/a|A/){
                $a_count ++;
           }
        elsif($_ =~ m/e|E/){
                $e_count ++;
           }
        elsif($_ =~ m/i|I/){
                $i_count ++;
           }
        elsif($_ =~ m/o|O/){
                $o_count ++;
           }
        elsif($_ =~ m/u|U/){
                $u_count ++;
           }
}
print "A_count:".$a_count."\n";
print "E_count:".$e_count."\n";
print "I_count:".$i_count."\n";
print "O_count:".$o_count."\n";
print "U_count:".$u_count."\n";
}
close(FILE);

sorting you need to do for yourself. but that shouldnt be so hard . right?

Name: Anonymous 2011-06-07 10:16

oh sry. this code counts the occurance of a,e,i,o,u. sry >:

Name: Anonymous 2011-06-07 10:21

here the right shit :
#!/usr/bin/perl -w

my $a_count = 0;
my $e_count = 0;
my $i_count = 0;
my $o_count = 0;
my $u_count = 0;
my $i;
my @splitted_i;
open(FILE,"/no_vowl/test");


while(defined($i = <FILE>)){
        print "i:".$i."\n";
   @splitted_i = split(/ /,$i);
        foreach(@splitted_i){
        print "splitted_i: ".$_."\n";
        if($_ =~ m/a|A/){
                #$a_count ++;
           }
        else{
                $a_count ++;
        }
        if($_ =~ m/e|E/){
                #$e_count ++;
           }
        else{
                $e_count ++;
        }
        if($_ =~ m/i|I/){
                #$i_count ++;
           }
        else{
                $i_count ++;
        }
        if($_ =~ m/o|O/){
                #$o_count ++;
           }
        else{
                $o_count ++;
        }
        if($_ =~ m/u|U/){
                #$u_count ++;
           }
        else{
                $u_count ++;
        }

}
print "A_count:".$a_count."\n";
print "E_count:".$e_count."\n";
print "I_count:".$i_count."\n";
print "O_count:".$o_count."\n";
print "U_count:".$u_count."\n";
}
close(FILE);

Name: Anonymous 2011-06-07 10:45

>>21
See >>3.

Name: VIPPER 2011-06-07 11:19

>>1
In my language every word is missing all wovels!

Name: VIPPER 2011-06-07 13:36

>>23
In my anus every word is a moving of bowels.

Name: Anonymous 2011-06-07 14:37

>>21
if it ain't Lisp, it's crap.

Name: Anonymous 2011-06-07 16:26


#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'"))
 
  (define (no-vowels-loop words no-A no-E no-I no-O no-U)
    (if (empty? words)
        (display-summary no-A no-E no-I no-O no-U)
        (let ((no-A-inc (+ no-A (if (string-contains-ci (car words) "a") 0 1)))
              (no-E-inc (+ no-E (if (string-contains-ci (car words) "e") 0 1)))
              (no-I-inc (+ no-I (if (string-contains-ci (car words) "i") 0 1)))
              (no-O-inc (+ no-O (if (string-contains-ci (car words) "o") 0 1)))
              (no-U-inc (+ no-U (if (string-contains-ci (car words) "u") 0 1))))
          (no-vowels-loop (cdr words) no-A-inc no-E-inc no-I-inc no-O-inc no-U-inc))))
  (no-vowels-loop words 0 0 0 0 0))

(no-vowels (string-tokenize (read-line)))

Name: Anonymous 2011-06-07 16:35

>>25
2/10

Name: Anonymous 2011-06-07 17:40


# pythonistas do it better

def check_word(word, counter):
    for i in range(len(counter)):
        if word.lower().find(counter[i][1]) == -1:
            counter[i][0] += 1

try:
    ls = [[0, 'a'], [0, 'e'], [0, 'i'], [0, 'o'], [0, 'u']]
   
    with open('words.txt', 'r') as f:
        for s in f:
            check_word(s.strip(), ls)

    ls.sort()
    ls.reverse()
    for i in range(len(ls)):
        print ls[i][0], ' words do not have \'', ls[i][1], '\''

except IOError as e:
    print e

Name: Anonymous 2011-06-07 17:47

>>28
    ls.sort()
    ls.reverse()

Why can't I stop laughing?

Name: Anonymous 2011-06-07 17:49

Name: Anonymous 2011-06-07 17:56

>>29
ls = sorted(ls, reverse=True)

Happy?

Name: Anonymous 2011-06-07 17:57

ls.sort(lambda x, y: cmp(y, x))

Name: Anonymous 2011-06-07 22:00

I think we should set a youtube video as the desktop background of this web sight

Name: Anonymous 2011-06-07 22:29

<?php
$file = file("words.txt");
$total = sizeof($file);
$letters = array('a', 'e', 'i', 'o', 'u');

array_map(
    function($letter, $count) use ($total) {
        echo ($total - $count) . ' words do not have ' . strtolower($letter) . ' or ' . strtoupper($letter) . "\n";
    },   
    $letters,
    array_map(
        function($letter, $count) use ($file, $total) {
            for ($i = 0; $i < $total; $count += false !== stripos($file[$i++], $letter) ? 1 : 0);
            return $count;
        },
        $letters,
        array_fill(0, sizeof($letters) - 1, 0)
    )
);

Name: Anonymous 2011-06-08 0:28

<?php
class Timer { public $start, $end; function __construct() { $this->start = microtime(true); } function end() { $this->end = $this->end ? $this->end : microtime(true); return $this->end - $this->start; } }
class Test { public $f, $t; function __construct(Closure $f) { $this->f = $f; } function exec() { $this->t = new Timer(); $f = $this->f; $f(); return $this->t->end(); } }
class Tester { const NL = "________\n\n"; public $tests = array(); function add(Closure $f) { $this->tests[] = new Test($f); } function exec() { $times = array(); foreach ($this->tests as $i => $t) {    echo self::NL, "Call $i \n", self::NL; $times[$i] = $t->exec(); echo self::NL, "Time: {$times[$i]} \n", self::NL; echo "\n"; } $fastest = array_reduce(array_keys($times), function($a, $b) use ($times) { return $times[$a] <= $times[$b] ? $a : $b; }, 0);echo "Fastest call was [$fastest] by: ", array_reduce($times, function($a, $b) use ($times, $fastest) { $diff = $b - $times[$fastest]; return !$diff ? $a : $a < $diff && $a > 0 ?  $a : $diff;}, 0), "\n"; } }

$t = new Tester();

$t->add(function() {
    $file = file("words.txt");
    $total = sizeof($file);
    $letters = array('a', 'e', 'i', 'o', 'u');

    array_map(
        function($letter, $count) use ($total) {
            echo ($total - $count) . ' words do not have ' . strtolower($letter) . ' or ' . strtoupper($letter) . "\n";
        },   
        $letters,
        array_map(
            function($letter) use ($file) {
                return array_reduce($file, function($count, $word) use ($letter) {
                    return $count + (stripos($word, $letter) ? 1 : 0);
                }, 0);
            },
            $letters
        )
    );
});


$t->add(function() {
    $file = file("words.txt");
    $total = sizeof($file);

    foreach($file as $word) {
        foreach (str_split($word) as $letter) {
            @${$letter}++;
        }
    }

    foreach (array('a', 'e', 'i', 'o', 'u') as $letter) {
        echo ($total - ${$letter}) . ' words do not have ' . strtolower($letter) . ' or ' . strtoupper($letter) . "\n";
    }
});

$t->exec();


I hate PHP actually

Name: Anonymous 2011-06-08 1:59

I would rather shove a fork in my anus than write a line of PHP code!

Name: chris 2011-06-08 2:15

>>36
why did i know that already.

freak!

Name: Anonymous 2011-06-08 2:54


try:
    counter = {'a':0, 'e':0, 'i':0, 'o':0, 'u':0}
    total = 0

    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: Anonymous 2011-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"; }

Name: Anonymous 2011-06-08 4:53

Whoops. I fucked up a minor detail.

#!/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) {
    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"; }

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List