Skip to content

Commit a453d97

Browse files
committed
Initial commit
0 parents  commit a453d97

File tree

6 files changed

+340
-0
lines changed

6 files changed

+340
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# IDE
2+
.vscode/
3+
4+
# Byte-compiled / optimized / DLL files
5+
__pycache__/
6+
*.py[cod]
7+
*.$py.class

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Numbers Evaille
2+
3+
## Description
4+
5+
This folder contains files to solve the Numbers Evaille problem:
6+
7+
Given a pack of cards with unique numbers and a required goal card from the pack. The cards are categorized into ranks. Find 4 cards of whose ranks differ from each other and the goal number card's rank, such that the sum of the 4 cards is equal to the goal card's number.
8+
9+
## Folder structure:
10+
11+
- _card.py_: Python file defining class Card
12+
- _deck.py_: Python file defining class Deck
13+
- _solution.py_: Python solution file
14+
- _input.txt_: Input file, currently only contains 107 first cards and their primary ranks
15+
- _README.md_: Instruction file
16+
17+
## How to use
18+
19+
- You need to install Python version 3.x on your machine, or have a python compiler in your IDE (Visual Studio, Pycharm,...).
20+
- In your terminal, move into this folder and run:
21+
- Window: `python solution.py input.txt`
22+
- Linux or Mac: `python3 solution.py input.txt`
23+
- Or you can run the program in your IDE, make sure to config `input.txt` as an argument.
24+
25+
## Status
26+
27+
- __The algorithm is not ready, currently the program just imports and prints cards' info.__
28+
- Solving for extra cards is currently unavailable.
29+
- The input file can only contain a rank per card. For cards with alternative ranks, you need to create a new input file.

card.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Card():
2+
"""A card has a number and a rank"""
3+
4+
def __init__(self, number, rank):
5+
self.number = number
6+
self.rank = rank
7+
8+
9+
def __init__(self, info: list):
10+
self.number = int(info[0])
11+
self.rank = int(info[-1])
12+
13+
14+
def print(self):
15+
print("Number:", self.number)
16+
print("Rank:", self.rank)

deck.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from card import Card
2+
3+
class Deck():
4+
"""A set of cards categorized by their ranks"""
5+
6+
def __init__(self):
7+
self.ranks = []
8+
9+
10+
def sorted_append(self, card: Card):
11+
index = 0
12+
flag = False
13+
14+
if len(self.ranks) == 0:
15+
pass
16+
else:
17+
for rank in self.ranks:
18+
if rank[0].rank == card.rank:
19+
rank.append(card)
20+
flag = True
21+
break
22+
elif rank[0].rank < card.rank:
23+
index += 1
24+
else:
25+
break
26+
27+
if flag != True:
28+
self.ranks.insert(index, [card])
29+
30+
31+
def is_empty(self):
32+
return len(self.ranks) == 0
33+
34+
35+
def print(self):
36+
if self.is_empty():
37+
print("Empty deck")
38+
else:
39+
for rank in self.ranks:
40+
print("Rank", rank[0].rank)
41+
42+
for card in rank:
43+
print("Number:", card.number)
44+
45+
print("_____________")
46+
47+
48+
def rank_of(self, number):
49+
if self.is_empty():
50+
return -1
51+
52+
result = 0
53+
54+
for rank in self.ranks:
55+
for card in rank:
56+
if card.number == number:
57+
result = card.rank
58+
59+
return result
60+
61+
62+
def min_num(self):
63+
if self.is_empty():
64+
return -1
65+
66+
min_lst = [rank[0].number for rank in self.ranks]
67+
rank_of_min_num = min_lst.index(min(min_lst))
68+
69+
return self.ranks[rank_of_min_num][0].number
70+
71+
72+
def max_num(self):
73+
if self.is_empty():
74+
return -1
75+
76+
max_lst = [rank[-1].number for rank in self.ranks]
77+
rank_of_max_num = max_lst.index(max(max_lst))
78+
79+
return self.ranks[rank_of_max_num][-1].number

