Hi guys, i am new here. Been on this code for some days and i seem stuck and not getting the expected output from this code.
first the code functions as a FIFO QUEUE where it takes input from the user and put it at the beginning of a list while deleting the last item on the list and creating a new list.
then i did function to search for four floating numbers (1.3,1.4,1.5,1.6) in the new list such that any float in the list that has identical integer part and decimal part split like any of the four floating numbers(1.4, 1.5, 1.6, 1.3) is denoted as a variant of the float e.g 17.34, 35.19 and 30.01 will be variants of float 1.3. Similarly 12.56, 57.17 and 10.50 will be variant of float 1.5. Also,a float like 14.31 if present in the list will be regarded as a variant of both 1.3 and 1.4 while float 15.61 will be regarded as a variant of both float 1.5 and 1.6.
So anytime the code runs, it takes the first THREE numbers each in the list or queue that are variants of 1.3,1.4,1.5 and 1.6 and create a new list for each variant. for example

list1 =[1.72, 10.60, 34.21,18.40, 11.13, 45.33, 1.03, 35.91]
this list has 4 variants of float 1.3
the new lists that will be created for float 1.3 from this list when i run the search function is:

new list a=[1.72, 10.60,34.21]

new list b=[1.72, 10.60, 34.21, 18.40,11.13]

new list c=[1.72, 10.60,34.21, 18.40, 11.13,45.33,1.03]

Lastly, the code performs a reverse indexing on each of the 3 new list with the second to the last number on each list having an index of 1 and third to the last digit having an index of 2. if there is any number in each of the new list that is equal to or greater than 2 with the same index index number, then there is a match. and the functions returns an output. the number only has to be equal to or greater than 2 but the index must match for all 3 new list.

from the above lists, the code returns an output like this" there is a match at index 1 for variant 1.3"

attached to this post is the code i have written. its a bit longer than it should if not i would have posted it on here. it runs without any problems for the first run, but subsequently fails to bring out expected output when i change the number in the list or when the queue FIFO starts working.

i will be happy if anyone can take a look and help check whats wrong with it or help modify it to work as i want

Recommended Answers

All 9 Replies

This is essentially the same question as posted here. You get the same answer. Show us what you have so far, and where you are having problems. You said you attached the code but I don't see it. Please post it inline using the code block tool </>. This makes it easier for us to look at the code, and that makes it more likely you will get an answer.

Based on your description of variant I don't see how 14.31 is a variant of 1.4. It has a 1 to the left of the decimal but not a 4 to the right. Be more specific as to what defines a variant. Doing it by example is not a complete definition.

commented: i will try it again +0
my_list = [7.48, 1.11, 1.16, 2.00, 1.49, 3.43, 4.20, 1.04, 20.56, 3.53, 5.61, 2.01, 1.58, 3.91, 3.63, 2.40, 2.03, 1.50, 1.05, 1.01]

#print(my_list[-1])
#print(my_list[0])


def search_engine(my_new_list):
    new_input = 1.3
    new_input = str(new_input)
    first_number1, second_number1 = new_input.split('.')
    reverse_new_input = new_input[::-1]
    first_number2, second_number2 = reverse_new_input.split('.')
    search_list =[]
    try:
        for item in my_new_list:
            first_number3, second_number3 = str(item).split('.')
            if (first_number1 == first_number3 and (second_number1 in second_number3)) or (first_number2 == first_number3 and (second_number2 in second_number3)):
                search_list.append(item)

        if len(search_list) > 3:

            search_list.remove(my_list[-1])  
            #print(search_list)
        elif len(search_list) < 3:
            print(f'No match for {new_input}')
            return 

        else:
            search_list = search_list

    except Exception as e:
        print(f'No match for {new_input}')
        return 

    index_list =[]
    for search_item in search_list:
        index = my_new_list.index(search_item)
        index_list.append(index)

    final_search1 = []
    for item in my_new_list[:index_list[0]]:
        final_search1.append(item)
    final_search1 = final_search1[::-1]

    final_search2 = []
    for item in my_new_list[:index_list[1]]:
        final_search2.append(item) 
    final_search2 = final_search2[::-1]

    final_search3 = []
    for item in my_new_list[:index_list[2]]:
        #if float(item) >=2.0:
        final_search3.append(item)
    final_search3 = final_search3[::-1] 
    new_list_two_id = (len(final_search1))

    count= -1
    newest_list1 = []
    for items in final_search1:
        count += 1
        try:
            if count <= new_list_two_id:
                if final_search1[count] >=2.0: 
                    newest_list1.append(count)      
            else:
                pass
        except Exception as e:
            pass


    count2= -1
    newest_list2 = []
    for items in final_search2:
        count2 += 1
        try:
            if final_search2[count2] >=2.0: 
                newest_list2.append(count2)
            else:
                pass
        except Exception as e:
            pass


    count3= -1
    newest_list3 = []
    for items in final_search3:
        count3 += 1
        try:
            if final_search3[count3] >=2.0: 
                newest_list3.append(count3)     
            else:
                pass
        except Exception as e:
            pass

    new_list_id = len(newest_list1)
    counter= -1
    final_list1 = []
    for items in newest_list1:
        counter += 1
        try:
            if counter <= new_list_id:
                if newest_list1[counter] == newest_list2[counter] == newest_list3[counter]: 
                    print(f"Decimal {new_input} checks at distance {counter + 1}")
                    final_list1.append(counter)    
            else:
                pass
        except Exception as e:
            pass


