Compare commits

...

6 Commits

Author SHA1 Message Date
3c7bf475bb Rename the folders. 2021-10-31 00:06:55 +02:00
7fa280187b Solve 2 2021-10-31 00:04:40 +02:00
0efa8136a2 Use Url class in 1 2021-10-31 00:04:23 +02:00
66203a72cf Use Url class in 0 2021-10-31 00:04:15 +02:00
4076dbc44e Solve 03 2021-10-31 00:03:54 +02:00
7af6522140 Add utils module. 2021-10-31 00:03:22 +02:00
10 changed files with 2606 additions and 13 deletions

4
0/0.py
View File

@ -1,4 +0,0 @@
solution = 2 ** 38
if __name__ == '__main__':
print(solution)

11
00/0.py Normal file
View File

@ -0,0 +1,11 @@
from utils.Url import Url
def main():
solution = 2 ** 38
builder = Url()
builder.solution = solution
print(builder.url)
if __name__ == '__main__':
main()

View File

@ -1,6 +1,5 @@
import string import string
from utils.Url import Url
URL = 'http://www.pythonchallenge.com/pc/def/map.html'
SECRET = ("g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq " SECRET = ("g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq "
"ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw " "ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw "
@ -27,13 +26,11 @@ def shift_chars(riddle_string, shift_by_int):
def main(): def main():
shifted_secret = shift_chars(SECRET, 2) shifted_secret = shift_chars(SECRET, 2)
print('Shifted secret:', shifted_secret) print(shifted_secret)
shifted_url = shift_chars(URL, 2) shifted_url = shift_chars('map', 2)
print('Shifted url:', shifted_url) builder = Url()
new_url = URL.replace('map', shifted_url.split('/')[-1][:3]) builder.solution = shifted_url
print('New url:', new_url) print(builder.url)
if __name__ == '__main__': if __name__ == '__main__':

29
02/counter.py Normal file
View File

@ -0,0 +1,29 @@
from collections import Counter
from utils.Url import Url
def count_string():
with open('string.txt') as file:
string = file.read()
return Counter(string)
def get_lowest(counted_dict):
lowest_string = ''
for key in counted_dict:
if counted_dict[key] == 1:
lowest_string += key
return lowest_string
def main():
counted = count_string()
lowest_string = get_lowest(counted)
builder = Url()
builder.solution = lowest_string
print(builder.url)
if __name__ == '__main__':
main()

1220
02/string.txt Normal file

File diff suppressed because it is too large Load Diff

23
03/find_pattern.py Normal file
View File

@ -0,0 +1,23 @@
from utils.Url import Url
from utils.read import read_string
import re
def find_pattern(pattern_string):
string = read_string('string.txt')
regex = re.compile(pattern_string)
matches = regex.findall(string)
return ''.join(matches)
def main():
pattern_string = r'[a-z][A-Z]{3}(?P<solution>[a-z]){01}[A-Z]{3}[a-z]'
string = find_pattern(pattern_string)
builder = Url()
builder.slug = 'php'
builder.solution = string
print(builder.url)
if __name__ == '__main__':
main()

1250
03/string.txt Normal file

File diff suppressed because it is too large Load Diff

53
utils/Url.py Normal file
View File

@ -0,0 +1,53 @@
class Url:
"""
Class to build the python challenge url with the solution string and an
optional slug.
"""
def __init__(self):
self._url = ''
self._slug = 'html'
self._solution = ''
BASE_URL = 'http://www.pythonchallenge.com/pc/def'
@property
def url(self):
"""
Gets the url string of the builder.
:return: url string
"""
return f'{self.BASE_URL}/{self.solution}.{self.slug}'
@property
def solution(self):
"""
Returns the solution string
:return: solution string
"""
return self._solution
@solution.setter
def solution(self, solution):
"""
Sets the solution string
:param solution: str
"""
self._solution = solution
@property
def slug(self):
"""
Returns the slug string
:return: slug string
"""
return self._slug
@slug.setter
def slug(self, slug):
"""
Sets the slug string
:param slug: str
:return: slug string
"""
self._slug = slug

0
utils/__init__.py Normal file
View File

14
utils/read.py Normal file
View File

@ -0,0 +1,14 @@
"""
Sone utility functions that can be reused for the python challenge.
"""
def read_string(path):
"""
Read a string from a file provided by a path string.
:param path: A path string
:return: file content as a string
"""
with open(path) as file:
string = file.read()
return string