# Program efficiency?



## PeterNewton (Mar 20, 2009)

i wrote a program for mirroring and/or inversing algorithms. it only deals with regular face movements, so cube rotations and double-layer moves are not allowed, but i will incorporate them in soon. it should be simple enough.
i am a beginner at programming so it was written using Turing.
could someone show me how to make the program more efficient?
here is the code:

```
setscreen ("graphics:1000;10000")
var ans, alg1, alg2 : string
var c : int
put "Note: do not type spaces between moves"
put ""
put "|  Face Turn  |  Single Layer  |  Double Layer  |"
put "|  Front      |  F             |  f             |"
put "|  Back       |  B             |  b             |"
put "|  Up         |  U             |  u             |"
put "|  Down       |  D             |  d             |"
put "|  Right      |  R             |  r             |"
put "|  Left       |  L             |  l             |"
put "|  Middle     |  M             |"
put "|  Equator    |  E             |"
put "|  Side       |  S             |"
put ""
put "Note: M is based on L"
put "Note: E is based on D"
put "Note: S is based on F"
put ""
put "|  Cube Turn        |  Single Move  |"
put "|  Pivot on x-axis  |  x            |"
put "|  Pivot on y-axis  |  y            |"
put "|  Pivot on z-axis  |  z            |"
put ""
put "Note: x is based on R with pivot on RL"
put "Note: y is based on U with pivot on UD"
put "Note: z is based on F with pivot on FB"
put ""
put "90 degree = + (N/A)"
put "180 degree = + (2)"
put "Clockwise = + (N/A)"
put "Counter-clockwise = + (')"
put ""
put "Note: 180 turn notation is used before Counter-clockwise notation"
put ""
loop
    alg2 := ""
    put "mirror or inverse (M/I)?"
    get ans
    if ans = "M" or ans = "I" then
        put "please enter algorithm"
        get alg1
    end if
    if ans = "M" then
        c := 1
        loop
            if alg1 (c) = "F" or alg1 (c) = "B" or alg1 (c) = "U" or alg1 (c) = "D" then
                alg2 := alg2 + alg1 (c)
            elsif alg1 (c) = "R" then
                alg2 := alg2 + "L"
            elsif alg1 (c) = "L" then
                alg2 := alg2 + "R"
            elsif alg1 (c) = "f" or alg1 (c) = "b" or alg1 (c) = "u" or alg1 (c) = "d" then
                alg2 := alg2 + alg1 (c)
            elsif alg1 (c) = "M" or alg1 (c) = "E" or alg1 (c) = "S" then
                alg2 := alg2 + alg1 (c)
            elsif alg1 (c) = "x" or alg1 (c) = "y" or alg1 (c) = "z" then
                alg2 := alg2 + alg1 (c)
            end if
            if c = length (alg1) then
                if alg1 (c) not= "'" and alg1 (c) not= "2" then
                    alg2 := alg2 + "'"
                end if
            else
                if alg1 (c + 1) = "'" then
                    c := c + 1
                elsif alg1 (c + 1) = "2" then
                    alg2 := alg2 + "2"
                    if c < length (alg1) - 1 then
                        if alg1 (c + 2) not= "'" then
                            alg2 := alg2 + "'"
                        elsif alg1 (c + 2) = "'" then
                            c := c + 1
                        end if
                    elsif c < length (alg1) then
                        alg2 := alg2 + "'"
                    end if
                    c := c + 1
                elsif alg1 (c + 1) not= "'" and alg1 (c + 1) not= "2" then
                    alg2 := alg2 + "'"
                end if
            end if
            exit when c = length (alg1)
            c := c + 1
        end loop
        put "mirror: ", alg2
        put ""
    elsif ans = "I" then
        c := length (alg1)
        loop
            if alg1 (c) = "'" or alg1 (c) = "2" then
                c := c - 1
                if alg1 (c) = "2" then
                    c := c - 1
                end if
            end if
            alg2 := alg2 + alg1 (c)
            if c = length (alg1) then
                if alg1 (c) not= "'" and alg1 (c) not= "2" then
                    alg2 := alg2 + "'"
                end if
            else
                if alg1 (c + 1) = "2" then
                    alg2 := alg2 + "2"
                    if c < length (alg1) - 1 and alg1 (c + 2) not= "'" then
                        alg2 := alg2 + "'"
                    elsif c < length (alg1) then
                        alg2 := alg2 + "'"
                    end if
                elsif alg1 (c + 1) not= "'" and alg1 (c + 1) not= "2" then
                    alg2 := alg2 + "'"
                end if
            end if
            exit when c = 1
            c := c - 1
        end loop
        put "inverse: ", alg2
        put ""
    else
        put "invalid option"
    end if
end loop
```

