aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-23 13:23:15 +0100
committerGitHub <noreply@github.com>2020-08-23 13:23:15 +0100
commit4044198e551c10183b031e99cfab94713fa88d57 (patch)
tree2e39bba9ed2c71d2dc146679bf45253824071a7c
parentcab4cd0d351e4ae1101bca7511f6680a7b68a63e (diff)
parent65ab3f0cc83999978ca82e3b3972d3f4108e48fc (diff)
downloadperlweeklychallenge-club-4044198e551c10183b031e99cfab94713fa88d57.tar.gz
perlweeklychallenge-club-4044198e551c10183b031e99cfab94713fa88d57.tar.bz2
perlweeklychallenge-club-4044198e551c10183b031e99cfab94713fa88d57.zip
Merge pull request #2128 from jeongoon/master
[ch074/jeongoon] Add Elm Solution and Perl and Raku cleaned up a bit
-rw-r--r--challenge-074/jeongoon/elm/elm.json24
-rw-r--r--challenge-074/jeongoon/elm/src/Ch1.elm22
-rw-r--r--challenge-074/jeongoon/elm/src/Ch2.elm38
-rw-r--r--challenge-074/jeongoon/elm/src/Main.elm106
-rw-r--r--challenge-074/jeongoon/perl/ch-1.pl2
-rw-r--r--challenge-074/jeongoon/perl/ch-2.pl30
-rw-r--r--challenge-074/jeongoon/raku/ch-2.raku8
7 files changed, 219 insertions, 11 deletions
diff --git a/challenge-074/jeongoon/elm/elm.json b/challenge-074/jeongoon/elm/elm.json
new file mode 100644
index 0000000000..dea3450db1
--- /dev/null
+++ b/challenge-074/jeongoon/elm/elm.json
@@ -0,0 +1,24 @@
+{
+ "type": "application",
+ "source-directories": [
+ "src"
+ ],
+ "elm-version": "0.19.1",
+ "dependencies": {
+ "direct": {
+ "elm/browser": "1.0.2",
+ "elm/core": "1.0.5",
+ "elm/html": "1.0.0"
+ },
+ "indirect": {
+ "elm/json": "1.1.3",
+ "elm/time": "1.0.0",
+ "elm/url": "1.0.0",
+ "elm/virtual-dom": "1.0.2"
+ }
+ },
+ "test-dependencies": {
+ "direct": {},
+ "indirect": {}
+ }
+}
diff --git a/challenge-074/jeongoon/elm/src/Ch1.elm b/challenge-074/jeongoon/elm/src/Ch1.elm
new file mode 100644
index 0000000000..b66343e505
--- /dev/null
+++ b/challenge-074/jeongoon/elm/src/Ch1.elm
@@ -0,0 +1,22 @@
+module Ch1 exposing (pairNumberListAndMajor)
+import String
+import Tuple
+import List exposing (map, head, tail)
+
+pairNumberListAndMajor : String -> ( List Int, Maybe Int )
+pairNumberListAndMajor newListString =
+ let newNumberList = List.filterMap
+ String.toInt (String.split " " newListString)
+ count k = List.length( List.filter (\y -> k == y) newNumberList )
+ halfLen = List.length( newNumberList ) // 2 -- //-> integer division
+ findMajor maybels =
+ case maybels of
+ Nothing -> Nothing
+ Just ls ->
+ case (List.head ls) of
+ Nothing -> Nothing
+ Just m -> if (count m) > halfLen
+ then Just m
+ else findMajor (List.tail ls)
+ in
+ ( newNumberList, findMajor (Just newNumberList) )
diff --git a/challenge-074/jeongoon/elm/src/Ch2.elm b/challenge-074/jeongoon/elm/src/Ch2.elm
new file mode 100644
index 0000000000..4a0f1c3768
--- /dev/null
+++ b/challenge-074/jeongoon/elm/src/Ch2.elm
@@ -0,0 +1,38 @@
+module Ch2 exposing (getFNR)
+import String exposing (join, split)
+import Dict
+import List exposing (head, tail)
+import Maybe exposing (withDefault)
+
+getFNR : String -> Maybe String
+getFNR newString =
+ let chrs = split "" newString
+ counts x dic = case (Dict.get x dic) of
+ Nothing -> 0
+ Just n -> n
+ doFind dic rfnr cs =
+ case (head cs) of
+ Nothing ->
+ if List.length rfnr == 0 then Nothing
+ else Just (join "" (List.reverse rfnr))
+ Just k ->
+ case (counts k dic) of
+ 0 -> doFind (Dict.insert k 1 dic) (k :: rfnr)
+ (withDefault [] (tail cs))
+ m -> doFind (Dict.insert k (m+1) dic)
+ ((findPrevious
+ (Dict.insert k (m+1) dic) rfnr)
+ :: rfnr)
+ (withDefault [] (tail cs))
+ findPrevious dic nrs =
+ case (head nrs) of
+ Nothing -> "#"
+ Just y ->
+ case (Dict.get y dic) of
+ Nothing -> y
+ Just u -> if u == 1 then y
+ else findPrevious dic
+ (withDefault [] (tail nrs))
+
+ in
+ doFind Dict.empty [] chrs
diff --git a/challenge-074/jeongoon/elm/src/Main.elm b/challenge-074/jeongoon/elm/src/Main.elm
new file mode 100644
index 0000000000..936a00d7a4
--- /dev/null
+++ b/challenge-074/jeongoon/elm/src/Main.elm
@@ -0,0 +1,106 @@
+{- Tested with:
+elm make src/Main.elm
+# and access elm/index.html in a web browser(firefox in my case)
+# which shows Task1 and Task2 altogether: couldn't find the better way
+-}
+
+module Main exposing (..)
+import Browser
+import Html exposing (..)
+import Html.Attributes exposing (..)
+import Html.Events exposing (onInput)
+
+import String
+import List exposing (map, head, tail)
+
+import Ch1
+import Ch2
+
+-- MAIN
+
+main =
+ Browser.sandbox { init = init, update = update, view = view }
+
+-- MODEL
+
+type alias Model =
+ { numberListString : String
+ , numberList : List Int
+ , majorNumber : Maybe Int
+ , charListString : String
+ , fnrString : Maybe String
+ }
+
+init : Model
+init =
+ Model "" [] Nothing "" Nothing
+
+-- UPDATE
+
+type Msg
+ = FindMajor String
+ | GetFNR String
+
+update : Msg -> Model -> Model
+update msg model =
+
+ case msg of
+ FindMajor newListString ->
+ let result = Ch1.pairNumberListAndMajor newListString
+ in { model
+ | numberListString = newListString,
+ numberList = Tuple.first result,
+ majorNumber = Tuple.second result }
+ GetFNR newString ->
+ { model
+ | charListString = newString,
+ fnrString = if String.length newString == 0 then Nothing
+ else Ch2.getFNR newString }
+
+
+-- VIEW
+
+view : Model -> Html Msg
+view model =
+ div [ style "width" "90%" ]
+ [ h1 [] [ text "Task1: Majority Element" ]
+ , viewInput "text" "Type List of numbers, separated by space"
+ model.numberListString FindMajor
+ , displaySizeOfList model
+ , displayMajor model
+ , h1 [] [ text "Task2: FNR" ]
+ , viewInput "text" "Type a string consists of characters"
+ model.charListString GetFNR
+ , displayFNR model
+ ]
+
+
+viewInput : String -> String -> String -> (String -> msg) -> Html msg
+viewInput t p v toMsg =
+ div [ style "background-color" "gray" ]
+ [ input [ size 150, type_ t, placeholder p, value v, onInput toMsg ] [] ]
+
+displaySizeOfList : Model -> Html msg
+displaySizeOfList model =
+ case (List.length model.numberList) of
+ 0 -> div [] [ text "Size: 0" ]
+ x -> div [] [ text ( "Size: " ++ (String.fromInt x) ) ]
+
+displayMajor : Model -> Html msg
+displayMajor model =
+ case model.majorNumber of
+ Nothing ->
+ if List.length model.numberList == 0 then
+ div [ style "color" "blue" ] [ text "List is empty" ]
+ else
+ div [ style "color" "red" ] [ text "Major Not Found: -1" ]
+ Just x ->
+ div [ style "color" "green" ] [ text ( "Major Number: " ++
+ String.fromInt(x) ) ]
+
+displayFNR model =
+ case model.fnrString of
+ Nothing ->
+ div [ style "color" "red" ] [ text "No String given" ]
+ Just s ->
+ div [ style "color" "green" ] [ text s ]
diff --git a/challenge-074/jeongoon/perl/ch-1.pl b/challenge-074/jeongoon/perl/ch-1.pl
index 9353563837..04bec4e2b9 100644
--- a/challenge-074/jeongoon/perl/ch-1.pl
+++ b/challenge-074/jeongoon/perl/ch-1.pl
@@ -10,7 +10,7 @@ sub get_major (@) {
@_ == 1 and return $_[0];
my @sorted = sort @_;
- my $halflen = int( .5 * @sorted ); # (halflen)gth
+ my $halflen = int .5 * @sorted; # (halflen)gth
my $pnum = shift @sorted; # (p)revious (num)ber
my $pcnt = 1; # (p)revious (c)ou(nt)
diff --git a/challenge-074/jeongoon/perl/ch-2.pl b/challenge-074/jeongoon/perl/ch-2.pl
index 1758963be6..794ec9ee82 100644
--- a/challenge-074/jeongoon/perl/ch-2.pl
+++ b/challenge-074/jeongoon/perl/ch-2.pl
@@ -4,17 +4,37 @@
use strict; use warnings;
-sub uniq_sorted {
- my %mem = ();
- map { exists $mem{$_} ? () : ($mem{$_} = $_) } @_
+sub unique {
+ scalar @_ or return ();
+ $_[0], map { $_[0] ne $_ ? $_: () } @_[1..$#_];
}
-sub printLNR ($) {
+# adapted from my own solution in common-lisp which is a bit lengthy
+sub printLNR {
+ my $str = shift;
+ for my $last_idx ( 1 .. length $str ) {
+ my $sub_chars = substr $str, 0, $last_idx;
+ my @candi = unique( split '', $sub_chars );
+
+ my ( $nr_pos, $nr_chr ) = ( -1, '#' );
+
+ for my $c ( @candi ) {
+ next if ( my $pos1 = index $sub_chars, $c ) == -1;
+ next if ( my $pos2 = index $sub_chars, $c, $pos1 + 1 ) != -1;
+ $nr_pos < $pos1 and ( $nr_pos = $pos1, $nr_chr = $c );
+ }
+ print "$nr_chr";
+ }
+ print $/;
+}
+
+# posted on 20th of Aug
+sub printLNR_by_split ($) {
my $str = shift;
for my $last_idx ( 1 .. length $str ) {
my $sub_chars = substr $str, 0, $last_idx;
- my @candi = uniq_sorted( split '', $sub_chars );
+ my @candi = unique( split '', $sub_chars );
my $nr_pos = -1;
my $nr_chr = '#';
diff --git a/challenge-074/jeongoon/raku/ch-2.raku b/challenge-074/jeongoon/raku/ch-2.raku
index ab03aa5e41..c3d9eab63e 100644
--- a/challenge-074/jeongoon/raku/ch-2.raku
+++ b/challenge-074/jeongoon/raku/ch-2.raku
@@ -7,13 +7,11 @@ use v6.d;
# solution
role fnr {
method sayLNR ( Str:D $str = self.Str ) {
- my @chars = $str.comb;
-
- for 1 .. @chars.elems -> $last-index {
+ for 1 .. $str.chars -> $last-index {
my $sub-chars = $str.substr( 0, $last-index );
my @candi = @sub-chars.unique;
- my $nr-pos = -1;
- my $nr-char = '#';
+ my ( $nr-pos, $nr-char ) = ( -1, '#' );
+
for @candi -> $c {
given $sub-chars.indices( $c ) {
.elems == 1 or next;