diff options
| -rw-r--r-- | challenge-272/sgreen/README.md | 4 | ||||
| -rwxr-xr-x | challenge-272/sgreen/perl/ch-1.pl | 13 | ||||
| -rwxr-xr-x | challenge-272/sgreen/perl/ch-2.pl | 22 | ||||
| -rwxr-xr-x | challenge-272/sgreen/python/ch-1.py | 26 | ||||
| -rwxr-xr-x | challenge-272/sgreen/python/ch-2.py | 36 | ||||
| -rwxr-xr-x | challenge-272/sgreen/python/test.py | 20 |
6 files changed, 119 insertions, 2 deletions
diff --git a/challenge-272/sgreen/README.md b/challenge-272/sgreen/README.md index c76a8f7987..553de4d128 100644 --- a/challenge-272/sgreen/README.md +++ b/challenge-272/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 271 +# The Weekly Challenge 272 -Blog: [Maximizing the 1 bits](https://dev.to/simongreennet/maximizing-the-1-bits-m8a) +Sorry, no blog this week. diff --git a/challenge-272/sgreen/perl/ch-1.pl b/challenge-272/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..923038d09f --- /dev/null +++ b/challenge-272/sgreen/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ($ip) { + $ip =~ s/\./[.]/g; + say $ip; +} + +main($ARGV[0]);
\ No newline at end of file diff --git a/challenge-272/sgreen/perl/ch-2.pl b/challenge-272/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..cc7d616c78 --- /dev/null +++ b/challenge-272/sgreen/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ($str) { + my $score = 0; + + # Convert the string to a list of ASCII values + my @ascii_values = map { ord($_) } split //, $str; + + # Calculate the score + foreach my $pos ( 0 .. $#ascii_values - 1 ) { + $score += abs( $ascii_values[$pos] - $ascii_values[ $pos + 1 ] ); + } + + say $score; +} + +main( $ARGV[0] );
\ No newline at end of file diff --git a/challenge-272/sgreen/python/ch-1.py b/challenge-272/sgreen/python/ch-1.py new file mode 100755 index 0000000000..9e72a22790 --- /dev/null +++ b/challenge-272/sgreen/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import sys + + +def defang_ip(ip: str) -> str: + """ + Replaces all occurrences of '.' in the given IP address with '[.]'. + + Args: + ip (str): The IP address to defang. + + Returns: + str: The defanged IP address. + + """ + return ip.replace('.', '[.]') + + +def main(): + result = defang_ip(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-272/sgreen/python/ch-2.py b/challenge-272/sgreen/python/ch-2.py new file mode 100755 index 0000000000..2a05a1f811 --- /dev/null +++ b/challenge-272/sgreen/python/ch-2.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +import sys + + +def string_store(word: str) -> int: + """ + Calculates the score of a given word based on the absolute difference + between ASCII values of consecutive characters. + + Args: + word (str): The input word. + + Returns: + int: The score of the word. + """ + + score = 0 + + # Convert the string to a list of ASCII values + ascii_values = [ord(char) for char in word] + + # Calculate the score + for pos in range(len(ascii_values)-1): + score += abs(ascii_values[pos] - ascii_values[pos+1]) + + return score + + +def main(): + result = string_store(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-272/sgreen/python/test.py b/challenge-272/sgreen/python/test.py new file mode 100755 index 0000000000..59a19f44c6 --- /dev/null +++ b/challenge-272/sgreen/python/test.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertEqual(ch_1.defang_ip('1.1.1.1'), '1[.]1[.]1[.]1') + self.assertEqual(ch_1.defang_ip('255.101.1.0'), '255[.]101[.]1[.]0') + + def test_ch_2(self): + self.assertEqual(ch_2.string_store('hello'), 13) + self.assertEqual(ch_2.string_store('perl'), 30) + self.assertEqual(ch_2.string_store('raku'), 37) + + +if __name__ == '__main__': + unittest.main() |
