Hack Forums
[Shell] Challenge: Strings - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Shell] Challenge: Strings (/showthread.php?tid=5142309)

Pages: Pages 1 2


Challenge: Strings - WOLGA - Jan 21, 2016

Challenge by Qwertyuioppasdfghjklzxc:

Given two alphanumeric strings, check to see if one can be rearranged case-insensitively to form the other. Spaces and quotes should not count for checking.

Example:

Code
$ check.bat "abcdefg" "gfedcba"
yes

$ check.bat "abc123" "312def"
no

$ check.bat "blah blah" "blahblah"
yes

$ check.bat "123123" "123"
no

If you need clarification please ask.


* - GL1TCH3D - Jan 21, 2016

I'm assuming that it's case insensitive?


* - jahwi - Jan 21, 2016

this one I'll enjoy doing...Will do this soon.


* - WOLGA - Jan 21, 2016

(Jan 21, 2016 08:00 PM)GL1TCH3D Wrote:  I'm assuming that it's case insensitive?

Yes, that is correct, and thank you for asking. I have updated the OP.


* - GL1TCH3D - Jan 21, 2016

http://pastebin.com/ywSzC6Y5

Should work


* - White Light - Jan 22, 2016

Rearrange as in no duplication of any characters, correct?

If that's the case... (Click to View)


* - WOLGA - Jan 22, 2016

I just confirmed both GL1TCH3D and White Light code as working. Very nice job, both of you! ๐Ÿค—

Even though White Light's code does not not count spaces, its a trivial fix.


* - jahwi - Jan 22, 2016

Not extensively tested but this should work:
Code
@echo off
setlocal enabledelayedexpansion
set one=%1
set two=%2
set one=!one: =!
set two=!two: =!
set answer=yes
set len1=
set len2=
set string=
set stringb=
set found=
::check length of both strings
call :findlength !one!
set len1=!length!
call :findlength !two!
set len2=!length!
if not "!len1!"=="!len2!" set answer=no
for /l %%a in (1,1,!len1!) do (
set found=no
set string=!one:~%%a,1!
for /l %%b in (1,1,!len2!) do (
set stringb=!two:~%%b,1!
if "!string!"=="!stringb!" set found=yes
)
)
if "!found!"=="no" set answer=no
echo !answer!
exit /b
:findlength
echo %1>%temp%\string.tmp
for %%? in (%temp%\string.tmp) do (
set /a length=%%~z? -2
)
exit /b



* - wurd - Jan 22, 2016

(Jan 22, 2016 01:41 AM)White Light Wrote:  set s1=%~1
set s2=%~2
set #=%s1%
set l=0
for %%A in (64 32 16 8 4 2 1) do if not "!#:~%%A!"=="" set #=!#:~%%A!&set /a l+=%%A
I saw this length algo used before but never analyzed it. Just looked at it now and the idea of using binary to find the length is a really clever idea.
Quote:if not "!s2:~%l%!"=="!s2:~%l%,1!" goto :no
I was wondering what the purpose of this line is? I understand what it does, I just dont understand why you need it? It just checks to see if the last character is equal to the last character, from my perspective.


* - White Light - Jan 22, 2016

(Jan 22, 2016 06:17 AM)Qwertyuiopasdfghjklzxc Wrote:  I just confirmed both GL1TCH3D and White Light code as working. Very nice job, both of you! ๐Ÿค—

Even though White Light's code does not not count spaces, its a trivial fix.

Don't know what you're talking about...? There's no reason it won't behave correctly with spaces, and in fact it actually behaves just fine even when you're using all spaces or interspersed.

(Jan 22, 2016 06:08 PM)wurd Wrote:  I was wondering what the purpose of this line is? I understand what it does, I just dont understand why you need it? It just checks to see if the last character is equal to the last character, from my perspective.

Not exactly, but close. With substrings, the length parameter is optional. Without it, you get from that position to the end of the string. In this case, if the lengths are equal, the position to the end of the string should be exactly one character (the last one), otherwise there will be at least one other character.

Just to reiterate: Say the strings are ABCD and 123456. I know the length of the first string (4, though it's saved as 3 for use), so I can check the second string using that info.

!s2:~3! would be 456 and !s2:~3,1! would just be 4. Since they're different, I know that the two strings are different length. This is a much faster way of comparing lengths than actually checking both (even using an optimized method like 2^n seeks).


* - 7_R3X - Jan 23, 2016

I'm not good at scripting.May I give it a try with C++ ? I'm good at it. ๐Ÿคจ


* - WOLGA - Jan 23, 2016

(Jan 22, 2016 06:54 PM)White Light Wrote:  Don't know what you're talking about...? There's no reason it won't behave correctly with spaces, and in fact it actually behaves just fine even when you're using all spaces or interspersed.

Quote:Spaces and quotes should not count for checking.



* - White Light - Jan 23, 2016


But that's stupid. Like, actually really freaking dumb. If you don't want it to count for spaces, just toss in a couple sX=%sX: =% at the start, but that's honestly a really frigging stupid restriction to put on code, even more so than annikk's "sensible" password generator needing to keep a wordlist in the script.


* - jahwi - Jan 23, 2016

(Jan 23, 2016 02:51 PM)White Light Wrote:  But that's stupid. Like, actually really freaking dumb. If you don't want it to count for spaces, just toss in a couple sX=%sX: =% at the start, but that's honestly a really frigging stupid restriction to put on code, even more so than annikk's "sensible" password generator needing to keep a wordlist in the script.
1. I did do that in my code
2. Annikk's challenge was fun.
3. exit /b


* - White Light - Jan 23, 2016

(Jan 23, 2016 04:23 PM)jahwi Wrote:  1. I did do that in my code
2. Annikk's challenge was fun.
3. exit /b

1. Doing it is not stupid. The requirement is stupid and does more harm than good.
2. It was, but he also added in stupid restrictions that do more harm than good.
3. goto :eof