Question

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 of numbers.
for number_string in number_string_list:
numbers.append(int(number_string))
return numbers # Return the list of integers

# Prompt the user to enter a single line of integers separated by spaces.
# Assign the list of integers to number_list. Print the resulting list.
number_list = parse_integer_list_from_string(input('Numbers?\n'))
print(number_list)

# Do not edit any of the code above this line.
# 1) Print the largest two digit number in number_list. If there are no
# two digits numbers in the list, print -1.
# Test cases:
# [1, 5, 99, 22, 3] -> 99
# [111, 4444] -> -1
# [44, 16, 8, 74] -> 74
# Pseudocode:
# Assign max_number to -1
# Loop through the numbers
# If number is greater than max_number and 10 <= number <= 99
# Assign number to max_number
# Print max_number

# 2) Swap the largest number in the list with
# the smallest number in the list. Print number_list.
# [23, 500, 99] -> [500, 23, 99]

# 3) Print the median of number_list.
# [23, 500, 99] -> 99

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

Required python program as below:

# This is a function definition. You can ignore this for now.
def parse_integer_list_from_string(string):
# 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 of numbers.
for number_string in number_string_list:
numbers.append(int(number_string))
# Return the list of integers
return numbers


# Prompt the user to enter a single line of integers separated by spaces.
# Assign the list of integers to number_list. Print the resulting list.
number_list = parse_integer_list_from_string(input('Numbers?\n'))
print(number_list)
# Do not edit any of the code above this line.

# Test cases:
# [1, 5, 99, 22, 3] -> 99
# [111, 4444] -> -1
# [44, 16, 8, 74] -> 74
# Pseudocode:

# 1) Print the largest two digit number in number_list. If there are no
# two digits numbers in the list, print -1.
# Assign max_number to -1
max_number = -1
# Loop through the numbers
for number in number_list:
# If number is greater than max_number and 10 <= number <= 99
if number>max_number and 10<=number<=99:
# Assign number to max_number
max_number = number
# Print max_number
print(max_number)


# 2) Swap the largest number in the list with
# the smallest number in the list. Print number_list.
# [23, 500, 99] -> [500, 23, 99]
largest_index = 0
smallest_index = 0
largest = number_list[largest_index]
smallest = number_list[smallest_index]
for index, item in enumerate(number_list):
if item>largest:
largest = item
largest_index = index
elif item<smallest:
smallest = item
smallest_index = index
temp = number_list[largest_index]
number_list[largest_index] = number_list[smallest_index]
number_list[smallest_index] = temp
print(number_list)


# 3) Print the median of number_list.
# [23, 500, 99] -> 99
sorted_number_list = sorted(number_list)
length = len(sorted_number_list)
mid = int(length/2)
if not length % 2:
print((sorted_number_list[mid] + sorted_number_list[mid - 1]) / 2.0)
else:
print(sorted_number_list[mid])

Screenshot:

File Edit Format Run Options Window Help # This is a function definition . You can ignore this f r now def parse integer listif not length 2: print ((sorted number_list[mid]sorted number 1is[mid - 1]) 2.0) else: print (sorted number_11st[mid1)

Output:

Add a comment
Know the answer?
Add Answer to:
urgent Help needed in python program ! Thanx # This is a function definition. You can...
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
  • IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can...

    IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can prints the number of words in the string. You can assume that words will only be separated with spaces, commas, and periods. "hello, world" -> 2 "this is cool" -> 3 4) Is Sorted Write a function isSorted() which takes in an list of integers and returns true if the numbers are sorted from smallest to largest.

  • Swapping Values in python Summary In this lab, you complete a Python program that swaps values...

    Swapping Values in python Summary In this lab, you complete a Python program that swaps values stored in three variables and determines maximum and minimum values. The Python file provided for this lab contains the necessary input and output statements. You want to end up with the smallest value stored in the variable named first and the largest value stored in the variable named third. You need to write the statements that compare the values and swap them if appropriate....

  • Help with programming in C++ Phase 1 is complete, please help with phase 2. Thank you....

    Help with programming in C++ Phase 1 is complete, please help with phase 2. Thank you. Phase 1 You must design 3 algorithms, and provide both a flow chart and pseudo code for the algorithms.    Algorithm descriptions: Given an integer parameter named current_number and two constant global variables: const int MIN_NUMBER = 1; const int MAX_NUMBER = 8; Create an algorithm named forward, that will advance ONE value through a sequence of numbers 1, 2, 3 ... MAX_NUMBER. In...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : Alex got a sequence of n integers a1,a2,…,an as a birthday present. Alex doesn't like negative numbers, so he decided to erase the minus signs from all negative numbers. As a result, he got a sequence of non-negative numbers. Print the resulting sequence. For example, if the sequence is  1, 5, -3, 4,...

  • Python 3 Write a program that calculates the factorial of the greatest odd number in a...

    Python 3 Write a program that calculates the factorial of the greatest odd number in a sequence of positive numbers (separated by spaces). The program must receive sequences of integers, one per line. The end of the input cases will be indicated by the number -1. For each line the program must print the factorial of the greatest odd number in each sequence. Note that for this question, there is always a greatest positive odd number in each line. Sample...

  • Help needed related python task ! Thanx again How you're doing it • Write a function...

    Help needed related python task ! Thanx again How you're doing it • Write a function write_to_file() that accepts a tuple to be added to the end of a file o Open the file for appending (name your file 'student_info.txt') o Write the tuple on one line (include any newline characters necessary) o Close the file • Write a function get_student_info() that o Accepts an argument for a student name o Prompts the user to input as many test scores...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • You will create a dimple Bubble Sort program to sort a string of random integers or...

    You will create a dimple Bubble Sort program to sort a string of random integers or text. Please read instructions and examples Please show screenshot of proof that the code works in the C program ECE 216 Programming Project 2 The Bubble Sort You will create a simple Bubble Sort program to sort a string of random integers or text or whatever you can punch in from the keyboard. For the input data, you will input a string of single...

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