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
#1
Heart 
Hello everyone I am Santiago Big Grin

I am doing a homework with python, I let the homework:

LONGEST INCREASING SUB-SEQUENCE
Write a program, which reads a sequence of integers (in one string, the numbers are separated by
spaces). The program then prints its longest increasing sub-sequence. If there are several increasing
sub-sequences of the same length, the program prints the first one. A sequence is increasing when
𝑎𝑎𝑖𝑖 < 𝑎𝑎𝑖𝑖+1 for each 𝑖𝑖.
Examples:
Input: 1 7 8 3 4 6 8 9 0 5 1 2 3
Output: 3 4 6 8 9
Input: 5 4 2 3 4 9 9 9 -8 -5 -3 -2 -1
Output: -8 -5 -3 -2 -1
Input: -3 -2 8
Output: -3 -2 8
Input: 5 4 3 2 1 0
Output: 5

I wrote some code and it is working pretty good (all the examples are working except the 1st one Think ). I let my code:

def printLIS(arr: list):
	for x in arr:
		print(x, end=" ")
	print()

def constructPrintLIS(arr: list, n: int):

	l = [[] for i in range(n)]
	l[0].append(arr[0])

	for i in range(1, n):
		for j in range(i):
			if arr[i] > arr[j] and (len(l[i]) < len(l[j]) + 1):
				l[i] = l[j].copy()
		l[i].append(arr[i])

	maxx = l[0]

	for x in l:
		if len(x) > len(maxx):
			maxx = x

	printLIS(maxx)

if __name__ == "__main__":

	arr = [1,7,8,3,4,6,8,9,0,5,1,2,3]
	n = len(arr)

	constructPrintLIS(arr, n)
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
Reply
#2
Not sure what you are asking, but will take a shot:
>>> str_of_numbers = input("Please enter numbers separated by spaces: ")
Please enter numbers separated by spaces: 1 7 8 3 4 6 8 9 0 5 1 2 3
>>> list_of_numbers = [int(x) for x in str_of_numbers.split()]
>>> list_of_numbers
[1, 7, 8, 3, 4, 6, 8, 9, 0, 5, 1, 2, 3]
>>>
Reply
#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
#4
Hello Serafin, I did it new (with empty list as you said now), but still having a problem with the Given list by user. I did a new post asking for this problem. I let you the link of my new post and code writ: https://python-forum.io/Thread-Need-some...#pid138309
Reply
#5
post # 2 as a function:
def get_numbers():
    str_of_numbers = input("Please enter numbers separated by spaces: ")
    list_of_numbers = [int(x) for x in str_of_numbers.split()]
    return list_of_numbers

mylist = get_numbers()
print(mylist)
Output:
Please enter numbers separated by spaces: 5 4 3 2 1 0 [5, 4, 3, 2, 1, 0]
Reply
#6
(Feb-27-2021, 08:19 PM)SantiagoPB Wrote:
def longestIncreasing(arr):
    res = []
    temp = [arr[0]]
    for i in range(1, len(arr)):
        if arr[i] > arr[i-1]:
            temp.append(arr[i])
            if i == len(arr)-1 and len(temp) > len(res): res = temp
        else:
            if len(temp) > len(res): res = temp
            temp = [arr[i]]
    return res

def output(arr):
    for n, i in enumerate(arr):
        if n == len(arr)-1: print(i)
        else: print(i, end = ' ')

if __name__ == "__main__":
    arr = [1, 7, 8, 3, 4, 6, 8, 9, 0, 5, 1, 2, 3]
    output(longestIncreasing(arr))
Output:
3 4 6 8 9
Reply


Forum Jump:

User Panel Messages

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