diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-07-10 18:56:53 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-07-10 18:56:53 +0200 |
| commit | a5f6057bc0293d12abb6ad6864a72196fa417d6e (patch) | |
| tree | 81803a2c40847151bbf0329a4bbc350a3758205a | |
| parent | d0829f697ac3fa9a5791db99ff1686b1acecc4c9 (diff) | |
| download | perlweeklychallenge-club-a5f6057bc0293d12abb6ad6864a72196fa417d6e.tar.gz perlweeklychallenge-club-a5f6057bc0293d12abb6ad6864a72196fa417d6e.tar.bz2 perlweeklychallenge-club-a5f6057bc0293d12abb6ad6864a72196fa417d6e.zip | |
feat(challenge-166/lubos-kolouch/perl,python/): Challenge 166 LK Perl Python
| -rw-r--r-- | challenge-166/lubos-kolouch/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-166/lubos-kolouch/perl/ch-2.pl | 20 | ||||
| -rw-r--r-- | challenge-166/lubos-kolouch/python/ch-1.py | 27 | ||||
| -rw-r--r-- | challenge-166/lubos-kolouch/python/ch-2.py | 29 |
4 files changed, 113 insertions, 0 deletions
diff --git a/challenge-166/lubos-kolouch/perl/ch-1.pl b/challenge-166/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..4e526699d0 --- /dev/null +++ b/challenge-166/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,37 @@ +use strict; +use warnings; + +sub hex_words { + my ($dictionary_path) = @_; + my %hex_map = + ( "o" => "0", "l" => "1", "i" => "1", "s" => "5", "t" => "7" ); + my $valid_chars = "abcdef" . join( "", values %hex_map ); + my @words; + + open my $fh, '<', $dictionary_path or die $!; + while ( my $line = <$fh> ) { + chomp $line; + $line = lc $line; + if ( 2 <= length($line) + && length($line) <= 8 + && $line =~ m/^[${valid_chars}]+$/ ) + { + $line =~ s/(.)/exists($hex_map{$1}) ? $hex_map{$1} : $1/eg; + push @words, "0x$line"; + } + } + close $fh; + + return @words; +} + +# path to dictionary file +my $dictionary_path = "../../../data/dictionary.txt"; + +# get hex words +my @hex_words = hex_words($dictionary_path); + +# print hex words +for my $word (@hex_words) { + print "$word\n"; +} diff --git a/challenge-166/lubos-kolouch/perl/ch-2.pl b/challenge-166/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..fe9d266b86 --- /dev/null +++ b/challenge-166/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; +use File::Slurp; + +my @dirs = ( 'dir_a', 'dir_b', 'dir_c' ); +my %all_files; + +foreach my $dir (@dirs) { + my @files = read_dir($dir); + foreach my $file (@files) { + push @{ $all_files{$file} }, $dir; + } +} + +foreach my $file ( keys %all_files ) { + if ( scalar @{ $all_files{$file} } != scalar @dirs ) { + print "$file is missing in ", + join( ", ", grep { not $_ ~~ @{ $all_files{$file} } } @dirs ), "\n"; + } +} diff --git a/challenge-166/lubos-kolouch/python/ch-1.py b/challenge-166/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..fef9376358 --- /dev/null +++ b/challenge-166/lubos-kolouch/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def hex_words(dictionary_path): + hex_map = {"o": "0", "l": "1", "i": "1", "s": "5", "t": "7"} + valid_chars = set("abcdef" + "".join(hex_map.values())) + words = [] + + with open(dictionary_path, "r") as f: + for line in f: + word = line.strip().lower() + if 2 <= len(word) <= 8 and set(word).issubset(valid_chars): + words.append("0x" + "".join(hex_map.get(c, c) for c in word)) + + return words + + +# path to dictionary file +dictionary_path = "../../../data/dictionary.txt" + +# get hex words +hex_words = hex_words(dictionary_path) + +# print hex words +for word in hex_words: + print(word) diff --git a/challenge-166/lubos-kolouch/python/ch-2.py b/challenge-166/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..48996dd42e --- /dev/null +++ b/challenge-166/lubos-kolouch/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os + + +def k_directory_diff(dirs): + all_files = set() + dir_files = {} + + for directory in dirs: + files = set(os.listdir(directory)) + all_files |= files + dir_files[directory] = files + + missing_files = {f: [] for f in all_files} + + for directory, files in dir_files.items(): + for file in all_files: + if file not in files: + missing_files[file].append(directory) + + for file, directories in missing_files.items(): + if directories: + print(file, "is missing in", ", ".join(directories)) + + +dirs = ["dir_a", "dir_b", "dir_c"] +k_directory_diff(dirs) |