def search_engine2(my_new_list):
    new_input = 1.4
    new_input = str(new_input)
    first_number1, second_number1 = new_input.split('.')
    reverse_new_input = new_input[::-1]
    first_number2, second_number2 = reverse_new_input.split('.')
    search_list =[]
    try:
        for item in my_new_list:
            first_number3, second_number3 = str(item).split('.')
            if (first_number1 == first_number3 and (second_number1 in second_number3)) or (first_number2 == first_number3 and (second_number2 in second_number3)):
                search_list.append(item)

        if len(search_list) > 3:

            search_list.remove(my_list[-1])  
            #print(search_list)
        elif len(search_list) < 3:
            print(f'No match for {new_input}')
            return 

        else:
            search_list = search_list

    except Exception as e:
        print(f'No match for {new_input}')
        return 

    index_list =[]
    for search_item in search_list:
        index = my_new_list.index(search_item)
        index_list.append(index)

    final_search1 = []
    for item in my_new_list[:index_list[0]]:
        final_search1.append(item)
    final_search1 = final_search1[::-1]

    final_search2 = []
    for item in my_new_list[:index_list[1]]:
        final_search2.append(item) 
    final_search2 = final_search2[::-1]

    final_search3 = []
    for item in my_new_list[:index_list[2]]:
        #if float(item) >=2.0:
        final_search3.append(item)
    final_search3 = final_search3[::-1] 
    new_list_two_id = (len(final_search1))

    count= -1
    newest_list1 = []
    for items in final_search1:
        count += 1
        try:
            if count <= new_list_two_id:
                if final_search1[count] >=2.0: 
                    newest_list1.append(count)      
            else:
                pass
        except Exception as e:
            pass


    count2= -1
    newest_list2 = []
    for items in final_search2:
        count2 += 1
        try:
            if final_search2[count2] >=2.0: 
                newest_list2.append(count2)
            else:
                pass
        except Exception as e:
            pass


    count3= -1
    newest_list3 = []
    for items in final_search3:
        count3 += 1
        try:
            if final_search3[count3] >=2.0: 
                newest_list3.append(count3)     
            else:
                pass
        except Exception as e:
            pass

    new_list_id = len(newest_list1)
    counter= -1
    final_list1 = []
    for items in newest_list1:
        counter += 1
        try:
            if counter <= new_list_id:
                if newest_list1[counter] == newest_list2[counter] == newest_list3[counter]: 
                    print(f"Decimal {new_input} checks at distance {counter + 1}")
                    final_list1.append(counter)    
            else:
                pass
        except Exception as e:
            pass


def search_engine3(my_new_list):
    new_input = 1.5
    new_input = str(new_input)
    first_number1, second_number1 = new_input.split('.')
    reverse_new_input = new_input[::-1]
    first_number2, second_number2 = reverse_new_input.split('.')
    search_list =[]
    try:
        for item in my_new_list:
            first_number3, second_number3 = str(item).split('.')
            if (first_number1 == first_number3 and (second_number1 in second_number3)) or (first_number2 == first_number3 and (second_number2 in second_number3)):
                search_list.append(item)

        if len(search_list) > 3:

            search_list.remove(my_list[-1])  
            #print(search_list)
        elif len(search_list) < 3:
            print(f'No match for {new_input}')
            return 

        else:
            search_list = search_list

    except Exception as e:
        print(f'No match for {new_input}')
        return 

    index_list =[]
    for search_item in search_list:
        index = my_new_list.index(search_item)
        index_list.append(index)

    final_search1 = []
    for item in my_new_list[:index_list[0]]:
        final_search1.append(item)
    final_search1 = final_search1[::-1]

    final_search2 = []
    for item in my_new_list[:index_list[1]]:
        final_search2.append(item) 
    final_search2 = final_search2[::-1]

    final_search3 = []
    for item in my_new_list[:index_list[2]]:
        #if float(item) >=2.0:
        final_search3.append(item)
    final_search3 = final_search3[::-1] 
    new_list_two_id = (len(final_search1))

    count= -1
    newest_list1 = []
    for items in final_search1:
        count += 1
        try:
            if count <= new_list_two_id:
                if final_search1[count] >=2.0: 
                    newest_list1.append(count)      
            else:
                pass
        except Exception as e:
            pass


    count2= -1
    newest_list2 = []
    for items in final_search2:
        count2 += 1
        try:
            if final_search2[count2] >=2.0: 
                newest_list2.append(count2)
            else:
                pass
        except Exception as e:
            pass


    count3= -1
    newest_list3 = []
    for items in final_search3:
        count3 += 1
        try:
            if final_search3[count3] >=2.0: 
                newest_list3.append(count3)     
            else:
                pass
        except Exception as e:
            pass

    new_list_id = len(newest_list1)
    counter= -1
    final_list1 = []
    for items in newest_list1:
        counter += 1
        try:
            if counter <= new_list_id:
                if newest_list1[counter] == newest_list2[counter] == newest_list3[counter]: 
                    print(f"Decimal {new_input} checks at distance {counter + 1}")
                    final_list1.append(counter)    
            else:
                pass
        except Exception as e:
            pass


def search_engine4(my_new_list):
    new_input = 1.6
    new_input = str(new_input)
    first_number1, second_number1 = new_input.split('.')
    reverse_new_input = new_input[::-1]
    first_number2, second_number2 = reverse_new_input.split('.')
    search_list =[]
    try:
        for item in my_new_list:
            first_number3, second_number3 = str(item).split('.')
            if (first_number1 == first_number3 and (second_number1 in second_number3)) or (first_number2 == first_number3 and (second_number2 in second_number3)):
                search_list.append(item)

        if len(search_list) > 3:

            search_list.remove(my_list[-1])  
            #print(search_list)
        elif len(search_list) < 3:
            print(f'No match for {new_input}')
            return 

        else:
            search_list = search_list

    except Exception as e:
        print(f'No match for {new_input}')
        return 

    index_list =[]
    for search_item in search_list:
        index = my_new_list.index(search_item)
        index_list.append(index)

    final_search1 = []
    for item in my_new_list[:index_list[0]]:
        final_search1.append(item)
    final_search1 = final_search1[::-1]

    final_search2 = []
    for item in my_new_list[:index_list[1]]:
        final_search2.append(item) 
    final_search2 = final_search2[::-1]

    final_search3 = []
    for item in my_new_list[:index_list[2]]:
        #if float(item) >=2.0:
        final_search3.append(item)
    final_search3 = final_search3[::-1] 
    new_list_two_id = (len(final_search1))

    count= -1
    newest_list1 = []
    for items in final_search1:
        count += 1
        try:
            if count <= new_list_two_id:
                if final_search1[count] >=2.0: 
                    newest_list1.append(count)      
            else:
                pass
        except Exception as e:
            pass


    count2= -1
    newest_list2 = []
    for items in final_search2:
        count2 += 1
        try:
            if final_search2[count2] >=2.0: 
                newest_list2.append(count2)
            else:
                pass
        except Exception as e:
            pass


    count3= -1
    newest_list3 = []
    for items in final_search3:
        count3 += 1
        try:
            if final_search3[count3] >=2.0: 
                newest_list3.append(count3)     
            else:
                pass
        except Exception as e:
            pass

    new_list_id = len(newest_list1)
    counter= -1
    final_list1 = []
    for items in newest_list1:
        counter += 1
        try:
            if counter <= new_list_id:
                if newest_list1[counter] == newest_list2[counter] == newest_list3[counter]: 
                    print(f"Decimal {new_input} checks at distance {counter + 1}")
                    final_list1.append(counter)    
            else:
                pass
        except Exception as e:
            pass





def update_list():
    f = open("list_file.txt", "r")
    print(f"Last Updated Liist: {f.read()}")
    enter_input = float(input("Please enter your input: "))
    my_list.insert(0, enter_input)
    my_list.remove(my_list[-1])
    my_new_list = my_list
    print(f"Updated Liist: {my_new_list}")
    search_engine(my_new_list)
    search_engine2(my_new_list)
    search_engine3(my_new_list)
    search_engine4(my_new_list)
    f = open("list_file.txt", "w")
    f.write(str(my_new_list))
    f.close()

    return my_new_list
