Python Forum
To find the index of the first occurrence of the key in the provided list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
To find the index of the first occurrence of the key in the provided list
#5
Hi All,

Please could someone help with the error I am getting with my code

Description
Find the intersection of two sorted lists

Output:
Input [[1, 2, 3, 5, 9], [3, 6, 9, 12]] Solution output 3 9 Expected output [3, 9] Testcase failed! The solution's output doesn't match the expected output. Input [[1, 1, 2, 4, 8, 8, 8, 9], [1, 2, 6, 7, 8, 8, 8]] Solution output 1 2 8 8 8 Expected output [1, 2, 8, 8, 8] Testcase failed! The solution's output doesn't match the expected output.
Sorry posted wrong code and error!!!!also just to let me know what changes to be done in the linked code to get the desired output.

# Reading the input
import ast 
input_lists = ast.literal_eval(input())

# Write your code here
# Python program to find intersection of 
# two sorted arrays 
# Function prints Intersection of arr1[] and arr2[] 
# m is the number of elements in arr1[] 
# n is the number of elements in arr2[] 
def printIntersection(arr1, arr2, m, n): 
    i,j = 0,0
    while i < m and j < n: 
        if arr1[i] < arr2[j]: 
            i += 1
        elif arr2[j] < arr1[i]: 
            j+= 1
        else:
            print(arr2[j])
            j += 1
            i += 1

#def printArray(arr2): 
#    j=0
#    print(arr2[j])  
# Driver program to test above function 

arr1 = input_lists[0]
arr2 = input_lists[1]

m = len(arr1) 
n = len(arr2) 

printIntersection(arr1, arr2, m, n) 
#printArray(arr2) 
I'm really new to python can anyone help me to correct the code
Reply


Messages In This Thread
Find the intersection of two sorted lists - by Angry_bird89 - Jun-20-2020, 06:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix list index out of range longmen 26 6,289 Apr-27-2022, 05:46 PM
Last Post: deanhystad
  list index out of range OliverG 3 2,408 Sep-03-2021, 12:04 AM
Last Post: itsmycode
  Index List a04j 2 2,972 Jul-10-2021, 01:14 PM
Last Post: BashBedlam
  List index out of range when turning CSV into dict ranbarr 15 6,656 May-12-2021, 10:38 AM
Last Post: ranbarr
  How to find difference between elements in a list? Using beginner Basics only. Anklebiter 8 4,449 Nov-19-2020, 07:43 PM
Last Post: Anklebiter
  List vs index: Frederico_Caldas 5 3,698 Jul-03-2020, 10:55 AM
Last Post: DeaD_EyE
  How do you find the length of an element of a list? pav1983 13 5,078 Jun-13-2020, 12:06 AM
Last Post: pav1983
  Find first letter from a list DariusCG 3 2,610 Nov-27-2019, 11:08 PM
Last Post: ichabod801
  Generating a dict from provided randomizer beLIEve 0 1,570 Oct-11-2019, 09:25 AM
Last Post: beLIEve
  list index out of range mcgrim 2 2,971 May-25-2019, 07:44 PM
Last Post: mcgrim

Forum Jump:

User Panel Messages

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