diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-29 08:20:01 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-29 08:20:01 +0100 |
| commit | d6aee3c441405b37a744f011be57651d5c0919c4 (patch) | |
| tree | e0494e5098378a3a34fe0006a83bf80de2a944c6 | |
| parent | 8930161a531fb6704be2c3f9ce605daf2480feba (diff) | |
| parent | d6ac72f68709c2bc8717461e37fc3a898aef9261 (diff) | |
| download | perlweeklychallenge-club-d6aee3c441405b37a744f011be57651d5c0919c4.tar.gz perlweeklychallenge-club-d6aee3c441405b37a744f011be57651d5c0919c4.tar.bz2 perlweeklychallenge-club-d6aee3c441405b37a744f011be57651d5c0919c4.zip | |
Merge pull request #5116 from pauloscustodio/devel
Devel
50 files changed, 1484 insertions, 50 deletions
diff --git a/.gitignore b/.gitignore index 6f111fe0c2..ba8194b15c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,8 @@ *.bak ch-1 ch-2 +ch_1 +ch_2 go.mod go.sum tags diff --git a/challenge-001/paulo-custodio/Makefile b/challenge-001/paulo-custodio/Makefile new file mode 100644 index 0000000000..24da326a16 --- /dev/null +++ b/challenge-001/paulo-custodio/Makefile @@ -0,0 +1,8 @@ +all: brainfuck/ch-1.bf brainfuck/ch-2.bf + perl ../../challenge-001/paulo-custodio/test.pl + +brainfuck/ch-1.bf: brainfuck/ch-1.pl + perl brainfuck/ch-1.pl > $@ + +brainfuck/ch-2.bf: brainfuck/ch-2.pl + perl brainfuck/ch-2.pl > $@ diff --git a/challenge-001/paulo-custodio/brainfuck/ch-1.bf b/challenge-001/paulo-custodio/brainfuck/ch-1.bf new file mode 100644 index 0000000000..b1d94a8bb7 --- /dev/null +++ b/challenge-001/paulo-custodio/brainfuck/ch-1.bf @@ -0,0 +1 @@ +[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++. diff --git a/challenge-001/paulo-custodio/brainfuck/ch-1.pl b/challenge-001/paulo-custodio/brainfuck/ch-1.pl new file mode 100644 index 0000000000..c9f6754286 --- /dev/null +++ b/challenge-001/paulo-custodio/brainfuck/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl + +# Challenge 001 +# +# Challenge #1 +# Write a script to replace the character ‘e’ with ‘E’ in the string +# ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ +# is found in the string. + +# Output brainfuck program + +use Modern::Perl; + +my $s = "Perl Weekly Challenge"; +my $text = ($s =~ tr/e/E/)." ".$s; + +for (split //, $text) { + print "[-]"; # zero cell + print "+" x ord($_); # set cell to ASCII value of character + print "."; # output character +} +print "\n"; diff --git a/challenge-001/paulo-custodio/brainfuck/ch-2.bf b/challenge-001/paulo-custodio/brainfuck/ch-2.bf new file mode 100644 index 0000000000..0a252de458 --- /dev/null +++ b/challenge-001/paulo-custodio/brainfuck/ch-2.bf @@ -0,0 +1 @@ +[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++. diff --git a/challenge-001/paulo-custodio/brainfuck/ch-2.pl b/challenge-001/paulo-custodio/brainfuck/ch-2.pl new file mode 100644 index 0000000000..7ef784110a --- /dev/null +++ b/challenge-001/paulo-custodio/brainfuck/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +# Challenge 001 +# +# Challenge #2 +# Write a one-liner to solve the FizzBuzz problem and print the numbers 1 +# through 20. However, any number divisible by 3 should be replaced by the word +# ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both +# divisible by 3 and 5 become ‘fizzbuzz’. + +# Output brainfuck program + +use Modern::Perl; + +my $text = ""; +for my $n (1..20) { + if ($n%15==0) { + $text .= "fizzbuzz\n"; + } + elsif ($n%3==0) { + $text .= "fizz\n"; + } + elsif ($n%5==0) { + $text .= "buzz\n"; + } + else { + $text .= "$n\n"; + } +} + +for (split //, $text) { + print "[-]"; # zero cell + print "+" x ord($_); # set cell to ASCII value of character + print "."; # output character +} +print "\n"; diff --git a/challenge-001/paulo-custodio/go.pl b/challenge-001/paulo-custodio/go.pl index 97670bc755..d4639380d3 100644 --- a/challenge-001/paulo-custodio/go.pl +++ b/challenge-001/paulo-custodio/go.pl @@ -7,7 +7,23 @@ use Path::Tiny; or die "Usage: ",path($0)->basename," nr\n"; my $nr = sprintf("%03d", $ARGV[0]); -for my $dir (qw( ada awk basic c cpp d forth lua perl python t )) { +for my $dir (qw( + ada + awk + basic + bc + brainfuck + c + cpp + d + forth + fortran + lua + pascal + perl + python + t + )) { path("challenge-$nr/paulo-custodio/$dir")->mkpath; } path("challenge-$nr/paulo-custodio/README")->spew("Solution by Paulo Custodio\n"); diff --git a/challenge-001/paulo-custodio/stats.pl b/challenge-001/paulo-custodio/stats.pl index 1cb928a96a..bc344a2db3 100644 --- a/challenge-001/paulo-custodio/stats.pl +++ b/challenge-001/paulo-custodio/stats.pl @@ -12,11 +12,14 @@ our %LANG = ( awk => 'awk', basic => 'bas', bc => 'bc', + brainfuck=>'bf', c => 'c', cpp => 'cpp', d => 'd', forth => 'fs', + fortran => 'f90', lua => 'lua', + pascal => 'pas', perl => 'pl', python => 'py', ); diff --git a/challenge-001/paulo-custodio/t/test-1.yaml b/challenge-001/paulo-custodio/t/test-1.yaml index c36262e3b7..2c3894252e 100644 --- a/challenge-001/paulo-custodio/t/test-1.yaml +++ b/challenge-001/paulo-custodio/t/test-1.yaml @@ -3,8 +3,3 @@ args: Perl Weekly Challenge input: output: 5 PErl WEEkly ChallEngE -- setup: - cleanup: - args: Champion - input: - output: 0 Champion diff --git a/challenge-001/paulo-custodio/test.pl b/challenge-001/paulo-custodio/test.pl index 4e2418c4ae..c8dc2b9f75 100644 --- a/challenge-001/paulo-custodio/test.pl +++ b/challenge-001/paulo-custodio/test.pl @@ -17,7 +17,7 @@ our %LANG = ( awk => 'awk', basic => 'bas', bc => 'bc', - brainfuck=>'bfpp', + brainfuck=>'bf', c => 'c', cpp => 'cpp', d => 'd', @@ -137,15 +137,15 @@ sub build { return "gawk -f $prog --"; } if (/^basic$/) { - run("fbc $prog -o $prog_wo_ext") if (!-f $exe || -M $exe > -M $prog); + run("fbc $prog") if (!-f $exe || -M $exe > -M $prog); return $exe; } if (/^bc$/) { return "bc -lq $prog"; } if (/^brainfuck$/) { - run("perl bfpp.pl <$prog_wo_ext.bfpp >$prog_wo_ext.bf"); - return "brainfuck $prog_wo_ext.bf"; + #run("perl bfpp.pl <$prog_wo_ext.bfpp >$prog_wo_ext.bf"); + return "bf $prog_wo_ext.bf"; } if (/^c$/) { run("gcc $prog -o $prog_wo_ext -lmpfr -lgmp") if (!-f $exe || -M $exe > -M $prog); diff --git a/challenge-003/paulo-custodio/Makefile b/challenge-003/paulo-custodio/Makefile new file mode 100644 index 0000000000..6316089eb8 --- /dev/null +++ b/challenge-003/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-003/paulo-custodio/perl/ch-1.pl b/challenge-003/paulo-custodio/perl/ch-1.pl index e1c2d5b0f8..afd712b149 100644 --- a/challenge-003/paulo-custodio/perl/ch-1.pl +++ b/challenge-003/paulo-custodio/perl/ch-1.pl @@ -7,17 +7,8 @@ # or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more # information, please check this wikipedia. -use strict; -use warnings; -use 5.030; - -sub min { - my($min, @a) = @_; - for (@a) { - $min = $_ if $min > $_; - } - return $min; -} +use Modern::Perl; +use List::Util 'min'; # return an iterator to generate the sequence # the sequence is a merge of all multiples of 2, 3 and 5 diff --git a/challenge-003/paulo-custodio/perl/ch-2.pl b/challenge-003/paulo-custodio/perl/ch-2.pl index bb3afc06e2..83ab59311f 100644 --- a/challenge-003/paulo-custodio/perl/ch-2.pl +++ b/challenge-003/paulo-custodio/perl/ch-2.pl @@ -7,9 +7,7 @@ # the command line. The Pascal Triangle should have at least 3 rows. For more # information about Pascal Triangle, check this wikipedia page. -use strict; -use warnings; -use 5.030; +use Modern::Perl; my($rows) = @ARGV; diff --git a/challenge-003/paulo-custodio/test.pl b/challenge-003/paulo-custodio/test.pl deleted file mode 100644 index ba6c37260b..0000000000 --- a/challenge-003/paulo-custodio/test.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env perl -use Modern::Perl; -use Test::More; -require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-120/paulo-custodio/Makefile b/challenge-120/paulo-custodio/Makefile new file mode 100644 index 0000000000..2cbe39ae87 --- /dev/null +++ b/challenge-120/paulo-custodio/Makefile @@ -0,0 +1,5 @@ +all: brainfuck/ch-1.bf + perl ./test.pl + +brainfuck/ch-1.bf: brainfuck/ch-1.bfpp bfpp.pl + perl bfpp.pl < $< > $@ diff --git a/challenge-120/paulo-custodio/forth/ch-2.fs b/challenge-120/paulo-custodio/forth/ch-2.fs index 078dbfbc19..6728e3b202 100644 --- a/challenge-120/paulo-custodio/forth/ch-2.fs +++ b/challenge-120/paulo-custodio/forth/ch-2.fs @@ -40,25 +40,3 @@ DUP 180 > IF 360 SWAP - THEN ; NEXT-ARG parse_time clock_angle . CR BYE - -\ #include <stdio.h> -\ #include <stdlib.h> -\ #include <stdbool.h> -\ -\ -\ int clock_angle(int hh, int mm) { -\ int mm_angle = mm*360/60; -\ int hh_angle = (hh % 12)*360/12 + mm_angle/12; -\ int angle = abs(hh_angle - mm_angle); -\ if (angle > 180) { angle = 360 - angle; } -\ return angle; -\ } -\ -\ int main(int argc, char* argv[]) { -\ if (argc != 2) return EXIT_FAILURE; -\ int hh, mm; -\ if (!parse_time(argv[1], &hh, &mm)) return EXIT_FAILURE; -\ int angle = clock_angle(hh, mm); -\ printf("%d\n", angle); -\ } -\ diff --git a/challenge-123/paulo-custodio/Makefile b/challenge-123/paulo-custodio/Makefile new file mode 100644 index 0000000000..6316089eb8 --- /dev/null +++ b/challenge-123/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-123/paulo-custodio/awk/ch-1.awk b/challenge-123/paulo-custodio/awk/ch-1.awk new file mode 100644 index 0000000000..78dffcaa09 --- /dev/null +++ b/challenge-123/paulo-custodio/awk/ch-1.awk @@ -0,0 +1,74 @@ +#!/usr/bin/gawk + +# TASK #1 > Ugly Numbers +# Submitted by: Mohammad S Anwar +# You are given an integer $n >= 1. +# +# Write a script to find the $nth element of Ugly Numbers. +# +# Ugly numbers are those number whose prime factors are 2, 3 or 5. For example, +# the first 10 Ugly Numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12. +# +# Example +# Input: $n = 7 +# Output: 8 +# +# Input: $n = 10 +# Output: 12 + +function min(a, b) { + return (a<b) ? a : b; +} + +function min3(a, b, c) { + return min(a, min(b, c)); +} + +function alen(a, i, k) { + k = 0; + for(i in a) k++; + return k; +} + +function push(a, v, n) { + n = alen(a); + while (n in a) n++; + a[n] = v; +} + +function shift(a, i, v) { + v = a[0]; + for (i=1; i<alen(a); i++) + a[i-1] = a[i]; + delete a[alen(a)-1]; + return v; +} + +function hamming( n) { + # get the smallest of the multiples + n = min3(seq2[0], seq3[0], seq5[0]); + + # shift used multiples + if (seq2[0] == n) shift(seq2); + if (seq3[0] == n) shift(seq3); + if (seq5[0] == n) shift(seq5); + + # push next multiple + push(seq2, n*2); + push(seq3, n*3); + push(seq5, n*5); + + return n; +} + +BEGIN { + # sequences of hamming numbers 2*n, 3*n, 5*n + seq2[0] = 1; + seq3[0] = 1; + seq5[0] = 1; + + n = ARGV[1] ? ARGV[1] : 5; + for (i=0; i<n; i++) + print hamming(); + exit 0; +} diff --git a/challenge-123/paulo-custodio/basic/ch-1.bas b/challenge-123/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..fbd74db6fa --- /dev/null +++ b/challenge-123/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,159 @@ +' Challenge 003 +' +' TASK #1 > Ugly Numbers +' Submitted by: Mohammad S Anwar +' You are given an integer $n >= 1. +' +' Write a script to find the $nth element of Ugly Numbers. +' +' Ugly numbers are those number whose prime factors are 2, 3 or 5. For example, +' the first 10 Ugly Numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12. +' +' Example +' Input: $n = 7 +' Output: 8 +' +' Input: $n = 10 +' Output: 12 + +' deque of integers +type ElemType + nValue as integer + pNext as ElemType ptr +end type + +type QType + pFront as ElemType ptr + pBack as ElemType ptr +end type + +function QEmpty(byref q as QType) as boolean + if q.pFront = 0 then + QEmpty = true + else + QEmpty = false + end if +end function + +function QFront(byref q as QType) as integer + QFront = q.pFront->nValue +end function + +function QBack(byref q as QType) as integer + QBack = q.pBack->nValue +end function + +sub QPushBack(byref q as QType, nValue as integer) + dim pElem as ElemType ptr + + pElem = callocate(1, sizeof(ElemType)) + pElem->nValue = nValue + + if q.pFront = 0 then + q.pFront = pElem + q.pBack = pElem + else + q.pBack->pNext = pElem + q.pBack = pElem + end if +end sub + +sub QPopBack(byref q as QType) + dim pElem as ElemType ptr + + if q.pFront = 0 then ' empty + elseif q.pFront = q.pBack then ' only one element + deallocate q.pFront + q.pFront = 0 + q.pBack = 0 + else ' more than one element + pElem = q.pFront + do while pElem->pNext <> q.pBack + pElem = pElem->pNext + loop + deallocate q.pBack + q.pBack = pElem + pElem->pNext = 0 + end if +end sub + +sub QPushFront(byref q as QType, nValue as integer) + dim pElem as ElemType ptr + + pElem = callocate(1, sizeof(ElemType)) + pElem->nValue = nValue + + if q.pFront = 0 then + q.pFront = pElem + q.pBack = pElem + else + pElem->pNext = q.pFront + q.pFront = pElem + end if +end sub + +sub QPopFront(byref q as QType) + dim pElem as ElemType ptr + + if q.pFront = 0 then ' empty + elseif q.pFront = q.pBack then ' only one element + deallocate q.pFront + q.pFront = 0 + q.pBack = 0 + else ' more than one element + pElem = q.pFront + q.pFront = pElem->pNext + deallocate pElem + end if |