thanks!
EDIT: what the heck, why is the indentation ok when i view it when writing, but everything gets left-aligned in the actual post..?
i guess the only way someone can help is by copy/pasting the code to Turing and pressing indent. this is too confusing. even i cant understand what it says.


----------



## Johannes91 (Mar 20, 2009)

PeterNewton said:


> could someone show me how to make the program more efficient?


Why do you want to make it more efficient?

I've never used that language, but there doesn't seem to be any nested loops so the code is O(n). Only the constant factor could be optimized, but there's no need for that unless the code really is too slow to be usable.

If you meant you want to learn how to make the code shorter/clearer, that's a good question. I always prefer to use some proper data structure for storing the moves instead of plain strings. Not sure how good idea that would be for Turing and how easy parsing the input would be, but at least in the languages I use it's much nicer than passing everything around in strings.

Also helper functions, like opposite that maps U to D etc., can help reduce copypasta.



PeterNewton said:


> EDIT: what the heck, why is the indentation ok when i view it when writing, but everything gets left-aligned in the actual post..?


Put [noparse]

```

```
[/noparse] around the code so the forum doesn't mess it up.


----------



## cuBerBruce (Mar 20, 2009)

PeterNewton said:


> EDIT: what the heck, why is the indentation ok when i view it when writing, but everything gets left-aligned in the actual post..?
> i guess the only way someone can help is by copy/pasting the code to Turing and pressing indent. this is too confusing. even i cant understand what it says.


Use the {code} ... {/code} tag when posting code, but use square brackets, not curly braces.


----------



## PeterNewton (Mar 20, 2009)

Johannes91 said:


> PeterNewton said:
> 
> 
> > could someone show me how to make the program more efficient?
> ...


lol actually i have 3 loops. 2 are nested within 1 big one. one of the inside loops takes care of mirroring, and the other takes care of inversing. whichever loop is chosen according to whether the user says they want to mirror or inverse. the big loop around the two middle loops just allows the program to automatically go back to the beginning so that the user can input another alg.

EDIT: i wonder if it is possible to combine the two inner loops and reduce the number of lines of code. hmm it might be possible to find some common elements..


----------



## Johannes91 (Mar 20, 2009)

PeterNewton said:


> lol actually i have 3 loops. 2 are nested within 1 big one. one of the inside loops takes care of mirroring, and the other takes care of inversing.


Right, I was referring to the inner loops.



PeterNewton said:


> i wonder if it is possible to combine the two inner loops and reduce the number of lines of code. hmm it might be possible to find some common elements..


They both do basically this (inversing also reverses the list):

```
for i in 1 .. length(moves)
    new_moves(i) := f(moves(i))
end
```
Where f is some function that takes a move and returns a move. If it's possible to pass functions as arguments in Turing, you could write a generic function something like this:

```
function modify_alg (f: move -> move, alg: list move) : list move
```
Then mirroring and inversing could be done just by calling modify_alg with the right function.


----------



## PeterNewton (Mar 20, 2009)

Johannes91 said:


> PeterNewton said:
> 
> 
> > lol actually i have 3 loops. 2 are nested within 1 big one. one of the inside loops takes care of mirroring, and the other takes care of inversing.
> ...


yeah that would reduce a lot of lines.
i started programming class last september and was taught Turing as a basic beginning language. so i don't know how do call up fuctions. it might not even be possible in Turing.
but when my visual basic guide arrives from amazon next month, i'll be learning this stuff in a more industrial-grade program (vb).
i'll add edit the code tomorrow to accommodate cube rotations and double-layer-turns.
what i will do to make it more efficient for the time being is that i'll store similar types of moves in arrays. that should make it better.


----------



## PeterNewton (Mar 21, 2009)

well, i edited the code and it supports double layer turns and cube rotations now. anyone with Turing can feel free to use it.


----------



## bundat (Mar 22, 2009)

Googling this was challenging, the results were littered with topics about the "Turing Machine".

http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures

Here's a tutorial on functions to help you.
Maybe it can help clean it up quite a bit.
Or you can also test the idea of passing a function as an argument.


----------



## PeterNewton (Mar 22, 2009)

bundat said:


> Googling this was challenging, the results were littered with topics about the "Turing Machine".
> 
> http://wiki.compsci.ca/index.php?title=Turing_Functions_and_Procedures
> 
> ...


ahahaa thanks man. i actually was not going to learn anymore syntax for turing because i was going to switch over to visual basic. but before switching, now i will learn some more because you showed me such a great site.
i love how the cubing community is so friendly and how people are willing to go to such lengths to help out someone they've never even met. thanks again.


----------



## coolmission (Mar 22, 2009)

PeterNewton said:


> because i was going to switch over to visual basic.



I wouldn't do that.


----------



## PeterNewton (Mar 22, 2009)

coolmission said:


