Skip to content

Instantly share code, notes, and snippets.

@bmontana
Created March 20, 2018 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmontana/e1c593ef59c60a5d1d9fe58d76efaee6 to your computer and use it in GitHub Desktop.
Save bmontana/e1c593ef59c60a5d1d9fe58d76efaee6 to your computer and use it in GitHub Desktop.
Algorithms and code for these Python List operations: myList.count(), x in myList, myList.index(), myList.reverse(), myList.sort()
# U11_Ex05_ListOperations.py
#
# Author: Bill Montana
# Course: Coding for OOP
# Section: A3
# Date: 20 Mar 2018
# IDE: PyCharm Community Edition
#
# Assignment Info
# Exercise: 5
# Source: Python Programming
# Chapter: 11
#
# Program Description
# Algorithms and code for these Python List operations:
# myList.count(), x in myList, myList.index(), myList.reverse(), myList.sort()
#
# Algorithm (pseudocode)
# count(myList, x) (like myList.count(x)):
# set accumulator to zero
# iterate over myList
# if an element is the same as x, increment an accumulator
# return the accumulator
#
# isin(myList, x) (like x in myList):
# set return value to false
# iterate over myList
# if an element of myList is found that is the same as x
# set return value to true
# exit loop
# return return value
#
# index(myList, x) (like myList.index(x)):
# set return value to -1
# iterate over myList
# if an element of myList is found that is the same as x
# set return value to its index
# exit loop
# return return value
#
# reverse(myList) (like myList.reverse()):
# make an empty accumulator list
# iterate over myList backwards (end to beginning)
# append to accumulator list current element of myList
# return accumulator list
# -OR-
# iterate over half of myList
# swap element i with element len-i
# return myList
#
# sort(myList) (like myList.sort()):
# make empty sortedList
# while length of myList > 1
# set smallest to index of first element of myList
# iterate over rest of myList
# if an element is found smaller than smallest so far
# set smallest to index of current element
# append to sortedList smallest element from myList
# delete smallest element from myList
# return sortedList
def count(l, x):
"""
Counts number of times x occurs in l
:param l: list -> list to search
:param x: any -> item to count in l
:return: int -> number of times x occurs in l
"""
occurs = 0
for elem in l:
if elem == x:
occurs += 1
return occurs
def isin(l, x):
"""
Checks to see if x is in l
:param l: list -> list to search
:param x: any -> item to find in l
:return: boolean -> true if x is found, false otherwise
"""
found = False
for elem in l:
if elem == x:
found = True
break
return found
def index(l, x):
"""
Finds index of x if it is in l
:param l: list -> list to search
:param x: any -> item to find in l
:return: int -> index of first element equal to x, -1 if x not found
"""
idx = -1
for i in range(len(l)):
if l[i] == x:
idx = i
break
return idx
def reverse(l):
"""
Reverses a list
:param l: list -> list to reverse
:return: list -> reversed list
"""
endIdx = len(l) // 2
lenL = len(l) - 1
for i in range(endIdx):
l[i], l[lenL - i] = l[lenL - i], l[i]
return l
def sort(l):
"""
Sorts l in ascending order
:param l: list -> list to sort
:return: list -> sorted list
"""
sList = []
while len(l) > 0:
smallest = 0
for i in range(1, len(l)):
if l[i] < l[smallest]:
smallest = i
sList.append(l[smallest])
del l[smallest]
return sList
if __name__ == '__main__':
reverse([0,1,2,3,4,5,6,7,8,9])
reverse([3,2,1])
reverse([7,7,3,4])
@bmontana
Copy link
Author

RESULTS:
========
count([], 2)                  -->   0 |   0 | [ Pass ]
count([1,2,3], 2)             -->   1 |   1 | [ Pass ]
count([1,1,3,4], 2)           -->   0 |   0 | [ Pass ]
count([1,2,3,2,4,2,5,2], 2)   -->   4 |   4 | [ Pass ]
========

RESULTS:
========
isin([], 2)                  -->   0 |       0 | [ Pass ]
isin([1,2,3], 2)             -->   1 |       1 | [ Pass ]
isin([1,1,3,4], 2)           -->   0 |       0 | [ Pass ]
isin([1,2,3,2,4,2,5,2], 2)   -->   1 |       1 | [ Pass ]
========

RESULTS:
========
index([], 2)                  -->   -1 |   -1 | [ Pass ]
index([1,2,3], 2)             -->    1 |    1 | [ Pass ]
index([1,1,3,4], 2)           -->   -1 |   -1 | [ Pass ]
index([1,2,3,2,4,2,5,2], 2)   -->    1 |    1 | [ Pass ]
index([1,3,3,5,4,7,5,2], 2)   -->    7 |    7 | [ Pass ]
========

RESULTS:
========
reverse([0,1,2,3,4,5,6,7,8,9])   -->   [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] |   [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] | [ Pass ]
reverse([3,2,1])                 -->                        [1, 2, 3] |                        [1, 2, 3] | [ Pass ]
reverse([7,7,3,4])               -->                     [4, 3, 7, 7] |                     [4, 3, 7, 7] | [ Pass ]
========

RESULTS:
========
sort([44,88,22,33,11,99,77,66,55])   -->   [11, 22, 33, 44, 55, 66, 77, 88, 99] |   [11, 22, 33, 44, 55, 66, 77, 88, 99] | [ Pass ]
========

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment