[Tutor] split on tab and new line

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sat Sep 4 01:30:56 CEST 2004



On Fri, 3 Sep 2004, kumar s wrote:

> A 12 13 [please read one space is one tab here]
> B 24 90
> C 34 45
>
> File 2:
>
> A Apple
> B Boy
>
> Now I want to check if A in file2 is existing in File1, if then, I want
> to print corresponding element of A in File2, Apple and corresponding
> value of A in File1.
>
> Desired output:
>
> Apple 12 13
> Boy 24 90


Hi Kumar,


I'd recommend breaking the problem into two independent subproblems.  It
looks like you're trying to juggle the whole problem, and that's hard.
Make things a little easier on yourself.  *grin*

By making and solving smaller subproblems, you can make progress without
having to do the whole thing all at once.



Subproblem1
---

One subproblem might be something to take a "label" ("A"), and translate
that into some value ("Apple").  In fact, we can hardcode something right
now:

###
def getLabelName(label):
    if label == 'A': return 'Apple'
    if label == 'B': return 'Boy'
    return 'I-Dont-Know-Yet'
###

Yes, it's a simple definition, but that's ok!  *grin* This does not yet
read from File2, but that's fine for a start.  Can you fix up
getLabelName() so that it does read its input from File2?

If it's not obvious how to do this yet, don't worry, because you can leave
it until later, and attack the other Subproblem2 instead.




Subproblem2
---

If you have some version of getLabelName(), you can concentrate on
transforming the contents of:

    A 12 13
    B 24 90
    C 34 45

into:

    Apple 12 13
    Boy 24 90
    Candy 34 45

If you use getLabelName(), your second subproblem can just focus on
looking at File1.  And this part of the problem will kinda work even if
Subproblem1 isn't perfectly working, since at the moment, getLabelName()
is hardcoded to know that 'A' means 'Apple' and 'B' means 'Boy'.  It just
means that you might see something like:

    Apple 12 13
    Boy 24 90
    I-Dont-Know-Yet 34 45

Does this make sense?  The subproblems here are designed so that you have
a choice to attack one or the other, without having to manage both at the
same time.


If you have more questions, please feel free to ask.  Good luck!




More information about the Tutor mailing list