Created
February 12, 2021 22:31
-
-
Save selimhamidou/a3e480c6a779c8e0816eabfbfe4572d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sum of unique elements | |
# from collections import Counter | |
# def getCount(myArray): | |
# count=0 | |
# unique_elements_array=[] | |
# myCounter=Counter(myArray) | |
# for i in myCounter: | |
# if myCounter[i]==1: | |
# unique_elements_array.append(i) | |
# count+=1 | |
# return count | |
# myArray=[1,2,3, 4,5] | |
# print(getCount(myArray)) | |
# Invalid tweets | |
# myArray=[SELECT tweet_id, content FROM Tweets] | |
# for i in myArray: | |
# if(len(i.content))>15: | |
# print("Tweet "+i.tweet_id+" has length = " + len(i.content)+".It is an invalid tweet") | |
# else: | |
# print("Tweet "+i.tweet_id+" has length = " + len(i.content)+".It is a valid tweet") | |
# Count Elements | |
# def countElements(myArray): | |
# count=0 | |
# for i in myArray: | |
# for j in myArray: | |
# if abs(i-j)==1: | |
# count+=1 | |
# return int(count/2) | |
# myArray=[1,2,3,4] | |
# print(countElements(myArray)) | |
# isBoomrang | |
# def isBoomrang(myArray): | |
# delta1=abs(myArray[1][1]-myArray[0][1])/(myArray[1][0]-myArray[0][0]) | |
# delta2=abs(myArray[2][1]-myArray[0][1])/(myArray[2][0]-myArray[0][0]) | |
# if delta1==delta2: | |
# return False | |
# else: | |
# return True | |
# myArray=[[1,1],[2,3], [3,3]] | |
# print(isBoomrang(myArray)) | |
# Check if Array Is Sorted and Rotated | |
def isInOrder(myArray): | |
for i in range(len(myArray)-1): | |
if myArray[i]+1 == myArray[i+1]: | |
continue | |
else: | |
return False | |
return True | |
def getChangesNumber(myArray): | |
count = 0 | |
for i in range(len(myArray)-1): | |
if myArray[i+1] == myArray[i]+1: | |
continue | |
else: | |
count += 1 | |
return count | |
# print(getChangesNumber([1,3,2])) | |
def isSortedAndRotated(myArray): | |
if isInOrder(myArray) == True: | |
return True | |
else: | |
if getChangesNumber(myArray) == 1: | |
return True | |
else: | |
return False | |
array_to_test = [1, 2, 4, 3, 5] | |
print(isSortedAndRotated(array_to_test)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment