I need some help with my code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: I need some help with my code (/thread-32711.html) |
I need some help with my code - SantiagoPB - Feb-27-2021 Hello everyone I am Santiago 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 ). 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 . 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 . 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 RE: I need some help with my code - Larz60+ - Feb-28-2021 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] >>> RE: I need some help with my code - Serafim - Feb-28-2021 (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. 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:
I implemented exactly this and here is the test run:
RE: I need some help with my code - SantiagoPB - Feb-28-2021 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-help-to-fix-my-code?pid=138309#pid138309 RE: I need some help with my code - Larz60+ - Feb-28-2021 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)
RE: I need some help with my code - naughtyCat - Aug-27-2021 (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)) |