Question

In: Computer Science

1. Write a Python program that performs the following: 2. Defines an array of integers from...

1. Write a Python program that performs the following: 2. Defines an array of integers from 1 to 10. The numbers should be filled automatically without the need for user inputs 3. Find the sum of the numbers that are divisible by 3 (i.e., when a number is divided by 3, the remainder is zero) 4. Swap the positions of the maximum and minimum elements in the array. First, you need to find the maximum element as shown in the class, then find the minimum element, finally perform the swap operation(don't assume the first element is the smallest and the last element is the largest). 5. Create a main() function to call your defined functions and print your results.

Solutions

Expert Solution

Source Code:

Output:

Code in text format (See above images of code for indentation):

#function to fill array
def fill_array():
a=[]
#fill array with 1 to 10
for i in range(10):
a.append(i+1)
#return a
return a
#function to calculate sum
def sumOf3(a):
sum=0
for i in range(10):
#check for divisible by 3
if(a[i]%3==0):
#calculate sum
sum+=a[i]
#return sum
return sum
#function to swap maximum and minimum
def swap(a):
max=0
maxind=0
minind=0
min=11
#find max min and their indexes
for i in range(len(a)):
if(max<a[i]):
max=a[i]
maxind=i
if(min>a[i]):
min=a[i]
minind=i
#swapping
t=a[maxind]
a[maxind]=a[minind]
a[minind]=t
return a
#main function
def main():
#fuctioncalls and print results
a=fill_array()
print("The array is: ",a)
s=sumOf3(a)
print("The sum of the numbers that are divisible by 3 is: ",s)
a=swap(a)
print("The array after swapping: ",a)
#function call to main
main()
  
  


Related Solutions

Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
use python 1. Write a program that a. defines a list of countries that are members...
use python 1. Write a program that a. defines a list of countries that are members of BRICS (Brazil, Russia, India, China, Sri Lanka) b. Check whether a country is a member of BRICS or not Program run Enter the name of country: Pakistan Pakistan is not a member of BRICS Enter the name of country : India India is a member of BRICS 2. Write a program to create a list of numbers in the range of 1 to...
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
Python Question: Write a function that checks to see if an array of integers is sorted...
Python Question: Write a function that checks to see if an array of integers is sorted in an increasing fashion, returning true if it is, false otherwise. Test it with at least4 arrays - 2 sorted and 2 not sorted. Use a CSV formatted input file as described in the previous question to run your program through some tests, where again the filename is passed as an argument. Heres what I have so far: import sys # command line arguement...
For Project 2, write a program for MIPS that performs the following steps. It 1. Receives...
For Project 2, write a program for MIPS that performs the following steps. It 1. Receives a string of upper- and/or lowercase letters from keyboard (user types the letters on the SPIM console window). 2. Stores ASCII codes of the letters in stack. 3. Stop receiving letters when ? is typed by the user. 4. Converts uppercase letters to their lowercase letters and vice versa, and prints the converted string on the SPIM console window. Assume only valid inputs are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT