Question

Codio challenge We will pass you 2 inputs an list of numbers a number, N, to...

Codio challenge

We will pass you 2 inputs

an list of numbers

a number, N, to look for

Your job is to loop through the list and find the number specified in the second input. Output the list element index where you find the number.

If N is not found in the list, output -1.

Tip: Remember the break statement? It exits the loop. You can (but don’t have to) use this.

# Get our input from the command line
import sys
N= int(sys.argv[2])

# Convert the list of strings into integers
numbers= []
for i in sys.argv[1].split(","):
if(i.isdigit()):
numbers.append(int(i))
# numbers now contains the list of integers


# Write your code below

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python Program:

"""
   loop through the list and find the number specified in the second input.
   Output the list element index where you find the number
"""

# Get our input from the command line
import sys
N= int(sys.argv[2])

# Convert the list of strings into integers
numbers= []
for i in sys.argv[1].split(","):
   if(i.isdigit()):
       numbers.append(int(i))
# numbers now contains the list of integers
           
# Set index to -1
index = -1

# Variable to iterate over list
i = 0

# Iterating over loop
while index == -1 and i < len(numbers):
  
   #Checking for N
   if numbers[i] == N:
       # Updating index
       index = i
  
   # Incrementing i
   i = i + 1
  
  
# Checking i value
if index == -1:
   # Printing error message
   print("\n Element not found... \n")
else:
   print("\n Element found at %d... \n"%(index))

_____________________________________________________________________________________________

Code Screenshot:

臼findPosition.py E 11 numbers=[] 12 貝for İ in sys.argv [1].split(,): 13 14 15 # numbers now contains the list of integers 1

_____________________________________________________________________________________________

Sample Run:

C:Windows lsystem321cmd.exe C:\Users SaiBabu AppData Local\Programs Python\Python35>python d:Python findPosition. py 23,6,18,

Add a comment
Know the answer?
Add Answer to:
Codio challenge We will pass you 2 inputs an list of numbers a number, N, to...
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
  • 3.6.x10 every other We are passing in 3 inputs A list of numbers A multiplier value,...

    3.6.x10 every other We are passing in 3 inputs A list of numbers A multiplier value, M A value, N You should multiply every Nth element ( do not multiply the 0th element)by M. So if N is 3 you start with the 3rd element which is index 2 If there are less than N elements then you should output the unchanged input list Get our input from the command line port sys İnt(sys.argv [2] ) int (sys.argv[3]) Collape) Challengs...

  • 3.5 max number We will pass in a list of numbers your job is to find...

    3.5 max number We will pass in a list of numbers your job is to find the largest number in that list and outputted it’s index , not the actual value # Get our numbers from the command line import sys 4 numbers sys.argv[1].split',') Collagse) Challenges 5 numbers int(i) for i in numbers] 3. 5. Max number # Your code goes here We will pass in a list of numbers. Your job is to find the largest number in that...

  • 3. 1. Count from 0 to N We will pass in a value, N. You should...

    3. 1. Count from 0 to N We will pass in a value, N. You should write a program that outputs all values from 0 up to an including N. # Get N from the command line import sys N = int(sys.argv[1]) # Your code goes here

  • B Codio - IT-140-R5545 Intro Mail - thon a https://codio.com/twinters/file-handling:5cb76949180630 Codio Project File Edit Find View...

    B Codio - IT-140-R5545 Intro Mail - thon a https://codio.com/twinters/file-handling:5cb76949180630 Codio Project File Edit Find View Tools Education Help ngth-.. # Get the filepath from the command line import sys P= sys.argv[1] F= sys.argv[2] L= sys.argv[3] B= sys.argv[4] # Your Code Goes Here open (P, "r") f.readlines () f lines f.close() f- open (P, "a") for line in lines: line[:16].strip() and L = line[16:32].strip () : if F == print ("%16s%16s%16s "% ( F, L, B) ) else: print (line)...

  • Need some explanation on what im doing wrong.? solved it, forgot to add strings # Get...

    Need some explanation on what im doing wrong.? solved it, forgot to add strings # Get N from the command line import sys N = int(sys.argv[1]) # Your code goes here if N == 100: print (Hit) else: print(Miss) use Conditions explained 2.5. Challenge == We will provide you with a number, N. . If N is equal to 100, output 'Hit' • otherwise, output 'Miss Remember that strings are case-sensitive! Check it! LAST RUN on 5/8/2020, 10:44:31 PM Program...

  • You will develop a program that will read a list of integers into an array. Data...

    You will develop a program that will read a list of integers into an array. Data entry will end when a negative number is entered. When data entry is completed, the program will ask for a nominal value. When this number is entered, two lists will be displayed-one containing the numbers equal to or above the nominal value and a second one with the values below the nominal. A typical output is shown below: This program inputs a list of...

  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • The prime factorization of a number is the unique list of prime numbers that, when multiplied,...

    The prime factorization of a number is the unique list of prime numbers that, when multiplied, gives the number. For example, the prime factorization of 60 is 2 ∗ 2 ∗ 3 ∗ 5. In this problem you must write code to recursively find and return the prime factorization of the given number. You must print these in ascending sorted order with spaces in between. For example, if your input is: 120 then you should print the following output: 2...

  • urgent Help needed in python program ! Thanx # This is a function definition. You can...

    urgent Help needed in python program ! Thanx # This is a function definition. You can ignore this for now. def parse_integer_list_from_string(string): """ Returns a list of integers from a string containing integers separated by spaces. """ # Split the line into a list of strings, each containing a number. number_string_list = string.split() # Create an empty list to store the numbers as integers. numbers = [] # Convert each string to an integer and add it to the list...

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
ADVERTISEMENT