diff options
| author | Simon Green <mail@simon.green> | 2023-01-08 17:40:26 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2023-01-08 17:40:26 +1100 |
| commit | 22b2654027f6f7cb3a712efc63181fd006ecac08 (patch) | |
| tree | 65b4a18ff477a80957d9bed2d8009201c48b19c1 | |
| parent | 5d3aa9b2255c96c148cedb5a2df5aa113fe70520 (diff) | |
| download | perlweeklychallenge-club-22b2654027f6f7cb3a712efc63181fd006ecac08.tar.gz perlweeklychallenge-club-22b2654027f6f7cb3a712efc63181fd006ecac08.tar.bz2 perlweeklychallenge-club-22b2654027f6f7cb3a712efc63181fd006ecac08.zip | |
Simon's solution to challenge 198
| -rw-r--r-- | challenge-198/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-198/sgreen/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-198/sgreen/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-198/sgreen/perl/ch-2.pl | 33 | ||||
| -rwxr-xr-x | challenge-198/sgreen/python/ch-1.py | 31 | ||||
| -rwxr-xr-x | challenge-198/sgreen/python/ch-2.py | 34 |
6 files changed, 134 insertions, 2 deletions
diff --git a/challenge-198/sgreen/README.md b/challenge-198/sgreen/README.md index e88b63ed82..5d5a8941b6 100644 --- a/challenge-198/sgreen/README.md +++ b/challenge-198/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 197 +# The Weekly Challenge 198 -Blog [Sorting Lists](https://dev.to/simongreennet/sorting-lists-35fh) +[Blog](https://dev.to/simongreennet/weekly-challenge-198-2jbl) diff --git a/challenge-198/sgreen/blog.txt b/challenge-198/sgreen/blog.txt new file mode 100644 index 0000000000..130b262845 --- /dev/null +++ b/challenge-198/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-198-2jbl
\ No newline at end of file diff --git a/challenge-198/sgreen/perl/ch-1.pl b/challenge-198/sgreen/perl/ch-1.pl new file mode 100644 index 0000000000..616806f34b --- /dev/null +++ b/challenge-198/sgreen/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@list) { + my $gap = 0; # The maximum gap + my $count = 0; # Number of occurrences + + # Sort the list + @list = sort { $a <=> $b } @list; + + # Iterate through the list + foreach my $i ( 0 .. $#list - 1 ) { + my $diff = $list[ $i + 1 ] - $list[$i]; + if ( $diff > $gap ) { + # We have a new maximum, reset the count + $gap = $diff; + $count = 1; + } + elsif ( $diff == $gap ) { + # Add to the count + $count++; + } + } + + # Display the result + say $count; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-198/sgreen/perl/ch-2.pl b/challenge-198/sgreen/perl/ch-2.pl new file mode 100644 index 0000000000..55443b9098 --- /dev/null +++ b/challenge-198/sgreen/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub is_prime ($number) { + # Return true or false if the number is a prime + return if $number < 2; + + foreach my $i ( 2 .. sqrt($number) ) { + return if $number % $i == 0; + } + + # It's a prime + return 1; +} + +sub main ($n) { + # Count the number of primes + my $count = 0; + + # Iterate from 1 to n + foreach my $i ( 1 .. $n - 1 ) { + # If it is a prime, add to count + ++$count if is_prime($i); + } + + say $count; +} + +main( $ARGV[0] );
\ No newline at end of file diff --git a/challenge-198/sgreen/python/ch-1.py b/challenge-198/sgreen/python/ch-1.py new file mode 100755 index 0000000000..c1981be24c --- /dev/null +++ b/challenge-198/sgreen/python/ch-1.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +import sys + + +def main(n): + gap = 0 # The maximum gap + count = 0 # Number of occurrences + + # Sort the list + n = sorted(n) + + # Iterate through the list + for i in range(len(n)-1): + diff = n[i+1] - n[i] + if diff > gap: + # We have a new maximum, reset the count + gap = diff + count = 1 + elif diff == gap: + # Add to the count + count += 1 + + # Display the result + print(count) + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) diff --git a/challenge-198/sgreen/python/ch-2.py b/challenge-198/sgreen/python/ch-2.py new file mode 100755 index 0000000000..1846353a16 --- /dev/null +++ b/challenge-198/sgreen/python/ch-2.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +import math +import sys + + +def is_prime(number): + '''Return true or false if the number is a prime''' + if number < 2: + return False + + for i in range(2, int(math.sqrt(number)) + 1): + if number % i == 0: + return False + + # It's a prime + return True + + +def main(n): + # Count the number of primes + count = 0 + + # Iterate from 1 to n-1 + for i in range(1, n): + # If it is a prime, add to count + if is_prime(i): + count += 1 + + print(count) + + +if __name__ == '__main__': + main(int(sys.argv[1])) |
