FAQ: Binary Search Trees: Python - Introduction

This community-built FAQ covers the “Introduction” exercise from the lesson “Binary Search Trees: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Pass the Technical Interview with Python

FAQs on the exercise Introduction

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why is does this hint have a semicolon?

instance_name = className(argument);

Why is it valid?
| ((@mtf)) |

A semi-colon is valid in Python, but it also signals another statement on the same line, which this one does not, so I don’t have an answer as to why it is there. Just know it is not invalid, however superfluous in this instance.

>>> u = lambda x: x ** 3; v = range(1, 9); w = [*map(u, v)]; w
[1, 8, 27, 64, 125, 216, 343, 512]
>>> 
1 Like

I’ve seen that before when I you were explaining conditional expressions and I found 1 line for loops with multiple statements separated by semicolon. But I would not write a semicolon because it is rather uncommon when there is not multiple statements plus it makes you type more.

Why?

I’ve also been starting to instantiate multiple instance variable in one line like this:

self.some1, self.some2 = val1, val2

Is this recommended?

Why not? It is a value sequence assignment. However there is no comparing this to the previous question.

What about lets see which one is shorter:
Value Sequence Assignment:

one = 1 two = 2

:
15 keys needed(including spaces, letters, signs, numbers, newlines and characters).

one, two = 1, 2

:
15 keys needed.
Same typing so I’ll chose which one I prefer. I’ll use sequence assignment sometimes which is less common. But the first one is more understandable or readable.
Do you think that sequence assignment is better(in general) than normal variable assignment? When should sequence assignment be used and when normal assignment?
I mean the advantages and disadvantages of both.
I just saw this site:
https://www.programiz.com/python-programming/variables-constants-literals
.


Look at this helpful image which is sequence assignment(first one). What is the second thing called?

In our earlier example we are assigning a sequence to variables. If the unpack is unsuccessful it will raise an error.

The thing to make note of is that assignment is from right to left.

When there are multiple assignments of the same value, it comes from right to left.

>>> a = b = c = 1
>>> s = a, b, c
>>> s
(1, 1, 1)
>>> a = 0
>>> s = a, b, c
>>> s
(0, 1, 1)
>>> 

This shows that while we can give all three variables the same initial value, it does not preclude independent assignment ad hoc, afterward.


Let’s see…

>>> a = b = c = []
>>> a.append(1)
>>> b.append(2)
>>> c.append(3)
>>> a
[1, 2, 3]
>>> b
[1, 2, 3]
>>> c
[1, 2, 3]
>>> 

What does this teach us?

It teaches us the even though a, b, c are variables but they were all a list and share the same link. Does a, b and c share the same memory and id location?

I don’t understand the example. I don’t see that it comes from right to left. Show me what part/parts are from right to left.

Do you mean the first or second?


.

Only as long as they are all the same value.

So the if you assign different variables to the same value they always have the same memory and id location in the computer/device. Is that absolutely true?

In Python, what is true is that numeric values are not stored twice in memory. If two variables have the same assignment, then it will be in one place in memory.

Do numeric values share the same link. What about other data structures/types?

We would need to devise some tests. My expectation would be that only numbers show this behavior. If ‘a’ and ‘b’ are given the same string value, they will not have the same id. This suggests that if we could scan through the code in memory, the ids are the physical location (address) of the string literals being assigned.

It’s not a link, as such. More Python’s way of treating numbers in memory. No duplicates, period. So literals in code are still not necessarily given an id that corresponds with their address. The first instance of a number will be the one with the id.

It is my understanding that roughly the first 900 or so positive integers are hard coded into the language core. On any given machine we could test this by seeing if the ids are sequential for a sequence of numbers. (My assumption, not something I know; it’s an hypothesis, is all.)

Keep in mind that integers use 32 bytes of memory.

>>> a = 1
>>> b = 2
>>> id(a)
140713570715296
>>> id(b)
140713570715328
>>> 

Notice the addresses are exactly 32 bytes apart.

>>> c = 10
>>> id(c)
140713570715584
>>> id(c) - id(a)
288
>>> 

As expected. So those numbers do appear to be fixed in memory, in sequence.

>>> f = 100 - 90
>>> id(f)
140713570715584
>>> 

What we need is a test of values up to the point where this breaks down (an address is far different from the expected). That will give us the limit of the built in integers. Think with your massive amount of learning you can tackle this? Can’t wait for the result.


Give up? Here’s my first attempt…

>>> x = 0
>>> y = 1
>>> while id(y) - id(x) == 32:
	x += 1
	y += 1

	
>>> x
256
>>> y
257
>>>  

We cannot conclude anything, but for certain the first 256 integers are in memory, in sequence.

Where I got that 900 number is so far out of my day to day memory that it does come into question. However, if memory serves, the number was not 256. Some more knowledge is needed here, or investigation to come up with a revised algorithm for determining the exact number of hard coded integers there are in Python.

1 Like