I'm having problems with my teacher's colon.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2188 Views - Last Post: 19 September 2012 - 10:45 AM

#1 Midwest Product  Icon User is offline

  • D.I.C Head

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

I'm having problems with my teacher's colon.

Posted 18 September 2012 - 06:55 PM

I was given a hashing algorithm to manipulate today, but I'm not quite sure of the syntax in some parts. I believe it is C/C++ (it probably doesn't really matter though). Anyways, there are a few instances where a colon is used in an array manipulation (I think?) as some sort of shortcut notation.

The lines that I am not sure about are commented.

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)



Thanks for the help! I'm sure that this will clear up the issues that I am having understanding this algorithm.

Is This A Good Question/Topic? 0
  • +

Replies To: I'm having problems with my teacher's colon.

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 07:00 PM

Are you sure this is C/++ ? Because the for and if look like Python, although the comments don't. Also, the semi-colon is used to slice lists in Python. I may be wrong, but that's my two cents.

Quote

I believe it is C/C++ (it probably doesn't really matter though)

Won't it matter if you entered your neighbor's house instead of your own?
Was This Post Helpful? 2
  • +
  • -

#3 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 07:08 PM

I'm pretty sure this one's Python. By the way, thread title is contender for best of the year. ^^
Was This Post Helpful? 2
  • +
  • -

#4 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 07:13 PM

Just a second, this can't be Python. Look at line 14 of OP's code.
for block_32 in input:
    hash ← hash xor block_32
    if hash[0] % 2:
        for i in [0, 32):
hash[i] ← (hash[i] + 42) % 256 //I mean this line
    if hash[1] % 3:


As you can see, before there is indentation because of ifs and fors. But suddenly, the indentation vanishes for one line, and then comes back again. When I used it, Python used to throw an error if I tried something like this.

This post has been edited by aresh: 18 September 2012 - 07:13 PM

Was This Post Helpful? 0
  • +
  • -

#5 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 07:26 PM

I bet it's because of the formatting, nothing more. The slicing of the array is used in Python and the colons after the if's and for's are definitely used in Python. The printing part is also Python.
Was This Post Helpful? 0
  • +
  • -

#6 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 08:01 PM

Well, I'm not too sure about the arrow part. Is it also a part of Python?
Was This Post Helpful? 0
  • +
  • -

#7 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 08:18 PM

I'm not even sure there's a programming language that uses an arrow symbol done like that. Google says Haskell has one, but it uses a dash and a compare symbol to do the arrow (->, -<). Maybe the code's copied from the Python cmd line?
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,531
  • Joined: 05-May 12

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 08:28 PM

Is the range on line 12, also supported in Python? [0, 32)
Was This Post Helpful? 0
  • +
  • -

#9 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 08:30 PM

I don't think so. As far as I know, you have to write
for i in range(0,32):

But well, I'm not too sure.
Was This Post Helpful? 0
  • +
  • -

#10 Midwest Product  Icon User is offline

  • D.I.C Head

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

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 08:36 PM

I guess the language isn't really important, I just need to understand what the colon is doing so I can implement it. The other use of the colons do look like python...I will look into it.
Was This Post Helpful? 0
  • +
  • -

#11 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 08:41 PM

Well, as I said, the language matters. Can some mod move this to the Python section, if DIC has one?
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8363
  • View blog
  • Posts: 31,059
  • Joined: 12-June 08

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 08:45 PM

Moving to python.. try and keep the languages in their right area.. and if you need it moved just use the 'report' button and type in where you think it should go.
Was This Post Helpful? 0
  • +
  • -

#13 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 08:54 PM

View PostSkydiver, on 19 September 2012 - 03:28 AM, said:

Is the range on line 12, also supported in Python? [0, 32)


Could it be a mistake? I can't find anything that closes that bracket or opens that closing parenthesis. aresh is actually right about the usage of range in Python.
Was This Post Helpful? 0
  • +
  • -

#14 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 3,031
  • Joined: 21-June 11

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 09:01 PM

View PostMidwest Product, on 19 September 2012 - 05:36 AM, said:

I guess the language isn't really important, I just need to understand what the colon is doing so I can implement it. The other use of the colons do look like python...I will look into it.


Of course the language matters. Different notations mean different things in different languages. There's probably a language out there where writing hash[:10] means "delete my hard drive" (or maybe not, but you get my point, I hope).

Since this is almost certainly pseudo code, the colon notation means whatever the person who wrote it intended it to mean.

If this were Python, the hash[:10] and hash[10:] constructs would be list slices, as aresh mentioned. Since this meaning makes sense in the context of the algorithm, there's a good chance that that's indeed the intended meaning. The only way to know for sure though would be to ask your teacher.

View PostfromTheSprawl, on 19 September 2012 - 05:54 AM, said:

Could it be a mistake? I can't find anything that closes that bracket or opens that closing parenthesis.


It's a common notation for half-open intervals in maths.

This post has been edited by sepp2k: 18 September 2012 - 09:26 PM

Was This Post Helpful? 4
  • +
  • -

#15 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: I'm having problems with my teacher's colon.

Posted 18 September 2012 - 10:06 PM

Oh, math. That explains why I can't place it. Thanks dude! ^^
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2