Coding challenge for any interested parties

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
ShadowThief
Expert
Posts: 1056
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Coding challenge for any interested parties

#16 Post by ShadowThief » 25 Nov 2021 09:53

T3RRY wrote:
24 Nov 2021 21:24
ShadowThief wrote:
24 Nov 2021 16:00
Classic off-by-one errors. Also, I totally missed that the first two lines in your sample input were where you were getting user input.
Updated: https://github.com/sintrode/stringwalker
I've edited the example and added an second example to avoid any further confusion

There remains one final error in your script - Your method of testing an empty definition for char also fails for space
And now it doesn't.

There's a backspace between the period and the space on line 37 in case GitHub decided to not preserve it.

Lowsun
Posts: 21
Joined: 14 Apr 2019 17:22

Re: Coding challenge for any interested parties

#17 Post by Lowsun » 25 Nov 2021 10:07

An updated with version with no number literals/misc changes.

Code: Select all

@ECHO OFF
SET /P "str=Enter string : "
SET /P "max=Enter number : "
SET /A "num=o=PROCESSOR_LEVEL / PROCESSOR_LEVEL", "t=o + o", "z=o - o"

CALL SET "str=%%str:~,%max%%%"
(
    ECHO :LOOP
    ECHO CALL :"%%str:~0,%o%%%" %t%^>NUL
    ECHO ECHO %%num%%:"%%str:~0,%o%%%"
    ECHO SET "str=%%str:~%o%%%"
    ECHO SET /A "num+=%o%"
    ECHO GOTO :LOOP
    ECHO :"~%z%
    ECHO PAUSE
    ECHO EXIT
)>TEMPCALLCHALL.bat
CALL TEMPCALLCHALL.bat

T3RRY
Posts: 194
Joined: 06 May 2020 10:14

Re: Coding challenge for any interested parties

#18 Post by T3RRY » 25 Nov 2021 10:42

@Lowsun
Your approach is succesful,Unfortunately however, error output is occuring for strings containing Doublequotes:

Code: Select all

C:\Users\tcdou>lowsun2
Enter string : !@#$%^&* :"": = end
Enter number : 20
1:"!"
2:"@"
3:"#"
4:"$"
5:"%"
6:"^"
7:"&"
8:"*"
9:" "
10:":"
The system cannot find the batch label specified - """
11:"""
The system cannot find the batch label specified - """
12:"""
13:":"
14:" "
15:"="
16:" "
17:"e"
18:"n"
19:"d"
Press any key to continue . . .
This can be remedied by shifting the redirection to the start of the line on the Call to the function:

Code: Select all

    ECHO :LOOP
    ECHO %t%^>NUL CALL :"%%str:~0,%o%%%"

T3RRY
Posts: 194
Joined: 06 May 2020 10:14

Re: Coding challenge for any interested parties

#19 Post by T3RRY » 25 Nov 2021 10:50

ShadowThief wrote:
25 Nov 2021 09:53
T3RRY wrote:
24 Nov 2021 21:24
ShadowThief wrote:
24 Nov 2021 16:00
Classic off-by-one errors. Also, I totally missed that the first two lines in your sample input were where you were getting user input.
Updated: https://github.com/sintrode/stringwalker
I've edited the example and added an second example to avoid any further confusion

There remains one final error in your script - Your method of testing an empty definition for char also fails for space
And now it doesn't.

There's a backspace between the period and the space on line 37 in case GitHub decided to not preserve it.
Syntax error for space is resolved, however `=` results in a syntax error. the following modification to your :get_substring function can resolve this problem:

Code: Select all

:get_substring
set /a index_label=counter+one
call set "char=%%string:~%counter%,%one%%%"
rem throw an error if we've gone past the end of the string
set char>nul %two%>nul || exit /b %one%
echo %index_label%:"%char%"
set /a exit_code=%index_label%/%limit%
exit /b %exit_code%
Provided the variable name does not form a prefix for a system environment variable, this method is a safe equivalent to If not defined

penpen
Expert
Posts: 1943
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Coding challenge for any interested parties

#20 Post by penpen » 26 Nov 2021 07:36

You should also check against code injection inputs like 'test=123" & echo(' with a big enough length like 10.
I only found one reliable way to check whether the input string is empty.
I'm not sure if i should post it here as it may spoil the fun out of it for others to discover that for themselves.
I personally don't plan to participate, though i solved that task (for fun) - i suspect the batch only solutions to be pretty similar.
(You also could initialize 'zero' and 'one' without beeing dependent on the existence of other values.)


Btw @T3RRY another question:
Do we need to check if the the bounds of the given integer is within [0, 8191] or if that integer wasn't given at all (empty, like for example the string)?
In case the answer is yes, how to act in case it's bigger (for example print as many as possible even if greater than 8291, only up to 8191, or don't print anything at all)?

T3RRY
Posts: 194
Joined: 06 May 2020 10:14

Re: Coding challenge for any interested parties

#21 Post by T3RRY » 26 Nov 2021 08:35

penpen wrote:
26 Nov 2021 07:36
You should also check against code injection inputs like 'test=123" & echo(' with a big enough length like 10.
I only found one reliable way to check whether the input string is empty.
I'm not sure if i should post it here as it may spoil the fun out of it for others to discover that for themselves.
I personally don't plan to participate, though i solved that task (for fun) - i suspect the batch only solutions to be pretty similar.
(You also could initialize 'zero' and 'one' without beeing dependent on the existence of other values.)
Indeed, and apart from some since resolved syntax errors, so far so good with regards to submissions abilities to resist code injection.
I had hoped people would refrain from posting for the suggested timeframe, however, since that hasn't happened I've already submitted y own solution. I'd expect given some of the approaches you mention the methods used in it would indeed be reasonably similar to your own.

I'm still very interested to see batch hybrid solutions, but am doubtful theres a significant enough pool of people who have both the knowledge to script batch hybrids and the interest in participating.
penpen wrote:
26 Nov 2021 07:36
Btw @T3RRY another question:
Do we need to check if the the bounds of the given integer is within [0, 8191] or if that integer wasn't given at all (empty, like for example the string)?
In case the answer is yes, how to act in case it's bigger (for example print as many as possible even if greater than 8291, only up to 8191, or don't print anything at all)?
The 8191 limit is the maximum I will be testing to. If the method of input used to take the string input does not support this string length, I will test to the maximum string length supported by the form of input. Participants may take it upon themselves to restrict the value of the Integer input to the maximum value supported by the input method of the string, however they are not required to.

There is no need to cater for:
- Input Numbers exceeding 8191
- Negative input numbers
- input order being reversed

In case you need any further clarfication of the accompaning note: "The program must be able to cope with empty inputs ( terminate without output )
- Inputs if provided will always be provided in string / integer order"
- If either input is empty, the program should Terminate or show no output prior to completing.
- Both inputs may be empty, The string may be empty or the Integer may be empty, however:
- While a number, as a combination of ASCII characters in the stated range may be input as the string, the string will never be supplied as the input for the Integer.

Post Reply