aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-04 18:08:05 +0100
committerGitHub <noreply@github.com>2020-08-04 18:08:05 +0100
commit8eccd58adb3aec2be506d27adb9ffc7f54853d03 (patch)
tree6621904b4e12ee4f93c326b1469d2fc5d58cedc4
parent31b7c3812b44631f28b9cc9a6d2aba334105a855 (diff)
parent59c8d8e8ddd53b4b482d0f075e06e3e3e52dff16 (diff)
downloadperlweeklychallenge-club-8eccd58adb3aec2be506d27adb9ffc7f54853d03.tar.gz
perlweeklychallenge-club-8eccd58adb3aec2be506d27adb9ffc7f54853d03.tar.bz2
perlweeklychallenge-club-8eccd58adb3aec2be506d27adb9ffc7f54853d03.zip
Merge pull request #2034 from jeongoon/ch-072
Challenge 72 Completed by Jeon
-rw-r--r--challenge-072/jeongoon/common-lisp/ch-1.lisp48
-rw-r--r--challenge-072/jeongoon/haskell/ch-1.hs62
-rw-r--r--challenge-072/jeongoon/haskell/ch-2.hs58
-rw-r--r--challenge-072/jeongoon/perl/ch-1.pl60
-rw-r--r--challenge-072/jeongoon/perl/ch-2.pl37
-rw-r--r--challenge-072/jeongoon/raku/ch-1.raku20
-rw-r--r--challenge-072/jeongoon/raku/ch-2.raku16
7 files changed, 301 insertions, 0 deletions
diff --git a/challenge-072/jeongoon/common-lisp/ch-1.lisp b/challenge-072/jeongoon/common-lisp/ch-1.lisp
new file mode 100644
index 0000000000..250f4a3697
--- /dev/null
+++ b/challenge-072/jeongoon/common-lisp/ch-1.lisp
@@ -0,0 +1,48 @@
+;; Ref:
+;; http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/fun_map.html
+;; https://stackoverflow.com/questions/47602677/recursive-factorial-function-in-common-lisp
+;; https://stackoverflow.com/questions/22522108/how-to-map-a-function-in-common-lisp
+;; https://stackoverflow.com/questions/1021778/getting-command-line-arguments-in-common-lisp
+;; https://riptutorial.com/common-lisp/example/8770/summing-a-list
+;; https://www.tutorialspoint.com/lisp/lisp_strings.htm
+
+(defun my-command-line ()
+ (or
+ #+CLISP *args*
+ #+SBCL *posix-argv*
+ #+LISPWORKS system:*line-arguments-list*
+ #+CMU extensions:*command-line-words*
+ nil))
+
+(defun range (min max &optional (step 1))
+ (when (<= min max)
+ (cons min (range (+ min step) max step))))
+
+(defun factorial (n)
+ (if (= n 1)
+ 1
+ (* n (factorial (- n 1)))))
+
+(defun nth-root-of-five (x)
+ (if (= (mod x 5) 0)
+ (+ 1 (nth-root-of-five (/ x 5)))
+ 0 ))
+
+(defun get-answer (n)
+ (let ((seq (range 1 n)))
+ (reduce '+ (map 'list #'nth-root-of-five (range 1 n)))))
+
+(defun show-divided-number(n prompt answer)
+ (let* ((str (write-to-string n))
+ (needle (- (length str) answer 1))
+ (str-left (subseq str 0 needle))
+ (str-right (subseq str (+ needle 1))))
+ (format t "~a~a | ~a~%" prompt str-left str-right)))
+
+(progn
+ (let* ((cmd-line (my-command-line))
+ (n (parse-integer (nth 1 cmd-line)))
+ (answer (get-answer n)))
+ (format t "N: ~a~%" n)
+ (show-divided-number (factorial n) "N!: " answer)
+ (format t "~%Answer: ~a~%" answer)))
diff --git a/challenge-072/jeongoon/haskell/ch-1.hs b/challenge-072/jeongoon/haskell/ch-1.hs
new file mode 100644
index 0000000000..0775791436
--- /dev/null
+++ b/challenge-072/jeongoon/haskell/ch-1.hs
@@ -0,0 +1,62 @@
+import System.Environment
+import System.Exit
+import Data.Char
+
+{-- Tested with:
+runhaskell ch-1.hs [--show-divided] N
+--}
+
+{-- Refs:
+https://alvinalexander.com/haskell/how-to-use-command-line-arguments-in-haskell/
+https://stackoverflow.com/questions/2784271/haskell-converting-int-to-string
+http://learnyouahaskell.com
+http://learnyouahaskell.com/modules
+https://stackoverflow.com/questions/940382/what-is-the-difference-between-dot-and-dollar-sign
+... and many more..
+--}
+
+factorial :: Integer -> Integer
+factorial x = product [ 1.. x ]
+
+nthRootOfFive :: Integer -> Integer
+nthRootOfFive x
+ | x `mod` 5 /= 0 = 0
+ | otherwise = 1 + nthRootOfFive ( x `div` 5 )
+
+
+getN :: [String] -> Integer
+getN [] = 0
+getN (x:xs) = if all isNumber x then read x :: Integer else getN xs
+
+printGenTrim :: [String] -> Integer -> Integer -> IO ()
+printGenTrim x n a
+ | ( "--show-divided" `elem` x ) = do
+ putStrLn $ "N! = " ++ fnstr
+ putStrLn $ "N! = " ++ take h fnstr ++ " | " ++ drop h fnstr
+ | otherwise = return ()
+ where
+ fnstr = show $ factorial $ n
+ a' = fromIntegral a
+ h = length fnstr - a'
+
+main :: IO ()
+main = do
+ args <- getArgs -- IO [String]
+{--
+ progName <- getProgName -- IO String
+ putStrLn "The Program name is:"
+ putStrLn progName
+ putStrLn "The arguments are:"
+ mapM putStrLn args
+--}
+
+ let n = getN args
+ if show n `elem` args then
+ return ()
+ else
+ die "Could not find N (Not given or Invalid)"
+
+ putStrLn $ "Given Number: " ++ show n
+ let answer = sum [ nthRootOfFive x | x <- [ 1 .. n ] ]
+ printGenTrim args n answer
+ putStrLn $ "Answer: " ++ show answer
diff --git a/challenge-072/jeongoon/haskell/ch-2.hs b/challenge-072/jeongoon/haskell/ch-2.hs
new file mode 100644
index 0000000000..14d0fb9054
--- /dev/null
+++ b/challenge-072/jeongoon/haskell/ch-2.hs
@@ -0,0 +1,58 @@
+import System.Environment
+import System.IO
+import System.IO.Error
+import System.Exit
+import Data.List
+import Control.Exception
+
+{-- Tested with:
+runhaskell ch-2.hs [<file name>] [A] [B]
+--}
+
+{-- Refs:
+http://learnyouahaskell.com/input-and-output#files-and-streams
+https://hackage.haskell.org/package/base-4.14.0.0/docs/System-Exit.html
+https://stackoverflow.com/questions/21208771/exception-handling-for-readfile
+https://stackoverflow.com/questions/42609508/not-in-scope-catch
+--}
+
+
+putLinesBetween :: Handle -> Int -> Int -> Int -> IO()
+putLinesBetween fh from to acc
+ | from > to =
+ do
+ putStrLn $ "[INF] Nothing to print: from > to"
+ return ()
+ | from <= 0 =
+ do
+ putStrLn $ "[WRN] line starts from: 1 not: " ++ show from
+ putStrLn $ "[INF] -> Starts with line no. 1 again."
+ putLinesBetween fh 1 to acc
+ | acc >= from && acc <= to = do
+ line <- hGetLine fh
+ putStrLn line
+ putLinesBetween fh from to (acc+1)
+ | otherwise = return ()
+
+toTry :: IO ()
+toTry = do
+ args <- getArgs
+
+ if length args /= 3 then
+ die $ "[ERR] Wrong Number of Args: " ++ (show (length args)) ++ " must be == 3"
+ else
+ return ()
+
+ let filePath = args !! 0
+ let from = read ( args !! 1 ) :: Int
+ let to = read ( args !! 2 ) :: Int
+
+ putStrLn $ "[INF] FilePath: " ++ filePath
+ fh <- openFile filePath ReadMode
+ putLinesBetween fh from to 1
+ hClose fh
+
+handler :: IOError -> IO ()
+handler e = putStrLn $ "[ERR] Aborted: " ++ show e
+
+main = toTry `catch` handler
diff --git a/challenge-072/jeongoon/perl/ch-1.pl b/challenge-072/jeongoon/perl/ch-1.pl
new file mode 100644
index 0000000000..dd96919611
--- /dev/null
+++ b/challenge-072/jeongoon/perl/ch-1.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl
+# -*- Mode: cperl; cperl-indent-level:4 tab-width: 8; indent-tabs-mode: nil -*-
+# -*- coding: utf-8 -*-
+
+use strict; use warnings;
+use bigint;
+
+my $N = $ARGV[0];
+if ( not defined $N ) {
+ print "use default value: 10.\n";
+}
+
+my $e;
+
+
+sub f {
+ return 1 if $_[0] == 1;
+ return $_[0] * f ($_[0]-1);
+}
+
+sub zero_length::f_and_count ($) {
+ # make a (big) number
+ # take only zeros from the end
+ # and return length of it
+ ( my $n = f( $_[0] ) ) =~ m/(0+$)/;
+ length $1;
+}
+
+sub zero_length::reduce ($) {
+
+ my $z = 0;
+=pod
+
+ => Every 10 ( 2 * 5 ) makes a zero digit in the end.
+ A. if find the even number count it
+ B: if find the the number divisible by 5
+ then count how many times divisible by 5a
+
+ But even numbers exsists plenty which more than the numbers divisible by 5
+ ** sorry I cannot prove it mathematically but..
+
+ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
+ A 1 2 3 4 5 6 7 8 9 10 11 12
+ B 1 2 3 4 6
+
+ graphically it shows it is right.
+
+ if A.count > B.count then ( which seems correct)
+ sum of B.count would be how many zeros in the end of the number
+
+=cut
+ map {
+ my $i = 0;
+ ( ++$z, ++$i ) while not $_ % ( 5**($i+1) );
+ $N == $_? $z :();
+ } 1 .. $N;
+}
+
+print "Counted: ", zero_length::f_and_count( $N ), $/;
+print "Caculated:", zero_length::reduce( $N ), $/;
diff --git a/challenge-072/jeongoon/perl/ch-2.pl b/challenge-072/jeongoon/perl/ch-2.pl
new file mode 100644
index 0000000000..d2b8007514
--- /dev/null
+++ b/challenge-072/jeongoon/perl/ch-2.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+# -*- Mode: cperl; cperl-indent-level:4 tab-width: 8; indent-tabs-mode: nil -*-
+# -*- coding: utf-8 -*-
+
+use strict; use warnings;
+
+my ( $filePath, $from, $to ) = @ARGV;
+$filePath ||= "<not given>";
+$from ||= "<not given>";
+$to ||= "<not given>";
+
+-r $filePath or
+ die "Could not find the file: $filePath";
+
+(scalar map { /^\d+$/ ? () : 0 } $from, $to) and
+ die "Wrong number: from: $from, to: $to";
+
+$from < 0 and warn "line starts from no. 1 not: $from\n".
+ "-> start from ".($from = 1);
+
+$to < $from and warn( "end line is below than begin line: Quit." ), exit 0;
+
+open( my $fh, '<', $filePath ) or die "Unexpected Error: $@\n: Quit.";
+
+warn "Showing File: $filePath from : $from to: $to";
+
+my $lnum = 1;
+my $printing = 0;
+
+while ( defined (my $line = <$fh>) ) {
+ if ( $lnum == $from ) { $printing ^= 1 }
+ elsif ( $lnum == $to ) { print $line; last }
+ print $line if $printing;
+ ++$lnum;
+}
+
+close $fh;
diff --git a/challenge-072/jeongoon/raku/ch-1.raku b/challenge-072/jeongoon/raku/ch-1.raku
new file mode 100644
index 0000000000..ad06e87774
--- /dev/null
+++ b/challenge-072/jeongoon/raku/ch-1.raku
@@ -0,0 +1,20 @@
+#!/usr/bin/env raku
+# -*- Mode: Raku; indent-tabs-mode: nil; coding: utf-8 -*-
+# vim: set et ts=4 sw=4:
+
+use v6;
+
+sub f ( Int $n where * > 0 ) { [*] 1 .. $n }
+sub zero-length-f-and-count ( Int $n where * > 0 ) {
+ f($n).match( /0+$/ ).chars;
+}
+
+sub zero-length-reduce ( Int $n where * > 0 ) {
+ reduce { my $i = 0; ++$i while $^b %% 5**($i+1); $^a + $i }, 0 .. $n
+}
+sub MAIN ( Int \N where * > 0 ) {
+ say "Where N = {N}";
+ say " N! = {f(N)}";
+ say "Counted: " ~ zero-length-f-and-count(N);
+ say "Reduced: " ~ zero-length-reduce(N);
+}
diff --git a/challenge-072/jeongoon/raku/ch-2.raku b/challenge-072/jeongoon/raku/ch-2.raku
new file mode 100644
index 0000000000..30b59ae828
--- /dev/null
+++ b/challenge-072/jeongoon/raku/ch-2.raku
@@ -0,0 +1,16 @@
+#!/usr/bin/env raku
+# -*- Mode: Raku; indent-tabs-mode: nil; coding: utf-8 -*-
+# vim: set et ts=4 sw=4:
+use v6;
+
+sub MAIN (
+ Str:D $F where { $F.IO.r }, #= file path to open
+ Int:D $A where { $A > 0 }, #= from
+ Int:D $B where { $B >= $A }, #= to
+ ) {
+ my $lnum = 1;
+ for $F.IO.lines -> $line {
+ say $line if $lnum == $A fff^ $lnum++ == $B;
+ last if $A == $B;
+ }
+}