diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-06-27 01:20:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-27 01:20:19 +0100 |
| commit | e915b79d21fd9050d0141c7c0463e1b1149ccff2 (patch) | |
| tree | 7cc69145eea0848b35d46c4ade1c13df1f5cd0d8 /challenge-118 | |
| parent | 0d0d5481ca9a3f18403eb433a8b00df549892773 (diff) | |
| parent | d9042f20619d9a9072dcfd3a1ba88b8b76457eb2 (diff) | |
| download | perlweeklychallenge-club-e915b79d21fd9050d0141c7c0463e1b1149ccff2.tar.gz perlweeklychallenge-club-e915b79d21fd9050d0141c7c0463e1b1149ccff2.tar.bz2 perlweeklychallenge-club-e915b79d21fd9050d0141c7c0463e1b1149ccff2.zip | |
Merge pull request #4346 from Abigail/abigail/week-118
Abigail/week 118
Diffstat (limited to 'challenge-118')
49 files changed, 829 insertions, 51 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 <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o + */ + +int main (void) { + printf ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2\n"); + exit (0); +} diff --git a/challenge-118/abigail/cobol/ch-2.cb b/challenge-118/abigail/cobol/ch-2.cb new file mode 100644 index 0000000000..a756c0f8d2 --- /dev/null +++ b/challenge-118/abigail/cobol/ch-2.cb @@ -0,0 +1,14 @@ +IDENTIFICATION DIVISION. +PROGRAM-ID. XXX. + +*> +*> See ../README.md +*> + +*> +*> Run as: cobc -xF -o ch-2.o ch-2.cb; ./ch-2.o +*> + +PROCEDURE DIVISION. + DISPLAY "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2". + STOP RUN. diff --git a/challenge-118/abigail/csh/ch-2.csh b/challenge-118/abigail/csh/ch-2.csh new file mode 100644 index 0000000000..bdcc9cee6d --- /dev/null +++ b/challenge-118/abigail/csh/ch-2.csh @@ -0,0 +1,11 @@ +#!/bin/csh + +# +# See ../README.md +# + +# +# Run as: csh ch-2.csh +# + +echo "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" diff --git a/challenge-118/abigail/erlang/ch-2.erl b/challenge-118/abigail/erlang/ch-2.erl new file mode 100644 index 0000000000..8357a62e7d --- /dev/null +++ b/challenge-118/abigail/erlang/ch-2.erl @@ -0,0 +1,15 @@ +% +% See ../README.md +% + +% +% Run as: ln ch-2.erl ch2.erl +% erl -compile ch2 +% erl -noshell -s ch2 main -s init stop +% + +-module (ch2). +-export ([main/0]). + +main () -> + io:fwrite ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2\n"). diff --git a/challenge-118/abigail/forth/ch-2.fs b/challenge-118/abigail/forth/ch-2.fs new file mode 100644 index 0000000000..adf08a7137 --- /dev/null +++ b/challenge-118/abigail/forth/ch-2.fs @@ -0,0 +1,5 @@ +\ +\ See ../README.md +\ + +.( a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2) diff --git a/challenge-118/abigail/fortran/ch-2.f90 b/challenge-118/abigail/fortran/ch-2.f90 new file mode 100644 index 0000000000..bb8c2851b0 --- /dev/null +++ b/challenge-118/abigail/fortran/ch-2.f90 @@ -0,0 +1,12 @@ +! +! See ../README.md +! + +! +! Run as: gfortran -o ch-2.o ch-2.f90; ./ch-2.o +! + +program ch2 + implicit none + write (*, *) "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" +end diff --git a/challenge-118/abigail/go/ch-1.go b/challenge-118/abigail/go/ch-1.go new file mode 100644 index 0000000000..466a5e574f --- /dev/null +++ b/challenge-118/abigail/go/ch-1.go @@ -0,0 +1,41 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" + "strconv" +) + +func reverse (str string) string { + rev := [] rune (str) + for i, j := 0, len (rev) - 1; i < j; i, j = i + 1, j - 1 { + rev [i], rev [j] = rev [j], rev [i] + } + return string (rev) +} + +func main () { + var dec int64 + for { + var n, err = fmt . Scanf ("%d", &dec) + if (n != 1 || err != nil) { + break + } + var bin = strconv . FormatInt (dec, 2) + if (bin == reverse (bin)) { + fmt . Println (1) + } else { + fmt . Println (0) + } + } +} + + + diff --git a/challenge-118/abigail/go/ch-2.go b/challenge-118/abigail/go/ch-2.go new file mode 100644 index 0000000000..6fd5616aec --- /dev/null +++ b/challenge-118/abigail/go/ch-2.go @@ -0,0 +1,15 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go +// + +import "fmt" + +func main () { + fmt . Print ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2\n") +} diff --git a/challenge-118/abigail/java/ch-1.java b/challenge-118/abigail/java/ch-1.java new file mode 100644 index 0000000000..a806d2c237 --- /dev/null +++ b/challenge-118/abigail/java/ch-1.java @@ -0,0 +1,34 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file +// + +import java.util.*; + +public class ch1 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + try { + while (true) { + int dec = scanner . nextInt (); + String bin = Integer . toBinaryString (dec); + + if (bin . equals (new StringBuilder (bin) . + reverse () . toString ())) { + System . out . println (1); + } + else { + System . out . println (0); + } + } + } + catch (Exception e) { + // + // EOF + // + } + } +} diff --git a/challenge-118/abigail/java/ch-2.java b/challenge-118/abigail/java/ch-2.java new file mode 100644 index 0000000000..d9f0c4edcc --- /dev/null +++ b/challenge-118/abigail/java/ch-2.java @@ -0,0 +1,13 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-2.java ch2.java; javac ch2; java ch2 +// + +public class ch2 { + public static void main (String [] args) { + System . out . print ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2\n"); + } +} diff --git a/challenge-118/abigail/lua/ch-1.lua b/challenge-118/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..a129db19e4 --- /dev/null +++ b/challenge-118/abigail/lua/ch-1.lua @@ -0,0 +1,32 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +-- +-- Find the binary representation of a number. +-- Note that the function return a string in reverse order; +-- this will do for our purpose as we want a palindrome anyway. +-- +function dec2bin (dec) + local bin = {} + while dec > 0 do + bin [#bin + 1] = dec % 2 + dec = math . floor (dec / 2) + end + return table . concat (bin) +end + +for line in io . lines () do + bin = dec2bin (tonumber (line)) + if bin == string . reverse (bin) then + print (1) + else + print (0) + end +end diff --git a/challenge-118/abigail/lua/ch-2.lua b/challenge-118/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..5c40dfc99c --- /dev/null +++ b/challenge-118/abigail/lua/ch-2.lua @@ -0,0 +1,11 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua +-- + +print ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2") diff --git a/challenge-118/abigail/m4/ch-2.m4 b/challenge-118/abigail/m4/ch-2.m4 new file mode 100644 index 0000000000..6ebcee6dd2 --- /dev/null +++ b/challenge-118/abigail/m4/ch-2.m4 @@ -0,0 +1 @@ +a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2 diff --git a/challenge-118/abigail/mmix/ch-2.mms b/challenge-118/abigail/mmix/ch-2.mms new file mode 100644 index 0000000000..416ce1c81e --- /dev/null +++ b/challenge-118/abigail/mmix/ch-2.mms @@ -0,0 +1,16 @@ +% +% See ../README.md +% + +% +% Run as: mmixal -o ch-2.mmo ch-2.mms; mmix -q ch-2.mmo +% + LOC Data_Segment + GREG @ +Text BYTE "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2",10,0 + + LOC #100 + +Main LDA $255,Text + TRAP 0,Fputs,StdOut + TRAP 0,Halt,0 diff --git a/challenge-118/abigail/node/ch-1.js b/challenge-118/abigail/node/ch-1.js new file mode 100644 index 0000000000..7247cb5eef --- /dev/null +++ b/challenge-118/abigail/node/ch-1.js @@ -0,0 +1,16 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', (line) => { + let bin = (+line) . toString (2) + console . log (bin == bin . split ("") . reverse () . join ("") ? 1 : 0) +}) diff --git a/challenge-118/abigail/node/ch-2.js b/challenge-118/abigail/node/ch-2.js new file mode 100644 index 0000000000..e032ef0448 --- /dev/null +++ b/challenge-118/abigail/node/ch-2.js @@ -0,0 +1,11 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js +// + +console . log ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2") diff --git a/challenge-118/abigail/ocaml/ch-2.ml b/challenge-118/abigail/ocaml/ch-2.ml new file mode 100644 index 0000000000..553e3af70f --- /dev/null +++ b/challenge-118/abigail/ocaml/ch-2.ml @@ -0,0 +1,9 @@ +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: ocaml ch-2.ml *) +(* *) + +print_endline "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2"; |
