Skip to content

Commit 6936225

Browse files
committed
Created oneliners for the daily for fun
1 parent 61b64e1 commit 6936225

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def getHappyString(self, n: int, k: int) -> str:
3+
return (
4+
_pm_dfs := (
5+
lambda rem, n, output:
6+
''.join(output) if not n else
7+
_pm_dfs(
8+
rem - (pot_per_path if (temp := int(rem > (pot_per_path := 2 ** (n - 1)))) else 0),
9+
n - 1,
10+
output + ['abc'.replace(output[-1], '')[temp]]
11+
)
12+
)
13+
)(
14+
k - x * pot_n_min_1,
15+
n,
16+
[chr(ord('a') + x)]
17+
) if (x := (k - 1) // (pot_n_min_1 := 2 ** (n := n - 1))) <= 2 else \
18+
''
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def getHappyString(self, n: int, k: int) -> str:
3+
return (_pm_dfs := (lambda rem, n, output: ''.join(output) if not n else _pm_dfs(rem - (pot_per_path if (temp := int(rem > (pot_per_path := 2 ** (n - 1)))) else 0), n - 1, output + ['abc'.replace(output[-1], '')[temp]])))(k - x * pot_n_min_1,n, [chr(ord('a') + x)]) if (x := (k - 1) // (pot_n_min_1 := 2 ** (n := n - 1))) <= 2 else ''

my-submissions/m1415.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### V1
2+
3+
My initial attempt which was quite optimal. Achieves $O(n)$ using combinatorics where $n$ is the length of the output string aka the input $n$.
4+
5+
### V2
6+
7+
A oneliner I produced with walrus operators. Both in an expanded "bit more readable" version and in the pure oneline state.

0 commit comments

Comments
 (0)