diff options
| author | Doomtrain14 <yet.ebreo@gmail.com> | 2019-09-04 18:51:46 +0800 |
|---|---|---|
| committer | Doomtrain14 <yet.ebreo@gmail.com> | 2019-09-04 18:51:46 +0800 |
| commit | ae15c22e798605975af7feb763703d2d77849022 (patch) | |
| tree | b2e5b57fbfba028ab05d0a23d18f082eeba06650 /challenge-024 | |
| parent | 6bfbf4b0a4b60a35654abac0259e7229bd5f96cc (diff) | |
| download | perlweeklychallenge-club-ae15c22e798605975af7feb763703d2d77849022.tar.gz perlweeklychallenge-club-ae15c22e798605975af7feb763703d2d77849022.tar.bz2 perlweeklychallenge-club-ae15c22e798605975af7feb763703d2d77849022.zip | |
Added p5/p6 solutions to ch#24
Diffstat (limited to 'challenge-024')
| -rw-r--r-- | challenge-024/yet-ebreo/perl5/ch-1.sh | 1 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl5/ch-2.pl | 71 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl5/file1.txt | 56 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl5/file2.txt | 25 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl5/file3.txt | 6 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl5/file4.txt | 4 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl5/file5.txt | 30 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl5/generate_text_files.pl | 20 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl6/ch-1.sh | 1 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl6/ch-2.p6 | 37 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl6/file1.txt | 56 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl6/file2.txt | 25 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl6/file3.txt | 6 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl6/file4.txt | 4 | ||||
| -rw-r--r-- | challenge-024/yet-ebreo/perl6/file5.txt | 30 |
15 files changed, 372 insertions, 0 deletions
diff --git a/challenge-024/yet-ebreo/perl5/ch-1.sh b/challenge-024/yet-ebreo/perl5/ch-1.sh new file mode 100644 index 0000000000..047dce298d --- /dev/null +++ b/challenge-024/yet-ebreo/perl5/ch-1.sh @@ -0,0 +1 @@ +perl -e '$%'
\ No newline at end of file diff --git a/challenge-024/yet-ebreo/perl5/ch-2.pl b/challenge-024/yet-ebreo/perl5/ch-2.pl new file mode 100644 index 0000000000..6537087a21 --- /dev/null +++ b/challenge-024/yet-ebreo/perl5/ch-2.pl @@ -0,0 +1,71 @@ +use strict; +use warnings; +use 5.010; + +die "Usage:\n\tch-2.pl <search word(s)> <files> \n\nExample:\n\tch-2.pl \"i sing eat and love\" file1.txt file2.txt\n\n" if @ARGV < 2; + +#Set the minimum length of words to be included +my $minimum_length = 1; + +# Get the words to search. Convert it to lowercase. +# Use as keys in a hash to get only unique words +my %hash_words = map { lc $_ => 1 } shift=~/([^, ]+) *,?/g; +# Retrieve the keys and store in @words +my @words = keys %hash_words; + +# Create index from files +my @files = @ARGV; +my %index; +for my $file (@files) { + open(my $fh, "<", $file); + while(<$fh>) { + for my $w (grep { y///c >=$minimum_length } /(\w+) ?/g) { + $index{lc $w}{lc $file=~s/^\.\\//r}++ + } + } + close($fh); +} + +use Text::Table::Tiny 'generate_table'; +sub print_search_result { + #Show search result + my $rows; + push @{$rows} , ["Words","File(s)"]; + for my $w (sort @words) { + my $sub; + for my $f (sort keys %{$index{$w}}) { + $sub .= "$f "; + } + push @{$rows} , [$w,$sub||"(N/A)"]; + } + print generate_table(rows => $rows, header_row => 1); +} + +sub show_index { + #Show File Index + my $rows; + push @{$rows} , ["Words","File(s)"]; + for my $w (sort keys %index) { + my $sub; + for my $f (sort keys %{$index{$w}}) { + $sub .= "$f "; + } + push @{$rows} , [$w,$sub]; + } + print generate_table(rows => $rows, header_row => 1); +} + +&print_search_result; + +=begin +perl .\ch-2.pl "i sing eat and love" .\file1.txt .\file2.txt .\file3.txt .\file4.txt .\file5.txt ++-------+--------------------------------+ +| Words | File(s) | ++-------+--------------------------------+ +| and | file1.txt file2.txt | +| eat | file4.txt | +| i | file1.txt file2.txt file4.txt | +| love | file2.txt file5.txt | +| sing | (N/A) | ++-------+--------------------------------+ +=cut
\ No newline at end of file diff --git a/challenge-024/yet-ebreo/perl5/file1.txt b/challenge-024/yet-ebreo/perl5/file1.txt new file mode 100644 index 0000000000..6e8637c9a5 --- /dev/null +++ b/challenge-024/yet-ebreo/perl5/file1.txt @@ -0,0 +1,56 @@ +In Milwaukee it is snowing +on the golden statue +of the 1970s television star +whose television house +was in Milwaukee + +and also on the Comet Cafe +and on the white museum +the famous Spanish architect +built with a glass + +elevator through it +and a room with a button +that when you press it +makes two wings + +on the sides of the building +more quickly than you might +imagine mechanically +rise like a clumsy + +thoughtful bird +thinking now +I am at last ready +over the lake + +that has many moods +to fly but it will not +and people ask +who are we who see + +so much evil and try +to stop it and fail +and know we are no longer +for no reason worrying + +the terrible governors +are evil or maybe +just mistaken and nothing +can stop them not even + +the workers who keep +working even when +it snows on their heads +and on the bridge + +that keeps our cars +above the water +for an hour +in northern California +today it snowed + +and something +happened people +turned their beautiful +sparkling angry faces up
\ No newline at end of file diff --git a/challenge-024/yet-ebreo/perl5/file2.txt b/challenge-024/yet-ebreo/perl5/file2.txt new file mode 100644 index 0000000000..34e5cd1a91 --- /dev/null +++ b/challenge-024/yet-ebreo/perl5/file2.txt @@ -0,0 +1,25 @@ +So much of this happened +in basements, in thick woolen sweaters, in B major +but with strong passages in minor. On the outskirts. +That's where we were from +but our thoughts had wings like the pigeons +and like them tried to find urban quarters +where the life of the spirit was more shaded, fluttering +over stone walls heavy with history. The shadow-play of thoughts +exposed what words concealed, that no love is as strong +as the one that goes unrequited. A gentle drizzle +fell over the bike rack. I remember everything +from inside the rain. + +No one has said it more plainly than Barney Bigard +in his clarinet solo in "Creole Love Call" +with Duke Ellington's orchestra. Literally +heartrending. For those who love each other music +is just the background. For the one who's no longer loved +it's everything. He hears it from inside the drizzle +up the street and down again. Who has the right +in this context to poke fun at Pathetic images: +a heart pierced by a spindle +on a rotating platter. +Then notes rise up +to which the shadows can dance; the others' shadows.
\ No newline at end of file diff --git a/challenge-024/yet-ebreo/perl5/file3.txt b/challenge-024/yet-ebreo/perl5/file3.txt new file mode 100644 index 0000000000..a7e1047b0b --- /dev/null +++ b/challenge-024/yet-ebreo/perl5/file3.txt @@ -0,0 +1,6 @@ +Schweigt, unberauschte, finstre Richter! +Ich trinke Wein, und bin ein Dichter. +Tut mir es nach, und trinket Wein, +So seht ihr meine Schoenheit ein. +Sonst wahrlich, unberauschte Richter, +Sonst wahrlich seht ihr sie nicht ein!
\ No newline at end of file diff --git a/challenge-024/yet-ebreo/perl5/file4.txt b/challenge-024/yet-ebreo/perl5/file4.txt new file mode 100644 index 0000000000..ee9bb4bfae --- /dev/null +++ b/challenge-024/yet-ebreo/perl5/file4.txt @@ -0,0 +1,4 @@ +A woman wails, boo hoo, mourning her man +Shut up, shame on you, don't cry to the hills! +O little sister, I should have warned you +Don't eat the meat, if it makes you cough blood!
\ No newline at end of file diff --git a/challenge-024/yet-ebreo/perl5/file5.txt b/challenge-024/yet-ebreo/perl5/file5.txt new file mode 100644 index 0000000000..d16053a6fb --- /dev/null +++ b/challenge-024/yet-ebreo/perl5/file5.txt @@ -0,0 +1,30 @@ +Tell as a Marksman - were forgotten +Tell - this Day endures +Ruddy as that coeval Apple +The Tradition bears - + +Fresh as Mankind that humble story +Though a statelier Tale +Grown in the Repetition hoary +Scarcely would prevail - + +Tell had a son - The ones that knew it +Need not linger here - +Those who did not to Human Nature +Will subscribe a Tear - + +Tell would not bare his Head +In Presence +Of the Ducal Hat - +Threatened for that with Death - by Gessler - +Tyranny bethought + +Make of his only Boy a Target +That surpasses Death - +Stolid to Love's supreme entreaty +Not forsook of Faith - + +Mercy of the Almighty begging - +Tell his Arrow sent - +God it is said replies in Person +When the cry is meant -
\ No newline at end of file diff --git a/challenge-024/yet-ebreo/perl5/generate_text_files.pl b/challenge-024/yet-ebreo/perl5/generate_text_files.pl new file mode 100644 index 0000000000..3a59a01e07 --- /dev/null +++ b/challenge-024/yet-ebreo/perl5/generate_text_files.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; +use 5.010; + +# hmmm... how about we use the random poems to generate some text files? +use LWP::Simple qw($ua get); +use JSON; +$ua->timeout(30); +my $api_content = get "https://www.poemist.com/api/v1/randompoems" || die "Request Timeout\n"; +my @data = @{ decode_json($api_content) }; + +my $file_ctr = 1; +for (@data) { + if ($_->{title}!~/\p{Arabic}/ && $_->{content}!~/[^\x00-\x7E]/m) { + open(my $fh, '>', "file$file_ctr\.txt"); + print $fh $_->{content}; + close $fh; + $file_ctr++; + } +}
\ No newline at end of file diff --git a/challenge-024/yet-ebreo/perl6/ch-1.sh b/challenge-024/yet-ebreo/perl6/ch-1.sh new file mode 100644 index 0000000000..323b5054ac --- /dev/null +++ b/challenge-024/yet-ebreo/perl6/ch-1.sh @@ -0,0 +1 @@ +perl6 -e '$%'
\ No newline at end of file diff --git a/challenge-024/yet-ebreo/perl6/ch-2.p6 b/challenge-024/yet-ebreo/perl6/ch-2.p6 new file mode 100644 index 0000000000..580fcfb3e2 --- /dev/null +++ b/challenge-024/yet-ebreo/perl6/ch-2.p6 @@ -0,0 +1,37 @@ +my $minimum_length = 1; + +multi sub MAIN( + Str $words, #= Space/Comma separated words enclosed with "" + *@files #= File(s) to search +) { + # Get the words to search. Convert it to lowercase. + # Get only unique words (with Set). + my Str @words = (map {.lc},$words.split(/<[\s,]>/)).Set.keys; + + # Create index from files + my %index; + for @files -> $file { + for $file.IO.lines -> $line { + %index{ .lc }{ $file.subst(/^\.\\/,'') }++ for $line.split(/\W/).grep(/^ \w ** {$minimum_length..*} $/) + } + } + + #Show search result + for @words.sort -> $w { + my @f; + for %index{$w}.keys.sort -> $f { + @f.push($f) + } + say "$w:\t"~(@f[] || "(N/A)") + } + +} +#`[ +# perl6 .\ch-2.p6 "i sing eat and love" .\file1.txt .\file2.txt .\file3.txt .\file4.txt .\file5.txt +# and: file1.txt file2.txt +# eat: file4.txt +# i: file1.txt file2.txt file4.txt +# love: file2.txt file5.txt +# sing: (N/A) +# ] + diff --git a/challenge-024/yet-ebreo/perl6/file1.txt b/challenge-024/yet-ebreo/perl6/file1.txt new file mode 100644 index 0000000000..6e8637c9a5 --- /dev/null +++ b/challenge-024/yet-ebreo/perl6/file1.txt @@ -0,0 +1,56 @@ +In Milwaukee it is snowing +on the golden statue +of the 1970s television star +whose television house +was in Milwaukee + +and also on the Comet Cafe +and on the white museum +the famous Spanish architect +built with a glass + +elevator through it +and a room with a button +that when you press it +makes two wings + +on the sides of the building +more quickly than you might +imagine mechanically +rise like a clumsy + +thoughtful bird +thinking now +I am at last ready +over the lake + +that has many moods +to fly but it will not +and people ask +who are we who see + +so much evil and try +to stop it and fail +and know we are no longer +for no reason worrying + +the terrible governors +are evil or maybe +just mistaken and nothing +can stop them not even + +the workers who keep +working even when +it snows on their heads +and on the bridge + +that keeps our cars +above the water +for an hour +in northern California +today it snowed + +and something +happened people +turned their beautiful +sparkling angry faces up
\ No newline at end of file diff --git a/challenge-024/yet-ebreo/perl6/file2.txt b/challenge-024/yet-ebreo/perl6/file2.txt new file mode 100644 index 0000000000..34e5cd1a91 --- /dev/null +++ b/challenge-024/yet-ebreo/perl6/file2.txt @@ -0,0 +1,25 @@ +So much of this happened +in basements, in thick woolen sweaters, in B major +but with strong passages in minor. On the outskirts. +That's where we were from +but our thoughts had wings like the pigeons +and like them tried to find urban quarters +where the life of the spirit was more shaded, fluttering +over stone walls heavy with history. The shadow-play of thoughts +exposed what words concealed, that no love is as strong +as the one that goes unrequited. A gentle drizzle +fell over the bike rack. I remember everything +from inside the rain. + +No one has said it more plainly than Barney Bigard +in his clarinet solo in "Creole Love Call" +with Duke Ellington's orchestra. Literally +heartrending. For those who love each other music +is just the background. For the one who's no longer loved +it's everything. He hears it from inside the drizzle +up the street and down again. Who has the right +in this context to poke fun at Pathetic images: +a heart pierced by a spindle +on a rotating platter. +Then notes rise up +to which the shadows can dance; the others' shadows.
\ No newline at end of file diff --git a/challenge-024/yet-ebreo/perl6/file3.txt b/challenge-024/yet-ebreo/perl6/file3.txt new file mode 100644 index 0000000000..a7e1047b0b --- /dev/null +++ b/challenge-024/yet-ebreo/perl6/file3.txt @@ -0,0 +1,6 @@ +Schweigt, unberauschte, finstre Richter! +Ich trinke Wein, und bin ein Dichter. +Tut mir es nach, und trinket Wein, +So seht ihr meine Schoenheit ein. +Sonst wahrlich, unberauschte Richter, +Sonst wahrlich seht ihr sie nicht ein!
\ No newline at end of file diff --git a/challenge-024/yet-ebreo/perl6/file4.txt b/challenge-024/yet-ebreo/perl6/file4.txt new file mode 100644 index 0000000000..ee9bb4bfae --- /dev/null +++ b/challenge-024/yet-ebreo/perl6/file4.txt @@ -0,0 +1,4 @@ +A woman wails, boo hoo, mourning her man +Shut up, shame on you, don't cry to the hills! +O little sister, I should have warned you +Don't eat the meat, if it makes you cough blood!
\ No newline at end of file diff --git a/challenge-024/yet-ebreo/perl6/file5.txt b/challenge-024/yet-ebreo/perl6/file5.txt new file mode 100644 index 0000000000..d16053a6fb --- /dev/null +++ b/challenge-024/yet-ebreo/perl6/file5.txt @@ -0,0 +1,30 @@ +Tell as a Marksman - were forgotten +Tell - this Day endures +Ruddy as that coeval Apple +The Tradition bears - + +Fresh as Mankind that humble story +Though a statelier Tale +Grown in the Repetition hoary +Scarcely would prevail - + +Tell had a son - The ones that knew it +Need not linger here - +Those who did not to Human Nature +Will subscribe a Tear - + +Tell would not bare his Head +In Presence +Of the Ducal Hat - +Threatened for that with Death - by Gessler - +Tyranny bethought + +Make of his only Boy a Target +That surpasses Death - +Stolid to Love's supreme entreaty +Not forsook of Faith - + +Mercy of the Almighty begging - +Tell his Arrow sent - +God it is said replies in Person +When the cry is meant -
\ No newline at end of file |