> PeterNewton said:
> 
> 
> > because i was going to switch over to visual basic.
> ...


lol why not? nyways, they will be teaching it in my grade 11 class next year so i just wanted a head start over everyone else 
any specific reason why vb is bad?


----------



## coolmission (Mar 22, 2009)

PeterNewton said:


> coolmission said:
> 
> 
> > PeterNewton said:
> ...



Well, if you have it in school then it's fine.

It's not that VB is really that bad; but I learned it about 6 years ago and when I look back I think to myself: "You had all the options in the world; why VB?"


----------



## PeterNewton (Mar 22, 2009)

coolmission said:


> PeterNewton said:
> 
> 
> > coolmission said:
> ...


yea i thought of that too and i would rather learn python which they teach in the universities i will probably end up in, or even c++. but my high school does turing for grade 10, vb for 11, and flash for 12. so messed up. thanks for the heads up.


----------



## TheBB (Mar 22, 2009)

Messed up indeed. Flash?! Good riddance. Python is a delight and frankly I think it's the best choice, or one of the few anyway, for beginner programming classes. I don't know anything about Turing but I hear he was a clever fellow.


----------



## bundat (Mar 22, 2009)

Personally, I sometimes found VB useful whenever I needed to hack up a quick program with a GUI (until C# came along, anyway).

And Flash is nice to know too. It's not exactly what you'd want to be learning if you want to build up your experience and knowledge in programming concepts, but believe it or not, a lot of companies I've applied to actually prefer Web Programmers with Flash experience (in my experience, more look for this over knowledge in XHTML-compliance, which is sad in a way, but that's how it goes).

Still, a Java or C++ class will help a beginner much much more than these two mediocre "programming languages" (design enviroments, is more like it).


----------



## AvGalen (Mar 23, 2009)

VB is dead, long live VB.NET


----------



## abr71310 (Mar 23, 2009)

PeterNewton said:


> coolmission said:
> 
> 
> > PeterNewton said:
> ...



are you in the same school system as I'm in??
In some areas, the grade 10s / 11s ("first years") learn either PASCAL, Turing or Java? (sometimes Java, mostly Turing though) -- second years do Java / C++, and final (12th grade, almost always) does C++ or C# (all dependent on resources / demand for course).

Personally I took it for two years and did PASCAL --> C++, with Java learnt during camp over the summer, but my teacher (who LOVES C++) was trashing Turing so hard for being a crappy language to write games in (she wrote one to do hangman, and it ran in I think 5 minutes?? for 2000 lines).

I don't see anything wrong with your code that hasn't already been mentioned, but you might want to ALLOW spaces so the input doesn't throw off your userbase when inputting moves (since you have to do RR to mean R2, it's easier to write R R rather than RR in such an input space).

EDIT: My dad knows .NET, VB, ASQ, SQL (is ASQ a language or a program??), it's insane.
He says that VB was really meant for pre-2000 stuff (or was that basic?), and that everything AFTER that was meant for C# or C++ (although he told me he hated reading my C++ code since he's too enthralled with his current languages that he hates the "oldies").


----------



## PeterNewton (Mar 23, 2009)

abr71310 said:


> PeterNewton said:
> 
> 
> > coolmission said:
> ...


yupp i am in mississauga.
what computer camp did you go to? i want to go to one this summer but i cant find any on the internet.
-_- 2000 lines for hangman? maybe you teacher has problems with efficiency. i made hangman in 104 lines.
here is the code for proof. it was a bonus for grade 10 (last semester). lol i bet i could have made it shorter but our classes are 1.25 hours.

