How to change separator in a list from "," to ";" not using the join function or any list operations

user10336367

I have to set a method that returns a string from the content of a given object. I am able to make the string of the list of numbers but my professor wants the separator between each element to be a semicolon instead of a comma, which I am having trouble doing. I tried using the join function but I then realized that my professor does not want to use any list operations like len or in besides append and reading and writing to an index .

I have already tried to concatenate the semicolon to the string but of course, that only concatenates the semicolon at the end of the string and doesn't separates each element with a semicolon.

Here is my code

class LinkedList:
  def __init__(self, lst=[], size = 0):
    self.L = lst
    self.size = size
  def addLast(self, x):
    self.L.append(x)
    self.size += 1
  def removeFirst(self):
    L = self.L
    p = L[1:]
    return p
  def __str__(self):
    result = ''
    result += str(self.L)
    return result

Using this as a test case, you can see that it prints out as [1, 2, 3] instead of "[1;2;3]"

lst = LinkedList()
print(lst) # prints []
print(lst.size) # prints 0
lst.addLast(1)
lst.addLast(2)
print(lst.size) # prints 2
lst.addLast(3)
print(lst) # this should print "[1;2;3]"
Barmar

Use a loop to concatenate the list elements, appending ; between each.

def __str__(self):
    result = '['
    if self.size > 0:
        result += str(self.L[0]) # print first element normally
        for item in self.L[1:]: # print the rest, preceding each with ';'
            result += ';' + str(item)
    result += ']'
    return result

If you're not allowed to use for-in, you can use a while loop.

def __str__(self):
    result = '['
    if self.size > 0:
        result += str(self.L[0]) # print first element normally
        i = 1
        while i < self.size
            result += ';' + str(self.L[i])
            i += 1
    result += ']'
    return result

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using comma as list separator with AngularJS

Is there any haskell function to concatenate list with separator?

How to change the default separator of my list items?

Function to join the first 2 elements from each list in a list of lists

How to select a certain character from list which was created using a function

How to name a list of elements directly from the argument of the function using apply

Joining Strings from List into a Single String by Putting Separator in Between Them Using a Fold Function (No Recursion)

How to split a list item by separator and call a function on each of the seperated item?

Display list items with custom separator using print() function

How to split a string using any word from a list of word

Joining python string from list by using .join function is not outputing wanted result

How to return only the latest entries from a list using QUERY function?

How can I change the colour of separator in list of SwiftUI?

how to change a user defined function using list to pandas series

How to combine list operations?

Change the separator of the Solr synonyms list

using python function any() on a list of floats

How to join multiple boost ranges and return as a result from function w/o using boost::any_range

How to add 2 list from a function without using global variables?

Using input within a function to change element in a list

In Python, how to join a list of strings with a regular expression as separator without escaping with the backslash character

Python: Mathematical Operations using Operators from a list

How to convert a list into float for using the '.join' function?

how to change properties of list tile from its onTap() function in flutter?

Using whitespace as a list separator

How to convert a haskell List into a monadic function that uses list values for operations?

how to flatten 2D list and add a separator character using zip and list comprehension in python?

How to change the background color of an item in a list when using the function List.generate in flutter

How do I change list2 without changing list1 in from a function in Python?