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...
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 function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT