Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What was my input? 7

Status
Not open for further replies.

ca8msm

Programmer
May 9, 2002
11,327
0
0
GB
Consider the following examples:

Example 1:

Input: tektips
Output: 928@909@1070@1276@1260@1456@16108

Example 2:

Input: puzzle
Output: 784@936@1098@1220@1188@12127

Example 3:

Input: example
Output: 404@600@582@763@896@972@10104

If I therefore had the following output, what was my input string:

Output: 560@684@777@824@1026@970@1199@1308@1313@1596@1725@1792@1989@2196@2318@2160@21215

You can use any language or method that you want as long as you show your workings.

If you need a hint, read this:
Code:
[COLOR=white]ASCII character codes[/color]


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Code:
[white]
Here is the code:

        Dim s As String = "560@684@777@824@1026@970@1199@1308@1313@1596@1725@1792@1989@2196@2318@2160@21215"
        Dim sa As String() = s.Split("@")
        Dim text As String = ""
        For i As Integer = 0 To sa.Length - 1
            If i < sa.Length - 1 Then
                text += Chr(Convert.ToInt32(sa(i)) / (i + sa.Length + 1))
            Else
                text += Chr(Convert.ToInt32(sa(i).Substring(0, sa(i).Length - 1)) / (i + sa.Length + 1))
            End If
        Next
        Debug.Write(text)


