aboutsummaryrefslogtreecommitdiff
path: root/challenge-192
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-192')
-rw-r--r--challenge-192/mohammad-anwar/java/theweeklychallenge/BinaryFlip.java48
-rw-r--r--challenge-192/mohammad-anwar/perl/ch-1.pl35
-rw-r--r--challenge-192/mohammad-anwar/python/ch-1.py32
-rw-r--r--challenge-192/mohammad-anwar/raku/ch-1.raku35
-rw-r--r--challenge-192/mohammad-anwar/swift/ch-1.swift54
5 files changed, 204 insertions, 0 deletions
diff --git a/challenge-192/mohammad-anwar/java/theweeklychallenge/BinaryFlip.java b/challenge-192/mohammad-anwar/java/theweeklychallenge/BinaryFlip.java
new file mode 100644
index 0000000000..b282e9d096
--- /dev/null
+++ b/challenge-192/mohammad-anwar/java/theweeklychallenge/BinaryFlip.java
@@ -0,0 +1,48 @@
+package theweeklychallenge;
+
+/*
+
+Week 192:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-192
+
+Task #1: Binary Flip
+
+ You are given a positive integer, $n.
+
+ Write a script to find the binary flip.
+
+Compile and Run:
+
+ mohammad-anwar/java$ javac theweeklychallenge/BinaryFlip.java
+ mohammad-anwar/java$ java theweeklychallenge.BinaryFlip
+
+*/
+
+import junit.framework.TestCase;
+import static junit.framework.Assert.*;
+
+public class BinaryFlip extends TestCase {
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(
+ theweeklychallenge.BinaryFlip.class
+ );
+ }
+
+ public void testBinaryFlip() {
+ assertEquals(2, binaryFlip(5));
+ assertEquals(3, binaryFlip(4));
+ assertEquals(1, binaryFlip(6));
+ }
+
+ public static int binaryFlip(int n) {
+ String[] bits = Integer.toBinaryString(n).split("");
+ String flipped = "";
+ for(String bit : bits) {
+ flipped += (bit.equals("0")) ? '1' : '0';
+ }
+
+ return Integer.parseInt(flipped, 2);
+ }
+}
diff --git a/challenge-192/mohammad-anwar/perl/ch-1.pl b/challenge-192/mohammad-anwar/perl/ch-1.pl
new file mode 100644
index 0000000000..53da11d9bc
--- /dev/null
+++ b/challenge-192/mohammad-anwar/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/perl
+
+=head1
+
+Week 192:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-192
+
+Task #1: Binary Flip
+
+ You are given a positive integer, $n.
+
+ Write a script to find the binary flip.
+
+=cut
+
+use v5.36;
+use Test2::V0;
+
+is binary_flip(5), 2, 'Example 1';
+is binary_flip(4), 3, 'Example 2';
+is binary_flip(6), 1, 'Example 3';
+
+done_testing;
+
+#
+#
+# METHOD
+
+sub binary_flip($n) {
+ return oct '0b'.
+ join q{},
+ map { tr/10/01/ and $_ }
+ split //,sprintf '%b', $n;
+}
diff --git a/challenge-192/mohammad-anwar/python/ch-1.py b/challenge-192/mohammad-anwar/python/ch-1.py
new file mode 100644
index 0000000000..3ff80be425
--- /dev/null
+++ b/challenge-192/mohammad-anwar/python/ch-1.py
@@ -0,0 +1,32 @@
+#!/usr/bin/python3
+
+'''
+
+Week 192:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-192
+
+Task #1: Binary Flip
+
+ You are given a positive integer, $n.
+
+ Write a script to find the binary flip.
+
+'''
+
+import unittest
+
+def binaryFlip(n) -> int:
+ return int(''.join(['1' if i == '0' else '0' for i in "{0:b}".format(n)]),2)
+
+#
+#
+# Unit test class
+
+class TestBinaryFlip(unittest.TestCase):
+ def test_binaryFlip(self):
+ self.assertEqual(binaryFlip(5), 2, 'Example 1')
+ self.assertEqual(binaryFlip(4), 3, 'Example 2')
+ self.assertEqual(binaryFlip(6), 1, 'Example 3')
+
+unittest.main()
diff --git a/challenge-192/mohammad-anwar/raku/ch-1.raku b/challenge-192/mohammad-anwar/raku/ch-1.raku
new file mode 100644
index 0000000000..ef12503cbc
--- /dev/null
+++ b/challenge-192/mohammad-anwar/raku/ch-1.raku
@@ -0,0 +1,35 @@
+#!/usr/bin/env raku
+
+=begin pod
+
+Week 192:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-192
+
+Task #1: Binary Flip
+
+ You are given a positive integer, $n.
+
+ Write a script to find the binary flip.
+
+=end pod
+
+use Test;
+
+is binary-flip(5), 2, 'Example 1';
+is binary-flip(4), 3, 'Example 2';
+is binary-flip(6), 1, 'Example 3';
+
+done-testing;
+
+#
+#
+# METHOD
+
+sub binary-flip(Int $n is copy --> Int) {
+ return $n.base(2)
+ .comb()
+ .map(-> $v { ($v == 0)??(1)!!(0) })
+ .join(q{})
+ .parse-base(2);
+}
diff --git a/challenge-192/mohammad-anwar/swift/ch-1.swift b/challenge-192/mohammad-anwar/swift/ch-1.swift
new file mode 100644
index 0000000000..3677ed01a9
--- /dev/null
+++ b/challenge-192/mohammad-anwar/swift/ch-1.swift
@@ -0,0 +1,54 @@
+import Foundation
+
+/*
+
+Week 192:
+
+ https://perlweeklychallenge.org/blog/perl-weekly-challenge-192
+
+Task #1: Binary Flip
+
+ You are given a positive integer, $n.
+
+ Write a script to find the binary flip.
+
+ACTION:
+
+ $ swift ch-1.swift 5
+ $ swift ch-1.swift 4
+ $ swift ch-1.swift 6
+
+*/
+
+enum ParamError: Error {
+ case missingNumber
+ case invalidNumber
+}
+
+do {
+ let paramCount:Int = Int(CommandLine.argc)
+
+ if paramCount <= 1 {
+ throw ParamError.missingNumber
+ }
+
+ let num:Int = Int(CommandLine.arguments[1])!
+ if num > 0 {
+ let binary:String = String(num, radix: 2)
+ var flipped:String = ""
+ Array(binary).forEach{ flipped += ($0 == "0") ? "1" : "0" }
+ print(Int(flipped, radix: 2)!)
+ }
+ else {
+ throw ParamError.invalidNumber
+ }
+}
+catch ParamError.missingNumber {
+ print("Missing number.")
+}
+catch ParamError.invalidNumber {
+ print("Invalid number.")
+}
+catch let error {
+ print(error)
+}