aboutsummaryrefslogtreecommitdiff
path: root/challenge-110
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-05-25 19:00:03 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-05-25 19:00:03 +0200
commit31421edb60e97acb8fadb9e871fc0ca1c3d3c816 (patch)
tree0f751548e73b17022f37027a8f52692c80cc5be7 /challenge-110
parentb860115509e49b10448c7dcce8dd6f7ecd3d9290 (diff)
downloadperlweeklychallenge-club-31421edb60e97acb8fadb9e871fc0ca1c3d3c816.tar.gz
perlweeklychallenge-club-31421edb60e97acb8fadb9e871fc0ca1c3d3c816.tar.bz2
perlweeklychallenge-club-31421edb60e97acb8fadb9e871fc0ca1c3d3c816.zip
feat(challenge-110/lubos-kolouch/perl,python): Challenge 110 LK Perl Python
Diffstat (limited to 'challenge-110')
-rw-r--r--challenge-110/lubos-kolouch/perl/ch-1.pl16
-rw-r--r--challenge-110/lubos-kolouch/perl/ch-2.pl29
-rw-r--r--challenge-110/lubos-kolouch/python/ch-1.py20
-rw-r--r--challenge-110/lubos-kolouch/python/ch-2.py21
4 files changed, 86 insertions, 0 deletions
diff --git a/challenge-110/lubos-kolouch/perl/ch-1.pl b/challenge-110/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..696b5bc942
--- /dev/null
+++ b/challenge-110/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+my $filename = 'phone_numbers.txt';
+
+open(FH, '<', $filename) or die $!;
+
+while (<FH>) {
+ if ($_ =~ m/^(\+|\()?(\d{2,4})\)?(\s|\-)?\d{2,4}(\s|\-)?\d{2,4}(\s|\-)?\d{2,4}$/) {
+ print $_;
+ }
+}
+
+close(FH);
+
diff --git a/challenge-110/lubos-kolouch/perl/ch-2.pl b/challenge-110/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..9fad57b99f
--- /dev/null
+++ b/challenge-110/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+open my $fh, '<', 'input.txt' or die $!;
+
+my @lines = <$fh>;
+
+chomp(@lines);
+
+my @fields = map { [split /,/] } @lines;
+
+my $transposed = [];
+
+for my $row (@fields) {
+ for my $column (0 .. $#{$row}) {
+ push @{$transposed->[$column]}, $row->[$column];
+ }
+}
+
+open my $fh_out, '>', 'transposed_input.txt' or die $!;
+
+for my $row (@$transposed) {
+ print $fh_out join(',', @$row), "\n";
+}
+
+close $fh;
+close $fh_out;
+
diff --git a/challenge-110/lubos-kolouch/python/ch-1.py b/challenge-110/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..c852da1766
--- /dev/null
+++ b/challenge-110/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import re
+
+
+def valid_phone_numbers(filename):
+ with open(filename, 'r') as f:
+ contents = f.readlines()
+
+ valid_numbers = []
+
+ for line in contents:
+ if re.match(r"^(\+|\()?(\d{2,4})\)?(\s|\-)?\d{2,4}(\s|\-)?\d{2,4}(\s|\-)?\d{2,4}$", line.strip()):
+ valid_numbers.append(line.strip())
+
+ return valid_numbers
+
+
+print(valid_phone_numbers('phone_numbers.txt'))
diff --git a/challenge-110/lubos-kolouch/python/ch-2.py b/challenge-110/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..7837aca6d7
--- /dev/null
+++ b/challenge-110/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+def transpose_file(filename):
+ with open(filename, 'r') as f:
+ lines = f.readlines()
+
+ # Split each line into a list of fields
+ lines = [line.strip().split(',') for line in lines]
+
+ # Use zip() to transpose rows to columns
+ transposed = zip(*lines)
+
+ # Join the fields back together and write to a new file
+ with open('transposed_' + filename, 'w') as f:
+ for row in transposed:
+ f.write(','.join(row))
+ f.write('\n')
+
+
+transpose_file('input.txt')