aboutsummaryrefslogtreecommitdiff
path: root/challenge-098
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-02-14 16:32:59 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-02-14 16:32:59 +0000
commitef14c20e2e2d61abe4cc73e5389733122df02d42 (patch)
treed4346068d07aac04bae6094c595e1350fa6fe2c2 /challenge-098
parent40abdaf0147a405f36b5d43d7919eda15e131a3d (diff)
downloadperlweeklychallenge-club-ef14c20e2e2d61abe4cc73e5389733122df02d42.tar.gz
perlweeklychallenge-club-ef14c20e2e2d61abe4cc73e5389733122df02d42.tar.bz2
perlweeklychallenge-club-ef14c20e2e2d61abe4cc73e5389733122df02d42.zip
Add Awk solution to challenge 004
Fix Awk syntax on previouws submissions - semicolon is needed at the end of statements
Diffstat (limited to 'challenge-098')
-rw-r--r--challenge-098/paulo-custodio/awk/ch-1.awk23
-rw-r--r--challenge-098/paulo-custodio/awk/ch-2.awk47
2 files changed, 34 insertions, 36 deletions
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 0
+ exit 0;
}
diff --git a/challenge-098/paulo-custodio/awk/ch-2.awk b/challenge-098/paulo-custodio/awk/ch-2.awk
index cbb40fd7dc..65b0234116 100644
--- a/challenge-098/paulo-custodio/awk/ch-2.awk
+++ b/challenge-098/paulo-custodio/awk/ch-2.awk
@@ -2,7 +2,7 @@
# Challenge 098
#
-# TASK #2 › Search Insert Position
+# TASK #2 > Search Insert Position
# Submitted by: Mohammad S Anwar
# You are given a sorted array of distinct integers @N and a target $N.
#
@@ -27,22 +27,21 @@
# print contents of array (nums, nums_size)
function print_array() {
- sep = ""
+ sep = "";
printf "("
for (i=0; i<nums_size; i++) {
- printf sep nums[i]
- sep = ", "
+ printf sep nums[i];
+ sep = ", ";
}
- print ")"
+ print ")";
}
# insert an element in (nums, nums_size), shifting all other positions up
function insert(p, n) {
- nums_size++
- for (i=nums_size-1; i>p; i--) {
- nums[i] = nums[i-1]
- }
- nums[p] = n
+ nums_size++;
+ for (i=nums_size-1; i>p; i--)
+ nums[i] = nums[i-1];
+ nums[p] = n;
}
# use bisect method to search for position
@@ -52,7 +51,7 @@ function search_insert(n) {
return 0;
}
else if (n < nums[0]) { # before first
- insert(0, n)
+ insert(0, n);
return 0;
}
else if (n > nums[nums_size-1]) {# after last
@@ -60,33 +59,33 @@ function search_insert(n) {
return nums_size-1;
}
else { # bisect
- bot = 0; top = nums_size
- mid = int((top + bot) / 2)
+ bot = 0; top = nums_size;
+ mid = int((top + bot) / 2);
while (bot+1 < top) {
if (n == nums[mid])
return mid;
else if (n < nums[mid])
- top = mid
+ top = mid;
else
- bot = mid
+ bot = mid;
mid = int((top + bot) / 2)
}
# not found, insert at mid+1
- insert(mid+1, n)
- return mid+1
+ insert(mid+1, n);
+ return mid+1;
}
}
BEGIN {
- n = ARGV[1]
- nums_size = 0 # nums[] size
+ n = ARGV[1];
+ nums_size = 0; # nums[] size
for (i = 2; i < ARGC; i++) {
- nums[nums_size++] = ARGV[i]
+ nums[nums_size++] = ARGV[i];
}
- p = search_insert(n)
- print p
- print_array()
- exit 0
+ p = search_insert(n);
+ print p;
+ print_array();
+ exit 0;
}