Data Structures in Python: Stack -- Convert Integer to Binary

preview_player
Показать описание
Problem: Use a stack data structure to convert integer values to their corresponding binary representation.

This video is part of the "Data Structures" series. The solutions in this series focus on the Python language:

The stack class we defined in a previous video may be found here:

The code used in this video may be found here:

Website:
Рекомендации по теме
Комментарии
Автор

My friend for the moment you the best teacher I ever had on youtube, just finished Algorithms and move to Data Structures, in 2 days learned more than a few months with an online university... :D <3

xmrshadyxx
Автор

Thanks so much! After learning from you I implemented this solution. It takes up less memory and uses 1 while loop.
def int_to_bin(num):
in_bin = ""
while num > 0:
in_bin = str(num%2) + in_bin
num //= 2
return int(in_bin)

pelagiamajoni
Автор

Finally I found the only best resource to learn data structure. Thanks for your heart!!

jetra_h
Автор

We need this channel to blow up! Im sharing this with all my cs colleagues!

rumikinss
Автор

Hey, you're one of the best and definitely the coolest person ( evident from your projects and variety of content on this channel and your website) out there! Just like the thousands of "Thank You's" that you receive, here's another one ---- Thank You! ^_^

And also, I think there's an edge case missing in the code. Consider the input number as 0. Then it'd print out nothing. So it's better to add that corner case. All in all, this playlist is awesome! Would you be updating it anytime soon with more DS and Algos.?

damercy
Автор

thanku u lucid alot ur data structure tutorial give's me a ton of energy to give a pace in my programming and i did this from my side but without stack


def div(dec_num):


bin_no = ""
while dec_num > 0: # it works till dec_num become zero
r = dec_num % 2 # remainder will count
print(r)
bin_no = str(r) + bin_no # here we insert the remainder in our binary list
dec_num = dec_num // 2 # here we take next dec_num
return int(bin_no)


print(div(142))

Hitesh-Salgotra
Автор

THANK YOU SIR FOR THIS AWESOME LECTURE

code
Автор

Yo Atlast i found the best DS in Python
Thanks
😊

vinayakgs
Автор

Nice lesson but I think it'd be better to check if dec_num == 0 inside of while True loop so you'd be able to handle the situation when input dec_num == 0. Anyway thanks for you videos

ДмитрийВоробей-съ
Автор

Hello, why is my answer giving me I followed your code. I think the problem is with my pop function but I don't understand what exactly is wrong. BTW I use PyCharm IDE.

jitendradude
Автор

Also, why do we save bin_num as a string?

ROCLife
Автор

isn't it more efficient to build the binary representation of the number with a number variable (num*10+digit) vs appending strings (due to repeated dynamic memory allocations)?

idan
Автор

class Stack:
def __init__(self):
self.stack=[]
def push(self, data):
return self.stack.append(data)
def pop(self):
return self.stack.pop()
def top(self):
return self.stack[-1]
def emp(self):
if self.stack==[]:
return True
else:
return False
def get(self):
return self.stack
def convert(stack, num):
while num>0:
c=num
bina=c%2
stack.push(bina)
num//=2
return stack.get()
stack=Stack()
print(convert(stack, 27))

khaihoang
Автор

Why do we need the "while not s.is_empty" statement? can't we just use a for loop?

ROCLife
Автор

class bin_test:
def get_bin(self, num):
s = StackThree()
while num !=0:
bin = num % 2
num = num // 2
s.push(bin)
return s.get_items()[::-1]

test4 = bin_test()
print(test4.get_bin(242))

bharatapar
Автор

dec_num //= dec_num, that's prettier

maxbranco