Question

In: Computer Science

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 1. a csv file that contained rows of numbers to be tested.

file_name = sys.argv[1]

file = open(file_name)

# function that will check the csv file and see if the rows of numbers are sorted in an increasing pattern.

def is_sorted(arr):

_result = []

for i in range(len(arr) - 1):

if current_arr[i] > current_arr[i+1]:

return False

return True


# Read the file line by line

for line in file.readlines():

# For each line read,

# We create a new array to:

# Store the number we convert from strings,

current_arr = []

# Split the current line by commas,

for number in line.split(','):

# then convert each string in an array to an integer and

# then add it to the current array of numbers

current_arr += [int(number)]

when I order numbers in my csv file not sorted, I still get True returned instead of False

found_index = is_sorted(current_arr)

print(found_index)

Solutions

Expert Solution

I tried with the given code. It gives me the expected results.Make sure your indentation is proper. Attaching the updated properly indented code below:

CSV Data:

1,0,5,6
10,13,14,15
2,4,5,6
9,1,2,2

Python Code:

import sys

# command line arguement 1. a csv file that contained rows of numbers to be tested.
#file_name = sys.argv[1]
file = open('sample.csv')

# function that will check the csv file and see if the rows of numbers are sorted in an increasing pattern.
def is_sorted(arr):
  for i in range(len(arr) - 1):
    if current_arr[i] > current_arr[i+1]:
      return False
  return True

# Read the file line by line
for line in file.readlines():
  # For each line read,
  # We create a new array to:
  # Store the number we convert from strings,
  current_arr = []
  # Split the current line by commas,
  for number in line.split(','):
    # then convert each string in an array to an integer and
    # then add it to the current array of numbers
    current_arr += [int(number)]
  found_index = is_sorted(current_arr)
  print(found_index)

Output:

False

True

True

False

Please take a look and let me know if you have any other specific format/number you are trying in csv which is not working. I will debug and check any other changes to be made.


Related Solutions

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...
Python question Write a function int(lofi, alofi) that consumes two sorted lists of distinct integers lofi...
Python question Write a function int(lofi, alofi) that consumes two sorted lists of distinct integers lofi and alofi, and returns a sorted list that contains only elements common to both lists. You must obey the following restrictions: No recursion or abstract list functions, intersect must run in O(n) where n is the combined length of the two parameters. sort function is not allowed as well as list comprehensions math is the only library that can be imported Example: int([4, 13,...
Write a program to compute intersection of two sorted array of integers and compute the CPU...
Write a program to compute intersection of two sorted array of integers and compute the CPU time for different sets of unsigned integers generated by a random number generator. Test this using the same data sets: atleast 3 of size 1000 integers, atleast 3 of size 10000 integers, atleast 3 of size 100000 integers, atleast 3 of one million integers and atleast 3 of size 10 million integers DONT FORGET CPU TIME FOR EACH ONE NO HASH SET
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
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....
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....
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT