data-structures

Why does my while loop calculate incorrect value of the string?

Why does my while loop calculate incorrect value of the string? Question: I am trying to find greatest length of a word from the string return it by using values of each letter from alphabets by assigning each letter it’s value as per it’s rank . So for example For a string s = ‘abcd …

Total answers: 3

Multiplying Values from two different Dictionaries

Multiplying Values from two different Dictionaries Question: I have two dictionaries which I need to multiply and get the total of, both with the same keys (I need the total of the stock and price of some items). # Create a list "menu" with 4 items menu = ["sandwich", "burger", "fish", "chips"] # Create a …

Total answers: 4

Optimizing performance for max sum in list-of-lists for original list

Optimizing performance for max sum in list-of-lists for original list Question: I am working on my python, doing codewars. The description is as follows: The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers: max_sequence([-2, 1, -3, 4, -1, 2, 1, -5, 4]) …

Total answers: 2

Python variable references

Python variable references Question: New to Python and picking up some practice on DSA problems though I have previously only worked in Java. Why does option 2 work and option 1 does not? I have seen Python passes by object reference and suspect the answer is to do with using references of references. Option 1 …

Total answers: 1

A function that returns a string which resemples a playing board in python

A function that returns a string which resemples a playing board in python Question: I want to write a function that returns a square playing board containing ‘*’ and ‘ ‘ based on the input number. The intended output should be like this: board_of_5 = ( ‘ * * n’ ‘* * *n’ ‘ * …

Total answers: 2

why the pointer is not incrementing at line 27

why the pointer is not incrementing at line 27 Question: i am implementing the trie data structure, why my pointer is not incrementing at line# 27. all the characters are getting into forst node only. this is my code class Trienode: data:str next:list = [None]*26 isTerminal:bool = False def __init__(self,data): self.data = data class Trie: …

Total answers: 1

How to sort an already sorted array by a second property?

How to sort an already sorted array by a second property? Question: Lets say I have a list of tuples, and by calling sort where key = first element, our list comes back as: [(1, ‘e’), (2, ‘d’), (3, ‘c’), (3, ‘a’), (3, ‘b’), (4, ‘f’)] Now, how do I apply a sort that applies …

Total answers: 1

I have a problem with syntax in nodes (data Structure)

I have a problem with syntax in nodes (data Structure) Question: This code should create a node but I have a problem with it I tried to fix it but I couldn`t I want to know why there is a problem at in the Linked_List (Next_Node) that is what show in the error "(Cannot assign …

Total answers: 2

Insert two or more values at same index of list

Insert two or more values at same index of list Question: Not asking particularly about the slicing, Basically I want to replace the value of specific index of list by 2 or more elements. For Example: list_a = [1, 2, 3] and list_b = [4, 5] so what I want is result = [4, 5, …

Total answers: 2

Algorithm to calculate number of child per each parent from excel file

Algorithm to calculate number of child per each parent from excel file Question: I have an excel file containing 2 columns & 763 row, screenshot : parent-child file Those strange strings is just a code for a mobile sites. As a description, this file has in both columns a mobile sites names, and as you …

Total answers: 1