Help with creating a BASIC simulator

This is the place for queries that don't fit in any of the other categories.

Help with creating a BASIC simulator

Postby dboxall123 » Fri Jul 12, 2013 5:49 pm

Hi everyone!
I'm new here, and also new to programming. I'm hoping that someone here will help me with this problem. I'm doing an online Python course at http://cscircles.cemc.uwaterloo.ca/15a-basic/, and I'm really, really stuck.It has asked me to create a Python simulator of a BASIC program, and has split it into 3 parts for me. This is the part that I'm stuck on:


Define a function findLine(prog, target) to perform the following. Assume prog is a list of strings containing a BASIC program, like the type generated by getBASIC(); assume target is a string containing a line number, which is the target of a GOTO statement. The function should return the index i (a number between 0 and len(prog)-1) such that prog[i] is the line whose label equals target.
Sample input/output: If you call

findLine(['10 GOTO 20','20 END'], '10')

then the output should be 0, since item 0 of the list is the line with label 10.


It also gives a hint 'You will have to use split()'. Surely I will have to use more methods though? prog.split() doesn't work because prog is a list. And converting prog to a string and then using prog.split() just returns prog in the same way that it started. I'm very confused. I'm not asking anyone to solve the problem for me, because then I wouldn't learn anything. Just a nudge in the right direction would be nice, a hint or two.
I would really appreciate it if someone could help me,
Many thanks
Dan
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Help with creating a BASIC simulator

Postby ochichinyezaboombwa » Fri Jul 12, 2013 6:06 pm

The wording seemed a bit confusing to me too at first, but what they want is this:
a) iterate though all lines in prog (i.e. all items in this list);
b) split each line and see if its 1st element equals target;
c) if it does, return the corresponding number.
d) address the case when it's not found anywhere.

I recommend you to also look at enumerate(). The resulting code should be five lines long (or so).
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Help with creating a BASIC simulator

Postby dboxall123 » Fri Jul 12, 2013 6:14 pm

Hi, thanks for the reply. My problem is, I have absolutely no idea how to split a list, because str.spilt() doesn't work on list types.
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Help with creating a BASIC simulator

Postby ochichinyezaboombwa » Fri Jul 12, 2013 6:21 pm

I said:
ochichinyezaboombwa wrote:b) split each line ....

-not the list. You iterate though the list.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Help with creating a BASIC simulator

Postby dboxall123 » Fri Jul 12, 2013 6:42 pm

Ahh, now I'm on the right track! I haven't quite got there yet, but I'm actually getting somewhere now. Thanks for that, much appreciated, I will post the code when I've completed it.
Last edited by dboxall123 on Fri Jul 12, 2013 7:04 pm, edited 1 time in total.
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Help with creating a BASIC simulator

Postby dboxall123 » Fri Jul 12, 2013 6:58 pm

I'VE DONE IT!!!!!!!

Code: Select all
def findLine(prog,target):
   position=0
   for i in prog:
      x=i.split()
      if x[0]==target:
         return position
      position+=1


Thanks a lot for that, really, I would have never worked that out. Seriously, thanks mate :D I'm worried now that it's not really a very good code though, because you said it should around 5 lines and I have 8. How would you do it (if you don't me asking)?
Last edited by micseydel on Sat Jul 13, 2013 5:37 am, edited 1 time in total.
Reason: Added code tags. Note to original poster: in the future, use code tags yourself.
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Creating a BASIC simulator

Postby dboxall123 » Sat Jul 13, 2013 10:58 am

Hello everyone! I'm trying to create a BASIC simulator, and I am really stuck. I'm fairly new to programming, and would really appreciate some help. Here is my code so far:


Code: Select all
def findLine(prog,target):
   for i in range (0,len(prog)):
      x=prog[i].split()
      if x[0]==target:
         return i
     
def execute(prog):
  location = 0
  while True:
    if location==len(prog)-1: return "success"
   
    for i in prog:
       T=i.split()
       T=T[location]
    location = findLine(prog, T)

execute(['10 goto 20','20 goto 10']) 




This code returns success, but what I need it to do is enter into an infinite loop (I will eventually be writing out a part that returns 'infinite loop', but I don't think that there is any point in trying to write this part until I can actaully get it to enter an infinite loop). So I want it go into infinite loop if I run:

Code: Select all
execute(['10 got 20','20 goto 10']),  but if I run

execute(['10 goto 20','20 goto 30'.'30 END'])
, I need it to return 'success'. I know the function findLine works properly, I'm just having trouble with the execute part. Can anyone help me?

Thanks
Dan
Last edited by Yoriz on Sat Jul 13, 2013 11:08 am, edited 1 time in total.
Reason: Added code tags
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Help with creating a BASIC simulator

Postby stranac » Sat Jul 13, 2013 11:52 am

I would just keep track of lines you already visited.
If you encounter the same line more than once, you're in a loop.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1117
Joined: Thu Feb 07, 2013 3:42 pm

Re: Help with creating a BASIC simulator

Postby dboxall123 » Sat Jul 13, 2013 12:13 pm

Yes but the issue is that I can't get it to visit the same line twice, my code is not working properly. I already have an idea of how I'm going to detect an infinite loop, the problem is that in its current state my code won't go into an infinite loop, it just returns success every time. I would really appreciate a clue on how I can make it do this.
Thanks
Dan
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Help with creating a BASIC simulator

Postby stranac » Sat Jul 13, 2013 1:40 pm

Oh, I see.

Well, your execute() function is wrong.
You're looping through every line, and then finding itself(T[location] takes the first thing, which is the label of the current line).
That means the last iteration of the for loop will always leave location equal to the index of the last line.
That makes your function return 'success'.

To fix, you'll have to take the last thing in a line.
Also, I'm not sure your success condition is corect.
What if the code is something like:
Code: Select all
10 goto 40
20 goto 10
30 end
40 goto 20

That would make the code go into an infinite loop, but you would still return 'success'.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1117
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 4 guests