# Understanding a simple slice

Page 1 of 1

## 4 Replies - 775 Views - Last Post: 24 September 2012 - 12:40 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=292994&amp;s=a1e0ac89aec4396817724fcdfd3c52c7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Midwest Product

Reputation: 2
• Posts: 74
• Joined: 05-February 10

# Understanding a simple slice

Posted 23 September 2012 - 10:04 PM

Hello, I need help verifying my understanding for a simple slice so I can write it in a C++ program. I don't believe C++ uses a slice mechanism like Python does.

Here is the statement, where key is a string of 32 characters, and input is a string of unknown length entered by the user.

if length(input) == 0 || length(input) % 32:
input += key[:32-length(input)%32]

The purpose of this statement is to ensure that the input string is 32 bytes (or a multiple of 32), and if not, it will proceed to append the necessary number of bytes, taken from key.

I get that much, but what I don't understand is specifically how this statement works:

input += key[:32-length(input)%32]

What I do understand is that the first 32 characters of key are being examined (however, the key is only 32 bytes long, so that doesn't really matter). However, what gets subtracted, and from which end of the string? Say for example, the key is "Hamilton's awesome hash function" (which is 32 characters), and I need 5 characters to make 32, will I get "Hamil" or will I get "ction" from this statement?

I need to know this so I can recreate it in C++. I would compile and run the code given, but the whole program is just Python pseudocode.

This post has been edited by Midwest Product: 23 September 2012 - 10:05 PM

Is This A Good Question/Topic? 0

## Replies To: Understanding a simple slice

### #2 fromTheSprawl

• Bloodborne

Reputation: 521
• Posts: 2,102
• Joined: 28-December 10

## Re: Understanding a simple slice

Posted 23 September 2012 - 10:14 PM

32-length(input)%32

Is evaluated as 32 minus the length of input % 32. When slicing, if the arguments is at the right side it will get everything starting from 0 to the right side argument.

"apple"[:3] #prints 'app'

Also, install Python on your machine. You can just try statements and not write whole classes to test something out, like what I did with the apple. Maybe you'll learn Python too.

### #3 atraub

• Pythoneer

Reputation: 827
• Posts: 2,231
• Joined: 23-December 08

## Re: Understanding a simple slice

Posted 23 September 2012 - 10:25 PM

it's not length, it's len. The general syntax of a slice is:
mySequence[start(inclusive):stop(exclusive):step]

If I leave any part blank, it'll put in a default value. Leaving start blank makes the sequence start from the beginning, leaving stop blank will make the sequence stop at the end, leaving step blank makes it step by '1'.

Definitely install python and play with it.

### #4 Midwest Product

Reputation: 2
• Posts: 74
• Joined: 05-February 10

## Re: Understanding a simple slice

Posted 24 September 2012 - 09:44 AM

I guess I didn't think of an order of operations hierarchy. It makes sense now that I think of it as
[:(32-length(input)%32)]

Hopefully that is right

• Saucy!

Reputation: 6237
• Posts: 24,002
• Joined: 23-August 08

## Re: Understanding a simple slice

Posted 24 September 2012 - 12:40 PM

The key here, is less about understanding the "slice", but understanding what you're trying to do, which is told to you in your original topic on this:

input ← “string to be hashed”
key ← “Hamilton's awesome hash function”
//pad input to ensure the length is a multiple of 32
//this will append the necessary number of bytes from
// the beginning of the key
if length(input) == 0 || length(input) % 32:
input += key[:32-length(input)%32] // HERE IS AN INSTANCE!!!
hash ← key
for block_32 in input: //break input into 32-byte blocks
hash ← hash xor block_32
if hash[0] % 2:
for i in [0, 32):
//increment each byte by 42 (modulo 256)
hash[i] ← (hash[i] + 42) % 256
if hash[1] % 3:
//move the first 10 bytes to the end
hash ← hash[10:] + hash[:10] // HERE IS ANOTHER INSTANCE!!!
//your hash may contain non-printable characters, so make
// sure you hex encode it
print hex(hash)

This!

Quote

//pad input to ensure the length is a multiple of 32
//this will append the necessary number of bytes from
// the beginning of the key