Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Product of maximum in first array and minimum in second Thethispointer Programmer named Tim Posts: 7 Threads: 2 Joined: Jan 2018 Reputation: 1 Likes received: 0 #1 Jan-19-2018, 05:32 AM So I am practicing the following Quote:Given two arrays of size N1 and N2 respectively, the task is to calculate the product of max element of first array and min element of second array. https://practice.geeksforgeeks.org/probl...n-second/0 I was writing this program and got hung up on an error... Quote:Traceback (most recent call last): File "C:\Users\groundZero Admin\Documents\Python\arrayPracetice1.py", line 22, in y = A[i] IndexError: list index out of range ```#Given two arrays of size N1 and N2 respectively, the task is to #calculate the product of max element of first array and min #element of second array. # # A = [1, 50, 2, 7, 3, 8] B = [1, 50, 2, 7, 3, 8, 75, 77, 0] listALength = len(A) listBLength = len(B) #These print functions were used to test the len function on arrays during #this study session. They're now commented out to make them inactive. #print(listALength) #print(listBLength) #Take the length of the array - 1 to increment the list via a loop x = A[0] for i in A: y = A[i] if y > x: x = y print(x) ```Please help me! Thank you! j.crater Posts: 1,082 Threads: 33 Joined: Sep 2016 Reputation: 42 Likes received: 152 #2 Jan-19-2018, 06:35 AM (This post was last modified: Jan-19-2018, 06:35 AM by j.crater. Edited 1 time in total.) The "i" in your line `for i in A:` is not an index as you have probably expected. It is the element of A accessed in a single iteration of for loop. Try this: ```for i in A: print(i) ```For getting indexes you can use `enumerate`. Here is an example: ```my_list = ['one', 'two', 'three'] for i, number in enumerate(my_list): print(i, number) ```As a side note, you may want to get yourself familiar with min() and max() Python functions. Also, array in Python is called list, if you care about avoiding "foul language" ;P Thethispointer Programmer named Tim Posts: 7 Threads: 2 Joined: Jan 2018 Reputation: 1 Likes received: 0 #3 Jan-19-2018, 06:53 AM (This post was last modified: Jan-19-2018, 06:53 AM by Thethispointer. Edited 4 times in total.) Awesome, awesome, awesome! Thank you! Here is the functional code, now let's add min() and max()... or maybe tomorrow morning as it's getting pretty late here in the Midwest. ```#Given two Lists of size N1 and N2 respectively, the task is to #calculate the product of max element of first List and min #element of second List. A = [1, 50, 2, 7, 3, 8] B = [1, 50, 2, 7, 3, 8, 75, 77, 0] listALength = len(A) listBLength = len(B) x = A[0] for i, number in enumerate(A): y = A[i] if y > x: x = y print(x)``` Okay, with min() and max(). ```#Given two Lists of size N1 and N2 respectively, the task is to #calculate the product of max element of first List and min #element of second List. A = [1, 50, 2, 7, 3, 8] B = [1, 50, 2, 7, 3, 8, 75, 77, 0] print(min(B) + max(A))``` j.crater Posts: 1,082 Threads: 33 Joined: Sep 2016 Reputation: 42 Likes received: 152 #4 Jan-19-2018, 07:08 AM Code looks much nicer/shorter. Except the task ultimately requires a product, not sum of those elements, if I understood right =) Thethispointer Programmer named Tim Posts: 7 Threads: 2 Joined: Jan 2018 Reputation: 1 Likes received: 0 #5 Jan-19-2018, 04:29 PM (Jan-19-2018, 07:08 AM)j.crater Wrote: Code looks much nicer/shorter. Except the task ultimately requires a product, not sum of those elements, if I understood right =) Yes indeed you are correct! I guess I should read more carefully! Thank you again for your help, I will definitely post some more code onto the forum when I find another issue I am hung up on! j.crater Posts: 1,082 Threads: 33 Joined: Sep 2016 Reputation: 42 Likes received: 152 #6 Jan-19-2018, 04:44 PM (This post was last modified: Jan-19-2018, 04:45 PM by j.crater. Edited 1 time in total.) No problem, we are glad to help! =) Beside posting when you hit an obstacle, feel free to also post in Completed Scripts/Snippets subforum, if you would like to share your working code and get some feedback for improvement ;) Larz60+ aetate et sapientia Posts: 8,155 Threads: 299 Joined: Sep 2016 Reputation: 326 Likes received: 1098 #7 Jan-19-2018, 05:23 PM Quote:Given two arrays of size N1 and N2 respectively, the task is to calculate the product of max element of first array and min element of second array. >>> A = [1, 50, 2, 7, 3, 8] >>> B = [1, 50, 2, 7, 3, 8, 75, 77, 0] >>> max(A) * min(B) 0 >>> j.crater Posts: 1,082 Threads: 33 Joined: Sep 2016 Reputation: 42 Likes received: 152 #8 Jan-19-2018, 05:27 PM I guess that is alright. Example on the link Thethispointer provided even has a negative number in B list. So result can end up negative as well. Larz60+ aetate et sapientia Posts: 8,155 Threads: 299 Joined: Sep 2016 Reputation: 326 Likes received: 1098 #9 Jan-19-2018, 05:31 PM ```>>> B = [1, 50, 2, 7, 3, 8, 75, 77, 0, -5] >>> max(A) * min(B) -250 >>> ``` Thethispointer Programmer named Tim Posts: 7 Threads: 2 Joined: Jan 2018 Reputation: 1 Likes received: 0 #10 Jan-19-2018, 07:38 PM (Jan-19-2018, 04:44 PM)j.crater Wrote: No problem, we are glad to help! =) Beside posting when you hit an obstacle, feel free to also post in Completed Scripts/Snippets subforum, if you would like to share your working code and get some feedback for improvement ;) Yeah, I will do this! (Jan-19-2018, 05:23 PM)Larz60+ Wrote: Quote:Given two arrays of size N1 and N2 respectively, the task is to calculate the product of max element of first array and min element of second array. >>> A = [1, 50, 2, 7, 3, 8] >>> B = [1, 50, 2, 7, 3, 8, 75, 77, 0] >>> max(A) * min(B) 0 >>> Thank you! This is accurate « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Delete minimum occurence in a string RavCOder 10 312 Nov-12-2019, 01:08 PM Last Post: RavCOder Minimum size Amniote 8 476 Jul-10-2019, 02:58 PM Last Post: nilamo Product expression. jarrod0987 1 471 Dec-13-2018, 11:32 AM Last Post: buran Armstrong in minimum lines Gaurav 1 506 Sep-03-2018, 03:46 PM Last Post: j.crater

Forum Jump:

Users browsing this thread: 1 Guest(s)