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

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Pyhton troubles..


    I have been promted with this question and have been throughly stumped... Please help, Thanks!

    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. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    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 and Makefiles!
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    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
  6. #4
  7. 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.

IMN logo majestic logo threadwatch logo seochat tools logo