Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finished adding questions from easy coding challenges and updated the tracking table #6

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ Gradually adding the questions in different languages (Python & Julia)

## Kattis Problems

| Problem ID | Description | Difficulty | Type | Solution |
|--------------------|--------------------------------------------------|------------|------------------------|---------------------------------------------------------------------------------------------------------------|
| hip hip | Print "Hipp hipp hurra!" 20 times | Easy | Easy Coding Challenges | <a href="/src/kattis/hip_hip.py"><img src="/imgs/python-programming-language.webp" alt="hip hip" width="50"></a> |
| storafmaeli | Check if it's anniversary | Easy | Easy Coding Challenges | <a href="/src/kattis/storafmaeli.py"><img src="/imgs/python-programming-language.webp" alt="storafmaeli" width="50"></a> |
| fyrirtækjanafn | Filter out consonants from input | Easy | Easy Coding Challenges | <a href="/src/kattis/fyrirtækjanafn.py"><img src="/imgs/python-programming-language.webp" alt="fyrirtækjanafn" width="50"></a> |
| peningar | Calculate values accumulated from circular cells | Easy | Easy Coding Challenges | <a href="/src/kattis/peningar.py"><img src="/imgs/python-programming-language.webp" alt="peningar" width="50"></a> |
| framvindustika | Print progress bar and % | Easy | Easy Coding Challenges | <a href="/src/kattis/framvindustika.py"><img src="/imgs/python-programming-language.webp" alt="framvindustika" width="50"></a> |
| Problem ID | Description | Difficulty | Type | Solution |
|----------------|--------------------------------------------------|------------|------------------------|---------------------------------------------------------------------------------------------------------------|
| hip hip | Print "Hipp hipp hurra!" 20 times | Easy | Easy Coding Challenges | [<img src="/imgs/python-programming-language.webp" alt="hip hip" width="50">](/src/kattis/hip_hip.py) |
| storafmaeli | Check if it's anniversary | Easy | Easy Coding Challenges | [<img src="/imgs/python-programming-language.webp" alt="storafmaeli" width="50">](/src/kattis/storafmaeli.py) |
| fyrirtækjanafn | Filter out consonants from input | Easy | Easy Coding Challenges | [<img src="/imgs/python-programming-language.webp" alt="fyrirtækjanafn" width="50">](/src/kattis/fyrirtækjanafn.py) |
| peningar | Calculate values accumulated from circular cells | Easy | Easy Coding Challenges | [<img src="/imgs/python-programming-language.webp" alt="peningar" width="50">](/src/kattis/peningar.py) |
| framvindustika | Print progress bar and % | Easy | Easy Coding Challenges | [<img src="/imgs/python-programming-language.webp" alt="framvindustika" width="50">](/src/kattis/framvindustika.py) |
| message | Extract letters from nested list to form a message| Easy | Easy Coding Challenges | [<img src="/imgs/python-programming-language.webp" alt="message" width="50">](/src/kattis/message.py) |
| bidendalausbid | Calculate waited time in minutes | Easy | Easy Coding Challenges | [<img src="/imgs/python-programming-language.webp" alt="bidendalausbid" width="50">](/src/kattis/bidendalausbid.py) |
| hlaupafmaeli | Check birthday for leap year | Easy | Easy Coding Challenges | [<img src="/imgs/python-programming-language.webp" alt="hlaupafmaeli" width="50">](/src/kattis/hlaupafmaeli.py) |
| lidaskipting2 | Find min and max number of competitive teams that can be formed | Easy | Easy Coding Challenges | [<img src="/imgs/python-programming-language.webp" alt="lidaskipting2" width="50">](/src/kattis/lidaskipting2.py) |
| fleytitala | Find min and max number of competitive teams that can be formed | Easy | Easy Coding Challenges | [<img src="/imgs/python-programming-language.webp" alt="fleytitala" width="50">](/src/kattis/fleytitala.py) |
24 changes: 24 additions & 0 deletions src/kattis/bidendalausbid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys
from typing import Tuple


def process_multiline_input(message: list) -> Tuple[int, int, int, int]:
h1, m1 = map(int, message[0].strip().split(":"))
h2, m2 = map(int, message[1].strip().split(":"))