update_list()   

i have posted the code.its a bit bulky because i had to repeat one function four times. i know there is a shorter way to do this but i have not figured out how.

as for the variant question you asked.
14.31 is a variant of 1.4 but its a reversed variant. what matters is if the variant has one of the numbers of the float on the integer part of the number and the second number that makes up the float(number 4 in this example) will be on the decimal part.
for example 14.31 has number 1 on the its integer side and a 4 on its decimal side, hence its a variant of 1.4.
in the same vein, 14.50 is not a variant of 1.4 because its decimal side doesnt have a 1 or 4 but its integer side has a 4 and 1.

14.31 has number 1 on the its integer side and a 4 on its decimal side, hence its a variant of 1.4.

???

commented: That was a typo. I meant to say 14.31 has a 4 on its integer part and a 1 on its decimal side. +0

Any suggestion or solution will be greatly appreciated

Amazing. 447 lines of code and not one single comment. Since you are the second person to post this question I can only assume this is an assignment problem. As a former PA and marker I will tell you that I would have given this assignment a zero whether or not it produced the correct output. Programming is so much more than just producing code that works. Code must also be readable and comprehensible by humans. My rule of thumb is to assume that the person who will be maintaining my code is a psycho killer who knows where I live.

But I digress.

Having multiple methods named search_engine# is extremely poor design. At the very least I would expect one method with a signature like

def isVariant(a:str, b:str) -> bool:
    """Returns True if string <a> is a variant of string <b>

    <a> is a string representation of a floating point number
    <b> is a string of the form '#.#'

    <a> is a variant of <b> if any digit to the left of '.' matches
    one digit of <b>, and any digit to the right of '.' mstches
    the other digit of <b>.
    """

The method name gives me an indication of what it does. search_engine3 just tells me it searches something for something else.

Because floating point numbers can not be stored exactly as typed, you should be treating all numbers as strings. For one thing, this makes splitting numbers into "digits to the left of the decimal" and "digits to the right of the decimal" a simple split('.') operation.

i have posted the code.its a bit bulky because i had to repeat one function four times. i know there is a shorter way to do this but i have not figured out how.

One thing you can do is, rather than having separate final_searchn and newest_listn variables, use a list of lists for each of those. For example:

final_search = []
for i in range(3):
    final_search[i] = []
    for item in my_new_list[:index_list[0]]:
        final_search[i].append(item)
    final_search[i] = final_search[i][::-1]

As for the function search_engine(), I would parameterize new_input:

def search_engine(my_new_list, new_input):
    # ...


def update_list():
    f = open("list_file.txt", "r")
    print(f"Last Updated Liist: {f.read()}")
    enter_input = float(input("Please enter your input: "))
    my_list.insert(0, enter_input)
    my_list.remove(my_list[-1])
    my_new_list = my_list
    print(f"Updated List: {my_new_list}")
    for ni in ['3', '4', '5', '6']:
        search_engine(my_new_list, ni)

    f = open("list_file.txt", "w")
    f.write(str(my_new_list))
    f.close()

I'm going to weigh providing code for an assignment against giving an example of how you should be coding. The example wins out. Here is sample code for the isVariant function. Note that it indicates the expected types of the parameters (although this is not enforced via asserts) as well as the return type. It has a small but useful doc string. It also comes with some code to test it. The line that splits the first parameter appends a '.' to account for the case where the first parameter does not contain a '.'

def isVariant(a:str, b:str) -> bool:
    """Returns True if string <a> is a variant of string <b>

    <a> is a string representation of a floating point number
    <b> is a string of the form '#.#'

    <a> is a variant of <b> if any digit to the left of '.' matches
    one of the digits of <b>, and any digit to the right of '.' matches
    the other digit of <b>.
    """

    # Split a and b into digits left of '.' and right of '.'
    la,ra = (a + '.').split('.')[:2]
    lb,rb = b.split('.')

    return (lb in la and rb in ra) or (lb in ra and rb in la)

if __name__ == '__main__':
    a = '123.456'

    b = '1.4'
    print(f'{a=} {b=} {isVariant(a,b)=}')

    b = '4.1'
    print(f'{a=} {b=} {isVariant(a,b)=}')

    a = '1234'
    print(f'{a=} {b=} {isVariant(a,b)=}')
commented: a mistake in my first question. why does the your example output a false result if a=21.4 and b=1.4, +0

Ok thanks guys...
I can continue from here with the suggestion and example you have given.
I hooe someday i will be here to help someone as well wth their question

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.