aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-202/spadacciniweb/perl/ch-2.pl4
-rw-r--r--challenge-203/spadacciniweb/bash/ch-2.bash12
-rw-r--r--challenge-203/spadacciniweb/go/ch-1.go73
-rw-r--r--challenge-203/spadacciniweb/go/ch-2.go101
-rw-r--r--challenge-203/spadacciniweb/ksh/ch-2.ksh14
-rw-r--r--challenge-203/spadacciniweb/perl/ch-1.pl55
-rw-r--r--challenge-203/spadacciniweb/perl/ch-2.pl84
-rw-r--r--challenge-203/spadacciniweb/python/ch-1.py51
-rw-r--r--challenge-203/spadacciniweb/python/ch-2.py63
9 files changed, 455 insertions, 2 deletions
diff --git a/challenge-202/spadacciniweb/perl/ch-2.pl b/challenge-202/spadacciniweb/perl/ch-2.pl
index 5676cf92bf..eb9ebe294f 100644
--- a/challenge-202/spadacciniweb/perl/ch-2.pl
+++ b/challenge-202/spadacciniweb/perl/ch-2.pl
@@ -33,8 +33,8 @@ my @input = @ARGV;
die "Input error\n"
if scalar @input < 1
or
- scalar map { $_ =~ /^\d+$/ ? () : 1 }
- @input != 0;
+ (scalar map { $_ =~ /^\d+$/ ? () : 1 }
+ @input) != 0;
my @valley;
foreach my $min (0..$#input) {
diff --git a/challenge-203/spadacciniweb/bash/ch-2.bash b/challenge-203/spadacciniweb/bash/ch-2.bash
new file mode 100644
index 0000000000..022c932d20
--- /dev/null
+++ b/challenge-203/spadacciniweb/bash/ch-2.bash
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+echo -n "path source: "
+read SOURCE
+
+[ -d "$SOURCE" ] || { echo "$SOURCE directory does not exist."; exit 1; }
+
+echo -n "path target: "
+read TARGET
+[ -d "$TARGET" ] || { echo "$TARGET directory does not exist."; exit 1; }
+
+rsync -av -f"+ */" -f"- *" $SOURCE/ $TARGET
diff --git a/challenge-203/spadacciniweb/go/ch-1.go b/challenge-203/spadacciniweb/go/ch-1.go
new file mode 100644
index 0000000000..945656d43d
--- /dev/null
+++ b/challenge-203/spadacciniweb/go/ch-1.go
@@ -0,0 +1,73 @@
+/*
+Task 1: Special Quadruplets
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers.
+Write a script to find out the total special quadruplets for the given array.
+
+Special Quadruplets are such that satisfies the following 2 rules.
+1) nums[a] + nums[b] + nums[c] == nums[d]
+2) a < b < c < d
+
+
+Example 1
+Input: @nums = (1,2,3,6)
+Output: 1
+Since the only special quadruplets found is $nums[0] + $nums[1] + $nums[2] == $nums[3].
+
+Example 2
+Input: @nums = (1,1,1,3,5)
+Output: 4
+$nums[0] + $nums[1] + $nums[2] == $nums[3]
+$nums[0] + $nums[1] + $nums[3] == $nums[4]
+$nums[0] + $nums[2] + $nums[3] == $nums[4]
+$nums[1] + $nums[2] + $nums[3] == $nums[4]
+
+Example 3
+Input: @nums = (3,3,6,4,5)
+Output: 0
+*/
+
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+)
+
+type quad struct {
+ i, j, k, z int
+}
+
+func main() {
+ arrStr := os.Args[1:]
+ if (len(arrStr) < 4) {
+ log.Fatal("input error")
+ }
+
+ arrInt := make([]int, 0)
+ for i := 0; i <= len(arrStr)-1; i++ {
+ value, err := strconv.Atoi(arrStr[i])
+ if (err != nil) {
+ log.Fatal(err)
+ }
+ arrInt = append(arrInt, value)
+ }
+
+ quadruplets := make([]quad, 0)
+ for i := 0; i <= len(arrInt)-4; i++ {
+ for j := i+1; j <= len(arrInt)-3; j++ {
+ for k := j+1; k <= len(arrInt)-2; k++ {
+ for z := k+1; z <= len(arrInt)-1; z++ {
+ if arrInt[i] + arrInt[j] + arrInt[k] == arrInt[z] {
+ quadruplets = append(quadruplets, quad{i, j, k, z})
+ }
+ }
+ }
+ }
+ }
+
+ fmt.Println(len(quadruplets))
+}
diff --git a/challenge-203/spadacciniweb/go/ch-2.go b/challenge-203/spadacciniweb/go/ch-2.go
new file mode 100644
index 0000000000..98f4873535
--- /dev/null
+++ b/challenge-203/spadacciniweb/go/ch-2.go
@@ -0,0 +1,101 @@
+/*
+Task 2: Copy Directory
+Submitted by: Julien Fiegehenn
+
+You are given path to two folders, $source and $target.
+Write a script that recursively copy the directory from $source to $target except any files.
+
+Example
+Input: $source = '/a/b/c' and $target = '/x/y'
+
+Source directory structure:
+
+├── a
+│ └── b
+│ └── c
+│ ├── 1
+│ │ └── 1.txt
+│ ├── 2
+│ │ └── 2.txt
+│ ├── 3
+│ │ └── 3.txt
+│ ├── 4
+│ └── 5
+│ └── 5.txt
+
+Target directory structure:
+
+├── x
+│ └── y
+
+Expected Result:
+
+├── x
+│ └── y
+| ├── 1
+│ ├── 2
+│ ├── 3
+│ ├── 4
+│ └── 5
+*/
+
+package main
+
+import (
+ "fmt"
+ "log"
+ "path/filepath"
+ "os"
+ "strings"
+)
+
+func main() {
+ var dirs []string
+ var source string
+ var target string
+
+ fmt.Print("path source: ")
+ fmt.Scanf("%s", &source)
+
+ fileInfo, err := os.Stat(source)
+ if err != nil {
+ log.Fatal(err)
+ } else if !fileInfo.IsDir() {
+ log.Fatalf("%s directory does not exist.", source)
+ }
+
+ fmt.Print("path target: ")
+ fmt.Scanf("%s", &target)
+
+ fileInfo, err = os.Stat(target)
+ if err != nil {
+ log.Fatal(err)
+ } else if !fileInfo.IsDir() {
+ log.Fatalf("%s directory does not exist.", target)
+ }
+
+ err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
+ if err != nil {
+ return err
+ }
+ if info.IsDir() {
+ if len(path) > len(source) {
+ dirFmt := path[len(source)+1:]
+ dirs = append(dirs, dirFmt)
+ }
+ }
+ return err
+ })
+
+ if err != nil {
+ log.Println(err)
+ }
+
+ for _, currDir := range dirs {
+ outDir := strings.Join( []string{target, currDir}, "/")
+ err := os.Mkdir(outDir, 0750)
+ if err != nil && !os.IsExist(err) {
+ log.Fatal(err)
+ }
+ }
+}
diff --git a/challenge-203/spadacciniweb/ksh/ch-2.ksh b/challenge-203/spadacciniweb/ksh/ch-2.ksh
new file mode 100644
index 0000000000..768123ecac
--- /dev/null
+++ b/challenge-203/spadacciniweb/ksh/ch-2.ksh
@@ -0,0 +1,14 @@
+#!/usr/bin/ksh
+
+echo -n "path source: "
+read SOURCE
+
+[ -d "$SOURCE" ] || { echo "$SOURCE directory does not exist."; exit 1; }
+
+echo -n "path target: "
+read TARGET
+[ -d "$TARGET" ] || { echo "$TARGET directory does not exist."; exit 1; }
+
+cd $SOURCE
+find . -type d -exec mkdir -p "$TARGET/{}" \;
+cd -
diff --git a/challenge-203/spadacciniweb/perl/ch-1.pl b/challenge-203/spadacciniweb/perl/ch-1.pl
new file mode 100644
index 0000000000..72413345de
--- /dev/null
+++ b/challenge-203/spadacciniweb/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/env perl
+
+# Task 1: Special Quadruplets
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers.
+# Write a script to find out the total special quadruplets for the given array.
+#
+# Special Quadruplets are such that satisfies the following 2 rules.
+# 1) nums[a] + nums[b] + nums[c] == nums[d]
+# 2) a < b < c < d
+#
+#
+# Example 1
+# Input: @nums = (1,2,3,6)
+# Output: 1
+#
+# Since the only special quadruplets found is $nums[0] + $nums[1] + $nums[2] == $nums[3].
+#
+# Example 2
+# Input: @nums = (1,1,1,3,5)
+# Output: 4
+#
+# $nums[0] + $nums[1] + $nums[2] == $nums[3]
+# $nums[0] + $nums[1] + $nums[3] == $nums[4]
+# $nums[0] + $nums[2] + $nums[3] == $nums[4]
+# $nums[1] + $nums[2] + $nums[3] == $nums[4]
+#
+# Example 3
+# Input: @nums = (3,3,6,4,5)
+# Output: 0
+
+use strict;
+use warnings;
+
+my @input = @ARGV;
+die "Input error\n"
+ if scalar @input < 4
+ or
+ (scalar map { $_ =~ /^\d+$/ ? () : 1 }
+ @input) != 0;
+
+my @quadruplets = ();
+foreach my $i (0..$#input-3) {
+ foreach my $j ($i+1..$#input-2) {
+ foreach my $k ($j+1..$#input-1) {
+ foreach my $z ($k+1..$#input) {
+ push @quadruplets, [$i, $j, $k, $z]
+ if $input[$i] + $input[$j] + $input[$k] == $input[$z];
+ }
+ }
+ }
+}
+
+print scalar @quadruplets, "\n"
diff --git a/challenge-203/spadacciniweb/perl/ch-2.pl b/challenge-203/spadacciniweb/perl/ch-2.pl
new file mode 100644
index 0000000000..d41c813b8a
--- /dev/null
+++ b/challenge-203/spadacciniweb/perl/ch-2.pl
@@ -0,0 +1,84 @@
+#!/usr/bin/env perl
+
+# Task 2: Copy Directory
+# Submitted by: Julien Fiegehenn
+#
+# You are given path to two folders, $source and $target.
+# Write a script that recursively copy the directory from $source to $target except any files.
+#
+# Example
+# Input: $source = '/a/b/c' and $target = '/x/y'
+#
+# Source directory structure:
+#
+# ├── a
+# │ └── b
+# │ └── c
+# │ ├── 1
+# │ │ └── 1.txt
+# │ ├── 2
+# │ │ └── 2.txt
+# │ ├── 3
+# │ │ └── 3.txt
+# │ ├── 4
+# │ └── 5
+# │ └── 5.txt
+#
+# Target directory structure:
+#
+# ├── x
+# │ └── y
+#
+# Expected Result:
+#
+# ├── x
+# │ └── y
+# | ├── 1
+# │ ├── 2
+# │ ├── 3
+# │ ├── 4
+# │ └── 5
+
+use strict;
+use warnings;
+use File::Find;
+no warnings 'experimental::smartmatch';
+
+print "path source: ";
+my $source = <>;
+chop $source;
+unless (-d $source) {
+ printf "%s directory does not exist.", $source || '-';
+ exit 1;
+}
+
+print "path target: ";
+my $target = <>;
+chop $target;
+unless (-d $target) {
+ printf "%s directory does not exist.", $target || '-';
+ exit 1;
+}
+
+my @dirs;
+find({ wanted => \&finddirs }, $source);
+
+foreach my $dir (@dirs) {
+ mkdir $target . $dir or die($!);
+}
+
+exit 0;
+
+sub add_dir {
+ my $dir = shift;
+ if (-d $dir) {
+ $dir = substr $dir, length($source);
+ push @dirs, $dir
+ if $dir and
+ not($dir ~~ @dirs);
+ }
+}
+
+sub finddirs {
+ add_dir($File::Find::name);
+}
diff --git a/challenge-203/spadacciniweb/python/ch-1.py b/challenge-203/spadacciniweb/python/ch-1.py
new file mode 100644
index 0000000000..bd7860c779
--- /dev/null
+++ b/challenge-203/spadacciniweb/python/ch-1.py
@@ -0,0 +1,51 @@
+# Task 1: Special Quadruplets
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers.
+# Write a script to find out the total special quadruplets for the given array.
+#
+# Special Quadruplets are such that satisfies the following 2 rules.
+# 1) nums[a] + nums[b] + nums[c] == nums[d]
+# 2) a < b < c < d
+#
+#
+# Example 1
+# Input: @nums = (1,2,3,6)
+# Output: 1
+#
+# Since the only special quadruplets found is $nums[0] + $nums[1] + $nums[2] == $nums[3].
+#
+# Example 2
+# Input: @nums = (1,1,1,3,5)
+# Output: 4
+#
+# $nums[0] + $nums[1] + $nums[2] == $nums[3]
+# $nums[0] + $nums[1] + $nums[3] == $nums[4]
+# $nums[0] + $nums[2] + $nums[3] == $nums[4]
+# $nums[1] + $nums[2] + $nums[3] == $nums[4]
+#
+# Example 3
+# Input: @nums = (3,3,6,4,5)
+# Output: 0
+
+import re
+import sys
+
+if __name__ == "__main__":
+ input = sys.argv[1:]
+ if (len(input) < 4 or
+ len(list(filter(lambda x: re.search(r'\D', x), input))) > 0 ):
+ sys.exit("Input error\n")
+
+ input = list(map(int, input))
+ output = 0
+
+ quadruplets = []
+ for i in range(0, len(input)-3):
+ for j in range(i+1, len(input)-2):
+ for k in range(j+1, len(input)-1):
+ for z in range(k+1, len(input)):
+ if input[i] + input[j] + input[k] == input[z]:
+ quadruplets.append([i,j,k,z])
+
+ print(len(quadruplets));
diff --git a/challenge-203/spadacciniweb/python/ch-2.py b/challenge-203/spadacciniweb/python/ch-2.py
new file mode 100644
index 0000000000..52ffb23269
--- /dev/null
+++ b/challenge-203/spadacciniweb/python/ch-2.py
@@ -0,0 +1,63 @@
+# Task 2: Copy Directory
+# Submitted by: Julien Fiegehenn
+#
+# You are given path to two folders, $source and $target.
+# Write a script that recursively copy the directory from $source to $target except any files.
+#
+# Example
+# Input: $source = '/a/b/c' and $target = '/x/y'
+#
+# Source directory structure:
+#
+# ├── a
+# │ └── b
+# │ └── c
+# │ ├── 1
+# │ │ └── 1.txt
+# │ ├── 2
+# │ │ └── 2.txt
+# │ ├── 3
+# │ │ └── 3.txt
+# │ ├── 4
+# │ └── 5
+# │ └── 5.txt
+#
+# Target directory structure:
+#
+# ├── x
+# │ └── y
+#
+# Expected Result:
+#
+# ├── x
+# │ └── y
+# | ├── 1
+# │ ├── 2
+# │ ├── 3
+# │ ├── 4
+# │ └── 5
+
+import os
+import re
+import sys
+
+source = input("path source: ")
+if not(os.path.exists(source)):
+ sys.exit("{0} directory does not exist.\n".format(source))
+
+target = input("path target: ")
+if not(os.path.exists(target)):
+ sys.exit("{0} directory does not exist.\n".format(target))
+
+dirs = []
+
+for path, subdirs, files in os.walk(source):
+ for subdir in subdirs:
+ if len(path) == len(source):
+ dirs.append(subdir)
+ else:
+ dirs.append( os.path.join(path[len(source)+1:], subdir))
+
+for dir in dirs:
+ path = os.path.join(target, dir)
+ os.mkdir(path)