return h1, m1, h2, m2


def calculate_waited_time_in_minutes(h1: int, m1: int, h2: int, m2: int) -> int:
diff_in_minutes = h2 * 60 + m2 - h1 * 60 - m1

if diff_in_minutes < 0:
diff_in_minutes += 24 * 60

return diff_in_minutes


if __name__ == "__main__":
message = sys.stdin.readlines()
h1, m1, h2, m2 = process_multiline_input(message)
print(calculate_waited_time_in_minutes(h1, m1, h2, m2))
21 changes: 21 additions & 0 deletions src/kattis/fleytitala.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys
from typing import Tuple


def process_multiline_input(numbers: list) -> Tuple[int, int]:
d, b = int(numbers[0].strip()), int(numbers[1].strip())
return d, b


def calculate_distance_geom(d, b):
if d == 0:
return 0
# Sum of geometric series formula, for loop will run into time limit issue
distance = d * (1 - (1 / 2) ** (b + 1)) / (1 - 1 / 2)
return distance


if __name__ == "__main__":
message = sys.stdin.readlines()
d, b = process_multiline_input(message)
print(calculate_distance_geom(d, b))
36 changes: 36 additions & 0 deletions src/kattis/hlaupafmaeli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# There is only 1 input which is the year to check against 2020
# Function needed: 1. Check if the input year is a leap year 2. Count the number of leap years between 2020 and the input year


def is_leap_year(year):
# A year is a leap year if it is divisible by 4
if year % 4 == 0:
# However, if the year is also divisible by 100, it is not a leap year
# unless it is also divisible by 400
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False


def leap_year_up_to(year):
"""
Count the years divisible by 4 first,
then subtract the years divisible by 100 because
these 2 conditions are mutually exclusive.
Finally, add back some that are divisible by 400
"""
return year // 4 - year // 100 + year // 400


if __name__ == "__main__":
input_year = int(input())
if is_leap_year(input_year):
print(leap_year_up_to(input_year) - leap_year_up_to(2020))
else:
print("Neibb")
16 changes: 16 additions & 0 deletions src/kattis/lidaskipting2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from decimal import Decimal, getcontext

# Set the precision
getcontext().prec = 101


def get_min_max_teams(participants):
result = participants / Decimal(3)
result_ceiled = result.to_integral_value(rounding="ROUND_CEILING")

return f"{participants}\n{result_ceiled}"


if __name__ == "__main__":
participants = int(input())
print(get_min_max_teams(participants))
28 changes: 28 additions & 0 deletions src/kattis/message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys


def process_multiline_input(message: list):

# a list of dimensions (e.g. ['2', '3'])
dimension_lst = message[0].strip().split()

matrix = [list(line.strip()) for line in message[1:]]

if not len(matrix) == int(dimension_lst[0]) and all(
len(row) == int(dimension_lst[1]) for row in matrix
):
raise ValueError("The dimensions and the matrix do not match")

return matrix


def output_message(matrix):
message = "".join([char for row in matrix for char in row if char != "."])

return message


if __name__ == "__main__":
message = sys.stdin.readlines()
matrix = process_multiline_input(message)
print(output_message(matrix))
33 changes: 33 additions & 0 deletions tests/kattis/test_bidendalausbid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
import sys
import pytest


# Add the src/kattis directory to the PYTHONPATH
path_to_add = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../../src/kattis")
)
sys.path.insert(0, path_to_add)

from bidendalausbid import (
process_multiline_input,
calculate_waited_time_in_minutes,
)


@pytest.mark.parametrize(
"message, expected_output",
[
(["02:02\n", "20:20\n"], (2, 2, 20, 20)),
(["13:37\n", "13:42\n"], (13, 37, 13, 42)),
(["20:20\n", "02:02\n"], (20, 20, 2, 2)),
],
)
def test_process_multiline_input(message, expected_output):
assert process_multiline_input(message) == expected_output


