diff options
| author | Michael Manring <michael@manring> | 2022-08-13 14:18:53 +0700 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2022-08-13 17:29:40 +0700 |
| commit | 52efccfa9fdf1bd16074eab14ab18e8b93be3ace (patch) | |
| tree | 677c38b9c13973b07d46e2006c4c55d3ee9f745d /challenge-138 | |
| parent | df8382730840de4191d525d5dfc506b2b3f4a58e (diff) | |
| download | perlweeklychallenge-club-52efccfa9fdf1bd16074eab14ab18e8b93be3ace.tar.gz perlweeklychallenge-club-52efccfa9fdf1bd16074eab14ab18e8b93be3ace.tar.bz2 perlweeklychallenge-club-52efccfa9fdf1bd16074eab14ab18e8b93be3ace.zip | |
pwc138 solution in go and dart
Diffstat (limited to 'challenge-138')
| -rw-r--r-- | challenge-138/pokgopun/README | 1 | ||||
| -rw-r--r-- | challenge-138/pokgopun/dart/ch-1.dart | 20 | ||||
| -rw-r--r-- | challenge-138/pokgopun/dart/ch-2.dart | 40 | ||||
| -rw-r--r-- | challenge-138/pokgopun/go/ch-1.go | 26 | ||||
| -rw-r--r-- | challenge-138/pokgopun/go/ch-2.go | 52 |
5 files changed, 139 insertions, 0 deletions
diff --git a/challenge-138/pokgopun/README b/challenge-138/pokgopun/README new file mode 100644 index 0000000000..33dfd303a4 --- /dev/null +++ b/challenge-138/pokgopun/README @@ -0,0 +1 @@ +Solution by PokGoPun diff --git a/challenge-138/pokgopun/dart/ch-1.dart b/challenge-138/pokgopun/dart/ch-1.dart new file mode 100644 index 0000000000..286299bc0a --- /dev/null +++ b/challenge-138/pokgopun/dart/ch-1.dart @@ -0,0 +1,20 @@ +void main(List<String> args) { + int year = args.isNotEmpty && int.tryParse(args[0]) != null + ? int.parse(args[0]) + : 2021; + int workdays = + 52 * 5; // non-leap year has 52 weeks which yeilds 52 * 5 workdays + int daysLeft = + 1; // day(s) left from 52 weeks to check for a workday is 365 - ( 52 * 7 ) = 1 + if (year % 4 == 0) { + daysLeft++; // leap year will have another day + } + // find another workday in daysLeft + var t = DateTime(year, DateTime.december, 31); + for (int d = 0; d < daysLeft; d++) { + if (t.subtract(Duration(days: d)).weekday < DateTime.saturday) { + workdays++; + } + } + print('Input: year = $year\nOutput: $workdays'); +} diff --git a/challenge-138/pokgopun/dart/ch-2.dart b/challenge-138/pokgopun/dart/ch-2.dart new file mode 100644 index 0000000000..8c362b813b --- /dev/null +++ b/challenge-138/pokgopun/dart/ch-2.dart @@ -0,0 +1,40 @@ +import "dart:math"; + +void main(List<String> args) { + int n = args.isNotEmpty && int.tryParse(args[0]) != null + ? int.parse(args[0]) + : 9801; + print('Input: n = $n\nOutput: ${isSplitNum(n)}'); +} + +List<String> numSplit(String s, String t, List<String> res) { + var l = s.length; + if (l > 0) { + for (int i = 0; i < l; i++) { + numSplit(s.substring(i + 1), <String>[t, s.substring(0, i + 1)].join("+"), + res); + } + } else { + res.add(t.substring(1)); + return <String>[]; + } + return res; +} + +bool isSplitNum(int n) { + int nSqrt = sqrt(n).toInt(); + if (n != nSqrt * nSqrt) { + return false; + } + for (var splits in numSplit(n.toString(), "", <String>[])) { + if (splits + .split("+") + .map((str) => int.parse(str)) + .fold<int>(0, (prev, elem) => prev + elem) == + nSqrt) { + //print('Since sqrt($n) = $nSqrt = $splits'); + return true; + } + } + return false; +} diff --git a/challenge-138/pokgopun/go/ch-1.go b/challenge-138/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..60f8b70a78 --- /dev/null +++ b/challenge-138/pokgopun/go/ch-1.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "os" + "strings" + "time" +) + +func main() { + year := 2021 + fmt.Sscanf(strings.Join(os.Args[1:], " "), "%d", &year) + workdays := 52 * 5 // every year has at least 52 weeks each of which has 5 work days + daysLeft := 1 // non-leap year has 365 - (52 - 7) = 1 day left to check if it is a working day + if year%4 == 0 { // a leap year will add another day to daysLeft + daysLeft++ + } + // check if there a workday in daysLeft, start at the end of the year + t := time.Date(year, time.December, 31, 0, 0, 0, 0, time.UTC) + for d := 0; d < daysLeft; d++ { + if wd := t.AddDate(0, 0, -d).Weekday(); wd > time.Sunday && wd < time.Saturday { + workdays++ + } + } + fmt.Printf("Input: year = %d\nOutput: %d\n", year, workdays) +} diff --git a/challenge-138/pokgopun/go/ch-2.go b/challenge-138/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..5e0ecc82a8 --- /dev/null +++ b/challenge-138/pokgopun/go/ch-2.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "math" + "os" + "strconv" + "strings" +) + +func main() { + n := 9801 + fmt.Sscanf(strings.Join(os.Args[1:], " "), "%d", &n) + fmt.Printf("Input: n = %d\nOutput: %t\n", n, isSplitNum(n)) +} +func isSplitNum(n int) bool { + sqrt := int(math.Sqrt(float64(n))) + if n != sqrt*sqrt { + return false + } + nStr := strconv.Itoa(n) + var res string + splits := strings.TrimSuffix(numSplit(nStr, "", &res), " "+nStr) + for _, v := range strings.Split(splits, " ") { + if sumSplit(v) == sqrt { + //fmt.Printf("Since, sqrt(%d) = %d = %s\n", n, sqrt, v) + return true + } + } + return false +} + +func numSplit(s, t string, res *string) string { + l := len(s) + if l > 0 { + for i := 0; i < l; i++ { + numSplit(s[i+1:], strings.Join([]string{t, s[:i+1]}, "+"), res) + } + } else { + *res += " " + t[1:] + return "" + } + return (*res)[1:] +} + +func sumSplit(str string) (sum int) { + for _, v := range strings.Split(str, "+") { + n, _ := strconv.Atoi(v) + sum += n + } + return sum +} |
