diff options
Diffstat (limited to 'challenge-195/paulo-custodio/forth')
| -rw-r--r-- | challenge-195/paulo-custodio/forth/ch-1.fs | 54 | ||||
| -rw-r--r-- | challenge-195/paulo-custodio/forth/ch-2.fs | 77 |
2 files changed, 131 insertions, 0 deletions
diff --git a/challenge-195/paulo-custodio/forth/ch-1.fs b/challenge-195/paulo-custodio/forth/ch-1.fs new file mode 100644 index 0000000000..54118c7503 --- /dev/null +++ b/challenge-195/paulo-custodio/forth/ch-1.fs @@ -0,0 +1,54 @@ +#! /usr/bin/env gforth + +\ Challenge 195 +\ +\ Task 1: Special Integers +\ Submitted by: Mohammad S Anwar +\ You are given a positive integer, $n > 0. +\ +\ Write a script to print the count of all special integers between 1 and $n. +\ +\ An integer is special when all of its digits are unique. +\ +\ Example 1: +\ Input: $n = 15 +\ Output: 14 as except 11 all other integers between 1 and 15 are spcial. +\ Example 2: +\ Input: $n = 35 +\ Output: 32 as except 11, 22, 33 all others are special. + +CREATE digits 10 CELLS ALLOT + +: clear_digits ( -- ) digits 10 CELLS ERASE ; + +: count_digits ( n -- ) + BEGIN DUP 0> WHILE + DUP 10 MOD CELLS digits + 1 SWAP +! + 10 / + REPEAT + DROP +; + +: has_duplicate_digit ( -- f ) + FALSE + 10 0 DO + I CELLS digits + @ 1 > IF DROP TRUE THEN + LOOP +; + +: is_special_digit ( n -- f ) + clear_digits + count_digits + has_duplicate_digit 0= +; + +: count_special_ints ( n -- count ) + 0 SWAP + 1+ 1 ?DO + I is_special_digit IF 1+ THEN + LOOP +; + +NEXT-ARG 0 0 2SWAP >NUMBER 2DROP DROP +count_special_ints . CR +BYE diff --git a/challenge-195/paulo-custodio/forth/ch-2.fs b/challenge-195/paulo-custodio/forth/ch-2.fs new file mode 100644 index 0000000000..03d0eff8ad --- /dev/null +++ b/challenge-195/paulo-custodio/forth/ch-2.fs @@ -0,0 +1,77 @@ +#! /usr/bin/env gforth + +\ Challenge 195 +\ +\ Task 2: Most Frequent Even +\ Submitted by: Mohammad S Anwar +\ You are given a list of numbers, @list. +\ +\ Write a script to find most frequent even numbers in the list. In case you +\ get more than one even numbers then return the smallest even integer. For all +\ other case, return -1. +\ +\ Example 1 +\ Input: @list = (1,1,2,6,2) +\ Output: 2 as there are only 2 even numbers 2 and 6 and of those 2 appears +\ the most. +\ Example 2 +\ Input: @list = (1,3,5,7) +\ Output: -1 since no even numbers found in the list +\ Example 3 +\ Input: @list = (6,4,4,6,1) +\ Output: 4 since there are only two even numbers 4 and 6. They both appears +\ the equal number of times, so pick the smallest. + +CREATE nums 256 CELLS ALLOT +0 VALUE nums_size + +: nums[] ( idx -- addr ) + CELLS nums + +; + +: collect_args ( -- ) + BEGIN NEXT-ARG DUP WHILE + 0 0 2SWAP >NUMBER 2DROP DROP + nums_size nums[] ! + nums_size 1+ TO nums_size + REPEAT + 2DROP +; + +: max_nums ( -- n ) + 0 + nums_size 0 ?DO + I nums[] @ MAX + LOOP +; + +0 VALUE hist +0 VALUE hist_size + +: hist[] ( idx -- addr ) + CELLS hist + +; + +: make_hist ( -- ) + max_nums 1+ DUP TO hist_size + CELLS ALLOCATE THROW TO hist + hist hist_size CELLS ERASE + nums_size 0 ?DO + I nums[] @ hist[] 1 SWAP +! + LOOP +; + +: max_even ( -- ) + make_hist + -1 0 ( n max ) + hist_size 0 ?DO + I hist[] @ OVER > IF + 2DROP I I hist[] @ + THEN + 2 +LOOP + DROP +; + +collect_args +max_even . CR +BYE |
