Question

Python Expert, I need your help: I need to write a function(rev_int_rec) that take an integet...

Python Expert, I need your help:

I need to write a function(rev_int_rec) that take an integet number(num = 1234567) and retrive the last digit using (lastDigit = num % 10) and add it to a queue.

Remove each last digit from num using the % operator

Add each last digit to the queue, with each round of recursion reduce num by dividing by 10. Base case is when num is less than 10
# Base case: If num < 10
# Enqueue last digit and return

--------------------Queue()-----------------

class _QueueNode:
def __init__(self, item):
self.item = item
self.next = None

class Queue:

def __init__(self):

self._qhead = None

self._qtail = None
self._size = 0
def is_empty(self): 
return self._size == 0
def __len__(self):
return self._size
def enqueue(self, item):
node = _QueueNode(item)
if self.is_empty():
    self._qhead = node
else:
    self._qtail.next = node

self._qtail = node
self._size += 1
def dequeue(self):
node = self._qhead
if self._qhead is self._qtail:
    self._qtail = None
self._qhead = self._qhead.next
self._size -= 1
return node.item

For example:

lastDigit2 = number % 10

number = number // 10

lastDigit1 = number % 10

number = number // 10

lastDigit0 = number % 10

number = number // 10

reversed_number = lastDigit2 * Math.pow (10, 2) + lastDigit1 * Math.pow (10, 1) + lastDigit0 * Math.pow (10, 0);

reversed_number: 321

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class _QueueNode:

def __init__(self, item):

self.item = item

self.next = None

class Queue:

def __init__(self):

self._qhead = None

self._qtail = None

self._size = 0

def is_empty(self):

return self._size == 0

def __len__(self):

return self._size

def enqueue(self, item):

node = _QueueNode(item)

if self.is_empty():

self._qhead = node

else:

self._qtail.next = node

self._qtail = node

self._size += 1

def dequeue(self):

node = self._qhead

if self._qhead is self._qtail:

self._qtail = None

self._qhead = self._qhead.next

self._size -= 1

return node.item

#method to reverse the number using queue

def reverse(n):

q = Queue()

m=n

while(0<n):#while n is not empty

lastdigit = n%int(10)

#pushing it to queue

q.enqueue(lastdigit)

n=int(n/int(10))

s=0#reversing

while(0<m):

l = q.dequeue()

s=s*int(10) + int(l)

m=int(m/int(10))

return s

s = int(input("Enter number:"))

print("Reversed_number:",reverse(s))

output:

Enter number:123   

Reversed_number: 321   

  

  

...Program finished with exit code 0   

Press ENTER to exit console.   

//PLS give a thumbs up if you find this helpful, its helps me alot, thanks.

27 28 29 #method to reverse the number using queue 30 def reverse (n): 31 32 selt-size-= 1 return node.item q Queue() m-n while(0<n):#while is not empty n lastdigit n% int (10) #pushing it to queue q.enqueue(lastdigit) n-int (n/int (10)) 34 35 36 37 38 39- 40 41 42 43 44 sint 45 print(Reversed_number:,reverse(s)) 46 s-0#reversing while(0〈m): 1 - q.dequeue() s-s*int(10) +int(1) m-int (m/ í nt (10)) return s (input (Enter number:))

Add a comment
Know the answer?
Add Answer to:
Python Expert, I need your help: I need to write a function(rev_int_rec) that take an integet...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 3: Write a LinkedList method named contains, that takes a value as a parameter and...

    Python 3: Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display,...

    Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display, remove methods. You may use default arguments and/or helper functions. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list """ if self.head is...

  • Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list...

    Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list using node. (from chapter 24). Your code should utilize the selectionsort method from attached and create another method call selectionsort in linkedlist class. To demonstrate the usage of the selection sort method, you should manually create a link list of random integer (say of 5 numbers), and you need to demonstrate the use the selection sort to sorted the link list. Please submit your...

  • PYTHON: Conan is writing a module to contain different implementations of trees. After his first tree,...

    PYTHON: Conan is writing a module to contain different implementations of trees. After his first tree, the BinaryTreeclass, he wrote test code and is having problems understanding the error. Locate his problem and explain how you would fix it. class Node(object): def __init__(self, data=None): self.data = data def __str__(self): return "NODE: " + str(self.data)    class Tree(object): def __init__(self): self.root_node = None self.size = 0    def __len__(self): return self.size    def add(self, data): raise NotImplementedError("Add method not implemented.")    def inorder_traversal(self): raise NotImplementedError("inorder_traversal...

  • """ Add_to_front(self, val) and add_to_back(self, val) functions can be used which are already done in python...

    """ Add_to_front(self, val) and add_to_back(self, val) functions can be used which are already done in python class. Name of list is LList class node(object): def __init__(self, data, next=None): self.data = data self.next = next # Note: use the attributes directly; no setters or getters! class LList(object): def __init__(self): self._size = 0 self._head = None self._tail = None """ def set_data_at_index(self, idx, val): """ The value stored at index idx changes to val return True if the index was valid otherwise...

  • Finish each function python 3 LList.py class node(object): """ A version of the Node class with...

    Finish each function python 3 LList.py class node(object): """ A version of the Node class with public attributes. This makes the use of node objects a bit more convenient for implementing LList class.    Since there are no setters and getters, we use the attributes directly.    This is safe because the node class is defined in this module. No one else will use this version of the class. ''' def __init__(self, data, next=None): """ Create a new node for...

  • In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains,...

    In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains, insert, and normal_list methods. You may use default arguments and/or helper functions. The file must be named: LinkedList.py Here is what I have for my code so far. The methods I need the recursive implementations for will be bolded: class Node: """ Represents a node in a linked list (parent class) """ def __init__(self, data): self.data = data self.next = None class LinkedList: """...

  • from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked...

    from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked list. Note that this is considered a "private class", one which is only meant to be used in this module by the LinkedList class, but not by client code. === Attributes === item: The data stored in this node. next: The next node in the list, or None if there are no more nodes. """ item: Any next: Optional[_Node] def __init__(self, item: Any) ->...

  • Python question. i have to start from an empty linked list, using the method addNodeEnd() to...

    Python question. i have to start from an empty linked list, using the method addNodeEnd() to add the nodes containing the values (3*i+5)%17, where i is from 0 to 10. Then print the values of all the nodes in this linked list to the screen. This is the code that i created right here and i need help checking if i made any mistakes thanks! The code is below: class Node: def __init__(self, data): self.data = data self.next = None...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT