aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-10-21 18:53:18 +0100
committerGitHub <noreply@github.com>2021-10-21 18:53:18 +0100
commit40ec0d667785485f416cbdf4bf1009d85fcd6c44 (patch)
treef71bdc8699bd2197ab1089ff0f57bc2d2de42252
parent22e9c6d4521c6b2cbe0d8792d4a18b3f50c9706f (diff)
parentcb9f5d2247cec6070eea3595ab2dc1e785fb03d3 (diff)
downloadperlweeklychallenge-club-40ec0d667785485f416cbdf4bf1009d85fcd6c44.tar.gz
perlweeklychallenge-club-40ec0d667785485f416cbdf4bf1009d85fcd6c44.tar.bz2
perlweeklychallenge-club-40ec0d667785485f416cbdf4bf1009d85fcd6c44.zip
Merge pull request #5063 from pauloscustodio/devel
Add Perl and Python solutions to challenge 132
-rw-r--r--challenge-001/paulo-custodio/update.sh12
-rw-r--r--challenge-131/paulo-custodio/perl/ch-1.pl43
-rw-r--r--challenge-131/paulo-custodio/perl/ch-2.pl51
-rw-r--r--challenge-131/paulo-custodio/python/ch-1.py37
-rw-r--r--challenge-131/paulo-custodio/python/ch-2.py43
-rw-r--r--challenge-131/paulo-custodio/t/test-1.yaml20
-rw-r--r--challenge-131/paulo-custodio/t/test-2.yaml18
-rw-r--r--challenge-131/paulo-custodio/test.pl4
-rw-r--r--challenge-132/paulo-custodio/perl/ch-1.pl60
-rw-r--r--challenge-132/paulo-custodio/perl/ch-2.pl100
-rw-r--r--challenge-132/paulo-custodio/python/ch-1.py50
-rw-r--r--challenge-132/paulo-custodio/python/ch-2.py100
-rw-r--r--challenge-132/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-132/paulo-custodio/t/test-2.yaml13
-rw-r--r--challenge-132/paulo-custodio/test.pl4
-rw-r--r--challenge-133/paulo-custodio/python/ch-2.py109
16 files changed, 630 insertions, 49 deletions
diff --git a/challenge-001/paulo-custodio/update.sh b/challenge-001/paulo-custodio/update.sh
new file mode 100644
index 0000000000..83d7cebe71
--- /dev/null
+++ b/challenge-001/paulo-custodio/update.sh
@@ -0,0 +1,12 @@
+#!/bin/sh -ex
+
+git checkout master
+git pull
+git fetch upstream
+git merge upstream/master --ff-only
+git push -u origin master
+
+git checkout devel
+git pull
+git rebase master
+git push --force -u origin devel
diff --git a/challenge-131/paulo-custodio/perl/ch-1.pl b/challenge-131/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..544735f33e
--- /dev/null
+++ b/challenge-131/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl
+
+# TASK #1 > Consecutive Arrays
+# Submitted by: Mark Anderson
+# You are given a sorted list of unique positive integers.
+#
+# Write a script to return list of arrays where the arrays are consecutive
+# integers.
+#
+# Example 1:
+# Input: (1, 2, 3, 6, 7, 8, 9)
+# Output: ([1, 2, 3], [6, 7, 8, 9])
+# Example 2:
+# Input: (11, 12, 14, 17, 18, 19)
+# Output: ([11, 12], [14], [17, 18, 19])
+# Example 3:
+# Input: (2, 4, 6, 8)
+# Output: ([2], [4], [6], [8])
+# Example 4:
+# Input: (1, 2, 3, 4, 5)
+# Output: ([1, 2, 3, 4, 5])
+
+use Modern::Perl;
+use Data::Dump 'dump';
+
+my @input = @ARGV;
+my @output = cons_arrays(@input);
+say "[".join(", ", map {"[".join(", ", @$_)."]"} @output)."]";
+
+sub cons_arrays {
+ my(@input) = @_;
+ my @output = [shift @input];
+ while (@input) {
+ my $n = shift @input;
+ if ($n == $output[-1][-1] + 1) {
+ push @{$output[-1]}, $n;
+ }
+ else {
+ push @output, [$n];
+ }
+ }
+ return @output;
+}
diff --git a/challenge-131/paulo-custodio/perl/ch-2.pl b/challenge-131/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..d37470a793
--- /dev/null
+++ b/challenge-131/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+
+# TASK #2 > Find Pairs
+# Submitted by: Yary
+# You are given a string of delimiter pairs and a string to search.
+#
+# Write a script to return two strings, the first with any characters
+# matching the "opening character" set, the second with any matching
+# the "closing character" set.
+#
+# Example 1:
+# Input:
+# Delimiter pairs: ""[]()
+# Search String: "I like (parens) and the Apple ][+" they said.
+#
+# Output:
+# "(["
+# ")]"
+# Example 2:
+# Input:
+# Delimiter pairs: **//<>
+# Search String: /* This is a comment (in some languages) */ <could be a tag>
+#
+# Output:
+# /**/<
+# /**/>
+
+use Modern::Perl;
+
+my $delims = <>;
+my $string = <>;
+
+my $open_delims = "[";
+my $close_delims = "[";
+while (length($delims) >= 2) {
+ $open_delims .= "\\".substr($delims,0,1);
+ $close_delims .= "\\".substr($delims,1,1);
+ $delims = substr($delims,2);
+}
+$open_delims .= "]";
+$close_delims .= "]";
+
+my $open_string;
+my $close_string;
+for my $c (split //, $string) {
+ $open_string .= $c if $c =~ /$open_delims/;
+ $close_string .= $c if $c =~ /$close_delims/;
+}
+
+say $open_string;
+say $close_string;
diff --git a/challenge-131/paulo-custodio/python/ch-1.py b/challenge-131/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..3f047245c9
--- /dev/null
+++ b/challenge-131/paulo-custodio/python/ch-1.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+
+# TASK #1 > Consecutive Arrays
+# Submitted by: Mark Anderson
+# You are given a sorted list of unique positive integers.
+#
+# Write a script to return list of arrays where the arrays are consecutive
+# integers.
+#
+# Example 1:
+# Input: (1, 2, 3, 6, 7, 8, 9)
+# Output: ([1, 2, 3], [6, 7, 8, 9])
+# Example 2:
+# Input: (11, 12, 14, 17, 18, 19)
+# Output: ([11, 12], [14], [17, 18, 19])
+# Example 3:
+# Input: (2, 4, 6, 8)
+# Output: ([2], [4], [6], [8])
+# Example 4:
+# Input: (1, 2, 3, 4, 5)
+# Output: ([1, 2, 3, 4, 5])
+
+import sys
+
+def cons_arrays(input):
+ output = [[input.pop(0)]]
+ while len(input) > 0:
+ n = input.pop(0)
+ if n == output[-1][-1] + 1:
+ output[-1].append(n)
+ else:
+ output.append([n])
+ return output
+
+input = [int(x) for x in sys.argv[1:]]
+output = cons_arrays(input)
+print(output) \ No newline at end of file
diff --git a/challenge-131/paulo-custodio/python/ch-2.py b/challenge-131/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..52fe765741
--- /dev/null
+++ b/challenge-131/paulo-custodio/python/ch-2.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+# TASK #2 > Find Pairs
+# Submitted by: Yary
+# You are given a string of delimiter pairs and a string to search.
+#
+# Write a script to return two strings, the first with any characters
+# matching the "opening character" set, the second with any matching
+# the "closing character" set.
+#
+# Example 1:
+# Input:
+# Delimiter pairs: ""[]()
+# Search String: "I like (parens) and the Apple ][+" they said.
+#
+# Output:
+# "(["
+# ")]"
+# Example 2:
+# Input:
+# Delimiter pairs: **//<>
+# Search String: /* This is a comment (in some languages) */ <could be a tag>
+#
+# Output:
+# /**/<
+# /**/>
+
+delims = input()
+string = input()
+
+open_delims = delims[0::2]
+close_delims = delims[1::2]
+
+open_string = ""
+close_string = ""
+for c in string:
+ if c in open_delims:
+ open_string += c
+ if c in close_delims:
+ close_string += c
+
+print(open_string)
+print(close_string)
diff --git a/challenge-131/paulo-custodio/t/test-1.yaml b/challenge-131/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..dff99ecb49
--- /dev/null
+++ b/challenge-131/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 1 2 3 6 7 8 9
+ input:
+ output: [[1, 2, 3], [6, 7, 8, 9]]
+- setup:
+ cleanup:
+ args: 11 12 14 17 18 19
+ input:
+ output: [[11, 12], [14], [17, 18, 19]]
+- setup:
+ cleanup:
+ args: 2 4 6 8
+ input:
+ output: [[2], [4], [6], [8]]
+- setup:
+ cleanup:
+ args: 1 2 3 4 5
+ input:
+ output: [[1, 2, 3, 4, 5]]
diff --git a/challenge-131/paulo-custodio/t/test-2.yaml b/challenge-131/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..9fc70d6e9a
--- /dev/null
+++ b/challenge-131/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,18 @@
+- setup:
+ cleanup:
+ args:
+ input: |
+ ""[]()
+ "I like (parens) and the Apple ][+" they said.
+ output: |
+ "(["
+ ")]"
+- setup:
+ cleanup:
+ args:
+ input: |
+ **//<>
+ /* This is a comment (in some languages) */ <could be a tag>
+ output: |
+ /**/<
+ /**/>
diff --git a/challenge-131/paulo-custodio/test.pl b/challenge-131/paulo-custodio/test.pl
new file mode 100644
index 0000000000..ba6c37260b
--- /dev/null
+++ b/challenge-131/paulo-custodio/test.pl
@@ -0,0 +1,4 @@
+#!/usr/bin/env perl
+use Modern::Perl;
+use Test::More;
+require '../../challenge-001/paulo-custodio/test.pl';
diff --git a/challenge-132/paulo-custodio/perl/ch-1.pl b/challenge-132/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..5b5990ef2c
--- /dev/null
+++ b/challenge-132/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl
+
+# TASK #1 > Mirror Dates
+# Submitted by: Mark Anderson
+# You are given a date (yyyy/mm/dd).
+#
+# Assuming, the given date is your date of birth. Write a script to find the
+# mirror dates of the given date.
+#
+# Dave Cross has built cool site that does something similar.
+#
+# Assuming today is 2021/09/22.
+# Example 1:
+# Input: 2021/09/18
+# Output: 2021/09/14, 2021/09/26
+#
+# On the date you were born, someone who was your current age, would have
+# been born on 2021/09/14.
+# Someone born today will be your current age on 2021/09/26.
+# Example 2:
+# Input: 1975/10/10
+# Output: 1929/10/27, 2067/09/05
+#
+# On the date you were born, someone who was your current age, would have
+# been born on 1929/10/27.
+# Someone born today will be your current age on 2067/09/05.
+# Example 3:
+# Input: 1967/02/14
+# Output: 1912/07/08, 2076/04/30
+#
+# On the date you were born, someone who was your current age, would have
+# been born on 1912/07/08.
+# Someone born today will be your current age on 2076/04/30.
+
+use Modern::Perl;
+use DateTime;
+
+@ARGV==2 or die "Usage: ch-1.pl birth-date today\n";
+my($birth_text, $today_text) = @ARGV;
+my $birth = parse_date($birth_text);
+my $today = parse_date($today_text);
+
+my $days = $birth->delta_days($today);
+my $mirror1 = $birth->clone->subtract($days);
+my $mirror2 = $today->clone->add($days);
+
+say $mirror1->ymd('/'), ", ", $mirror2->ymd('/');
+
+sub parse_date {
+ my($str) = @_;
+ $str =~ m{^(\d{4})/(\d{2})/(\d{2})$}
+ or die "date format should be yyyy/mm/dd\n";
+ my($yy,$mm,$dd) = ($1,$2,$3);
+ my $date = DateTime->new(
+ year => $yy,
+ month => $mm,
+ day => $dd,
+ );
+ return $date;
+}
diff --git a/challenge-132/paulo-custodio/perl/ch-2.pl b/challenge-132/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..2fb0f1ca4f
--- /dev/null
+++ b/challenge-132/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,100 @@
+#!/usr/bin/env perl
+
+# TASK #2 > Hash Join
+# Submitted by: Mohammad S Anwar
+# Write a script to implement Hash Join algorithm as suggested
+# by wikipedia.
+#
+# 1. For each tuple r in the build input R
+# 1.1 Add r to the in-memory hash table
+# 1.2 If the size of the hash table equals the maximum
+# in-memory size:
+# 1.2.1 Scan the probe input S, and add matching join
+# tuples to the output relation
+# 1.2.2 Reset the hash table, and continue scanning the
+# build input R
+# 2. Do a final scan of the probe input S and add the resulting
+# join tuples to the output relation
+# Example
+# Input:
+#
+# @player_ages = (
+# [20, "Alex" ],
+# [28, "Joe" ],
+# [38, "Mike" ],
+# [18, "Alex" ],
+# [25, "David" ],
+# [18, "Simon" ],
+# );
+#
+# @player_names = (
+# ["Alex", "Stewart"],
+# ["Joe", "Root" ],
+# ["Mike", "Gatting"],
+# ["Joe", "Blog" ],
+# ["Alex", "Jones" ],
+# ["Simon","Duane" ],
+# );
+#
+# Output:
+#
+# Based on index = 1 of @players_age and
+# index = 0 of @players_name.
+#
+# 20, "Alex", "Stewart"
+# 20, "Alex", "Jones"
+# 18, "Alex", "Stewart"
+# 18, "Alex", "Jones"
+# 28, "Joe", "Root"
+# 28, "Joe", "Blog"
+# 38, "Mike", "Gatting"
+# 18, "Simon", "Duane"
+
+use Modern::Perl;
+use Data::Dump 'dump';
+
+my @player_ages = (
+ [20, "Alex" ],
+ [28, "Joe" ],
+ [38, "Mike" ],
+ [18, "Alex" ],
+ [25, "David" ],
+ [18, "Simon" ],
+);
+my $player_ages_key = 1;
+
+my @player_names = (
+ ["Alex", "Stewart"],
+ ["Joe", "Root" ],
+ ["Mike", "Gatting"],
+ ["Joe", "Blog" ],
+ ["Alex", "Jones" ],
+ ["Simon","Duane" ],
+);
+my $player_names_key = 0;
+
+my @result = hash_join(\@player_ages, $player_ages_key,
+ \@player_names, $player_names_key);
+for (@result) {
+ say join(", ", map {dump($_)} @$_);
+}
+
+sub hash_join {
+ my($table1, $key1, $table2, $key2) = @_;
+ my %build;
+ for my $row (@$table1) {
+ my $key = $row->[$key1];
+ $build{$key} ||= [];
+ push @{$build{$key}}, $row;
+ }
+ my @result;
+ for my $row_probe (@$table2) {
+ my $key = $row_probe->[$key2];
+ for my $row_build (@{$build{$key}}) {
+ my @row = (@$row_build,
+ @{$row_probe}[0..$key2-1, $key2+1..$#$row_probe]);
+ push @result, \@row;
+ }
+ }
+ return @result;
+}
diff --git a/challenge-132/paulo-custodio/python/ch-1.py b/challenge-132/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..adc4693080
--- /dev/null
+++ b/challenge-132/paulo-custodio/python/ch-1.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+
+# TASK #1 > Mirror Dates
+# Submitted by: Mark Anderson
+# You are given a date (yyyy/mm/dd).
+#
+# Assuming, the given date is your date of birth. Write a script to find the
+# mirror dates of the given date.
+#
+# Dave Cross has built cool site that does something similar.
+#
+# Assuming today is 2021/09/22.
+# Example 1:
+# Input: 2021/09/18
+# Output: 2021/09/14, 2021/09/26
+#
+# On the date you were born, someone who was your current age, would have
+# been born on 2021/09/14.
+# Someone born today will be your current age on 2021/09/26.
+# Example 2:
+# Input: 1975/10/10
+# Output: 1929/10/27, 2067/09/05
+#
+# On the date you were born, someone who was your current age, would have
+# been born on 1929/10/27.
+# Someone born today will be your current age on 2067/09/05.
+# Example 3:
+# Input: 1967/02/14
+# Output: 1912/07/08, 2076/04/30
+#
+# On the date you were born, someone who was your current age, would have
+# been born on 1912/07/08.
+# Someone born today will be your current age on 2076/04/30.
+
+import sys
+import datetime
+
+def parse_date(s):
+ return datetime.datetime.strptime(s, "%Y/%m/%d")
+
+def format_date(d):
+ return d.strftime("%Y/%m/%d")
+
+birth = parse_date(sys.argv[1])
+today = parse_date(sys.argv[2])
+days = today - birth
+mirror1 = birth - days
+mirror2 = today + days
+
+print(format_date(mirror1)+", "+format_date(mirror2))
diff --git a/challenge-132/paulo-custodio/python/ch-2.py b/challenge-132/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..6d48ba2c8c
--- /dev/null
+++ b/challenge-132/paulo-custodio/python/ch-2.py
@@ -0,0 +1,100 @@
+#!/usr/bin/env python
+
+# TASK #2 > Hash Join
+# Submitted by: Mohammad S Anwar
+# Write a script to implement Hash Join algorithm as suggested
+# by wikipedia.
+#
+# 1. For each tuple r in the build input R
+# 1.1 Add r to the in-memory hash table
+# 1.2 If the size of the hash table equals the maximum
+# in-memory size:
+# 1.2.1 Scan the probe input S, and add matching join
+# tuples to the output relation
+# 1.2.2 Reset the hash table, and continue scanning the
+# build input R
+# 2. Do a final scan of the probe input S and add the resulting
+# join tuples to the output relation
+# Example
+# Input:
+#
+# @player_ages = (
+# [20, "Alex" ],
+# [28, "Joe" ],
+# [38, "Mike" ],
+# [18, "Alex" ],
+# [25, "David" ],
+# [18, "Simon" ],
+# );
+#
+# @player_names = (
+# ["Alex", "Stewart"],
+# ["Joe", "Root" ],
+# ["Mike", "Gatting"],
+# ["Joe", "Blog" ],
+# ["Alex", "Jones" ],
+# ["Simon","Duane" ],
+# );
+#
+# Output:
+#
+# Based on index = 1 of @players_age and
+# index = 0 of @players_name.
+#
+# 20, "Alex", "Stewart"
+# 20, "Alex", "Jones"
+# 18, "Alex", "Stewart"
+# 18, "Alex", "Jones"
+# 28, "Joe", "Root"
+# 28, "Joe", "Blog"
+# 38, "Mike", "Gatting"
+# 18, "Simon", "Duane"
+
+player_ages = [
+ [20, "Alex" ],
+ [28, "Joe" ],
+ [38, "Mike" ],
+ [18, "Alex" ],
+ [25, "David" ],
+ [18, "Simon" ],
+]
+player_ages_key = 1
+
+player_names = [
+ ["Alex", "Stewart"],
+ ["Joe", "Root" ],
+ ["Mike", "Gatting"],
+ ["Joe", "Blog" ],
+ ["Alex", "Jones" ],
+ ["Simon","Duane" ],
+]
+player_names_key = 0
+
+def format_col(s):
+ if type(s) == int:
+ return str(s)
+ else:
+ return '"'+s+'"'
+
+def hash_join(table1, key1, table2, key2):
+ build = {}
+ for row in table1:
+ key = row[key1]
+ if key not in build:
+ build[key] = []
+ build[key].append(row)
+ result = []
+ for row_probe in table2:
+ key = row_probe[key2]
+ for row_build in build[key]:
+ row = row_build + \
+ row_probe[:key2] + \
+ row_probe[key2+1:]
+ row = [format_col(x) for x in row]
+ result.append(row)
+ return result
+
+result = hash_join(player_ages, player_ages_key,
+ player_names, player_names_key)
+for row in result:
+ print(', '.join(row))
diff --git a/challenge-132/paulo-custodio/t/test-1.yaml b/challenge-132/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..32cff5c56f
--- /dev/null
+++ b/challenge-132/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 2021/09/18 2021/09/22
+ input:
+ output: 2021/09/14, 2021/09/26
+- setup:
+ cleanup:
+ args: 1975/10/10 2021/09/22
+ input:
+ output: 1929/10/27, 2067/09/05
+- setup:
+ cleanup:
+ args: 1967/02/14 2021/09/22
+ input:
+ output: 1912/07/08, 2076/04/30
diff --git a/challenge-132/paulo-custodio/t/test-2.yaml b/challenge-132/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..f57a59a386
--- /dev/null
+++ b/challenge-132/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,13 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: |
+ 20, "Alex", "Stewart"
+ 18, "Alex", "Stewart"
+ 28, "Joe", "Root"
+ 38, "Mike", "Gatting"
+ 28, "Joe", "Blog"
+ 20, "Alex", "Jones"
+ 18, "Alex", "Jones"
+ 18, "Simon", "Duane" \ No newline at end of file
diff --git a/challenge-132/paulo-custodio/test.pl b/challenge-132/paulo-custodio/test.pl
new file mode 100644
index 0000000000..ba6c37260b
--- /dev/null
+++ b/challenge-132/paulo-custodio/test.pl
@@ -0,0 +1,4 @@
+#!/usr/bin/env perl
+use Modern::Perl;
+use Test::More;
+require '../../challenge-001/paulo-custodio/test.pl';
diff --git a/challenge-133/paulo-custodio/python/ch-2.py b/challenge-133/paulo-custodio/python/ch-2.py
index e03c74f7ab..1cab0e4e43 100644
--- a/challenge-133/paulo-custodio/python/ch-2.py
+++ b/challenge-133/paulo-custodio/python/ch-2.py
@@ -1,49 +1,60 @@
-#!/usr/bin/env python3
-
-# TASK #2 > Smith Numbers
-# Submitted by: Mohammad S Anwar
-# Write a script to generate first 10 Smith Numbers in base 10.
-#
-# According to Wikipedia:
-#
-# In number theory, a Smith number is a composite number for which, in a given
-# number base, the sum of its digits is equal to the sum of the digits in its
-# prime factorization in the given number base.
-#
-
-from sympy import isprime
-
-def get_prime_factors(n):
- i = 2
- prime_factors = []
- while i*i <= n:
- if n%i == 0:
- prime_factors.append(i)
- n //= i
- else:
- i += 1
-
- if n>1:
- prime_factors.append(n)
-
- return prime_factors
-
-def is_smith(n):
- if isprime(n):
- return False
- digits = [int(x) for x in str(n)]
- sum1 = sum(digits)
- factors = ''.join([str(x) for x in get_prime_factors(n)])
- fact_digits = [int(x) for x in factors]
- sum2 = sum(fact_digits)
- return sum1==sum2
-
-n=1
-count=0
-while count<10:
- n+=1
- if is_smith(n):
- print(n)
- count+=1
-
- \ No newline at end of file
+#!/usr/bin/env python3
+
+# TASK #2 > Smith Numbers
+# Submitted by: Mohammad S Anwar
+# Write a script to generate first 10 Smith Numbers in base 10.
+#
+# According to Wikipedia:
+#
+# In number theory, a Smith number is a composite number for which, in a given
+# number base, the sum of its digits is equal to the sum of the digits in its
+# prime factorization in the given number base.
+#
+
+def is_prime(n):
+ if n <= 1:
+ return 0
+ elif n <= 3:
+ return 1
+ elif n % 2 == 0 or n % 3 == 0:
+ return 0
+ else:
+ for i in range(5, n+1, 6):
+ if i*i>n:
+ break
+ if n % i == 0 or n % (i+2) == 0:
+ return 0
+ return 1
+
+def get_prime_factors(n):
+ i = 2
+ prime_factors = []
+ while i*i <= n:
+ if n%i == 0:
+ prime_factors.append(i)
+ n //= i
+ else:
+ i += 1
+
+ if n>1:
+ prime_factors.append(n)
+
+ return prime_factors
+
+def is_smith(n):
+ if is_prime(n):
+ return False
+ digits = [int(x) for x in str(n)]
+ sum1 = sum(digits)
+ factors = ''.join([str(x) for x in get_prime_factors(n)])
+ fact_digits = [int(x) for x in factors]
+ sum2 = sum(fact_digits)
+ return sum1==sum2
+
+n=1
+count=0
+while count<10:
+ n+=1
+ if is_smith(n):
+ print(n)
+ count+=1