Computer Science Canada

Repeating code

Author:  aaa [ Tue Jan 21, 2014 10:32 am ]
Post subject:  Repeating code

What is it you are trying to achieve?
<Replace all the <> with your answers/code and remove the <>>
I am trying to make the code shorter

What is the problem you are having?
<Answer Here>
The code is repeating a lot of the something

Describe what you have tried to solve this problem
<Answer Here>
I tried to looking it up

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


 if input = "a" then  %1
                if c (1) not= 1 then % 2
                    mesg %3
                end if %4
                if c (1) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "b" then
                if c (2) not= 1 then
                    mesg
                end if
                if c (2) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "c" then
                if c (3) not= 1 then
                    mesg
                end if
                if c (3) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "d" then
                if c (4) not= 1 then
                    mesg
                end if
                if c (4) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "e" then
                if c (5) not= 1 then
                    mesg
                end if
                if c (5) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "f" then
                if c (6) not= 1 then
                    mesg
                end if
                if c (6) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "g" then
                if c (7) not= 1 then
                    mesg
                end if
                if c (7) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "h" then
                if c (8) not= 1 then
                    mesg
                end if
                if c (8) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "i" then
                if c (9) not= 1 then
                    mesg
                end if
                if c (9) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "j" then
                if c (10) not= 1 then
                    mesg
                end if
                if c (10) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "k" then
                if c (11) not= 1 then
                    mesg
                end if
                if c (11) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "l" then
                if c (12) not= 1 then
                    mesg
                end if
                if c (12) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "m" then
                if c (13) not= 1 then
                    mesg
                end if
                if c (13) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "n" then
                if c (14) not= 1 then
                    mesg
                end if
                if c (14) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "o" then
                if c (15) not= 1 then
                    mesg
                end if
                if c (15) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "p" then
                if c (16) not= 1 then
                    mesg
                end if
                if c (16) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "q" then
                if c (17) not= 1 then
                    mesg
                end if
                if c (17) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "r" then
                if c (18) not= 1 then
                    mesg
                end if
                if c (18) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "s" then
                if c (19) not= 1 then
                    mesg
                end if
                if c (19) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "t" then
                if c (20) not= 1 then
                    mesg
                end if
                if c (20) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "u" then
                if c (21) not= 1 then
                    mesg
                end if
                if c (21) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "v" then
                if c (22) not= 1 then
                    mesg
                end if
                if c (22) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "w" then
                if c (23) not= 1 then
                    mesg
                end if
                if c (23) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "x" then
                if c (24) not= 1 then
                    mesg
                end if
                if c (24) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "y" then
                if c (25) not= 1 then
                    mesg
                end if
                if c (25) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if

            if input = "z" then
                if c (26) not= 1 then
                    mesg
                end if
                if c (26) not= 1 & index (text (random), input) = 0 then
                    striker -= 1
                end if
            end if



Please specify what version of Turing you are using
4.1.1

Author:  Nathan4102 [ Tue Jan 21, 2014 10:46 am ]
Post subject:  RE:Repeating code

Heres a hint, see if you can figure out the rest on your own:

Turing:
put (ord("a") - 96)
put (ord("b") - 96)


This prints:

Code:
1
2

Author:  aaa [ Tue Jan 21, 2014 11:02 am ]
Post subject:  Re: Repeating code

I am still lost

Author:  Nathan4102 [ Tue Jan 21, 2014 11:07 am ]
Post subject:  RE:Repeating code

Ok, ill show you how to do it. Ord and Chr are quite useful when working with chars or strings:

Turing:
if ord(input) > 96 and ord(input) < 123 then %Check if input is between a and z (Use ASCII tables)
    if c(ord(input) - 96) not= 1 then
        mesg
    end if
    if c(ord(input) - 96) not= 1 & index(text(random, input) = 0 then
        striker -= 1
    end if
end if

EDIT forgot the -96, just added it in

Author:  aaa [ Tue Jan 21, 2014 3:34 pm ]
Post subject:  Re: Repeating code

Ah, i see how everything works now. Thanks! I notices that ord and only take a 1 letter string.

Author:  Tony [ Tue Jan 21, 2014 4:47 pm ]
Post subject:  RE:Repeating code

Yeah. Multi-byte characters are not part of ASCII. Read more about ord

But you could also compare string's lexicographical order
code:

if "foo" > "barrrrrr" then
   ...


: