Python Forum
I need some help with my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need some help with my code
#3
(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 Cry . 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 Wall .

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
I think you are complicating things.

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.
When you have scanned through to the end of "arr", you might have just added the last entry in "arr" to "xx" so you need to make a last check if "xx" is longer than "maxx"
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 >
Reply


Messages In This Thread
I need some help with my code - by SantiagoPB - Feb-27-2021, 08:19 PM
RE: I need some help with my code - by Larz60+ - Feb-28-2021, 01:23 AM
RE: I need some help with my code - by Serafim - Feb-28-2021, 02:03 PM
RE: I need some help with my code - by SantiagoPB - Feb-28-2021, 02:14 PM
RE: I need some help with my code - by Larz60+ - Feb-28-2021, 06:03 PM
RE: I need some help with my code - by naughtyCat - Aug-27-2021, 04:53 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020