Python Forum
Little help with my code - Linked List
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Little help with my code - Linked List
#1
Star 
Hi everyone Big Grin , I'm Santiago,

I have a small problem that I cannot detect in my code Confused .

It is a homework from the university Cool .

I have all the code written and it works "fine" Sleepy .

As you can see, the final result is correct, but until the program reaches it makes many prints that I do not understand why he does them Doh .

If someone could help me I would appreciate it.

I leave the homework statement and the code I have so that you can check and see where it fails.

Thanks a lot! Heart Heart


The exercise is as follows:

Write a python program, which reads two strings S1 and S2. You can consider the input strings are not empty (contain at least one character). The strings are then converted to the linked lists L1 and L2 – the nodes of linked lists contain individual characters of the original strings. The program then finds (and prints) the number of occurrences of the list L2 in the list L1.

Examples:

Input: alphabetical
al

Output: 2

The input strings S1 is “alphabetical” and S2 is “al”. The linked lists L1 and L2 are therefore.

Input: alphabetical
ax

Output: 0

Thank you very much for your help, and I am very sorry for the problems and for not understanding the linked list.

The code I have:
# node implementation
s = input('enter the full string: ') 
s_sub =input('enter the character to match: ')

class node:
     def __init__(self, x):
          self.value = x
          self.next = None

def moving_onto(L):
     while L is not None:
          print(L.value, end = " ")
          L = L.next
     print()
     
count = 0
for i in range(len(s)):
     L = node(s_sub)
     end = L
     if s[i:i+len(s_sub)] == s_sub:
          count +=1
          end.next = node(s_sub)
          end = end.next
     print("no of sub_string: ", count)
Reply
#2
I think that if you just remove the indention from the line print("no of sub_string: ", count) so that it is no longer a part of the for loop that it will behave the way that you expect it to.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How can I create a linked list that contains each folder with its files? noahverner1995 8 2,411 Dec-25-2021, 10:46 PM
Last Post: noahverner1995
  Linked List - Ordering by largest population with country name. PaleHorse 2 2,941 Jun-16-2020, 09:04 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020