def test_calculate_waited_time_in_minutes():
assert calculate_waited_time_in_minutes(2, 2, 20, 20) == 1098
assert calculate_waited_time_in_minutes(13, 37, 13, 42) == 5
assert calculate_waited_time_in_minutes(20, 20, 2, 2) == 342
38 changes: 38 additions & 0 deletions tests/kattis/test_fleytitala.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import sys
import pytest


# Add the src/kattis directory to the PYTHONPATH
path_to_add = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../../src/kattis")
)
sys.path.insert(0, path_to_add)


from fleytitala import process_multiline_input, calculate_distance_geom


@pytest.mark.parametrize(
"input_str, expected_output",
[
(["0\n", "10\n"], (0, 10)),
(["1\n", "1\n"], (1, 1)),
(["12\n", "4\n"], (12, 4)),
],
)
def test_process_multiline_input(input_str, expected_output):
assert process_multiline_input(input_str) == expected_output


@pytest.mark.parametrize(
"d, b, expected_output",
[
(0, 0, 0),
(1, 1, 1.5),
(12, 4, 23.25),
(0, 10, 0.0),
],
)
def test_calculate_waited_time_in_minutes(d, b, expected_output):
assert calculate_distance_geom(d, b) == expected_output
31 changes: 31 additions & 0 deletions tests/kattis/test_hlaupafmaeli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import sys
import pytest


# Add the src/kattis directory to the PYTHONPATH
path_to_add = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../../src/kattis")
)
sys.path.insert(0, path_to_add)

from hlaupafmaeli import is_leap_year, leap_year_up_to


def test_is_leap_year():
assert is_leap_year(2020) == True
assert is_leap_year(2021) == False
assert is_leap_year(1900) == False
assert is_leap_year(2000) == True
assert is_leap_year(2400) == True
assert is_leap_year(2401) == False


def test_leap_year_up_to():
assert leap_year_up_to(2020) == 490 # Assuming 2020 is the 490th leap year
assert (
leap_year_up_to(2021) == 490
) # 2021 is not a leap year, so the count remains the same
assert (
leap_year_up_to(2024) == 491
) # 2024 is the next leap year, so the count increases by 1
28 changes: 28 additions & 0 deletions tests/kattis/test_lidaskipting2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import sys
import pytest


# Add the src/kattis directory to the PYTHONPATH
path_to_add = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../../src/kattis")
)
sys.path.insert(0, path_to_add)

from lidaskipting2 import get_min_max_teams


@pytest.mark.parametrize(
"participants, expected_output",
[
(6, "6\n2"),
(61, "61\n21"),
(100, "100\n34"),
(
10000000000000000000000000000000000000000,
"10000000000000000000000000000000000000000\n3333333333333333333333333333333333333334",
),
],
)
def test_get_min_max_teams(participants, expected_output):
assert get_min_max_teams(participants) == expected_output
65 changes: 65 additions & 0 deletions tests/kattis/test_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import sys
import pytest


# Add the src/kattis directory to the PYTHONPATH
path_to_add = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../../src/kattis")
)
sys.path.insert(0, path_to_add)

from message import process_multiline_input, output_message


@pytest.mark.parametrize(
"message, expected_output",
[
(
["3 3\n", "sn.\n", ".a.\n", ".ke\n"],
[
["s", "n", "."],
[".", "a", "."],
[".", "k", "e"],
],
),
(
["5 6\n", "pa....\n", "......\n", ".u.l..\n", ".....a\n", "......\n"],
[
["p", "a", ".", ".", ".", "."],
[".", ".", ".", ".", ".", "."],
[".", "u", ".", "l", ".", "."],
[".", ".", ".", ".", ".", "a"],
[".", ".", ".", ".", ".", "."],
],
),
# Add more input-output pairs as needed
],
)
def test_process_multiline_input(message, expected_output):
assert process_multiline_input(message) == expected_output


def test_output_message():
assert (
output_message(
[
["s", "n", "."],
[".", "a", "."],
[".", "k", "e"],
]
)
== "snake"
)
assert (
output_message(
[
["p", "a", ".", ".", ".", "."],
[".", ".", ".", ".", ".", "."],
[".", "u", ".", "l", ".", "."],
[".", ".", ".", ".", ".", "a"],
[".", ".", ".", ".", ".", "."],
]
)
== "paula"
)