diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-06-27 20:22:30 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-06-27 20:22:30 +0100 |
| commit | 287d70fb03bbb2ba51e636cf051775400e94bcb4 (patch) | |
| tree | 85a39b2a331bbb5b49d17f63ae76796f3999e7ce /challenge-118 | |
| parent | 9beb73e6913f7e462c4aca8719048a7be98e4ad2 (diff) | |
| parent | 1b3142a75a8db8a886596770514e2d9fa1cd9187 (diff) | |
| download | perlweeklychallenge-club-287d70fb03bbb2ba51e636cf051775400e94bcb4.tar.gz perlweeklychallenge-club-287d70fb03bbb2ba51e636cf051775400e94bcb4.tar.bz2 perlweeklychallenge-club-287d70fb03bbb2ba51e636cf051775400e94bcb4.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-118')
86 files changed, 2351 insertions, 78 deletions
diff --git a/challenge-118/abigail/README.md b/challenge-118/abigail/README.md index 7db94b7f39..ba9aad7a0c 100644 --- a/challenge-118/abigail/README.md +++ b/challenge-118/abigail/README.md @@ -1,27 +1,20 @@ # Solutions by Abigail -## [Missing Row](https://perlweeklychallenge.org/blog/perl-weekly-challenge-117/#TASK1) +## [Binary Palindrome](https://perlweeklychallenge.org/blog/perl-weekly-challenge-118/#TASK1) -> You are given text file with rows numbered `1-15` in random order but -> there is a catch one row in missing in the file. +> You are given a positive integer $N. +> +> Write a script to find out if the binary representation of the given +> integer is Palindrome. Print 1 if it is otherwise 0. +> +### Examples ~~~~ -11, Line Eleven -1, Line one -9, Line Nine -13, Line Thirteen -2, Line two -6, Line Six -8, Line Eight -10, Line Ten -7, Line Seven -4, Line Four -14, Line Fourteen -3, Line three -15, Line Fifteen -5, Line Five -~~~~ +Input: $N = 5 +Output: 1 as binary representation of 5 is 101 which is Palindrome. -Write a script to find the missing row number. +Input: $N = 4 +Output: 0 as binary representation of 4 is 100 which is NOT Palindrome. +~~~~ ### Solutions * [AWK](awk/ch-1.awk) @@ -36,54 +29,65 @@ Write a script to find the missing row number. * [Ruby](ruby/ch-1.rb) ### Blog -[Missing Row](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-117-1.html) +[Binary Palindrome](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-118-1.html) -## [Find Possible Paths](https://perlweeklychallenge.org/blog/perl-weekly-challenge-115/#TASK2) +## [Adventure of Knight](https://perlweeklychallenge.org/blog/perl-weekly-challenge-118/#TASK2) -> You are given size of a triangle. -> -> Write a script to find all possible paths from top to the bottom -> right corner. -> -> In each step, we can either move horizontally to the right (`H`), or -> move downwards to the left (`L`) or right (`R`). +> A knight is restricted to move on an 8x8 chessboard. The knight is denoted +> by `N` and its way of movement is the same as what it is defined in Chess. +> `*` represents an empty square. `x` represents a square with treasure. +> +> > The Knight's movement is unique. It may move two squares vertically and +> > one square horizontally, or two squares horizontally and one square +> > vertically (with both forming the shape of an L). +> +> There are 6 squares with treasures. +> +> Write a script to find the path such that Knight can capture all +> treasures. The Knight can start from the top-left square. > -> BONUS: Try if it can handle triangle of size `10` or `20`. - -### Examples -~~~~ -Input: $N = 2 - - S - / \ - / _ \ - /\ /\ - /__\ /__\ E - -Output: RR, LHR, LHLH, LLHH, RLH, LRH -~~~~ - ~~~~ -Input: $N = 1 - - S - / \ - / _ \ E - -Output: R, LH + a b c d e f g h + 8 N * * * * * * * 8 + 7 * * * * * * * * 7 + 6 * * * * x * * * 6 + 5 * * * * * * * * 5 + 4 * * x * * * * * 4 + 3 * x * * * * * * 3 + 2 x x * * * * * * 2 + 1 * x * * * * * * 1 + a b c d e f g h ~~~~ ### Solutions * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) +* [Basic](basic/ch-2.bas) +* [bc](bc/ch-2.bc) +* [Befunge-93](befunge-93/ch-2.bf93) * [C](c/ch-2.c) +* [Cobol](cobol/ch-2.cb) +* [Csh](csh/ch-2.csh) +* [Erlang](erlang/ch-2.erl] +* [Forth](forth/ch-2.fs] +* [Fortran](fortran/ch-2.f90] * [Go](go/ch-2.go) * [Java](java/ch-1.java) * [Lua](lua/ch-2.lua) +* [m4](m4/ch-2.m4] * [Node.js](node/ch-2.js) +* [OCaml](ocaml/ch-2.ml] +* [Pascal](pascal/ch-2.p] * [Perl](perl/ch-2.pl) +* [PHP](php/ch-2.php] +* [PostScript](postscript/ch-2.ps] * [Python](python/ch-2.py) +* [R](r/ch-2.r] +* [Rexx](rexx/ch-2.rexx] * [Ruby](ruby/ch-2.rb) +* [Scheme](scheme/ch-2.scm] +* [sed](sed/ch-2.sed] +* [Tcl](tcl/ch-2.tcl] ### Blog -[Find Possible Paths](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-117-2.html) +[Adventure of Knight](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-118-2.html) diff --git a/challenge-118/abigail/awk/ch-1.awk b/challenge-118/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..490a5ceefc --- /dev/null +++ b/challenge-118/abigail/awk/ch-1.awk @@ -0,0 +1,36 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +# +# Get a binary representation +# +function dec2bin (dec, bin) { + while (dec) { + bin = dec % 2 bin + dec = int (dec / 2) + } + return (bin) +} + +{ + bin = dec2bin($1) + l = length (bin) + # + # Check if it's a palindrome + # + for (i = 1; i < l / 2; i ++) { + if (substr (bin, i, 1) != substr (bin, l - i + 1, 1)) { + print (0) + next + } + } + print (1) +} + diff --git a/challenge-118/abigail/awk/ch-2.awk b/challenge-118/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..d4cf905f2e --- /dev/null +++ b/challenge-118/abigail/awk/ch-2.awk @@ -0,0 +1,14 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk +# + +BEGIN { + print "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" +} + diff --git a/challenge-118/abigail/bash/ch-1.sh b/challenge-118/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..873decd636 --- /dev/null +++ b/challenge-118/abigail/bash/ch-1.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +function dec2bin () { + dec=$1 + bin="" + while ((dec > 0)) + do bin=$((dec % 2))$bin + ((dec /= 2)) + done +} + +while read dec +do dec2bin $dec + for ((i = 0; i < ${#bin} / 2; i ++)) + do if [ "${bin:$i:1}" = "${bin:$((${#bin} - i - 1)):1}" ] + then continue + fi + echo 0 + continue 2 + done + echo 1 +done diff --git a/challenge-118/abigail/bash/ch-2.sh b/challenge-118/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..e48430410e --- /dev/null +++ b/challenge-118/abigail/bash/ch-2.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh +# + +echo "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" diff --git a/challenge-118/abigail/basic/ch-2.bas b/challenge-118/abigail/basic/ch-2.bas new file mode 100644 index 0000000000..8e07ba8840 --- /dev/null +++ b/challenge-118/abigail/basic/ch-2.bas @@ -0,0 +1,9 @@ +010 REM +020 REM See ../README.md +030 REM + +040 REM +050 REM Run as: basic ch-2.bas +060 REM + +100 PRINT "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" diff --git a/challenge-118/abigail/bc/ch-2.bc b/challenge-118/abigail/bc/ch-2.bc new file mode 100644 index 0000000000..5125d6a827 --- /dev/null +++ b/challenge-118/abigail/bc/ch-2.bc @@ -0,0 +1,10 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-2.bc +# +"a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2 +" +quit diff --git a/challenge-118/abigail/befunge-93/ch-2.bf93 b/challenge-118/abigail/befunge-93/ch-2.bf93 new file mode 100644 index 0000000000..b31e3efee9 --- /dev/null +++ b/challenge-118/abigail/befunge-93/ch-2.bf93 @@ -0,0 +1,2 @@ +< v,_@#:< "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" +55 + > ^ diff --git a/challenge-118/abigail/blog.txt b/challenge-118/abigail/blog.txt new file mode 100644 index 0000000000..80b7af5f39 --- /dev/null +++ b/challenge-118/abigail/blog.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-118-1.html diff --git a/challenge-118/abigail/blog1.txt b/challenge-118/abigail/blog1.txt new file mode 100644 index 0000000000..f34b88bb1f --- /dev/null +++ b/challenge-118/abigail/blog1.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-118-2.html diff --git a/challenge-118/abigail/c/ch-1.c b/challenge-118/abigail/c/ch-1.c new file mode 100644 index 0000000000..637c938d0e --- /dev/null +++ b/challenge-118/abigail/c/ch-1.c @@ -0,0 +1,42 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + + +int main (void) { + long long dec; + while (scanf ("%lld", &dec) == 1) { + /* + * Find the largest power of 2 smaller than dec; + * this is the position of the leading 1. + */ + long long i = 1; + int k = 0; + for (k = 0; i <= dec; k ++, i = i << 1); + /* + * We overshot by 1 + */ + k -= 1; + + /* + * Now, compare the bits + */ + int is_palin = 1; + for (int j = 0; j < k; k --, j ++) { + if (((dec & (1 << j)) >> j) != ((dec & (1 << k)) >> k)) { + is_palin = 0; + break; + } + } + printf ("%d\n", is_palin); + } + return (0); +} diff --git a/challenge-118/abigail/c/ch-2.c b/challenge-118/abigail/c/ch-2.c new file mode 100644 index 0000000000..74531ee18d --- /dev/null +++ b/challenge-118/abigail/c/ch-2.c @@ -0,0 +1,16 @@ +# include <stdlib.h> +# include <stdio.h> +# include <st |
