Skip to content

Commit 4245740

Browse files
committed
add singleton pattern and test for OOP/cold
1 parent c2ba3cb commit 4245740

17 files changed

+1762
-797
lines changed

demo-assignments/A1-OOP/cold/Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
DOCS = docs
22
PYTHON = python3
3+
PLANTUML = java -jar ~/plantuml.jar
34

45
.PHONY: all
56
all: check-style fix-style check-types run-test run-test-coverage create-docs create-uml clean
@@ -57,9 +58,9 @@ ifeq ($(wildcard ~/plantuml.jar), )
5758
@echo "Downloading plantuml.jar in home folder..."
5859
curl -L -o ~/plantuml.jar https://sourceforge.net/projects/plantuml/files/plantuml.jar/download
5960
endif
60-
$(PLANTUML) docs/uml/HelloWorld.plantuml
61-
$(PLANTUML) docs/uml/Main.plantuml
62-
$(PLANTUML) docs/uml/solution.plantuml
61+
$(PLANTUML) ./uml/temperature.plantuml
62+
$(PLANTUML) ./uml/solution.plantuml
63+
$(PLANTUML) ./uml/interaction.plantuml
6364
@echo "UML diagrams created and saved in uml folder"
6465

6566

demo-assignments/A1-OOP/cold/cold.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ def read_data(self, source: Any) -> None:
5151
self._data = data[1].strip()
5252
self._temps = [Temperature(int(i)) for i in self._data.split()]
5353

54-
def get_n(self) -> int:
54+
@property
55+
def n(self) -> int:
5556
"""Returns the number of temperature readings
5657
5758
Returns:
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#! /usr/bin/env python3
2+
3+
"""
4+
Solving Kattis problem - cold using Singleton pattern
5+
https://open.kattis.com/problems/cold
6+
"""
7+
8+
from __future__ import annotations
9+
10+
__author__ = "Ram Basnet"
11+
__date__ = "2022/1/1"
12+
__license__ = "MIT"
13+
__version__ = "0.1.0"
14+
__maintainer__ = "Ram Basnet"
15+
16+
17+
from typing import Any, List
18+
import sys
19+
from temperature import Temperature
20+
21+
22+
class Solution():
23+
""" Solution class to solve the problem using Singleton pattern"""
24+
25+
_instance: "Solution" | None = None
26+
27+
def __new__(cls) -> "Solution":
28+
if cls._instance is None:
29+
cls._instance = super().__new__(cls)
30+
return cls._instance
31+
32+
def __init__(self) -> None:
33+
""" Constructor
34+
"""
35+
self._n: int = 0 # number of temperatures
36+
self._data: str = '' # data
37+
self._temps: List[Temperature] = [] # list of temperatures
38+
39+
def find_answer(self) -> int:
40+
"""Counts the number of temperatures below zero
41+
42+
Returns:
43+
int: number of temperatures below zero
44+
"""
45+
count = 0
46+
for temp in self._temps:
47+
if temp.is_negative():
48+
count += 1
49+
return count
50+
51+
def read_data(self, source: Any) -> None:
52+
""" Reads data from provided source.
53+
54+
Args:
55+
source (Any): stdin typically for kattis problem
56+
"""
57+
# ignore the first line
58+
data = source.readlines()
59+
self._n = int(data[0])
60+
self._data = data[1].strip()
61+
self._temps = [Temperature(int(i)) for i in self._data.split()]
62+
63+
def get_n(self) -> int:
64+
"""Returns the number of temperature readings
65+
66+
Returns:
67+
int: number of temperature readings
68+
"""
69+
return self._n
70+
71+
def get_data(self) -> str:
72+
""" Returns the data
73+
74+
Returns:
75+
str: data
76+
"""
77+
return self._data
78+
79+
def solve(self, source: Any) -> None:
80+
""" Solves the problem.
81+
"""
82+
self.read_data(source)
83+
print(self.find_answer())
84+
# sys.stdout.write('1')
85+
86+
@staticmethod
87+
def main() -> None:
88+
"""Entry static method
89+
"""
90+
sol = Solution()
91+
sol.solve(sys.stdin)
92+
93+
94+
if __name__ == "__main__":
95+
Solution.main() # pragma: no cover

demo-assignments/A1-OOP/cold/docs/code-docs/cold.html

Lines changed: 634 additions & 368 deletions
Large diffs are not rendered by default.

demo-assignments/A1-OOP/cold/docs/code-docs/index.html

Lines changed: 217 additions & 0 deletions
Large diffs are not rendered by default.

demo-assignments/A1-OOP/cold/docs/code-docs/search.js

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo-assignments/A1-OOP/cold/docs/code-docs/temperature.html

Lines changed: 656 additions & 420 deletions
Large diffs are not rendered by default.

demo-assignments/A1-OOP/cold/tests/test_cold.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ def tearDown(self) -> None:
3838
def test_read_data1(self) -> None:
3939
"""Tests readData method"""
4040
self.sol.read_data(self.input1)
41-
self.assertEqual(self.sol.get_n(), 3)
41+
self.assertEqual(self.sol.n, 3)
4242
self.assertEqual(self.sol.get_data(), '5 -10 15')
4343

4444
def test_read_data2(self) -> None:
4545
"""
4646
Tests readData method
4747
"""
4848
self.sol.read_data(self.input2)
49-
self.assertEqual(self.sol.get_n(), 5)
49+
self.assertEqual(self.sol.n, 5)
5050
self.assertEqual(self.sol.get_data(), '-14 -5 -39 -5 -7')
5151

5252
def test_find_answer1(self) -> None:

demo-assignments/A1-OOP/cold/tests/test_solution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def tearDown(self) -> None:
3838
def test_read_data1(self) -> None:
3939
"""Tests readData method"""
4040
self.sol.read_data(self.input1)
41-
self.assertEqual(self.sol.get_n(), 3)
41+
self.assertEqual(self.sol.n, 3)
4242
self.assertEqual(self.sol.get_data(), '5 -10 15')
4343

4444
def test_read_data2(self) -> None:
@@ -47,7 +47,7 @@ def test_read_data2(self) -> None:
4747
:return: None
4848
"""
4949
self.sol.read_data(self.input2)
50-
self.assertEqual(self.sol.get_n(), 5)
50+
self.assertEqual(self.sol.n, 5)
5151
self.assertEqual(self.sol.get_data(), '-14 -5 -39 -5 -7')
5252

