For a string, we want to make it all lower case and remain only alphabetical or space element.
The right answer is
But for my code I think they are the same but did not work as well.
The first one is the right answer and the second one is my code. Could you help me figure out what happened? Thanks
Your return statement is nested inside the for loop, so it returns the value immediately.
You need to decrease the indentation of the return statement, so that the value is returned after the loop is completed.
Check Python Reference Manual - 2.1.7 Indentation
Related
PA=-2
MA=-1.8
if .005<(abs(PA)-(abs(MA)<.3:
print('Slightly Weakening Trend')
if .005<(abs(MA))-(abs(PA))<.3:
print('Slightly Strengthening Trend')
if (abs(PA))<(abs(MA)):
print('Weakening Trend')
if (abs(MA))<(abs(PA)):
print('Strengthening Trend')
It says there is a syntax error at the end of line 3
Does anyone have any ideas for optimization, I am new to python and am grateful for all the help!
Line 3 of the code says if .005<(abs(PA)-(abs(MA)<.3:. If you look very closely, there is a opening parenthesis after the first less-than sign, which is never closed (there is another unclosed parenthesis after the second less-than sign). Also, adding parenthesis around the abs function call does not have a difference. Parenthesis are usually used to specify the order of performing operations (such as multiplication or addition). The never-closed opening parenthesis should be removed, changing the line to:
if .005<abs(PA)-abs(MA)<.3:
Finally, I also suggest replacing the later (after the first) if statements to elifs, as the current code (after the parenthetical correction) will produce the output:
Slightly Weakening Trend
Strengthening Trend
This is because, in the code, more than one if statement is satisfied, resulting in more than one indicator being outputted. Using elifs ensures that only one statement in the chain can be satisfied, which may be beneficial in this case.
So here is an example of a multiline string in vscode/python:
Cursor is after the p , and then you press enter, and end up like this:
i.e. the string ends up indented, which seems what you almost never want - why have an arbitratly amount of whitespace on the next line of this string ?
Is there any way change this in vscode, i.e. for multiline strings, it should end up with this:
I think this problem is related to different coding styles of different people.
For example,
def example(x):
if x:
a = '''
This is help
'''
def example(x):
if x:
a = '''This is help
'''
The automatic indenting of vscode line breaks is based on code blocks. If you want Vscode can identify multiline string, I think it would be better to submit future request in github. I've submitted this issue for you.
I am not 100% sure if what OP meant is just to refer to the indentation in the editor (namely, VSC) or if, by this:
i.e. the string ends up indented, which seems what you almost never want - why have an arbitrary amount of white space on the next line of this string?
...they also meant to refer to the actual output of the multi-line string,
(or also, just in case anybody else finds this post looking for a way to avoid this affecting the actual output of the multi-line string), I'd like to add as a complementary answer (cannot comment yet) that this was already beautifully answered here.
If that's the case and you're reading this for that reason, in short, all you want is to import the standard lib 'inspect' and post-process your string with it, using the cleandoc method.
Without breaking the indentation in your IDE, this method makes sure to give you the string output you actually expected:
All leading whitespace is removed from the first line. Any leading whitespace that can be uniformly removed from the second line onwards is removed. Empty lines at the beginning and end are subsequently removed. Also, all tabs are expanded to spaces.
(From the docs link above)
Hope that helps anyone.
I'm trying to write a program for a homework using recursion to search for a word in a matrix (2x2 or more), it can be going from left to right or from up to down (no other directions), for example if I am searching for ab , in the matrix [['a','b'],['c','d']], the program should return in what direction the word is written (across), the starting index(0), ending index(2), and the index of the row or column(0).
My problem is that I have the idea of the recursion but, I can't implement it. I tried to break the problem down into more little proplems, like searching for the word in a given row, I started by thinking of the smallest case which is 2x2 matrix, at the first row and column, I need to search one to the right and one to the bottom of the first char, and check if they are equal to my given string, then give my recursion function a smaller problem with the index+1. However I can't think of what to make my function return at the base case of the recursion, been trying to solve it and think of ways to do it for two days, and I can't code what I think about or draw.
Note that I can't use any loops, I would really appreciate it if somone could push me in the right direction, any help would be pretty much appreciated, thanks in advance.
Edit: more examples: for input of matrix : [['a','b','c'],['d','e','f'],['g','h','i']] the outputs are:
with the string ab : across,0,0,2
with the string be : down,1,0,2
with the string ghi: across,2,0,3
I assume that the word we are looking for could be found starting from any place but we can move up to down or left to right only.
In that case, you should have a function that takes the start index and a direction and then the function keeps moving in the given direction starting from the given index and keeps moving until it doesn't find a mismatch, and it just returns true or false based on the match of the given string.
Now you need to call this function for each and every index of the matrix along with two directions up to down and left to right, and at any index, if you get the output of the function as true then you have found your answer.
This is a very basic idea to work, next it depends on you how you want to optimize the things in this method only.
Update:
To avoid using the loops.
The other way I can think of is that the function which we have defined now takes the row, column, and the string to find. So at each call, you will first check if the character at the given row and column matches the first character of the given string if so then it calls the two more functions, one in the right direction and the other in the down direction, along with the string with the first character removed.
Now to check all the columns of the matrix, you will anyway call the function in down and right direction with the exact same string.
The base case will be that if you reach the end of the string then you have found the answer and you will return True, otherwise False.
One more thing to notice here is that if any of the 4 function calls gives you a True response then the current row/column will also return True.
Cheers!
short Question in Code:
i=0
k_start[i]
[8515]
i=1
k_start[i]
139253
How can i avoid the parenthesis in this example? Or Why at least do they appear with when i is 0?
Edit: Sorry for this bad Question, the problem was that i had a list of lists.
I'm not sure why your first element is coming out as a sub-list but you can get rid of the brackets (in this one specific case) by calling k_start[0][0]... Try printing k_start to see what the whole array looks like. That way you can see what is happening and perhaps find a more general solution. It's hard to do from the outside without more info, such as upstream code or a printout of the array.
The problem is best explained by just showing the code:
a = True
b = True
while True:
"""
A
"""
if a == True:
pass
"""
B
"""
elif b == True:
pass
The issue being that there is a syntax error at "elif b", though when removing the comments, the issue disappears. I tried removing the indents on the comments which resulted in an expected indent on the closing comment line after "A". I know I could switch to using "#" to comment sections, though """ makes things much clearer and is more convenient for large chunks. Perhaps I'm missing something obvious, I would appreciate any help.
String literals aren't comments. You can sometimes sort of pretend they're comments, but they're not, and the fact that they're not is finally biting you.
An elif has to appear immediately after the end of the block associated with the preceding if or elif. There can be comments and whitespace in between, but no statements, and strings count. Use real comments, with #.
If you really want to keep pretending strings are comments, you can indent the B string into the body of the if, but it won't line up cleanly with the block it's intended to be a comment on, and you'll just keep having to mess with your formatting to patch up the differences between comments and string literals.
You're creating a new string when you use """triple quotes""". So, you essentially have an un-indented block of code before your elif, which requires a preceding if statement. The improper tabbing on the quotes ends your if block. Once your parser reaches the elif block, it doesn't have a matching if block, hence the error.
Triplequotes are used as docstrings in places, and can act like comments, but they're not actually comments.
Reference (search for """)