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


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