The 7 Continents of the World

This started out as a series of Advent challenges a couple of Christmases ago, but is now open for anyone to post challenges on any topic!
DDRM

Re: The 7 Continents of the World

Post by DDRM »

Hi Richard,

This is probably straying a bit far from the theme of this thread, but yes, I like Python, though in the current cases the choice is driven by necessity: the coursework MUST be written in Python, for marking reasons.

I agree the use of white space to define block structure is unusual and occasionally annoying, but with modern IDEs most of that is taken care of for you. One of the things I like most about it is the flexible lists, and the slicing functions that go with them. When used to construct multi-dimensional "arrays", it allows some incredibly sophisticated and concise ways of doing things - almost like database functions. The use of iterators (for example, to do everything using each member of a list/dictionary/set etc) is very powerful, and once you get used to it, intuitive. It also tends to be very fast compared to conventional looping - presumably because it is implemented behind the scenes in C (similar to your BBC BASIC array operations?). One advantage is that lists can contain mixed-type elements, so they integrate many of the advantages of your structures, but in a simpler way (albeit the programmer is responsible for knowing what is where!).

One are where I think BBC BASIC has a huge advantage is in its built-in graphical abilities - just doing a "quick and dirty" look at something, or building anything like a GUI is easier in BB4W. Having said that, Python has libraries that make some things very easy: add "import matplotlib.pyplot as plt" at the beginning of your code, and plotting a graph is as easy as adding "plt.plot(data)", or "plt.scatter(x_series,yseries)"!

As for cross-platform compatibility, most things (other than native-looking GUIs) will run "out of the box" as long as a standard python implementation is installed (like anaconda3) - this will automatically include the common libraries, and it is easy to set up virtual environments to include more specialised ones - the provision of a "requirements.txt" file allows this to be set up essentially automatically. Making a stand-alone application is harder, and much bigger, than the equivalent in BB4W, though - more like making one for Android, in that a separate application is required to do it. I would consider that a big plus for BBC BASIC (for me, esp. BB4W, though that probably reflects my lack of experience with BBC-SDL).

You are right that Python, as an interpreted language, is relatively slow, if you do everything yourself in Python- but (like BBC BASIC libraries with assembler) using libraries often means things are much faster, since they provide a wrapper onto underlying, and very fast, C code). Things like handling big data structures with PANDAS or numpy, and plotting with matplotlib, are really VERY fast - it will handle complex data structures with thousands or millions of lines of data, and do operations on all of them, apparently "instantaneously" - though obviously if you nest things deep enough it all slows down!

Having said all that, if I want a first look at a problem, or to make a game, BBC BASIC is almost invariably my first choice!

:-)

D

PS I started working on (BBC) code for this problem - I'll post it when it's done. Would you be interested if I posted a Python equivalent, for comparison, or is that stretching the point of the forum too much?
DDRM

Re: The 7 Continents of the World

Post by DDRM »

Here's my solution.

As Richard points out, it's not soluble with the full network shown: to see why, think about any nodes with an odd number of edges. If we start there, we can go and come back using an even number, and will have to leave again on the last one. Thus it will be impossible to start and finish at such a node. In general, there will be a solution to this kind of problem if either (a) all nodes have an even order, or (b) there are exactly two nodes of odd order, and these are the start and end points (so here, it should be possible to start in Europe and end in Asia (can you modify my code to do this?)).

D

Code: Select all

      REM First we reserve an array of structures, to represent the routes...
      DIM routes{(7) end1$,end2$,used%}
      REM ... and fill it with the route data (see below)
      FOR r = 0 TO 7
        READ routes{(r)}.end1$
        READ routes{(r)}.end2$
        routes{(r)}.used% = 0     : REM Initially all routes are unused
      NEXT r

      REM OK, now we just need to call our recursive routine with our current position,
      REM and hope it returns a path traversing the network of routes!
      outcome$ = FNFindRoute("Europe")
      PRINT outcome$

      END

      REM Recursive routine to test out the next step on the journey
      DEF FNFindRoute(start$)
      LOCAL x
      REM Try all possible next routes: need to go to the right place and not have been used before
      FOR x = 0 TO 7
        IF (routes{(x)}.used% = 0) AND (routes{(x)}.end1$ = start$ OR routes{(x)}.end2$ = start$) THEN
          REM This is a valid next path! Work out where it goes next
          IF routes{(x)}.end1$ = start$ THEN
            next$ = routes{(x)}.end2$
          ELSE
            next$ = routes{(x)}.end1$
          ENDIF
          REM Mark this route as used
          routes{(x)}.used% = 1
          REM If we've got back to Europe,and used all the routes, we have a complete solution!
          IF FNAllUsed AND next$ = "Europe" THEN = start$ + " to Europe"  :Cascade back down, passing this path
          REM Otherwise, we call the routine again, to find a next step
          res$ = FNFindRoute(next$)
          REM If that found a route (didn't return "Failed!"), we now pass back the solution, including this stage
          IF res$ <> "Failed!" THEN = start$ + " to "+ res$
          REM Otherwise we failed to find a route passing through this route: reset it as unused, and look for another
          routes{(x)}.used% = 0
        ENDIF
      NEXT x
      REM We've tried all the routes: this branch was a failure!
      ="Failed!"

      DEF FNAllUsed
      REM Test whether all the routes are marked as having been used
      LOCAL x
      FOR x = 0 TO 7
        IF routes{(x)}.used% = 0 THEN = 0
      NEXT x
      =1


      DATA "Europe","Africa"
      DATA "Europe", "NAmerica"
      DATA "Asia","Oceania"
      DATA "Africa","Asia"
      DATA "Africa","Antarctica"
      DATA "Africa","SAmerica"
      DATA "Antarctica","Oceania"
      DATA "NAmerica","SAmerica"
      REM We need to exclude this one to make it soluble!
      REM DATA "Europe","Asia"
DDRM

Re: The 7 Continents of the World

Post by DDRM »

...and yes, I know I have shamelessly used global variables! It avoids the need to make copies at every level of the recursion if elements are passed by reference, which I suspect arrays of structures are?

:-)

D
Hated Moron

BBC BASIC vs Python (was The 7 Continents of the World)

Post by Hated Moron »

DDRM wrote: Wed 29 Nov 2023, 08:56 This is probably straying a bit far from the theme of this thread
I'm happy to fork it to a different thread if you want. I've edited the subject line at least.
with modern IDEs most of that is taken care of for you.
Of course, but with most languages use of an IDE is optional. For example when coding in C I don't use Visual Studio or Xcode or any other IDE, I use a regular text editor. And when it comes to posting code in a forum or support group many strip off leading whitespace as a part of their routine formatting. Even here people often don't bother with the [code] tags when pasting BBC BASIC code.
One of the things I like most about it is the flexible lists, and the slicing functions that go with them.
Yes, that's what I meant by 'collections'.
One advantage is that lists can contain mixed-type elements, so they integrate many of the advantages of your structures
Structures are structures, they don't pretend to be lists. Presumably Python has structures too (every language I know does, pretty much).

How far would a 'listlib.bbc' library for BBC BASIC go towards meeting that particular objection? I appreciate that a library isn't as elegant (or fast) as a native capability, but under the hood BBC BASIC does support objects that can be either numeric or string - it's what a User-Defined Function returns internally - so it may be that a library can achieve more than you might think! I'd welcome a possible collaboration.
As for cross-platform compatibility, most things (other than native-looking GUIs) will run "out of the box"
Including running in a browser? Last time I checked support for running Python programs in a browser was very limited and many of the common libraries that you referred to as being so important weren't available on that platform. Has that changed?

In BBCSDL you can run a program in your local browser with three clicks: Compile... Deploy as a web application... Test. Is that possible in the Python you use? ;)
Having said all that, if I want a first look at a problem, or to make a game, BBC BASIC is almost invariably my first choice!
I want BBC BASIC to be seen as much more than just a language for games.
Would you be interested if I posted a Python equivalent, for comparison, or is that stretching the point of the forum too much?
Only if you think it illustrates some specific feature of Python which you miss in BBC BASIC.
Hated Moron

Re: The 7 Continents of the World

Post by Hated Moron »

DDRM wrote: Wed 29 Nov 2023, 10:27 ...and yes, I know I have shamelessly used global variables!
Could you not have used PRIVATE variables? They share many of the properties of globals (indeed they are implemented internally as 'hidden globals') without the 'shame' that accompanies them.