Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
TheAlgorithmPython/scripts/validate_solutions.py at Factorial · silam/TheAlgorithmPython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
silam
/
TheAlgorithmPython
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
Factorial
Breadcrumbs
TheAlgorithmPython
/
scripts
/
validate_solutions.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
96 lines (80 loc) · 3.41 KB
Factorial
Breadcrumbs
TheAlgorithmPython
/
scripts
/
validate_solutions.py
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
96 lines (80 loc) · 3.41 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
import
hashlib
import
importlib
.
util
import
json
import
os
import
pathlib
from
types
import
ModuleType
import
pytest
import
requests
PROJECT_EULER_DIR_PATH
=
pathlib
.
Path
.
cwd
().
joinpath
(
"project_euler"
)
PROJECT_EULER_ANSWERS_PATH
=
pathlib
.
Path
.
cwd
().
joinpath
(
"scripts"
,
"project_euler_answers.json"
)
with
open
(
PROJECT_EULER_ANSWERS_PATH
)
as
file_handle
:
PROBLEM_ANSWERS
:
dict
[
str
,
str
]
=
json
.
load
(
file_handle
)
def
convert_path_to_module
(
file_path
:
pathlib
.
Path
)
->
ModuleType
:
"""Converts a file path to a Python module"""
spec
=
importlib
.
util
.
spec_from_file_location
(
file_path
.
name
,
str
(
file_path
))
module
=
importlib
.
util
.
module_from_spec
(
spec
)
# type: ignore
spec
.
loader
.
exec_module
(
module
)
# type: ignore
return
module
def
all_solution_file_paths
()
->
list
[
pathlib
.
Path
]:
"""Collects all the solution file path in the Project Euler directory"""
solution_file_paths
=
[]
for
problem_dir_path
in
PROJECT_EULER_DIR_PATH
.
iterdir
():
if
problem_dir_path
.
is_file
()
or
problem_dir_path
.
name
.
startswith
(
"_"
):
continue
for
file_path
in
problem_dir_path
.
iterdir
():
if
file_path
.
suffix
!=
".py"
or
file_path
.
name
.
startswith
((
"_"
,
"test"
)):
continue
solution_file_paths
.
append
(
file_path
)
return
solution_file_paths
def
get_files_url
()
->
str
:
"""Return the pull request number which triggered this action."""
with
open
(
os
.
environ
[
"GITHUB_EVENT_PATH"
])
as
file
:
event
=
json
.
load
(
file
)
return
event
[
"pull_request"
][
"url"
]
+
"/files"
def
added_solution_file_path
()
->
list
[
pathlib
.
Path
]:
"""Collects only the solution file path which got added in the current
pull request.
This will only be triggered if the script is ran from GitHub Actions.
"""
solution_file_paths
=
[]
headers
=
{
"Accept"
:
"application/vnd.github.v3+json"
,
"Authorization"
:
"token "
+
os
.
environ
[
"GITHUB_TOKEN"
],
}
files
=
requests
.
get
(
get_files_url
(),
headers
=
headers
).
json
()
for
file
in
files
:
filepath
=
pathlib
.
Path
.
cwd
().
joinpath
(
file
[
"filename"
])
if
(
filepath
.
suffix
!=
".py"
or
filepath
.
name
.
startswith
((
"_"
,
"test"
))
or
not
filepath
.
name
.
startswith
(
"sol"
)
):
continue
solution_file_paths
.
append
(
filepath
)
return
solution_file_paths
def
collect_solution_file_paths
()
->
list
[
pathlib
.
Path
]:
if
os
.
environ
.
get
(
"CI"
)
and
os
.
environ
.
get
(
"GITHUB_EVENT_NAME"
)
==
"pull_request"
:
# Return only if there are any, otherwise default to all solutions
if
filepaths
:=
added_solution_file_path
():
return
filepaths
return
all_solution_file_paths
()
@
pytest
.
mark
.
parametrize
(
"solution_path"
,
collect_solution_file_paths
(),
ids
=
lambda
path
:
f"
{
path
.
parent
.
name
}
/
{
path
.
name
}
"
,
)
def
test_project_euler
(
solution_path
:
pathlib
.
Path
)
->
None
:
"""Testing for all Project Euler solutions"""
# problem_[extract this part] and pad it with zeroes for width 3
problem_number
:
str
=
solution_path
.
parent
.
name
[
8
:].
zfill
(
3
)
expected
:
str
=
PROBLEM_ANSWERS
[
problem_number
]
solution_module
=
convert_path_to_module
(
solution_path
)
answer
=
str
(
solution_module
.
solution
())
# type: ignore
answer
=
hashlib
.
sha256
(
answer
.
encode
()).
hexdigest
()
assert
(
answer
==
expected
),
f"Expected solution to
{
problem_number
}
to have hash
{
expected
}
, got
{
answer
}
"
You can’t perform that action at this time.