-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58a5558
commit 893bfe2
Showing
6 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Permutation Game | ||
|
||
There are n(n is even) players, conveniently labeled 1,2,3...n. These players will play m rounds of games. In each round | ||
of games. In each round of games, the players are split into 2 teams of n/2 players each. Two players x<y are said to | ||
have played against each other if they were on different teams for one of the m games. | ||
|
||
You are given 3 arguments, `n`, `m`, `games`. Your task is to check that for all pairs of players 1<=x, y<=n, player x has | ||
played against y. | ||
games is a 2D list that represents the m rounds of games among n players. | ||
|
||
Write a function `check(n, m, games)` that takes in 3 arguments | ||
|
||
games is a 2D list with m rows and n columns where `games[i]` is a permutation of `1,2,3...n` representing round | ||
number `i`. | ||
in particular for round `i`, | ||
`games[i][0]`, `games[i][1]`, `games[i][n/2-1]` is on 1 team. | ||
`games[i][n/2]`, `games[i][n/2+1]`, `games[i][n-1]` is on the other team | ||
|
||
`check(n, m, games)` should return a boolean. True if and only if all pairs of players have played against each other in | ||
the m rounds of games |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import List | ||
|
||
|
||
def check(n: int, m: int, games: List[List[int]]) -> bool: | ||
pairs = [(i, j) for i in range(1, n + 1) for j in range(i + 1, n + 1)] | ||
|
||
for game_round in games: | ||
for i, j in pairs: | ||
|
||
if i in game_round[:n // 2] and j in game_round[:n // 2]: | ||
return False | ||
|
||
if i in game_round[n // 2:] and j in game_round[n // 2:]: | ||
return False | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import unittest | ||
|
||
from . import check | ||
|
||
|
||
class CheckTestCase(unittest.TestCase): | ||
def test_1(self): | ||
"""should return True for n = 2, m = 1, games=[[1, 2]]""" | ||
n = 2 | ||
m = 1 | ||
games = [[1, 2]] | ||
actual = check(n, m, games) | ||
self.assertTrue(actual) | ||
|
||
def test_2(self): | ||
"""should return False for n = 4, m = 2, games=[[1, 2, 3, 4], [4,3,1,2]]""" | ||
n = 4 | ||
m = 2 | ||
games = [[1, 2, 3, 4], [4, 3, 1, 2]] | ||
actual = check(n, m, games) | ||
self.assertFalse(actual) | ||
|
||
def test_3(self): | ||
"""should return False for n = 4, m = 2, games=[[1, 2, 3, 4], [1, 3, 2, 4]]""" | ||
n = 4 | ||
m = 2 | ||
games = [[1, 2, 3, 4], [1, 3, 2, 4]] | ||
actual = check(n, m, games) | ||
self.assertTrue(actual) | ||
|
||
def test_4(self): | ||
"""should return False for n = 6, m = 6, games=[[1, 6, 4, 5, 2], [6, 4, 2, 3, 1, 5], [4, 2, 1, 5, 6, 3], [4, 5, 1, 6, 2, 3], [3, 2, 5, 1, 6, 4], | ||
[2, 3, 6, 4, 1, 5]]""" | ||
n = 6 | ||
m = 6 | ||
games = [[1, 6, 4, 5, 2], [6, 4, 2, 3, 1, 5], [4, 2, 1, 5, 6, 3], [4, 5, 1, 6, 2, 3], [3, 2, 5, 1, 6, 4], | ||
[2, 3, 6, 4, 1, 5]] | ||
actual = check(n, m, games) | ||
self.assertTrue(actual) | ||
|
||
def test_5(self): | ||
"""should return False for n = 6, m = 6, games=[[3, 1, 4, 5, 6, 2], [5, 3, 2, 4, 1, 6], [5, 3, 6, 4, 2, 1], [6, 5, 3, 2, 1, 4], [5, 4, 1, 2, 6, 3], | ||
[4, 1, 6, 2, 5, 3]]""" | ||
n = 6 | ||
m = 6 | ||
games = [[3, 1, 4, 5, 6, 2], [5, 3, 2, 4, 1, 6], [5, 3, 6, 4, 2, 1], [6, 5, 3, 2, 1, 4], [5, 4, 1, 2, 6, 3], | ||
[4, 1, 6, 2, 5, 3]] | ||
actual = check(n, m, games) | ||
self.assertFalse(actual) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Spreadsheet Encoding | ||
|
||
We will be considering how to solve the problem of implementing a function that converts a spreadsheet column ID (i.e., “A”, “B”, “C”, …, “Z”, “AA”, etc.) to the corresponding integer. For example, “A” equals 1 because it represents the first column, while “AA” equals 27 because it represents the 27th column. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
def spreadsheet_encode_column(column_id: str) -> int: | ||
""" | ||
Encodes a Spreadsheet column ID as an integer and returns it. Here the encoding uses the alphabets A, B, C, etc. and | ||
further uses the indexing of the alphabet starting from 1 like: A=1, B=2, C=3,..., Z=26 | ||
Complexity | ||
Where n represents the number of characters in the column_id | ||
- Time O(n) as we iterate over each character in the column ID to calculate the encoding | ||
- Space O(1) no extra space is required to perform the encoding | ||
Args: | ||
column_id (str): the column ID(name) as represented on a spreadsheet | ||
Returns: | ||
int: encoded column id as an integer | ||
""" | ||
num = 0 | ||
# count will help in determining the power of the base as we convert the column id into the corresponding integer | ||
count = len(column_id) - 1 | ||
|
||
for char in column_id: | ||
# we use the base 26 system here as there are 26 letters in the alphabet | ||
# ord(char) returns the unicode code point for char. In order to make sure that A=1, we need to determine the | ||
# relative difference from the result given by ord & from the representation we require for base 26 system | ||
# Now we know that ord('A') equals 65. So if we find the Unicode code point using ord() of a character, | ||
# subtract ord('A') from it, and then add 1 to it, we’ll get the representation we want for the base 26 system | ||
num += 26**count * (ord(char) - ord('A') + 1) | ||
|
||
# count is decremented by 1 as the power of the base decrements by 1 from the digits from left to right | ||
count -= 1 | ||
|
||
return num |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import unittest | ||
from . import spreadsheet_encode_column | ||
|
||
|
||
class SpreadsheetEncodeColumnTestCases(unittest.TestCase): | ||
def test_zz(self): | ||
"""ZZ should return 702""" | ||
column_name = "ZZ" | ||
expected = 702 | ||
actual = spreadsheet_encode_column(column_name) | ||
self.assertEqual(expected,actual) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |