1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
1
Rep Power
0

#### Pyhton troubles..

Write an algorithm for each of the following python operations and test you algorithm by writing a it up in a suitable function. You are not allowed to use the corresponding Python method to implement your function. a. count(myList, x) (like myList.count(x)) b. isin(myList, x) (like x in myList)) c. index(myList, x) (like myList.index(x)) d. reverse(myList) (like myList.reverse()) e. sort(myList) (like myList.sort())
2. Here's an example.
Code:
```def count(L,o):

'''
A doctest providing supporting evidence that
the function might be correct.
>>> a='forty-five'
>>> a.count('f') == count(a,'f')
True
'''

# longer, but still probably shorter than your version, that is, if you bothered to write a solution
occurrences = 0
for obj in L:
occurrences += obj == o
return occurrences

# short form
return sum(obj == o for obj in L)```
3. One possibility:
Code:
```def list_reverse(mylist):
return mylist[::-1]

mylist = list('abcde')
print(list_reverse(mylist))```
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
13
Rep Power
0
Lucky for me I wrote a sorting function today, because I wanted to sort 2 lists with corresponding indexes, Here it is:

Code:
```temp = []
while(len(fitness) >=1):
temp.append(fitness[fitness.index(max(fitness))])
del fitness[fitness.index(max(fitness))]```
it sorts highest to lowest.