Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 20th, 2012, 08:02 PM
 darnold4014
Registered User

Join Date: Nov 2012
Posts: 1
Time spent in forums: 3 m 43 sec
Reputation 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
November 20th, 2012, 08:58 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,140
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 17 m 40 sec
Reputation Power: 455
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)```
__________________
[code]Code tags[/code] are essential for python code!

#3
November 21st, 2012, 04:40 PM
 Dietrich
Contributing User

Join Date: Feb 2005
Posts: 574
Time spent in forums: 5 Days 4 h 6 m 36 sec
Reputation Power: 63
One possibility:
Code:
```def list_reverse(mylist):
return mylist[::-1]

mylist = list('abcde')
print(list_reverse(mylist))
```
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

#4
November 22nd, 2012, 01:22 AM
 hovestar
Registered User

Join Date: Oct 2012
Posts: 13
Time spent in forums: 3 h 44 m 21 sec
Reputation 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.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Pyhton troubles..