[SOLUTION] Ruby Quiz #53

Discussion in 'Ruby' started by Daniel Sheppard, Nov 2, 2005.

  1. My solution only acts on one 'line' at a time, by calling enumerating
    over the content. However, if you pass in an array with multiple lines
    in each index, it can use that too. Matches are based on the punctuation
    structure and word count of the lines. This works really well for the
    example given, but may not in other situations.

    http://members.iinet.net.au/~soxbox/ruby_quiz_53/dryer.rb
    http://members.iinet.net.au/~soxbox/ruby_quiz_53/array_sync_enum.rb

    The lambda names in the output all suck completely which always makes
    the output longer than the input, but I don't have the time to make them
    prettier. It would be easy enough to do a find/replace on the lambda
    names to make something more useful. I've tried as much as possible to
    ensure that the generated output will match the generated input exactly.

    It runs in O(n!) time in regards to line length, so putting an essay
    through this would take FOREVER. I would have been better off not using
    the "generate every regex that matches this line" approach - but I
    neglected to throw anything with a decent line-length at it until late
    in the piece, and then I couldn't be bothered changing it. Just getting
    a list of tokens for each line and then matching up the lines with the
    most matching tokens would have sufficed. Doofus.

    Here's the output generated for the quiz sample input.

    %CREATE_TABLE_ =3D lambda do |arg0|
    % "CREATE TABLE `"+arg0+"` ("
    %end
    %_id_int_11_NOT_NULL_auto_increment_ =3D lambda do ||
    % "`id` int(11) NOT NULL auto_increment,"
    %end
    %_varchar_NOT_NULL_default_ =3D lambda do |arg0,arg1|
    % "`"+arg0+"` varchar("+arg1+") NOT NULL default '',"
    %end
    %_description_text_NOT_NULL_ =3D lambda do ||
    % "`description` text NOT NULL,"
    %end
    %PRIMARY_KEY_id_ =3D lambda do ||
    % "PRIMARY KEY (`id`)"
    %end
    %_TYPE_MyISAM_AUTO_INCREMENT_ =3D lambda do |arg0|
    % ") TYPE=3DMyISAM AUTO_INCREMENT=3D"+arg0+" ;"
    %end
    %_int_11_NOT_NULL_default_0_ =3D lambda do |arg0|
    % "`"+arg0+"` int(11) NOT NULL default '0',"
    %end
    =09<%=3DCREATE_TABLE_["authors"]%>
    =09 <%=3D_id_int_11_NOT_NULL_auto_increment_[]%>
    =09 <%=3D_varchar_NOT_NULL_default_["firstname","50"]%>
    =09 <%=3D_varchar_NOT_NULL_default_["name","50"]%>
    =09 <%=3D_varchar_NOT_NULL_default_["nickname","50"]%>
    =09 <%=3D_varchar_NOT_NULL_default_["contact","50"]%>
    =09 <%=3D_varchar_NOT_NULL_default_["password","50"]%>
    =09 <%=3D_description_text_NOT_NULL_[]%>
    =09 <%=3DPRIMARY_KEY_id_[]%>
    =09<%=3D_TYPE_MyISAM_AUTO_INCREMENT_["3"]%>
    =20=20
    =09<%=3DCREATE_TABLE_["categories"]%>
    =09 <%=3D_id_int_11_NOT_NULL_auto_increment_[]%>
    =09 <%=3D_varchar_NOT_NULL_default_["name","20"]%>
    =09 <%=3D_varchar_NOT_NULL_default_["description","70"]%>
    =09 <%=3DPRIMARY_KEY_id_[]%>
    =09<%=3D_TYPE_MyISAM_AUTO_INCREMENT_["3"]%>
    =20=20
    =09<%=3DCREATE_TABLE_["categories_documents"]%>
    =09 <%=3D_int_11_NOT_NULL_default_0_["category_id"]%>
    =09 <%=3D_int_11_NOT_NULL_default_0_["document_id"]%>
    =09) TYPE=3DMyISAM ;
    =20=20
    =09<%=3DCREATE_TABLE_["documents"]%>
    =09 <%=3D_id_int_11_NOT_NULL_auto_increment_[]%>
    =09 <%=3D_varchar_NOT_NULL_default_["title","50"]%>
    =09 <%=3D_description_text_NOT_NULL_[]%>
    =09 <%=3D_int_11_NOT_NULL_default_0_["author_id"]%>
    =09 `date` date NOT NULL default '0000-00-00',
    =09 <%=3D_varchar_NOT_NULL_default_["filename","50"]%>
    =09 PRIMARY KEY (`id`),
    =09 KEY `document` (`title`)
    =09<%=3D_TYPE_MyISAM_AUTO_INCREMENT_["14"]%>
    #########################################################################=
    ############
    This email has been scanned by MailMarshal, an email content filter.
    #########################################################################=
    ############
    Daniel Sheppard, Nov 2, 2005
    #1
    1. Advertising

  2. Daniel Sheppard

    Hugh Sasse Guest

    On Wed, 2 Nov 2005, Daniel Sheppard wrote:

    > My solution only acts on one 'line' at a time, by calling enumerating
    > over the content. However, if you pass in an array with multiple lines
    > in each index, it can use that too. Matches are based on the punctuation


    I don't quite understand that -- it seems to
    reject {|x| String === x}
    knock the strings out of arrays.

    > structure and word count of the lines. This works really well for the
    > example given, but may not in other situations.
    >
    > http://members.iinet.net.au/~soxbox/ruby_quiz_53/dryer.rb
    > http://members.iinet.net.au/~soxbox/ruby_quiz_53/array_sync_enum.rb
    >
    > The lambda names in the output all suck completely which always makes
    > the output longer than the input, but I don't have the time to make them
    > prettier. It would be easy enough to do a find/replace on the lambda
    > names to make something more useful. I've tried as much as possible to
    > ensure that the generated output will match the generated input exactly.


    I've not really got into that bit yet, but to use a block for that
    is a novel suggestion.
    >
    > It runs in O(n!) time in regards to line length, so putting an essay
    > through this would take FOREVER. I would have been better off not using


    :) Maybe some duplicate checking up front might help.

    > the "generate every regex that matches this line" approach - but I
    > neglected to throw anything with a decent line-length at it until late
    > in the piece, and then I couldn't be bothered changing it. Just getting
    > a list of tokens for each line and then matching up the lines with the
    > most matching tokens would have sufficed. Doofus.
    >
    > Here's the output generated for the quiz sample input.
    >
    > %CREATE_TABLE_ = lambda do |arg0|
    > % "CREATE TABLE `"+arg0+"` ("
    > %end
    > %_id_int_11_NOT_NULL_auto_increment_ = lambda do ||
    > % "`id` int(11) NOT NULL auto_increment,"
    > %end

    [...]
    > <%=CREATE_TABLE_["authors"]%>
    > <%=_id_int_11_NOT_NULL_auto_increment_[]%>
    > <%=_varchar_NOT_NULL_default_["firstname","50"]%>

    [...]
    > <%=_description_text_NOT_NULL_[]%>
    > <%=PRIMARY_KEY_id_[]%>
    > <%=_TYPE_MyISAM_AUTO_INCREMENT_["3"]%>

    [...]


    I think this is the first one using erb.

    Thank you,
    Hugh
    Hugh Sasse, Nov 2, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. email55555 email55555

    [SOLUTION] Ruby Quiz #14 LCD Numbers

    email55555 email55555, Jan 9, 2005, in forum: Ruby
    Replies:
    8
    Views:
    179
    Dave Burt
    Jan 10, 2005
  2. email55555 email55555

    [SOLUTION] Ruby Quiz #14 LCD Numbers ( solution #2 )

    email55555 email55555, Jan 9, 2005, in forum: Ruby
    Replies:
    16
    Views:
    262
    David Tran
    Jan 10, 2005
  3. David Tran
    Replies:
    9
    Views:
    166
    David Tran
    Jan 21, 2005
  4. MenTaLguY
    Replies:
    12
    Views:
    270
    MenTaLguY
    Jun 7, 2007
  5. Daniel Moore
    Replies:
    10
    Views:
    284
    James Gray
    Jan 31, 2009
Loading...

Share This Page