Python Forum
Need some help to fix my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help to fix my code
#1
Question 
Hello every one Wink
I got a little problem with my code and I don't know how to fix it Confused .

Is a homework from python, I have to write a program and make a longest increasing subsequence.

I let you here 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

Currently, the code I have written works perfectly, but I need the array to be written by the user and not pre-written in the code (2nd line of the code "arr = [1,2,3,4,5,6]). I need that this list is written by the user at the time of running the program.

P.S.: I have tried changing the given array by:
arr = input ("Write enter whole number separated by spaces:")

arr = arr.split ("")
But it doesn't work with negative numbers Think .

Here is the code I have written now:

# Given list
arr = [5,4,2,3,4,9,9,9,-8,-5,-3,-2,-1]
# Storing array length and initialize variable
l = len(arr)
i = 0

# Difine the array maxium
max = 1

###Initialize the program###

# Define varialbes to work
s_counting = 0
end_counting = 0
bests_counting = 0
bestend_counting = 0

# While loop - Increment end anytime we found a longer array; update the max; update variables anytime we found a longer array.
while i<l:
    if i+1 < l and arr[i+1]>arr[i]:
        end_counting = end_counting + 1
        if (end_counting-s_counting+1) > max:
            max = (end_counting - s_counting + 1)
            bests_counting = s_counting
            bestend_counting = end_counting
            
# Reset the countings
    else:
        s_counting = i+1
        end_counting = i+1

    i = i + 1

print (arr[bests_counting:bestend_counting+1])
Thank you very much for your help Heart
Larz60+ write Feb-28-2021, 06:31 PM:
Please do not create duplicate posts, we read all forums.
Reply
#2
str.split() splits a string into a list of shorter strings. It does not convert the strings to numbers. Your code was sorting strings, and as a string, '-5' > '-4' (because '5' > '4'). You would find a similar problem with larger number strings, for example '1000000' < '5' (because '1' < '5').

Reading in a string containing digits, splitting it at blanks or commas, and converting the strings to int or float is a common problem with many solutions posted on the internet.
Reply
#3
(Feb-28-2021, 03:27 PM)deanhystad Wrote: str.split() splits a string into a list of shorter strings. It does not convert the strings to numbers. Your code was sorting strings, and as a string, '-5' > '-4' (because '5' > '4'). You would find a similar problem with larger number strings, for example '1000000' < '5' (because '1' < '5').

Reading in a string containing digits, splitting it at blanks or commas, and converting the strings to int or float is a common problem with many solutions posted on the internet.

Yes, this is my problem but I didn't find a solution for this Cry
I try to give the numbers as int(input()) but still not working and I don't know what else to do :S
Reply
#4
You need to do int() for each string returned by split. int() does not do a list of strings.
Reply
#5
The problem that you are having converting a list of strings to a list of negative numbers is that the character "-" in the list of strings is not a minus sign but a hyphen. In order for you to use int () to make the conversion, you will first need to change the hyphen to a minus sign. One way to do that is to replace it with the unicode character for minus which is U2212. Try this :

number_string = input ('Enter numbers seperated by a space : ') 
string_array = number_string.split (' ')
number_array = [int (x.replace (u'\u2212', '-')) for x in string_array]

print (string_array)
print (number_array)
Reply
#6
(Feb-28-2021, 10:33 PM)BashBedlam Wrote: The problem that you are having converting a list of strings to a list of negative numbers is that the character "-" in the list of strings is not a minus sign but a hyphen. In order for you to use int () to make the conversion, you will first need to change the hyphen to a minus sign. One way to do that is to replace it with the unicode character for minus which is U2212. Try this :

number_string = input ('Enter numbers seperated by a space : ') 
string_array = number_string.split (' ')
number_array = [int (x.replace (u'\u2212', '-')) for x in string_array]

print (string_array)
print (number_array)

Don't think thats the ptoblem. I had no problem converting to int after cutting and pasting the list from the post
Reply
#7
(Feb-28-2021, 10:33 PM)BashBedlam Wrote: In order for you to use int () to make the conversion, you will first need to change the hyphen to a minus sign
There is no need for that.
>>> number_string = input ('Enter numbers seperated by a space : ')
Enter numbers seperated by a space : 9 5 -5 2 -500

>>> numbers = [int(i) for i in number_string.split()]
>>> numbers
[9, 5, -5, 2, -500]

>>> numbers[1] + numbers[2]
0
>>> numbers[-1] + 1000
500
BashBedlam likes this post
Reply


Forum Jump:

User Panel Messages

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