+ 2
How is string slicing working in this code?
a = "This is a long string consisting of two lines.\nThis is the second line.\nThis is the third line." start = 0 size = 1 def func(a): return a[start:start+size] iterator = iter(lambda: func(a), '\n') # Will generate values until '\n' for out in iterator: print(f"Iterator loaded {out}") start += size print("Encountered Newline!")
23 Respuestas
+ 6
Maybe it will be easier for you to understand?
text = "This is a long string consisting of two lines.\nThis is the second line.\nThis is the third line."
def func(arg):
return arg
# Will generate values until '\n'
iterator = []
for i in text:
if i != '\n': iterator += [func(i)]
else: break
for out in iterator:
print(f"Iterator loaded {out}")
print("Encountered Newline!")
#👇
index = 0
func = lambda arg: arg[index]
# Will generate values until '\n'
iterator = []
for i in text:
if i != '\n': iterator += [func(i)]
else: break
for out in iterator:
print(f"Iterator loaded {out}")
index += 1
print("Encountered Newline!")
#👇
def read(arg):
for i in arg:
if i != '\n':
print(f"Iterator loaded {i}")
else:
print("Encountered Newline!")
break
read(text)
#👇
i = 0
func = lambda arg: arg[i]
iterator = iter(lambda: func(text), '\n')
for _ in iterator:
print(next(iterator))
i += 1
+ 2
Shantanu 👏👏👏👏👏 Bravo, now you can see what you have mastered: "lambda, iter, filter and regular expressions".
Let's complicate the task a little more. 😎
Try writing a function that displays a line of the user's choice: "1,2, or 3". 👋😎
P. S: "Are you cheating? Removed the period at the end of the sentence from the text." 😉
import re
a = "This is a long string consisting of two lines.\nThis is the second line.\nThis is the third line."
b = r"\n[A-Za-z0-9 ]{1,}.\n"
c = re.search(b, a)
z = ""
if c:
z = c.group()
s = (filter(lambda x: x != "\n", z))
u = len(z)
z = iter(s)
for i in range(u-2):
print(next(z))
👇
import re
a = "This is a long string consisting of two lines.\nThis is the second line\nThis is the third line."
b = r"[A-Za-z0-9 ]{1,}\n"
c = re.search(b, a)
if c:
z = c.group()
u = len(z)
z = iter(z)
for i in range(u-1):
print(next(z))
+ 2
Shantanu Great, you've done a great job. But why are you repeating the same mistakes that I have already corrected for you? And why write the same condition several times? Is it not possible to prescribe everything at once in one?
+ 2
Shantanu Look closely at my previous answer and compare it to your code.
+ 2
Shantanu great, now much better 👏👏👏👍😎
You either use a regular expression or a filter.
Correction:
#Run the code and type 1 if you want to print the 1st line, 2 for the second line and 3 for the third line
import re
a = "This is a long string consisting of two lines.\nThis is the second line.\nThis is the third line."
b = r"^([\w ]{1,}.\n)([\w ]{1,}.\n)([\w ]{1,}.$)"
e = int(input())
c = re.search(b, a)
_1 = 1
if c:
x,y,z = c.groups()
if e == 1:
s = x
elif e == 2:
s = y
elif e == 3:
s = z
_1 = 0
else: print("Text consists of three lines")
d = iter(s)
for i in range(len(s)-_1):
print(next(d))
Now put all of this into a function whose argument will accept user input.
Good luck!👋😎
+ 2
Shantanu You are welcome.
Trick question: "What if the text is 100 lines long, would you write a regular expression pattern with 100 groups?" 😎
+ 2
Shantanu I've edited the question.
+ 2
Shantanu , in my opinion this is the easiest way:
text = "This is a long string consisting of two lines.\nThis is the second line.\nThis is the third line."
def println(n):
for i in text.split('\n')[n-1]:
print(f"Iterator loaded {i}")
println(int(input()or 1))
#Or With ITER function:
def println(arg,n):
arg = arg.split('\n')
if n > len(arg):
return print(f'⚠️There is no line {n} in the text.')
i = 0
func = lambda arg: arg[n-1][i]
iterator=iter(lambda:func(arg),'.')
for _ in iterator:
print(next(iterator))
i += 1
print('.')
println(text,int(input()or 1))
#Or:
def println(n):
arrtext = text.split('\n')
if n > len(arrtext):
return print(f'⚠️There is no line {n} in the text.')
textln = iter(arrtext)
for _ in range(n-1):
next(textln)
for i in next(textln):
print(f"Iterator loaded {i}")
println(int(input()or 1))
+ 1
To pin the material, write a program to read the second line. 😎
+ 1
Yes 😎
+ 1
Shantanu , okay, but let's assume you don't know the length of the string if you let the text enter the user. How then? 😉
Try doing this using the iter() function according to the topic of this discussion. Good luck 👋😎
+ 1
+ 1
Solo thanks for correction.
+ 1
Solo, Your code is great 😃👍.
+ 1
Shantanu thanks 🖐️😎
0
Solo is this a challenge for me?
0
Yes
0
Solo what mistake?