The input was: [b]$''/*242;>>BGHA>[/b]

[/white]
 
I must admit, I wasn't expecting that RiverGuy! The solution you came up with does seem to work correctly on all the examples I gave, however that wasn't the input string I used!

That must mean that there are multiple answers of what the input string could be, however that's a problem with the question not your answer!


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Input string starts with 'p', right?

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Yes [smile]


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Here's an edit then

Code:
[white]
        Dim s As String = "560@684@777@824@1026@970@1199@1308@1313@1596@1725@1792@1989@2196@2318@2160@21215"
        Dim sa As String() = s.Split("@")
        Dim text As String = ""
        For i As Integer = 0 To sa.Length - 1
            If i < sa.Length - 1 Then
                text += Chr(Convert.ToInt32(sa(i)) / (i + Convert.ToInt32(Microsoft.VisualBasic.Right(sa(sa.Length - 1), 1))))
            Else
                text += Chr(Convert.ToInt32(sa(i).Substring(0, sa(i).Length - 1)) / (i + Convert.ToInt32(Microsoft.VisualBasic.Right(sa(sa.Length - 1), 1))))
            End If
        Next
        Debug.Write(text)

Which nets you:  [b] programmerspuzzle [/b]
[/white]
 
That's what I was looking for!

There's a nice use of .NET to solve the puzzle. Let's see what other languages/methods can be used...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Code:
[white]<%	@Language="JScript" %>
<%
	var s = "560@684@777@824@1026@970@1199@1308@1313@1596@1725@1792@1989@2196@2318@2160@21215";
	var arr = s.split("@");
	var l = arr.length;
	
	for( var base = arr[l-1]%10, res="", i=0; i<l-1; i++, base++)
		res+= String.fromCharCode(arr[i]/base);
		
	res+= String.fromCharCode(Math.floor(arr[l-1]/10)/(base));
		
	Response.write( res );
%>[/white]

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Another nice example vongrunt [smile]


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm, A VB6 version, admittedly using the same logic used in previous answers [sad]
Code:
[white]Dim Puzzle As String
Dim Parts() As String
Dim Answer As String
Dim i As Integer
Dim CheckDigit As Integer

Puzzle = "560@684@777@824@1026@970@1199@1308@1313@1596@1725@1792@1989@2196@2318@2160@21215"

Parts = Split(Puzzle, "@")

CheckDigit = Right(Parts(UBound(Parts)), 1)

text = ""

For i = 0 To UBound(Parts) - 1
    Answer = Answer & Chr(Parts(i) / (i + CheckDigit))
Next
                
Answer = Answer & Chr(Left(Parts(i), Len(Parts(i)) - 1) / (i + CheckDigit))

Debug.Print Answer[/white]
The examples here involve a lot less code than the last function I saw to decode strings generated in the same way as these [wink]

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
[lol]


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thought you'd maybe try an attempt in .NET harleyquinn? [wink]


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Haha! [lol]

Cheeky! You know I'm only just learning .NET... [wink]

To be honest I did, but there was already a good .NET answer and mine looked too much like my VB6 one... [smile]

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
As I'm an unix guy, here an awk solution:
Code:
[white]awk -F@ '{
  k=substr($0,length);$0=substr($0,1,length-1)
  for(i=1;i<=NF;++i)printf "%c",$i/(k+i-1);printf "\n"
}' /path/to/programmerspuzzle
[/white]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Here's a VB function (using similar logic to previous VB solutions)
Code:
[white]Public Function PuzzleSolver(strPuzzle As String) As String
    Dim codechar As Variant
    For Each codechar In Split(strPuzzle, "@")
        PuzzleSolver = PuzzleSolver & Chr(Left(codechar, 4) / (Len(PuzzleSolver) + Right(strPuzzle, 1)))
    Next
End Function[/white]
 
Heres the encoding, decoding, and proof test in Visual FoxPro.

Code:
[white]CLEAR
DO ProveIt

*************************
PROCEDURE ProveIt()
	*************************
	* Show proof of Encode/Decode viability
	* Results are shown on screen
	*************************
	LOCAL lcPlainText, lcCipherText, loExc AS EXCEPTION
	TRY
		lcPlainText = 'tektips'
		lcCipherText = Encode(lcPlainText,8)
		?lcCipherText
		lcPlainText = Decode(lcCipherText)
		?lcPlainText
		?
		lcPlainText = 'puzzle'
		lcCipherText = Encode(lcPlainText,7)
		?lcCipherText
		lcPlainText = Decode(lcCipherText)
		?lcPlainText
		?
		lcPlainText = 'example'
		lcCipherText = Encode(lcPlainText,4)
		?lcCipherText
		lcPlainText = Decode(lcCipherText)
		?lcPlainText
		?
		?Decode('560@684@777@824@1026@970@1199@1308@1313@1596@1725@1792@1989@2196@2318@2160@21215')
	CATCH TO loExc
	ENDTRY
ENDPROC

*************************
FUNCTION Encode(tcPlainText, tnKey)
	*************************
	* Encode plaintext using perceived algorithm
	*************************
	LOCAL lcReturn, lnCharCounter, lnLenText, loExc AS EXCEPTION
	lcReturn = ''
	TRY
		lnLenText = LEN(tcPlainText)
		*!* Iterate through plaintext character by character
		FOR lnCharCounter = 1 TO lnLenText
			lcReturn = lcReturn + '@' + ;
				TRANSFORM(ASC(SUBSTR(tcPlainText, lnCharCounter, 1)) *;
				(tnKey + lnCharCounter - 1))
		ENDFOR
		*!* Strip extra @ symbol and concatenate key
		lcReturn = SUBSTR(lcReturn, 2) + TRANSFORM(tnKey)
	CATCH TO loExc
		THROW loExc
	ENDTRY
	RETURN lcReturn
ENDFUNC

*************************
FUNCTION Decode(tcCipherText)
	*************************
	* Decode ciphertext using perceived algorithm
	*************************
	LOCAL lcReturn, lnValCounter, lnALen, lnKey, loExc AS EXCEPTION
	lcReturn = ''
	TRY
		*!* Get key that was used to encode
		lnKey = VAL(RIGHT(ALLTRIM(tcCipherText),1))
		*!* Split tcCipherText on the @ symbol into an array
		lnALen = ALINES(aValues, LEFT(tcCipherText, LEN(tcCipherText) - 1), 4, '@')
		*!* Iterate through array to reconstitute plaintext
		FOR lnValCounter = 1 TO lnALen
			lcReturn = lcReturn + CHR(VAL(aValues(lnValCounter))/(lnKey + lnValCounter - 1))
		ENDFOR
	CATCH TO loExc
		THROW loExc
	ENDTRY
	RETURN lcReturn
ENDFUNC[/white]

boyd.gif

SweetPotato Software Website
My Blog
 
Here is SQL Version

Code:
[COLOR=white]
Create function dbo.DecodeMyOutput (@Puzzle varchar(1000), @Separator char(1) = '@')
returns varchar(50)
as
begin
declare @Parts Varchar (10),
	@Answer varchar(100), 
	@i int, 
	@CheckDigit int

select	@checkdigit = right(@Puzzle,1),
	@puzzle = left(@puzzle, len(@puzzle) -1) + @Separator,
	@i = 0

while charindex(@Separator, @Puzzle) > 0
begin

select @Parts = left(@Puzzle ,charindex( @Separator, @Puzzle) -1)

select 	@Answer = isnull(@Answer, '') + char(@Parts / (isnull(@i,0)+@CheckDigit) ),
	@i = @i +1,
	@Puzzle = right(@Puzzle, len(@Puzzle) - charindex( @Separator, @Puzzle) )
end

return (@answer)

end
[/color]

Code:
select dbo.[b]DecodeMyOutput [/b]('560@684@777@824@1026@970@1199@1308@1313@1596@1725@1792@1989@2196@2318@2160@21215', '@')

Regards,


"There are no secrets to success. It is the result of preparation, hard work, and learning from failure." -- Colin Powell
 
Hmm no 'split' or simular in Delphi. so most of this is breaking up the input string..
Code:
[white]
const S = '560@684@777@824@1026@970@1199@1308@1313@1596@1725@1792@1989@2196@2318@2160@21215';

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var sa: string;
    multi, offset,p, count, c: integer;
begin
   multi := strtoint(rightstr(S,1)); // get the last char
   p := 0;
   c :=0;
   repeat
      inc(p);
      Offset := posex('@',S, p);
      if offset = 0 then
         begin // we must handle the end bit
           sa := copy(s,p,10); // assume it wont be longer than this
           sa := leftstr(sa,length(sa)-1);// lopp off the last char
           count := 0;
         end
      else
         begin
           count := Offset - P;
           sa := copy(S, p, count);
           P := Offset;
         end;
      application.ProcessMessages;
      edit1.Text := edit1.Text + chr(strtoint(sa) div (multi + c));
      inc(c);
   until (count <=0);
end;
[/white]

Steve: Delphi a feersum engin indeed.
 
Since python seems to be my puzzle language of choice:
Code:
[COLOR=white]def decodeString(puzzleStr,result=""):
	for s in puzzleStr[:-1].split("@"): result += chr(int(s)/(int(puzzleStr[-1:]) + len(result)))
	return result[/color]

called like so:
Code:
print decodeString("560@684@777@824@1026@970@1199@1308@1313@1596@1725@1792@1989@2196@2318@2160@21215")
print decodeString("928@909@1070@1276@1260@1456@16108")
print decodeString("784@936@1098@1220@1188@12127")
print decodeString("404@600@582@763@896@972@10104")

-t

barcode_1.gif
 
Here's an assembly version for Linux in IA-32. The code is not really commented or optimized, just practice.

Code:
[white]
# start code
.section .text
.globl _start
_start:
	cmpl	$2, (%esp)
	jne	exit
	movl	8(%esp), %edx
count_loop_start:
	movb	(%edx), %al
	cmpb	$0, %al
	je	count_loop_end
	movl	%eax, %edi
	incl	%edx
	jmp	count_loop_start
count_loop_end:
	# Clear last item
	decl	%edx
	movb	$0, (%edx)
	andl	$0x0f, %edi	# conv ascii code to integer
	decl	%edi	# k is set

	# Go through list
	movl	8(%esp), %esi
ptr_loop_start:
	movl	$0, %eax
	movl	$0, %ecx
itm_loop_start:
	movb	(%esi), %cl
	cmpl	$0, %ecx
	jz	itm_loop_end
	cmpl	$0x40, %ecx
	je	itm_loop_end
	imull	$10, %eax
	andb	$0xf, %cl
	addl	%ecx, %eax
	incl	%esi
	jmp	itm_loop_start
itm_loop_end:
	incl	%edi	# ++k
	movl	$0, %edx
	divl	%edi

	# Print char of eax
	pushl	%eax
	movl	$1, %edx
	movl	%esp, %ecx
	movl	$1, %ebx
	movl	$4, %eax
	int	$0x80
	popl	%eax
	cmpb	$0, (%esi)
	jz	ptr_loop_end
	incl	%esi
	jmp	ptr_loop_start
ptr_loop_end:
	movl	$1, %edx
	pushl	$0xa
	movl	%esp, %ecx
	movl	$1, %ebx
	movl	$4, %eax
	int	$0x80
	popl	%eax
exit:
	movl	$0x12, %ebx
	movl	$1, %eax
	int	$0x80
# end code
[/white]
Code:
[white]
# assemble and link
as ans.s -o ans.o && ld -s ans.o -o ans
# run as
./ans 928@909@1070@1276@1260@1456@16108
./ans 404@600@582@763@896@972@10104
[/white]


Cheers,
ND [smile]

[small]bigoldbulldog AT hotmail[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top