5353
def test_find_answer1(self) -> None:
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Unit tests for the singleton solution.
2+
"""
3+
4+
__author__ = "Ram Basnet"
5+
__date__ = "2025/02/10"
6+
__license__ = "MIT"
7+
__version__ = "0.1.0"
8+
9+
10+
from io import StringIO
11+
from cold_singleton import Solution
12+
13+
14+
def test_singleton() -> None:
15+
"""Test if the solution is a singleton.
16+
"""
17+
a = Solution()
18+
b = Solution()
19+
assert a is b
20+
21+
22+
def test_find_answer() -> None:
23+
"""Test find_answer method
24+
"""
25+
a = Solution()
26+
a.read_data(StringIO("3\n5 -10 15\n"))
27+
expected = a.find_answer()
28+
assert expected == 1
29+
30+
31+
def test_read_data() -> None:
32+
""" Test read_data method
33+
"""
34+
a = Solution()
35+
a.read_data(StringIO("3\n5 -10 15\n"))
36+
assert a.get_n() == 3
37+
38+
39+
def test_get_data() -> None:
40+
"""Test get_data method
41+
"""
42+
a = Solution()
43+
a.read_data(StringIO("3\n5 -10 15\n"))
44+
assert a.get_data() == "5 -10 15"
45+
46+
47+
def test_solve() -> None:
48+
"""Test solve method
49+
"""
50+
a = Solution()
51+
a.solve(StringIO("3\n5 -10 15\n"))
52+
# we'll learn how to patch the stdout later
53+
# for now we'll just check the answer
54+
assert a.find_answer() == 1
Loading
54.6 KB
Loading
63.2 KB
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@startuml ClassInteraction
2+
title "UML Class Interaction"
3+
Solution "1" - Temperature: manages > *
4+
@enduml
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@startuml Solution
2+
scale 2
3+
title "UML Class Diagram"
4+
class Solution {
5+
.. Class Variables ..
6+
_instance: Solution
7+
.. Instance Variables ..
8+
- _n: int
9+
- _data: str
10+
- _temps: List[Temperature]
11+
+ n: int {get; }
12+
.. Overloaded Methods ..
13+
+ __init__(): None
14+
.. Instance Methods ..
15+
+ read_data(source: Any): None
16+
+ find_answer(): int
17+
+ get_data(): str
18+
+ solve(source: Any): None
19+
.. Static Methods ..
20+
+ main(): None
21+
}
22+
@enduml
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@startuml Temperature
2+
scale 2
3+
title "UML Class Diagram"
4+
5+
class Temperature {
6+
.. Instance Variables ..
7+
- _temp: int
8+
- _unit: str
9+
+ temp: int {get; set;}
10+
+ unit: str {get; set;}
11+
.. Overloaded Methods ..
12+
+ __init__(side1: float, side2: float, side3: float): None
13+
+ __str__(): str
14+
+ __repr__(): str
15+
+ __lt__(other: Temperature): bool
16+
+ __gt__(other: Temperature): bool
17+
+ __eq__(other: object): bool
18+
+ __le__(other: Temperature): bool
19+
+ __ge__(other: Temperature): bool
20+
.. Instance Methods ..
21+
+ is_negative(): bool
22+
}
23+
@enduml

demo-assignments/A2-ABC/egypt/uml/solution.plantuml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@startuml
1+
@startuml Solution
22
scale 2
33
title "UML Class Diagram"
44
class Solution {

0 commit comments

Comments
 (0)