```
setscreen ("graphics")
var again : string
put "CLASSIC HANGMAN!"
delay (1500)
cls
loop
    var wrd, rev1, rev2, rev3 : string
    var incom, d, len : int
    incom := 0
    d := 1
    rev1 := ""
    rev2 := ""
    rev3 := ""
    put "player 1: please enter a secret word"
    get wrd
    len := length (wrd) + 7
    var guess : array 1 .. len of string
    cls
    for c : 1 .. length (wrd)
        rev1 := rev1 + "*"
        rev2 := rev2 + "*"
    end for
    put "player 2: guess the secret word by inputting letters"
    locate (2, 1)
    put rev1
    loop
        get guess (d)
        exit when length (guess (d)) not= 1
        for c : 1 .. length (wrd)
            if guess (d) = wrd (c) then
                for e : 1 .. length (wrd)
                    if e = c then
                        rev3 := rev3 + wrd (c)
                    else
                        rev3 := rev3 + rev2 (e)
                    end if
                end for
                rev2 := rev3
                rev3 := ""
            end if
            if c = length (wrd) and rev2 = rev1 then
                put "There is no letter ", guess (d), " in the word, or it was already entered before"
                incom := incom + 1
            end if
        end for
        if incom > 0 then
            drawline (200, 0, 200, 120, black)
        end if
        if incom > 1 then
            drawline (200, 120, 260, 120, black)
        end if
        if incom > 2 then
            drawline (260, 120, 260, 90, black)
        end if
        if incom > 3 then
            drawoval (260, 80, 10, 10, black)
        end if
        if incom > 4 then
            drawline (260, 70, 260, 40, black)
        end if
        if incom > 5 then
            drawline (250, 60, 260, 70, black)
            drawline (270, 60, 260, 70, black)
        end if
        if incom > 6 then
            drawline (250, 30, 260, 40, black)
            drawline (270, 30, 260, 40, black)
        end if
        exit when incom = 7
        if rev2 not= rev1 then
            rev1 := rev2
            locate (2, 1)
            put rev1
        end if
        exit when rev1 = wrd
        d := d + 1
    end loop
    cls
    if length (guess (d)) not= 1 or incom = 7 then
        drawline (200, 0, 200, 120, black)
        drawline (200, 120, 260, 120, black)
        drawline (260, 120, 260, 90, black)
        drawoval (260, 80, 10, 10, black)
        drawline (260, 70, 260, 40, black)
        drawline (250, 60, 260, 70, black)
        drawline (270, 60, 260, 70, black)
        drawline (250, 30, 260, 40, black)
        drawline (270, 30, 260, 40, black)
    end if
    if length (guess (d)) not= 1 then
        put "The word was ", wrd
        put "You were booted out of the game for inputting several letters at once"
    elsif incom = 7 then
        put "You lose. The word was ", wrd
    else
        put "The word was ", wrd
        put "You guessed the word correctly"
    end if
    put "do you want to play again? (y/n)"
    get again
    exit when again = "n"
end loop
put "Thank you for playing the game."
```
and yes, turing does suck for games. i would know since my ISU was 1200 lines. it was just a shooter game with shots that come back at the shooter if they miss, and 4 moving balls with different y axis values, which move horizontally. i threw in the ability for the balls to change sizes. but it might have been inefficient because we had to make our ISUs before learning about strings or arrays.
what do you mean you have to put RR for R2 in the input? it says in the instructions "put "180 degree = + (2)"". i duno, maybe everyone is interpreting it wrong. it means that add "2" are the initial move (e.g. R, F, etc.). but yes, i think allowing spaces is a good idea since it is what most people do. i'll do it soon.


----------



## PeterNewton (Mar 23, 2009)

bundat said:


> Personally, I sometimes found VB useful whenever I needed to hack up a quick program with a GUI (until C# came along, anyway).
> 
> And Flash is nice to know too. It's not exactly what you'd want to be learning if you want to build up your experience and knowledge in programming concepts, but believe it or not, a lot of companies I've applied to actually prefer Web Programmers with Flash experience (in my experience, more look for this over knowledge in XHTML-compliance, which is sad in a way, but that's how it goes).
> 
> Still, a Java or C++ class will help a beginner much much more than these two mediocre "programming languages" (design enviroments, is more like it).


are you referring to python and turing as the 2 mediocre ones? turing probably, but many universities in my area like toronto and waterloo use python, so it cant be all that bad.
what really bugs me about turing is the output window. i was wrote and ran a program for a list of multislotting techniques, ad when i tried to copy/paste it, it wouldnt let me.. ???!!?! i had to save it as a .bmp, copy the .bmp from paint into word (the font on the pic was smaller than size 5..), printed it, put it in a scanner and used the scanning software to pick up the letters and then i could copy, paste it into word as actual words. GEEZZ


----------



## AvGalen (Mar 23, 2009)

I have never heard of a programming language called ASQ. Maybe you mean ASP? ASP is dead as well, long live ASP.NET

Simply put, ASP and VB were used untill the coming of the .NET Framework in 2002. Now they are only used for legacy development. If you are going to learn a new language you should really ignore those two! VB.NET and C# (in combination with ASP.NET) have replaced them entirely


----------



## coolmission (Mar 23, 2009)

Go for Haskell or Prolog.

Dear lord, this is getting ridiculous


----------



## Johannes91 (Mar 23, 2009)

coolmission said:


> Go for Haskell or Prolog.


Doesn't really matter which language you start with, but learning Haskell (or some other functional language) at some point is indeed a really really good idea.


----------



## AvGalen (Mar 23, 2009)

Languages are secondary to methods.

Once you learn one procedural language, you should be able to use others without many problems.
Once you learn one object oriented language, etc, etc

Focussing on 1 language is only useful when you are required to use that language, or when you want to become an expert


----------



## bundat (Mar 25, 2009)

PeterNewton said:


> bundat said:
> 
> 
> > Personally, I sometimes found *VB* useful whenever ...
> ...


Err...what?
I was obviously talking about VB and Flash.


----------

