Feb-28-2021, 02:03 PM
(Feb-27-2021, 08:19 PM)SantiagoPB Wrote: My problem is that I need to put my input as one string, seprate by spaces, but I don't know how to do that . Also I wanted to do my array in a user give (Ex.: in(input("Write the numbers to find the longer sub-sequence")) but I tried but didn't work and I dont know why .I think you are complicating things.
P.S.: I don't know if is the best way to do this homework but I don't have experience in python, is just my 1st year of programming.
Thank you everyone for help me :D
First: Use the input method suggested by @Larz60+
Second: You don't need the length of the list.
Create two lists e.g. "xx" and "maxx" as empty lists
then scan your list (that you call "arr") and for every entry in it:
- if "xx" is empty or the entry is bigger than the last element in "xx", then append the entry to "xx".
- and if that is not the case,
- copy "xx" to "maxx" if "xx" is longer than "maxx"
- set "xx" to contain just the current entry.
I implemented exactly this and here is the test run:
Output:> python longest-increasing-sub-sequence.py
Please enter numbers separated by spaces: 1 7 8 3 4 6 8 9 0 5 1 2 3
3 4 6 8 9
> python longest-increasing-sub-sequence.py
Please enter numbers separated by spaces: 5 4 2 3 4 9 9 9 -8 -5 -3 -2 -1
-8 -5 -3 -2 -1
> python longest-increasing-sub-sequence.py
Please enter numbers separated by spaces: -3 -2 8
-3 -2 8
> python longest-increasing-sub-sequence.py
Please enter numbers separated by spaces: 5 4 3 2 1 0
5
>