Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary_linkedlist_to_Decimal #716

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ If you want to uninstall algorithms, it is as simple as:
- [swap_in_pairs](algorithms/linkedlist/swap_in_pairs.py)
- [is_sorted](algorithms/linkedlist/is_sorted.py)
- [remove_range](algorithms/linkedlist/remove_range.py)
- [binary_linkedlist_to_decimal](algorithms/linkedlist/binaryToDecimal.py)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rename the file using snake case

- [map](algorithms/map)
- [hashtable](algorithms/map/hashtable.py)
- [separate_chaining_hashtable](algorithms/map/separate_chaining_hashtable.py)
Expand Down
29 changes: 29 additions & 0 deletions algorithms/linkedlist/binaryToDecimal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# converts binary linked list to decimal value
# Eg : Linked List (Input): 1 -> 1 -> 1
# Decimal value (Output): 7


#to represent each node of Linked List
class Node:
def __init__(self,data):
self.data = data
self.next = None
#to create linkedlist

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to pep8, there should be two empty lines between classes.

class LinkedList:
def __init__(self):
self.head = None

def getDecimal(self,head):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use snake case naming convention

ans = 0
while head:
ans = (ans * 2) + head.data
head = head.next
return ans

list = LinkedList()

list.head = Node(1);
list.head.next = Node(1);
list.head.next.next = Node(1);

print ("Decimal Value : {}".format(list.getDecimal(list.head)))