diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-14 17:54:00 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-14 17:54:00 +0000 |
| commit | ab1cb4bf3b1f38397d7baa50dcac583de1616637 (patch) | |
| tree | 0112630c8b63a5146081a446a1d13fae270109e4 | |
| parent | 907e1c4627a07c2a1db68dd57ad93b2a78372e2c (diff) | |
| parent | b6c6c3866bb435f0b79732716c3fc49bb5fead87 (diff) | |
| download | perlweeklychallenge-club-ab1cb4bf3b1f38397d7baa50dcac583de1616637.tar.gz perlweeklychallenge-club-ab1cb4bf3b1f38397d7baa50dcac583de1616637.tar.bz2 perlweeklychallenge-club-ab1cb4bf3b1f38397d7baa50dcac583de1616637.zip | |
Merge pull request #3517 from pauloscustodio/099
Add Awk solution to challenge 004
24 files changed, 408 insertions, 242 deletions
diff --git a/challenge-001/paulo-custodio/awk/ch-1.awk b/challenge-001/paulo-custodio/awk/ch-1.awk index da8e38182a..001f74cd23 100644 --- a/challenge-001/paulo-custodio/awk/ch-1.awk +++ b/challenge-001/paulo-custodio/awk/ch-1.awk @@ -3,29 +3,29 @@ # Challenge 001 # # Challenge #1 -# Write a script to replace the character ‘e’ with ‘E’ in the string -# ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ +# Write a script to replace the character 'e' with 'E' in the string +# "Perl Weekly Challenge". Also print the number of times the character 'e' # is found in the string. function join(array, start, end, sep, result, i) { if (sep == "") - sep = " " + sep = " "; else if (sep == SUBSEP) # magic value - sep = "" - result = array[start] + sep = ""; + result = array[start]; for (i = start + 1; i <= end; i++) - result = result sep array[i] - return result + result = result sep array[i]; + return result; } function alen(a, i, k) { - k = 0 - for(i in a) k++ - return k + k = 0; + for(i in a) k++; + return k; } BEGIN { - text = join(ARGV, 1, alen(ARGV)) - print gsub(/e/, "E", text) " " text - exit 0 + text = join(ARGV, 1, alen(ARGV)); + print gsub(/e/, "E", text) " " text; + exit 0; } diff --git a/challenge-001/paulo-custodio/awk/ch-2.awk b/challenge-001/paulo-custodio/awk/ch-2.awk index f4c9a9fafb..2c04b2a074 100644 --- a/challenge-001/paulo-custodio/awk/ch-2.awk +++ b/challenge-001/paulo-custodio/awk/ch-2.awk @@ -5,13 +5,12 @@ # Challenge #2 # Write a one-liner to solve the FizzBuzz problem and print the numbers 1 # through 20. However, any number divisible by 3 should be replaced by the word -# ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both -# divisible by 3 and 5 become ‘fizzbuzz’. +# 'fizz' and any divisible by 5 by the word 'buzz'. Those numbers that are both +# divisible by 3 and 5 become 'fizzbuzz'. BEGIN { - num = ARGV[1] ? ARGV[1] : 20 - for (i=1; i<=num; i++) { - print (i%15)==0 ? "fizzbuzz" : (i%3)==0 ? "fizz" : (i%5)==0 ? "buzz" : i - } - exit 0 + num = ARGV[1] ? ARGV[1] : 20; + for (i=1; i<=num; i++) + print (i%15)==0 ? "fizzbuzz" : (i%3)==0 ? "fizz" : (i%5)==0 ? "buzz" : i; + exit 0; } diff --git a/challenge-001/paulo-custodio/test.pl b/challenge-001/paulo-custodio/test.pl index 41c54fdb5e..0d5a4711b0 100644 --- a/challenge-001/paulo-custodio/test.pl +++ b/challenge-001/paulo-custodio/test.pl @@ -35,14 +35,17 @@ else { $TESTS{$_}=1 for @ARGV; } +# to be used in eval{} in the tests +use vars qw( $prog $exec); + for my $lang (grep {-d} sort keys %LANG) { next unless $TESTS{$lang}; - for my $prog (path($lang)->children(qr/\.$LANG{$lang}$/)) { + for $prog (path($lang)->children(qr/\.$LANG{$lang}$/)) { $prog->basename =~ /^ch[-_](.*)\.$LANG{$lang}$/ or die $prog; my $task = $1; # compile if needed - my $exec = build($lang, $prog); + $exec = build($lang, $prog); for my $test (path("t")->children(qr/test-$task\.yaml$/)) { # execute each test from test-N.yaml @@ -55,15 +58,17 @@ for my $lang (grep {-d} sort keys %LANG) { $@ and die $@; # build test command line - my $cmd = "$exec ".($spec->{args} // ""); + my $cmd = "$exec ".value_or_eval($spec->{args}); chomp($cmd); + + # input if(defined($spec->{input})) { - path("in.txt")->spew($spec->{input}); + path("in.txt")->spew(value_or_eval($spec->{input})); $cmd .= " < in.txt"; } if (defined($spec->{output})) { $spec->{output} =~ s/^\|//mg; # delete initial bar - path("out_exp.txt")->spew($spec->{output}); + path("out_exp.txt")->spew(value_or_eval($spec->{output})); $cmd .= " > out.txt"; } @@ -139,3 +144,13 @@ sub run { my($cmd) = @_; ok 0==system($cmd), $cmd; } + +sub value_or_eval { + my($str) = @_; + $str //= ""; + my $value = ($str =~ /^eval\b/) ? eval($str) : $str; + $@ and die "eval '$str' failed: $@"; + return $value; +} + +1; diff --git a/challenge-002/paulo-custodio/awk/ch-1.awk b/challenge-002/paulo-custodio/awk/ch-1.awk index c5adaa5282..1db88fd0eb 100644 --- a/challenge-002/paulo-custodio/awk/ch-1.awk +++ b/challenge-002/paulo-custodio/awk/ch-1.awk @@ -6,10 +6,10 @@ # Write a script or one-liner to remove leading zeros from positive numbers. BEGIN { - num = ARGV[1] + num = ARGV[1]; if (match(num, /^0*([0-9]+)/, capture)) - print capture[1] + print capture[1]; else - print num - exit 0 + print num; + exit 0; } diff --git a/challenge-002/paulo-custodio/awk/ch-2.awk b/challenge-002/paulo-custodio/awk/ch-2.awk index 67b75f5c8d..752f93173b 100644 --- a/challenge-002/paulo-custodio/awk/ch-2.awk +++ b/challenge-002/paulo-custodio/awk/ch-2.awk @@ -9,55 +9,55 @@ function format_number(n, base, negative, output, d) { if (n < 0) { - negative = 1 + negative = 1; n = -n } else { - negative = 0 + negative = 0; } output = "" do { - d = n % base - n = int(n / base) - output = substr(digits, d+1, 1) output + d = n % base; + n = int(n / base); + output = substr(digits, d+1, 1) output; } while (n > 0) if (negative) - output = "-" output + output = "-" output; - return output + return output; } function scan_number(str, base, n, negative, ch, d) { n = 0; if (sub(/^-/, "", str)) - negative = 1 + negative = 1; else - negative = 0 + negative = 0; while (str != "") { - ch = toupper(substr(str, 1, 1)) - str = substr(str, 2) - d = index(digits, ch) - 1 + ch = toupper(substr(str, 1, 1)); + str = substr(str, 2); + d = index(digits, ch) - 1; if (d < 0 || d >= base) { - print "cannot parse '" str "'" - exit(1) + print "cannot parse '" str "'"; + exit(1); } - n = base * n + d + n = base * n + d; } if (negative) - n = -n - return n + n = -n; + return n; } BEGIN { - BASE = 35 - digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" + BASE = 35; + digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if (ARGV[1] == "-r") - print scan_number(ARGV[2], 35) + print scan_number(ARGV[2], 35); else - print format_number(ARGV[1], 35) + print format_number(ARGV[1], 35); exit 0 } diff --git a/challenge-003/paulo-custodio/awk/ch-1.awk b/challenge-003/paulo-custodio/awk/ch-1.awk index 12c7cea74a..024d960e91 100644 --- a/challenge-003/paulo-custodio/awk/ch-1.awk +++ b/challenge-003/paulo-custodio/awk/ch-1.awk @@ -12,54 +12,54 @@ function min(a, b) { } function min3(a, b, c) { - return min(a, min(b, c)) + return min(a, min(b, c)); } function alen(a, i, k) { - k = 0 - for(i in a) k++ - return k + k = 0; + for(i in a) k++; + return k; } function push(a, v, n) { - n = alen(a) - while (n in a) n++ - a[n] = v + n = alen(a); + while (n in a) n++; + a[n] = v; } function shift(a, i, v) { - v = a[0] + v = a[0]; for (i=1; i<alen(a); i++) - a[i-1] = a[i] - delete a[alen(a)-1] - return v + a[i-1] = a[i]; + delete a[alen(a)-1]; + return v; } function hamming( n) { # get the smallest of the multiples - n = min3(seq2[0], seq3[0], seq5[0]) + n = min3(seq2[0], seq3[0], seq5[0]); # shift used multiples - if (seq2[0] == n) shift(seq2) - if (seq3[0] == n) shift(seq3) - if (seq5[0] == n) shift(seq5) + if (seq2[0] == n) shift(seq2); + if (seq3[0] == n) shift(seq3); + if (seq5[0] == n) shift(seq5); # push next multiple - push(seq2, n*2) - push(seq3, n*3) - push(seq5, n*5) + push(seq2, n*2); + push(seq3, n*3); + push(seq5, n*5); - return n + return n; } BEGIN { # sequences of hamming numbers 2*n, 3*n, 5*n - seq2[0] = 1 - seq3[0] = 1 - seq5[0] = 1 + seq2[0] = 1; + seq3[0] = 1; + seq5[0] = 1; - n = ARGV[1] ? ARGV[1] : 5 + n = ARGV[1] ? ARGV[1] : 5; for (i=0; i<n; i++) - print hamming() - exit(0) + print hamming(); + exit 0; } diff --git a/challenge-003/paulo-custodio/awk/ch-2.awk b/challenge-003/paulo-custodio/awk/ch-2.awk index ab7703737c..40522f9b12 100644 --- a/challenge-003/paulo-custodio/awk/ch-2.awk +++ b/challenge-003/paulo-custodio/awk/ch-2.awk @@ -8,58 +8,57 @@ # information about Pascal Triangle, check this wikipedia page. function alen(a, i, k) { - k = 0 - for(i in a) k++ - return k + k = 0; + for(i in a) k++; + return k; } function push(a, v, n) { - n = alen(a) - while (n in a) n++ - a[n] = v + n = alen(a); + while (n in a) n++; + a[n] = v; } function spaces(n, i, output) { - output = "" + output = ""; for (i = 0; i < n; i++) - output = output " " - return output + output = output " "; + return output; } function join(array, start, end, sep, result, i) { if (sep == "") - sep = " " + sep = " "; else if (sep == SUBSEP) # magic value - sep = "" - result = array[start] + sep = ""; + result = array[start]; for (i = start + 1; i <= end; i++) - result = result sep array[i] - return result + result = result sep array[i]; + return result; } function draw_pascal(rows, row, r, i, nrow) { - row[0] = 1 - print spaces(rows-1) join(row, 0, alen(row)-1, " ") + row[0] = 1; + print spaces(rows-1) join(row, 0, alen(row)-1, " "); for (r = 1; r < rows; r++) { # compute next row - for (i in nrow) delete nrow[i] - nrow[0] = 1 + for (i in nrow) delete nrow[i]; + nrow[0] = 1; for (i = 0; i < alen(row)-1; i++) - push(nrow, row[i]+row[i+1]) - push(nrow, 1) + push(nrow, row[i]+row[i+1]); + push(nrow, 1); # swap row for (i in nrow) - row[i] = nrow[i] + row[i] = nrow[i]; # print - print spaces(rows-r-1) join(row, 0, alen(row)-1, " ") + print spaces(rows-r-1) join(row, 0, alen(row)-1, " "); } } BEGIN { - n = ARGV[1] ? ARGV[1] : 5 - draw_pascal(n) - exit(0) - + n = ARGV[1] ? ARGV[1] : 5; + draw_pascal(n); + exit(0); } diff --git a/challenge-004/paulo-custodio/awk/ch-1.awk b/challenge-004/paulo-custodio/awk/ch-1.awk new file mode 100644 index 0000000000..1121fe0fed --- /dev/null +++ b/challenge-004/paulo-custodio/awk/ch-1.awk @@ -0,0 +1,21 @@ +#!/usr/bin/gawk + +# Challenge 004 +# +# Challenge #1 +# Write a script to output the same number of PI digits as the size of your script. +# Say, if your script size is 10, it should print 3.141592653. +# +# we need a big-math library to compute any large number of digits + +BEGIN { + size = ARGV[1] ? ARGV[1] : 1000; + bc = "echo 'scale=" (size+10) "; 4*a(1)' | bc -l"; + pi = ""; + while ((bc | getline temp) > 0) { + gsub(/\\/, "", temp); + pi = pi temp; + } + pi = substr(pi, 1, size+1) + print pi; +} diff --git a/challenge-004/paulo-custodio/awk/ch-2.awk b/challenge-004/paulo-custodio/awk/ch-2.awk new file mode 100644 index 0000000000..18eb2cb3ae --- /dev/null +++ b/challenge-004/paulo-custodio/awk/ch-2.awk @@ -0,0 +1,33 @@ +#!/usr/bin/gawk + +# Challenge 004 +# +# Challenge #2 +# You are given a file containing a list of words (case insensitive 1 word per +# line) and a list of letters. Print each word from the file that can be made +# using only letters from the list. You can use each letter only once (though +# there can be duplicates and you can use each of them once), you don't have to +# use all the letters. +# (Disclaimer: The challenge was proposed by Scimon Proctor) + +function matches(word, letters, i, j, ch) { + for (i = 1; i <= length(letters); i++) { + ch = substr(letters, i, 1); + j = index(word, ch); + if (j > 0) + word = substr(word, 1, j-1) substr(word, j+1) + if (word == "") return 1; + } + return 0; +} + +BEGIN { + letters = tolower(ARGV[1]); + while ((getline word < "words.txt") > 0 ) { + if (length(word) >= 2 && + !(word ~ /[^a-zA-Z]/) && + matches(word, letters)) + print word; + } + exit 0; +} diff --git a/challenge-004/paulo-custodio/perl/ch-1.pl b/challenge-004/paulo-custodio/perl/ch-1.pl index 776f2286e9..356e712c44 100644 --- a/challenge-004/paulo-custodio/perl/ch-1.pl +++ b/challenge-004/paulo-custodio/perl/ch-1.pl @@ -3,7 +3,8 @@ # Challenge 004 # # Challenge #1 -# Write a script to output the same number of PI digits as the size of your script. Say, if your script size is 10, it should print 3.141592653. +# Write a script to output the same number of PI digits as the size of your script. +# Say, if your script size is 10, it should print 3.141592653. # # we need a big-math library to compute any large number of digits diff --git a/challenge-004/paulo-custodio/perl/ch-2.pl b/challenge-004/paulo-custodio/perl/ch-2.pl index 88e7e259b8..faaa875806 100644 --- a/challenge-004/paulo-custodio/perl/ch-2.pl +++ b/challenge-004/paulo-custodio/perl/ch-2.pl @@ -3,7 +3,12 @@ # Challenge 004 # # Challenge #2 -# You are given a file containing a list of words (case insensitive 1 word per line) and a list of letters. Print each word from the file that can be made using only letters from the list. You can use each letter only once (though there can be duplicates and you can use each of them once), you don’t have to use all the letters. (Disclaimer: The challenge was proposed by Scimon Proctor) +# You are given a file containing a list of words (case insensitive 1 word per +# line) and a list of letters. Print each word from the file that can be made +# using only letters from the list. You can use each letter only once (though +# there can be duplicates and you can use each of them once), you don't have to +# use all the letters. +# (Disclaimer: The challenge was proposed by Scimon Proctor) use strict; use warnings; @@ -22,7 +27,6 @@ while (<$fh>) { say $_ if matches($_, $letters); } - sub matches { my($word, $letters) = @_; for my $c (split //, $letters) { diff --git a/challenge-004/paulo-custodio/t/test-1.yaml b/challenge-004/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..f84bc1f9e2 --- /dev/null +++ b/challenge-004/paulo-custodio/t/test-1.yaml @@ -0,0 +1,9 @@ +- setup: + cleanup: + args: eval{-s $prog} + input: + output: | + |eval{ + | use Math::BigFloat; + | Math::BigFloat->bpi(-s $prog)."\n"; + |} diff --git a/challenge-004/paulo-custodio/t/test-2.yaml b/challenge-004/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..c9bcca786f --- /dev/null +++ b/challenge-004/paulo-custodio/t/test-2.yaml @@ -0,0 +1,19 @@ +- setup: 0==system("aspell -d en dump master | aspell -l en expand > words.txt"); + cleanup: unlink "words.txt" + args: world + input: + output: | + |or + |ow + |owl + |old + |lo + |low + |lord + |row + |rd + |rod + |do + |wold + |world + |word diff --git a/challenge-004/paulo-custodio/test.pl b/challenge-004/paulo-custodio/test.pl index 243a023078..a61c28ebb7 100644 --- a/challenge-004/paulo-custodio/test.pl +++ b/challenge-004/paulo-custodio/test.pl @@ -5,37 +5,4 @@ use warnings; use Test::More; use 5.030; -is capture("perl perl/ch-1.pl 20"), <<END; -3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367893 -END - -# build list of words for testing -ok 0==system("aspell -d en dump master | aspell -l en expand > words.txt"); - -is capture("perl perl/ch-2.pl world"), <<END; -or -ow -owl -old -lo -low -lord -row -rd -rod -do -wold -world -word -END - - -unlink "words.txt"; -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} +require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-005/paulo-custodio/awk/ch-1.awk b/challenge-005/paulo-custodio/awk/ch-1.awk new file mode 100644 index 0000000000..24251da5d9 --- /dev/null +++ b/challenge-005/paulo-custodio/awk/ch-1.awk @@ -0,0 +1,45 @@ +#!/usr/bin/gawk + +# Challenge 005 +# +# Challenge #1 +# Write a program which prints out all anagrams for a given word. For more +# information about Anagram, please check this wikipedia page. +# create a hash of all words in dictionary where key is sorted list of letters +# therefore two anagrams have the same key + +function alen(a, i, k) { + k = 0; + for(i in a) k++; + return k; +} + +function join(array, start, end, sep, result, i) { + if (sep == "") + sep = " "; + else if (sep == SUBSEP) # magic value + sep = ""; + result = array[start]; + for (i = start + 1; i <= end; i++) + result = result sep array[i]; + return result; +} + +function word_key(word, i, letters, key) { + for (i = 1; i <= length(word); i++) + letters[i] = substr(word, i, 1); + asort(letters); + key = join(letters, 1, alen(letters), SUBSEP); + return key; +} + +BEGIN { + word = tolower(ARGV[1]); + key = word_key(word); + while ((getline word < "words.txt") > 0 ) { + word = tolower(word); + if (word_key(word) == key) + print word; + } + exit 0; +} diff --git a/challenge-005/paulo-custodio/awk/ch-2.awk b/challenge-005/paulo-custodio/awk/ch-2.awk new file mode 100644 index 0000000000..93706dff9d --- /dev/null +++ b/challenge-005/paulo-custodio/awk/ch-2.awk @@ -0,0 +1,60 @@ +#!/usr/bin/gawk + +# Challenge 005 +# +# Challenge #2 +# Write a program to find the sequence of characters that has the most anagrams. +# +# create a hash of all words in dictionary where key is sorted list of letters +# therefore two anagrams have the same key + +function alen(a, i, k) { + k = 0; + for(i in a) k++; + return k; +} + +function join(array, start, end, sep, result, i) { + if (sep == "") + sep = " "; + else if (sep == SUBSEP) # magic value + sep = ""; + result = array[start]; + for (i = start + 1; i <= end; i++) + result = result sep array[i]; + return result; +} + +function word_key(word, i, letters, key) { + for (i = 1; i <= length(word); i++) + letters[i] = substr(word, i, 1); + asort(letters); + key = join(letters, 1, alen(letters), SUBSEP); + return key; +} + +BEGIN { + max_anagrams = 0; + while ((getline word < "words.txt") > 0) { + if (length(word) >= 2) { + if (!(word ~ /[^a-zA-Z]/)) { + key = word_key(tolower(word)); + num_anagrams = ++anagrams[key]; + if (num_anagrams > max_anagrams) + max_anagrams = num_anagrams; + } + } + } + + num_found = 0; + for (key in anagrams) { + if (anagrams[key] == max_anagrams) + found[num_found++] = key; + } + asort(found); + + print "Maximum of " max_anagrams " anagrams"; + for (i in found) + print found[i]; + exit 0; +} diff --git a/challenge-005/paulo-custodio/perl/ch-1.pl b/challenge-005/paulo-custodio/perl/ch-1.pl index 18fe86f780..c2dedd115b 100644 --- a/challenge-005/paulo-custodio/perl/ch-1.pl +++ b/challenge-005/paulo-custodio/perl/ch-1.pl @@ -3,11 +3,11 @@ # Challenge 005 # # Challenge #1 -# Write a program which prints out all anagrams for a given word. For more information about Anagram, please check this wikipedia page. +# Write a program which prints out all anagrams for a given word. For more +# information about Anagram, please check this wikipedia page. # create a hash of all words in dictionary where key is sorted list of letters # therefore two anagrams have the same key - use strict; use warnings; use 5.030; diff --git a/challenge-005/paulo-custodio/t/test-1.yaml b/challenge-005/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..63708186b7 --- /dev/null +++ b/challenge-005/paulo-custodio/t/test-1.yaml @@ -0,0 +1,30 @@ +- setup: 0==system("aspell -d en dump master | aspell -l en expand > words.txt"); + cleanup: + args: binary + input: + output: | + |binary + |brainy +- setup: + cleanup: + args: live + input: + output: | + |evil + |levi + |live + |veil + |vile +- setup: + cleanup: unlink "words.txt"; + args: casper + input: + output: | + |capers + |crapes + |parsec + |pacers + |recaps + |scrape + |spacer + |casper diff --git a/challenge-005/paulo-custodio/t/test-2.yaml b/challenge-005/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..f63585ea79 --- /dev/null +++ b/challenge-005/paulo-custodio/t/test-2.yaml @@ -0,0 +1,11 @@ +- setup: 0==system("aspell -d en dump master | aspell -l en expand > words.txt"); + cleanup: unlink "words.txt"; + args: + input: + output: | + |Maximum of 8 anagrams + |aceprs + |aels + |aelst + |aerst + |egor diff --git a/challenge-005/paulo-custodio/test.pl b/challenge-005/paulo-custodio/test.pl index 8952319125..a61c28ebb7 100644 --- a/challenge-005/paulo-custodio/test.pl +++ b/challenge-005/paulo-custodio/test.pl @@ -5,49 +5,4 @@ use warnings; use Test::More; use 5.030; -# build list of words for testing -ok 0==system("aspell -d en dump master | aspell -l en expand > words.txt"); - -is capture("perl perl/ch-1.pl binary"), <<END; -binary -brainy -END - -is capture("perl perl/ch-1.pl live"), <<END; -evil -levi -live -veil -vile -END - -is capture("perl perl/ch-1.pl casper"), <<END; -capers -crapes -parsec -pacers -recaps -scrape -spacer -casper -END - - -is capture("perl perl/ch-2.pl"), <<END; -Maximum of 8 anagrams -aceprs -aels -aelst -aerst -egor -END - -unlink "words.txt"; -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} +require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-098/paulo-custodio/awk/ch-1.awk b/challenge-098/paulo-custodio/awk/ch-1.awk index 4711a7babb..037675e073 100644 --- a/challenge-098/paulo-custodio/awk/ch-1.awk +++ b/challenge-098/paulo-custodio/awk/ch-1.awk @@ -2,7 +2,7 @@ # Challenge 098 # -# TASK #1 › Read N-characters +# TASK #1 > Read N-characters # Submitted by: Mohammad S Anwar # You are given file $FILE. # @@ -18,21 +18,20 @@ # read next N chars from file function readN(filename, read_len) { - skip_len = 0 - if (read_files[filename]) { - skip_len = read_files[filename] - } - read_files[filename] = skip_len + read_len + skip_len = 0; + if (read_files[filename]) + skip_len = read_files[filename]; + read_files[filename] = skip_len + read_len; - dd = "dd if=" filename " status=none bs=1 skip=" skip_len " count=" read_len - dd | getline text - return text + dd = "dd if=" filename " status=none bs=1 skip=" skip_len " count=" read_len; + dd | getline text; + return text; } BEGIN { for (i = 1; i < ARGC - 1; i += 2) { - text = readN(ARGV[i], ARGV[i+1]) - print text + text = readN(ARGV[i], ARGV[i+1]); + print text; } - exit |
