SW4 Codewars Exercises #6

Open
schaermicha1 wants to merge 5 commits from sw4_codewars_exercises into master
6 changed files with 112 additions and 0 deletions

View File

@ -0,0 +1,21 @@
import pandas as pd
def export_csv_with_name_and_grade(student_name: str, grade: str):
students_df = pd.read_csv("files/students.csv", index_col=False)
grades_df = pd.read_csv("files/grades.csv", index_col=False)
students_grades_df = students_df.merge(
grades_df, left_on="Student_ID", right_on="ID"
)
solution = students_grades_df[
(students_grades_df["Name"] == student_name)
& (students_grades_df["Grade"] == grade)
]
solution[["Student_ID", "Class"]].to_csv("files/output.csv", index=False)
if __name__ == "__main__":
export_csv_with_name_and_grade("Bob", "A")

View File

@ -0,0 +1,54 @@
import typing
class Router(object):
def __init__(self):
self.routes = {}
def bind(self, path: str, method: str, func: typing.Callable):
self.routes[(path, method)] = func
def run_request(self, path: str, method: str):
return self.routes.get((path, method), lambda: "Error 404: Not Found")()
if __name__ == "__main__":
# Should handle GET routes'
router = Router()
router.bind("/hello", "GET", lambda: "hello world")
router.bind("/login", "GET", lambda: "Please log-in.")
assert router.run_request("/hello", "GET") == "hello world"
assert router.run_request("/login", "GET") == "Please log-in."
# Should handle POST routes
router = Router()
router.bind("/vote", "POST", lambda: "Voted.")
router.bind("/comment", "POST", lambda: "Comment sent.")
assert router.run_request("/vote", "POST") == "Voted."
assert router.run_request("/comment", "POST") == "Comment sent."
# Should handle mixed routes.
router = Router()
router.bind("/login", "GET", lambda: "Please log-in.")
router.bind("/login", "POST", lambda: "Logging-in.")
assert router.run_request("/login", "GET") == "Please log-in."
assert router.run_request("/login", "POST") == "Logging-in."
# Should return 404 for non-existing routes.
router = Router()
assert (
router.run_request("/this-url-does-not-exist", "GET") == "Error 404: Not Found"
)
# Should modify existing routes
router = Router()
router.bind("/page", "GET", lambda: "First binding.")
router.bind("/page", "GET", lambda: "Modified binding.")
assert router.run_request("/page", "GET") == "Modified binding."
print("All tests passed!")

View File

@ -0,0 +1,24 @@
def likes(names):
match len(names):
case 0:
return "no one likes this"
case 1:
return f"{names[0]} likes this"
case 2:
return f"{names[0]} and {names[1]} like this"
case 3:
return f"{names[0]}, {names[1]} and {names[2]} like this"
case _:
return f"{names[0]}, {names[1]} and {len(names) - 2} others like this"
if __name__ == "__main__":
assert likes([]) == "no one likes this"
assert likes(["Peter"]) == "Peter likes this"
assert likes(["Jacob", "Alex"]) == "Jacob and Alex like this"
assert likes(["Max", "John", "Mark"]) == "Max, John and Mark like this"
assert (
likes(["Alex", "Jacob", "Mark", "Max"]) == "Alex, Jacob and 2 others like this"
)
print("All tests passed!")

View File

@ -0,0 +1,8 @@
ID,Class,Grade
32155,English,A
32155,History,B
32155,Science,B
43226,English,C
43226,Math,A
43226,Science,A
43226,History,B
1 ID Class Grade
2 32155 English A
3 32155 History B
4 32155 Science B
5 43226 English C
6 43226 Math A
7 43226 Science A
8 43226 History B

View File

@ -0,0 +1,2 @@
Student_ID,Class
32155,English
1 Student_ID Class
2 32155 English

View File

@ -0,0 +1,3 @@
Name,Student_ID
Bob,32155
Ron,43226
1 Name Student_ID
2 Bob 32155
3 Ron 43226