Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

Challenge: Strings

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.
I'm assuming that it's case insensitive?
[Image: 8vvxbk8.jpg]
this one I'll enjoy doing...Will do this soon.
(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.
http://pastebin.com/ywSzC6Y5

Should work
[Image: 8vvxbk8.jpg]
Rearrange as in no duplication of any characters, correct?

If that's the case... (Click to View)
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.
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
(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.
Contact me @ [email protected]
(Jan 22, 2016 06:17 AM)WOLGA 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).
I'm not good at scripting.May I give it a try with C++ ? I'm good at it. 🤨
(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.

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.
(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
(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


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by pro_fesnoobSep 24, 2022 04:24 PM 14 796 Sep 24, 2022 04:24 PM
Last Post: BinaryFox
[Shell] by WOLGAMay 8, 2015 07:56 AM 84 1,455 May 8, 2015 07:56 AM
Last Post: White Light
[.NET] by CatSoupNov 17, 2014 06:20 PM 9 631 Nov 17, 2014 06:20 PM
Last Post: NETExplorer