input.txt

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
001, 01
2+
002, 01
3+
003, 01
4+
004, 01
5+
005, 05
6+
006, 06
7+
007, 07
8+
008, 04
9+
009, 09
10+
010, 04
11+
011, 07
12+
012, 05
13+
013, 01
14+
014, 05
15+
015, 08
16+
016, 04
17+
017, 03
18+
018, 04
19+
019, 05
20+
020, 03
21+
021, 06
22+
022, 08
23+
023, 08
24+
024, 06
25+
025, 06
26+
026, 03
27+
027, 04
28+
028, 07
29+
029, 02
30+
030, 03
31+
031, 01
32+
032, 04
33+
033, 05
34+
034, 03
35+
035, 10
36+
036, 04
37+
037, 04
38+
038, 08
39+
039, 04
40+
040, 08
41+
041, 04
42+
042, 07
43+
043, 02
44+
044, 04
45+
045, 02
46+
046, 08
47+
047, 03
48+
048, 03
49+
049, 03
50+
050, 04
51+
051, 03
52+
052, 04
53+
053, 05
54+
054, 01
55+
055, 04
56+
056, 01
57+
057, 04
58+
058, 04
59+
059, 04
60+
060, 04
61+
061, 05
62+
062, 08
63+
063, 01
64+
064, 02
65+
065, 02
66+
066, 04
67+
067, 05
68+
068, 08
69+
069, 04
70+
070, 04
71+
071, 03
72+
072, 06
73+
073, 05
74+
074, 07
75+
075, 03
76+
076, 07
77+
077, 12
78+
078, 01
79+
079, 04
80+
080, 04
81+
081, 10
82+
082, 04
83+
083, 01
84+
084, 11
85+
085, 04
86+
086, 04
87+
087, 08
88+
088, 08
89+
089, 07
90+
090, 08
91+
091, 04
92+
092, 09
93+
093, 12
94+
094, 05
95+
095, 09
96+
096, 02
97+
097, 08
98+
098, 04
99+
099, 12
100+
100, 01
101+
101, 04
102+
102, 04
103+
103, 04
104+
104, 04
105+
105, 04
106+
106, 04
107+
107, 08

solution.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from sys import argv
2+
from card import Card
3+
from deck import Deck
4+
5+
6+
def import_card(string: str) -> Card:
7+
return Card(string.split(', '))
8+
9+
10+
def import_deck(filename: str) -> Deck:
11+
deck = Deck()
12+
13+
with open(filename, "r") as open_file_obj:
14+
for line in open_file_obj:
15+
deck.sorted_append(import_card(line))
16+
17+
return deck
18+
19+
20+
def solve(deck: Deck, goal: int):
21+
# """Solve the problem in Dynamic Programming style"""
22+
23+
# """Check for exceptions"""
24+
# if deck.is_empty():
25+
# raise Exception("Deck is empty")
26+
27+
# if len(deck.ranks) < 5:
28+
# raise Exception("Not enough ranks")
29+
30+
# if goal < deck.min_num() or goal > deck.max_num():
31+
# raise Exception("Goal number is out of deck range")
32+
33+
# """Step 1: List all possible combos"""
34+
# """Might check for different ranks as well"""
35+
# solutions = []
36+
37+
# # TODO
38+
# candidate_lst = deck.ranks
39+
# goal_rank = deck.rank_of(goal)
40+
41+
# for rank_ind in range (0, len(candidate_lst)):
42+
# if rank_ind[0].rank != goal_rank:
43+
# pass
44+
# else:
45+
# candidate_lst.pop(rank_ind)
46+
# break
47+
48+
49+
50+
# """Step 2: Check if their ranks are not the same"""
51+
# """Might be deleted later"""
52+
# if len(solutions) != 0:
53+
# unqualified = []
54+
55+
# for sol in solutions:
56+
# if sol[0].rank != sol[1].rank and sol[1].rank != sol[2].rank and sol[2].rank != sol[3].rank and sol[3].rank != sol[1].rank:
57+
# pass
58+
# else:
59+
# unqualified.append(sol)
60+
61+
# for sol in unqualified:
62+
# solutions.remove(sol)
63+
64+
return [] # solutions
65+
66+
67+
def main():
68+
deck = import_deck(argv[1])
69+
deck.print()
70+
71+
goal = int(input("Goal: "))
72+
73+
print("Rank of number", goal, "is:", deck.rank_of(goal))
74+
print("Min number in the deck is:", deck.min_num())
75+
print("Max number in the deck is:", deck.max_num())
76+
77+
solutions = solve(deck, goal)
78+
79+
"""Print result"""
80+
if len(solutions) == 0:
81+
print("No solutions were found")
82+
else:
83+
no = 1
84+
for sol in solutions:
85+
print("Solution", no)
86+
87+
for card in sol:
88+
card.print()
89+
90+
print("_____________")
91+
92+
no += 1
93+
94+
95+
if __name__ == "__main__":
96+
"""Input: (1) list of cards, sorted in ascending order of card number
97+
(2) the desired total number"""
98+
"""Output: list of solutions"""
99+
100+
main()
101+
102+

0 commit comments

Comments
 (0)