Python - intermediate question | Sololearn: Learn to code for FREE!

0

Python - intermediate question

So I got a file named texte-b.txt which contains an article, well the program accept 3 argv's the 1st must be a string which the program will try to find it and put it in the new file the 2nd input is how much words (the max) which come after that word you want to be shown and the 3rd input is how much words (the max) before your chosen string will be shown Exemple: The file: Hi I am a newbie coder, well I will say hi again. Hi again one more time. Inputs: hi 1 3 Output: Hi I I will say hi again Hi again And the question is, in lines of code 20 and 21, why do we have to write len (xx) -2 and not len ​​(xx) -1 to know the last index of the list "ar"? If we want to write len (xx) -1 instead of len (xx) -2, what do we need to change in the script below, while keeping 23 lines of code? https://code.sololearn.com/cA17a17A24A2/?ref=app https://code.sololearn.com/cA2a17a2153A/?ref=app

6/26/2021 11:02:43 PM

TheWitcher

8 Answers

New Answer

+2

also, you remove leading spaces from start of f, but not trailing spaces from end of a... so split give you an empty string as last item of ar ;P

+1

you've already posted this question few hours ago: https://www.sololearn.com/Discuss/2822059/?ref=app and you have receiving response as we cannot help you as we don't have the input file, nor the values passed as script arguments ^^ I also have advised you to rather save your code in a code playground project, and post only the link to it ;P

+1

you need -2 because you also add 1 at end of expression ^^

0

@visph i did as u said and also the file is here https://www.onlinegdb.com/fB954diTq

0

well, I tried to inspect your code... ... but it is your? as you asked "I wanna know whqt does this program do with my file?" ^^ however, as stated in your other thread, it is hard to guess what the goal of this code, as you doesn't give any hint on what is expected from the command line arguments ;P it seems that first argument expected is a string, and both next arguments are whole (int) numbers... it also seems that the string is searched accross each lines of the text file (opened in variable 'text' as read mode), but I cannot figure what is expected to be the two whole numbers... used as upper bound for iterating arrays created from split() on text (words) before the searched string 1st argument, and text (words) after it... however, we cannot know in advance what are the valid values, all the more accross different lines of input file ^^ so, you give an effort by providing a link to your code and your text file... but that's not a sololearn code playground: I was not able to even copy the text file

0

visph hi I changed the subject, can you check it nox please ?

0

you can just change: if len(ar)-2-o+1 >=0: j= ar [len(ar)-2-o+1]+" "+j by: if len(ar)-1-o >=0: j= ar [len(ar)-1-o]+" "+j however, you could do shorter by avoiding while loops and using slicing and join instead: if x: a=i[:x.start()] f=i[x.end():] a=re.sub(" +$","",a) # need to be added f=re.sub("^ +","",f) ar=re.split("\W+",a) if a else [] # need to be changed ff=re.split("\W+",f) if f else [] # need to be changed h=ff[:int(sys.argv[2]] j=ar[-int(sys.argv[3]:] sortie.write(" ".join([*j,x.group(),*h])+"\n") and anyway, your code only search for first occurence of first argument on each line ;P to view working version of your modified code (simulating input file, using user input to get arguments, and printng ouput file): https://code.sololearn.com/cHgDb40Es99u/?ref=app sample input: just 1 3 2nd line has 2 occurences of 'just', but only first one is outputed...

0

visph thanks man I appreciate your help it was so helpful, but here is the question that I need a clear response for it "in lines of code 20 and 21, why do we have to write len (xx) -2 and not len ​​(xx) -1 to know the last index of the list "ar"?" I mean what is the difference if we put -1 and not -2?