4 Replies - 513 Views - Last Post: 07 October 2013 - 08:57 PM Rate Topic: -----

#1 dexstar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 01-August 13

lexicographically smallest

Posted 04 October 2013 - 07:13 PM

Hey could someone please help me understand where to start when trying to solve this?

Quote

Given a list of indices and corresponding texts, find the index for the
lexicographically smallest phrase.


where phrases is the texts
[0,1] is the list of indices

import sys

S = lambda x : x

phrases = [ 
            [(S("1"),), (S("3"),)],
            [(S("2"),), (S("4"),)]
        ]



def findmin(ind, texts):
    minindex = 0


    return minindex

def main(args):

    print(findmin([0,1], phrases)) 

if __name__ == "__main__":
    main(sys.argv)


Thanks (:

Is This A Good Question/Topic? 0
  • +

Replies To: lexicographically smallest

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,753
  • Joined: 19-March 11

Re: lexicographically smallest

Posted 04 October 2013 - 07:19 PM

I think you might want to slow back a little there and use your thinking brain instead of your typing one.

First think: "lexicographically smallest" implies "sort" implies "can I sort these?" Can you sort a list? You can. (think list comprehension)

EDIT: If you want to do this in place, and so without a list comp, iterate the list and for each item ask whether it sorts previous to the lex_smallest so far. If so, it becomes the current smallest, and you move along. Return current smallest.

This post has been edited by jon.kiparsky: 04 October 2013 - 07:23 PM

Was This Post Helpful? 1
  • +
  • -

#3 dexstar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 01-August 13

Re: lexicographically smallest

Posted 07 October 2013 - 02:52 AM

The problem i'm having is that I dont understand how to access the element in this list.

E.g the code:

while i < len(texts):
        
        
            
        print(texts[i][ind[0]])
            
        i += 1



Outputs

('2',)
('4',)


But i'm expecting something like

('1',)
('2',)


Heres a better explaination of the function

Quote

Given a list of texts, find the text with the smallest phrase
(lexicographically). The phrase for a given text is indexed
using ind, e.g. texts[i][ind[i]] gives the current phrase in
text i.


Thanks for your reply
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,753
  • Joined: 19-March 11

Re: lexicographically smallest

Posted 07 October 2013 - 08:39 PM

I don't know what texts and ind are. Can't really help here.
Was This Post Helpful? 0
  • +
  • -

#5 dexstar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 01-August 13

Re: lexicographically smallest

Posted 07 October 2013 - 08:57 PM

View Postjon.kiparsky, on 07 October 2013 - 08:39 PM, said:

I don't know what texts and ind are. Can't really help here.

texts is :


 phrases = [ 
            [(S("1"),), (S("3"),)],
            [(S("2"),), (S("4"),)]
        ]


ind is:

[0,1]

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1