I just started reading the book "Elements of Programming Interviews in Python" and I'm starting to worry the book is a little bit too advance for me. I've been coding in Python for about a year now and I'm trying to get take my programming a lot more serious, which is why I picked up this book.
I was thinking about reading the book "Fluent Python" instead if this book was too advance for me.
Do you think if I put in enough time and work into this book, I would be able to understand the concepts? Or is it too advance material for a beginner like myself to understand?
Hey, r/cscareerquestions! I'm unsure if content like this is allowed here, but after reading the rules it sounds like it is, as long as the content isn't mine (it isn't) and helps others (I think it will.)
This last weekend I found myself about to board a several hour long flight to an onsite interview. Not wanting to waste time, I looked around quite a bit for resources that I could use offline on the flight, and stumbled upon http://www.programcreek.com/wp-content/uploads/2012/11/coding-interview-6.pdf. It has hundreds of common problems (from Leetcode) along with their solutions.
Hi. I have read Introduction to Algorithms but I can't solve Leetcode problems. Should I read Elements of Programming Interviews to complement or should I study solutions of Leetcode problems to be able to solve Leetcode problems?
Working on a function that checks the parity of binary
but I'm not understanding some of the characters they are using i haven't seen this before in python
def parity(x : int) -> int:
result = 0
while x:
result ^= x & 1
x>>= 1
return result
So first line I'm not understanding is the x : int what's that about
and then subsequently the
result ^= x & 1
x>>= 1
Lines
Can anyone explain what this does i haven't seen them before in python any help would be appreciated thanks
x: int is type hinting. Just telling the linter that x is supposed to be an int. This can help with autocompletion and warnings. It doesn't usually do much else in the standard python libs.
The other ones are bitwise operations.
https://en.wikipedia.org/wiki/Bitwise_operation
https://wiki.python.org/moin/BitwiseOperators
The first is a bitwise xor and bitwise and. The second is a bit shift to the right. There are tons of resources online to read about for bitwise operations so I won't go over them in detail, likely not explaining as well.
First, let's fix your formatting:
def parity(x : int) -> int:
result = 0
while x:
result ^= x & 1
x>>= 1
return result
*cracks knuckles*
def parity(x : int) -> int: starts the function definition, obviously, and the parts that confused you are type hints. In this case they indicate that x should contain an integer value, and that the function's return value is an integer. Note: Python doesn't enforce these, the hints are helpful for tools like mypy which helps you to find potential type errors, and for readability.
result ^= x & 1 uses two bitwise operations, in this case bitwise AND (&) and bitwise XOR ("eXclusive OR", ^). In layman's terms the line checks if x is an odd number, and flips the value of result if the two are different. result is either 0 or 1.
Finally, x>>= 1 is a bit shift operation. In this case it's the same as dividing x by two.