Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, the python program needed to do the following: Define a function length that

ID: 3733537 • Letter: H

Question

Hi, the python program needed to do the following:

Define a function length that expects a singly linked structure (the head of the structure) as an argument. The function returns the number of items (nodes) in the structure.

Define a function printStructure that expects a linked structure as an argument. The function prints each item in the structure. The function does not return a value.

Define a function insert that inserts an item into a singly linked structure in the correct position, so that the structure is always kept in ascending order (alphabetical). The function expects two arguments: the item and the linked structure (which may be empty). The function returns the modified linked structure.

Now it does do what it is supposed to, until capital letters are involved. It organizes Ana before algorithm, when it is supposed to organize algorithm before Ana. When it is all small caps, it organizes algorithm before ana. Why does the program do this?

This is the code:

from node import Node

def length(head):

probe = head
count = 0
while probe:
probe = probe.next
count +=1
return count


def insert(newItem, head):
  
newNode = Node(newItem)
if head == None:
head = newNode
else:
if newItem < head.data:
newNode.next = head
head = newNode
else:
probe = head;
while probe.next and probe.next.data<newItem:
probe = probe.next
newNode.next = probe.next;
probe.next = newNode
return head


def printStructure(head):
  
probe = head
answer = ""
while probe:
answer = answer + probe.data + " "
probe = probe.next
print (answer)


def main():

head = None
userInput = input('Please enter a word (or just hit enter to end): ')
while userInput != '':
head = insert(userInput, head)
userInput = input('Please enter a word (or just hit enter to end): ')
print('The structure contains', length(head), 'items:')
printStructure(head)

if __name__ == "__main__": main()

Here is the code for the node:

"""
File: node.py
Node classes for one-way linked structures and two-way
linked structures."""
class Node(object):
def __init__(self, data, next = None):
"""Instantiates a Node with default next of None"""
self.data = data
self.next = next

Explanation / Answer

Program is behaving correctly as A is smaller than a in terms of ascii values so
Ana comes before algorithm and when both are lowercase then algorithm comes after ana.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote