Coding challenge for any interested parties

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
T3RRY
Posts: 250
Joined: 06 May 2020 10:14

Coding challenge for any interested parties

#1 Post by T3RRY » 23 Nov 2021 09:35

*Following PenPen's suggestions, some edits have been made to provide clarity *

Background:
I enjoy batch scripting and a bit of code golfing in my spare time, and generally enjoy activities that require a degree of problem solving. I find batch to be interesting simply because there's a multitude of ways to approach problems, some simple and some rather obscure.

Id like to propose a challenge, that for many of the experts here should be simple enough, but I hope might still provide a bit of a thought excercise.

I propose also that any code responses be posted to an external site that shows an upload / edit time, and linked to a week from now so that responses arent influenced by others answers and we might see a few different approaches to solving the problem.

The task:

*Edit Clarified. Note: The edits made are not breaking - IE: they do not change the rules of the challenge or invalidate attempts made using the previous explanation of the rules.
Without using any of the following in your script:
- The literal characters 0 1 2 3 4 5 6 7 8 9
- If statements
- For loops
- DelayedExpansion

> Take 2 Inputs of the following types:
- A string containing any combination of printable ASCII characters ( TAB + Characters in the Decimal range 32 - 126 See: https://www.asciitable.com/ )
- Your script must be able to process an input string of any length supported by the method of reading input
- Accceptable methods of Input to use:
- Command line arguments
- Set /p prompt or read from file/s - Note: If reading both inputs from a single file, remember that For loops are prohibited.
- Hardcoded, provided that the input number is not used to generate other numbers the script will utilise
- Any other method of input you can concieve of that does not breach other rules of this challenge.
- An Integer (LEQ 8191)

> Walk the length of the string, and if the index number of the characters position is LEQ than the 2nd input and the string is not empty at that index, output the index position (1 indexed) and the character occupying it on a new line in the format:

Code: Select all

  Indexnumber:"Character"

Additional notes: *EDIT Repositioned and clarified:
- Testing will be done using windows 10 cmd.exe using code page 850
- Outside of The prohibited commands "If" "For" and the "DelayedExpansion" Environment, and keeping in mind the literal characters 0 1 2 3 4 5 6 7 8 9 are not permitted in your script, any executeable included in a standard windows 10 installation may be utilised by your script to generate the output. This includes but is not limited to using other languages in batch hybrids.
- No output to the console other than a prompt such as set /p for input or the expected output is permitted
- The program must be able to cope with empty inputs ( terminate without output )
- Inputs if provided will always be provided in string / integer order

Example One:

Code: Select all

rem input 1
string: This (!@#$%^&*() String
rem input 2
Stop at char #: 10
rem output
1:"T"
2:"h"
3:"i"
4:"s"
5:" "
6:"("
7:"!"
8:"@"
9:"#"
10:"$"
Example Two:

Code: Select all

rem input 1
string: <an>other example "
rem input 2
Stop at char #: 50
rem output
1:"<"
2:"a"
3:"n"
4:">"
5:"o"
6:"t"
7:"h"
8:"e"
9:"r"
10:" "
11:"e"
12:"x"
13:"a"
14:"m"
15:"p"
16:"l"
17:"e"
18:" "
19:"""
C:\Users\tcdou>
Last edited by T3RRY on 24 Nov 2021 21:12, edited 1 time in total.

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

Re: Coding challenge for any interested parties

#2 Post by Lowsun » 23 Nov 2021 23:24

I just thought of an interesting solution, but it doesn't fit the requirements (no number literals) and I don't think it's possible without them, so I'll just post it here.

Code: Select all

@ECHO OFF
SET /P "str=Enter string : "
SET /P "max=Enter number : "
SET "num=1"

(
    FINDSTR "^;" "%~f0"
    ECHO :%max%
    ECHO PAUSE
    ECHO EXIT
)>TEMPCALLCHALL.bat

CALL TEMPCALLCHALL.bat

;:LOOP
;CALL :"%str:~0,1%" 2>NUL
;ECHO %num%:"%str:~0,1%"
;CALL :%num% 2>NUL
;SET "str=%str:~1%"
;SET /A "num+=1"
;GOTO :LOOP

;:"~0
;PAUSE
;EXIT
I just use CALL to check for empty string or lowest input by CALLing a label with the input number (so I have to make a new bat file with that number as a label) or an empty string.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Coding challenge for any interested parties

#3 Post by ShadowThief » 24 Nov 2021 06:15

The hardest part was doing this without an if statement.
https://github.com/sintrode/stringwalker

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Coding challenge for any interested parties

#4 Post by aGerman » 24 Nov 2021 07:42

Not sure if I'll find the time to work on that any further. Proof of concept:

Code: Select all

@echo off &setlocal
set "s=This (!@#$%^&*() String"
set "n=15"

call set "s=%%s:~,%n%%%"
cmd /u /c ^"<nul set /p "=%s%"^"|find /v ""|findstr /n "^"

pause
Steffen

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

Re: Coding challenge for any interested parties

#5 Post by T3RRY » 24 Nov 2021 08:21

ShadowThief wrote:
24 Nov 2021 06:15
The hardest part was doing this without an if statement.
https://github.com/sintrode/stringwalker
Vey close, A couple of easily resolved Issues exist though:
- Display of the expanded string on line 26 (Which is not required) Results in a syntax error with redirection characters or pipes.
- Script continues until the maximum index is reached, when it should terminate or at least show no output if there is no character present at that index in the string.

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

Re: Coding challenge for any interested parties

#6 Post by T3RRY » 24 Nov 2021 08:23

aGerman wrote:
24 Nov 2021 07:42
Not sure if I'll find the time to work on that any further. Proof of concept:

Code: Select all

@echo off &setlocal
set "s=This (!@#$%^&*() String"
set "n=15"

call set "s=%%s:~,%n%%%"
cmd /u /c ^"<nul set /p "=%s%"^"|find /v ""|findstr /n "^"

pause
Steffen
Beautiful solution, the only shortcoming is that it doesn't adhere to the format of index:"character"
For anyone wondering, as it is a proof of concept It's not unreasonable to accept hardcoded input.

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

Re: Coding challenge for any interested parties

#7 Post by T3RRY » 24 Nov 2021 08:29

Lowsun wrote:
23 Nov 2021 23:24
I just thought of an interesting solution, but it doesn't fit the requirements (no number literals) and I don't think it's possible without them, so I'll just post it here.

Code: Select all

@ECHO OFF
SET /P "str=Enter string : "
SET /P "max=Enter number : "
SET "num=1"

(
    FINDSTR "^;" "%~f0"
    ECHO :%max%
    ECHO PAUSE
    ECHO EXIT
)>TEMPCALLCHALL.bat

CALL TEMPCALLCHALL.bat

;:LOOP
;CALL :"%str:~0,1%" 2>NUL
;ECHO %num%:"%str:~0,1%"
;CALL :%num% 2>NUL
;SET "str=%str:~1%"
;SET /A "num+=1"
;GOTO :LOOP

;:"~0
;PAUSE
;EXIT
I just use CALL to check for empty string or lowest input by CALLing a label with the input number (so I have to make a new bat file with that number as a label) or an empty string.
Almost there - It is indeed possible to generate variables with Integer values without using literal numbers.
Outside of that, your script fails to terminate at the input maximum index, which I'm sure you can resolve.
Last edited by T3RRY on 24 Nov 2021 08:38, edited 1 time in total.

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

Re: Coding challenge for any interested parties

#8 Post by T3RRY » 24 Nov 2021 08:35

Glad to see a number of people taking on the challenge - Seeing as the cats out the bag and multiple solutions have already been posted, I wont hold back till the weeks up with mine:

The original version:

Code: Select all

Rem original golfed version:
Rem https://pastebin.com/gp86e5mK
Rem Pastebin Pass: split

@Set x=&Set/p"s=string: "&Set/pn=Stop at char #: &Set/az=i=x&Call Set/au=%%z%%xa/%%z%%xa,e=u+u
:l
@Call Set "v=%%s:~%i%,%u%%%"&@(Set v>nul&&(Set/A"t=u/(n-i)"&&(Cmd/cSet/Au+i&@Call Echo(:"%%v%%"&Set/Ai+=u&Goto:l)))%e%>nul
The readbale version:

Code: Select all

@Echo off & Setlocal
 Set /P "string=string: "
 Set /P "stop=Stop at char #: "
 Set /A zero=index=_nulvar_
 Set /A one = %zero%xa / %zero%xa , STDerr = one + one

:loop
 Call Set "character=%%string:~%index%,%one%%%"
 (
  Set character>nul && (
   Set /A "t=one/(stop-index)" && (
    Cmd/c Set /A one + index
    Echo(:"%character%"
    Set /A index += one
    Goto:loop
   )
  )
 ) %STDerr%>nul
Endlocal

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Coding challenge for any interested parties

#9 Post by ShadowThief » 24 Nov 2021 08:37

T3RRY wrote:
24 Nov 2021 08:21
ShadowThief wrote:
24 Nov 2021 06:15
The hardest part was doing this without an if statement.
https://github.com/sintrode/stringwalker
Vey close, A couple of easily resolved Issues exist though:
- Display of the expanded string on line 26 (Which is not required) Results in a syntax error with redirection characters or pipes.
- Script continues until the maximum index is reached, when it should terminate or at least show no output if there is no character present at that index in the string.
How can there be no character at an index if the string continues past that index? If you pass the sample input you provided to my script, it matches your output exactly.

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

Re: Coding challenge for any interested parties

#10 Post by T3RRY » 24 Nov 2021 08:47

ShadowThief wrote:
24 Nov 2021 08:37
How can there be no character at an index if the string continues past that index? If you pass the sample input you provided to my script, it matches your output exactly.
T3RRY wrote:
24 Nov 2021 08:21
> Take 2 Inputs of the following types:
- A string containing any combination of printable ASCII characters that may be of any length supported by the method of reading input
- An Integer (LEQ 8191)
> Walk the length of the string, and if the index number of the characters position is LEQ than the 2nd input and the string is not empty at that index, output the index position (1 indexed) and the character occupying it on a new line in the format:
The issue arises when the index number provided as the second input is of a higher value than the strings length. Your script doesn't test a definition of the char variable before outputting the current index:"character", which results in the undesired behaviour of outputting:
index#:""
until the maximum index is reached, which fails to comply with the task rule: "if the index number of the characters position is LEQ than the 2nd input and the string is not empty at that index"

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

Re: Coding challenge for any interested parties

#11 Post by Lowsun » 24 Nov 2021 12:21

T3RRY wrote:
24 Nov 2021 08:29
Almost there - It is indeed possible to generate variables with Integer values without using literal numbers.
Outside of that, your script fails to terminate at the input maximum index, which I'm sure you can resolve.
Ah the solution is so simple...yet so obvious!
Though, I'm not sure what you mean by input maximum index. Do you mean inputting an int like 8191?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Coding challenge for any interested parties

#12 Post by ShadowThief » 24 Nov 2021 16:00

T3RRY wrote:
24 Nov 2021 08:47
ShadowThief wrote:
24 Nov 2021 08:37
How can there be no character at an index if the string continues past that index? If you pass the sample input you provided to my script, it matches your output exactly.
T3RRY wrote:
24 Nov 2021 08:21
> Take 2 Inputs of the following types:
- A string containing any combination of printable ASCII characters that may be of any length supported by the method of reading input
- An Integer (LEQ 8191)
> Walk the length of the string, and if the index number of the characters position is LEQ than the 2nd input and the string is not empty at that index, output the index position (1 indexed) and the character occupying it on a new line in the format:
The issue arises when the index number provided as the second input is of a higher value than the strings length. Your script doesn't test a definition of the char variable before outputting the current index:"character", which results in the undesired behaviour of outputting:
index#:""
until the maximum index is reached, which fails to comply with the task rule: "if the index number of the characters position is LEQ than the 2nd input and the string is not empty at that index"
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

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

Re: Coding challenge for any interested parties

#13 Post by penpen » 24 Nov 2021 16:16

I think, you need to provide some more detail on the input:
- the encoding of the characters: Which codepage do you assume to be used?
- the source type: Is the input stored in file or do you retrieve that from STDIN?
- the source format: Is there a delimiter, that seperates both inputs (string and number) or are they stored in two different files?
- how long is the input string allowed to be? ("set/p" is only able to read up to 1023 characters, but the task seems to ask to support up to 8191).
- complex issues like UTF-8 encoded input character might be broken into pieces by "set/p" reading (which might be a reason to use single byte character codepages only).

Edit: You also might want to limit to win10 cmd.exe only, because some solutions might not work on XP.
Edit 2: You might also want to restrict the executables, we are allowed to use (findstr.exe, certutil.exe, reg.exe, csc.exe (== c# compiler), ...).
Edit 3: Also i don't see the address (though i might have missed it) of the external site the code responses should be posted to.

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

Re: Coding challenge for any interested parties

#14 Post by T3RRY » 24 Nov 2021 21:14

penpen wrote:
24 Nov 2021 16:16
I think, you need to provide some more detail on the input
Edit: You also might want to limit to win10 cmd.exe only, because some solutions might not work on XP.
Thanks for your suggestions. I have edited the opening post to provide some more clarity about forms of input, the environment that will be used for testing and clarity around what other executables may be utilised to support completion of the task.

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

Re: Coding challenge for any interested parties

#15 Post by T3RRY » 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

Post Reply