Question

Python: problem 1 Given an array of integers, return the sum of two indices from this...

Python: problem 1

Given an array of integers, return the sum of two indices from this array based on input parameters, x and y.

Example:

Given nums = [2, 7, 11, 15],  x = 3, y = 1

Because nums[x] + nums[y] = 15 + 7 = 22,
return 22.

You may use this as a template for your code

class Solution:
    def two_sum(self, nums: List[int], x: int, y: int) -> int:
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

# define a class Solution
class Solution:
    # write a method two_sum  which return sum at two input indices
    def two_sum(self, nums, x, y):
        return nums[x] + nums[y]


if __name__ == '__main__':
    # create an object of class Solution
    solution = Solution()
    # call the method to calculate sum
    # pass valid input parameters to it

    result = solution.two_sum([2, 7, 11, 15], 3, 1)

    # print the result
    print("The sum of element at index {} and {} is : {}".format(3, 1, result))

screenshots:

# define a class solution class Solution: # write a method two_sum which return sum at two input indices def two sum (self, nOutput:

The sum of element at index 3 and 1 is : 22, Process finished with exit code o

Add a comment
Know the answer?
Add Answer to:
Python: problem 1 Given an array of integers, return the sum of two indices from this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Parvati is given an array of integers. She is asked to return the sum of all...

    Parvati is given an array of integers. She is asked to return the sum of all the two-digit numbers in the array. Please help her to perform this task. Write a function: class Solution { public int solution (int[] A); } that, given an array A consisting of N integers, returns the sum of all two-digit numbers. For example, given A = [1, 1000, 80, -91], the function should return -11 (as the two-digit numbers are 80 and -91). Given...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • c programming. Given a sorted array of 20 integers and a target integer value, your program...

    c programming. Given a sorted array of 20 integers and a target integer value, your program needs to print out all pairs of integers which have sum equal to the target value. If there is no pair of integers to return, print out “No pair of integers in the given array can be summed to the target value”. The array of integers and the target value should be given from the keyboard (taken as user input). Inputs and Outputs should...

  • Please write a Java program: Given an array of positive integers, return a count of the...

    Please write a Java program: Given an array of positive integers, return a count of the number of even integers. countEvens([2, 3, 5])-1 countEvens([4, 20]) - 2 countEvens([3, 7, 1, 11]) 0 Go Save, Compile, Run (ctrl-enter) int countEvens (int[] nums) {

  • Python DESCRIPTION Write a program that will read an array of integers from a file and...

    Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....

  • convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [...

    convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [ ] if len(nums) == 1: return nums [0] # first find freq freq dict d = {} for num in nums: if num in d: d[num] -= 1 # reverse the sign on the freq for the heap's sake else: d[num] = -1 h = [] from heapq import heappush, heappop for key in di heappush(h, (d[key], key)) res = [] count = 0...

  • Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our...

    Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our textbook) E.g.: Input: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Constraints: - List elements are integers - The function should return a reversed array, not print its elements You may use the following code as template: def reverse(my_list, index = None): # your code here

  • In Python class int Set (object): "" "An intSet is a set of integers The value...

    In Python class int Set (object): "" "An intSet is a set of integers The value is represented by a list of ints, self.vals. Each int in the set occurs in self.vals exactly once. """ definit__(self): """Create an empty set of integers""" self.vals = 0 def insert (self, e): """Assumes e is an integer and inserts e into self""" if not e in self.vals: self.vals.append(e) def member (self, e): """Assumes e is an integer Returns True if e is in...

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • Python: Create a function count_target_in_list that takes a list of integers and the target value then...

    Python: Create a function count_target_in_list that takes a list of integers and the target value then counts and returns the number of times the target value appears in the list. Write program in that uses get_int_list_from_user method to get a list of 10 numbers, then calls the count_target_list method to get the count then prints the number of times the target was found in the list. def get_int_list_from_user(): lst=[] y = int(input("Enter number of numbers")) for x in range(y): lst.append(int(input("Enter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT