mirror of
https://github.com/aykhans/leet-code-problems.git
synced 2025-04-08 08:34:00 +00:00
Added convert_linear_str_to_zigzag function
This commit is contained in:
parent
6b2e6effe5
commit
f2c03243b7
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,2 @@
|
||||
.venv
|
||||
.venv
|
||||
__pycache__
|
@ -39,4 +39,42 @@ class AcceptedMedium:
|
||||
l2.append(i+s)
|
||||
l = l2.copy()
|
||||
l2 = []
|
||||
return [] if l[0] == '' else l
|
||||
return [] if l[0] == '' else l
|
||||
|
||||
def convert_linear_str_to_zigzag(s: str, numRows: int) -> str:
|
||||
# sourcery skip: use-contextlib-suppress
|
||||
"""
|
||||
Link: https://leetcode.com/problems/zigzag-conversion/
|
||||
Runtime: 72 ms
|
||||
"""
|
||||
len_s = len(s)
|
||||
if len_s < 3 or numRows == 1: return s
|
||||
def x(l, t):
|
||||
if len(l) < t:
|
||||
l.append(None)
|
||||
x(l, t)
|
||||
return l
|
||||
l = []
|
||||
t = 0
|
||||
while t < len_s:
|
||||
l.append(list(s[t:numRows+t]))
|
||||
t += numRows
|
||||
l.append(list(reversed(x(list(s[t:numRows-2+t]), numRows-2))))
|
||||
t += numRows-2
|
||||
l = l[:-1] if l[-1] == [] else l
|
||||
len_l = len(l)
|
||||
s1 = ''
|
||||
s2 = ''
|
||||
s3 = ''
|
||||
t = len(l[0]) - 2
|
||||
for i in range(0, len_l, 2):
|
||||
s1 += l[i].pop(0)
|
||||
try:s3 += l[i].pop(t)
|
||||
except IndexError: pass
|
||||
for i in range(len(l[0])):
|
||||
for j in range(len_l):
|
||||
try:
|
||||
if l[j][i] is not None:
|
||||
s2 += l[j][i]
|
||||
except IndexError: pass
|
||||
return s1 + s2 + s3
|
@ -1,7 +1,7 @@
|
||||
from .accepted import (AcceptedEasy,
|
||||
from accepted import (AcceptedEasy,
|
||||
AcceptedMedium,
|
||||
AcceptedHard)
|
||||
from .refused import (RefusedEasy,
|
||||
from refused import (RefusedEasy,
|
||||
RefusedMedium,
|
||||
RefusedHard)
|
||||
|
||||
|
@ -0,0 +1,38 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.10.4 ('.venv': venv)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.4"
|
||||
},
|
||||
"orig_nbformat": 4,
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "79627d5650bbce6f1015448354c1d381e95e649e055f8bf31e4d4215309d4a32"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user