Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 07-12-2008, 04:28 PM   #1 (permalink)
Apprentice
 
Join Date: Nov 2008
Posts: 57
Question Urgent : Can any1 solve my problem?


Hi All,
This is the content of the file say 'input.txt'

Code:
apple   (
   ball  (
cat (
doctor(),
elephant()
),
cup (
dead()
)
));
As you can see its in a very haphazard manner, which is a big problem, but if you indent it properly it looks something like this

Code:
apple(
    ball(
        cat (
            doctor(),
            elephant()
        ),
        cup (
            dead()
        )
    )
);
Now what i reqire is the tree path for the last element example say for 'doctor' the tree-path is

Code:
apple.ball.cat.doctor()
and that of 'elephant' is

Code:
apple.ball.cat.elephant()
and that of 'dead' is
Code:
apple.ball.cup.dead()
';' indicates end of the path

can any automate this using any programming lang its very urgent
Thanks
srbharadwaj is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 08-12-2008, 01:30 AM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Urgent : Can any1 solve my problem?

Do all the files have only one name(...); structure?

I've not spent much time covering up all extra conditions but for the example you have given, which has only one of those structures, this following code in Python prints it properly to an outfile.

Code:
Code:
import sys
ofile = open(sys.argv[1]+"out", 'w')
def search(tree, path='', pathlen=0):
    for each in tree:
        path += each+"."
        if tree[each]:
            search(tree[each], path)
        else:
            ofile.write(path[:-1]+'()'+'\n')
        path = path[:path[:-1].rfind('.')+1]

ip = open(sys.argv[1]).read().replace(' ','').replace('\n','')
stack = []
level = 0
result = {}
x = ip[:ip.find('(')]
ip = ip[ip.find('(')+1:-2]
result[x] = {}
ref = result[x]

for index, each in enumerate(ip):
    if each == '(':
        element = "".join(stack)
        stack = []
        ref[element] = {}
        if ip[index+1] != ')':  
            skip = True      
            save = ref
            ref = ref[element]
        continue
    if each == ')':
        if not skip:
            ref = save
        skip = False
        continue
    if each == ',':
        continue
    stack.append(each)

search(result)
Output:
Code:
harsh@home-desktop:~$ python3.0 a.py file.txt 
harsh@home-desktop:~$ cat file.txtout 
apple.ball.cup.dead()
apple.ball.cat.elephant()
apple.ball.cat.doctor()
Code is partially hackish, but I'll fix that when I get free again. But for single structures its bound to work perfectly.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 08-12-2008, 12:49 PM   #3 (permalink)
Apprentice
 
Join Date: Nov 2008
Posts: 57
Default Re: Urgent : Can any1 solve my problem?

yes every file will have just one name(); structure
thank a lot harsh....

Harsh, can u pls brief me abt the logic used to find out the last element?

Last edited by srbharadwaj; 08-12-2008 at 06:19 PM. Reason: Automerged Doublepost
srbharadwaj is offline  
Old 08-12-2008, 09:00 PM   #4 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Urgent : Can any1 solve my problem?

I just checked if the next element after '(', in the space-and-newline stripped file, is a ')'. If that became true, I did not add a new node to the tree and preserved the pointer (references actually, in Python).

Simply speaking, my program sees your file like this "apple(ball(cat(doctor(),elephant()),cup(dead())));" and I looked for '()'. Hackish, ugh. But did your job so its cool. I'm lazy.

By the way, why not use XML to store such data? Life could be so much easier following standards!
__________________
Harsh J
www.harshj.com

Last edited by QwertyManiac; 09-12-2008 at 12:27 AM. Reason: Automerged Doublepost
QwertyManiac is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pls..Solve my problem!!! Zangetsu Hardware Q&A 17 07-07-2008 03:21 PM
urgent help required.please solve this program. quan chi Programming 5 30-12-2007 11:45 AM
Frustating problem help needed.....any1 there..... fun_theorem Software Q&A 2 28-07-2007 05:14 PM
help to solve my DVD problem reddragon Hardware Q&A 3 20-11-2006 10:53 AM
Urgent Help Needed 2 Solve Win Xp Problem SLIPKNOT Software Q&A 7 17-11-2006 12:22 PM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:02 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2