Latest Questions tagged Algorithm

Browse all latest questions tagged Algorithm

47
Votes
Published 19 Sep, 2022
Java - Kotlin: java.lang.UnsupportedOperationException in MutableList add element

I'm implementing a stack algorithm for study purpose in Kotlin class Stack<T:Comparable<T>>(list:MutableList<T>) { var items: MutableList<T> = list fun isEmpty():B...

92
Votes
Published 07 Sep, 2022
Java - detect last foreach loop iteration

Supposing that I have some foreach loop like this: Set<String> names = new HashSet<>(); //some code for (String name: names) { //some code } Is there a way to check inside foreach...

58
Votes
Published 25 Aug, 2022
Python - Solving a cubic equation

As part of a program I'm writing, I need to solve a cubic equation exactly (rather than using a numerical root finder): a*x**3 + b*x**2 + c*x + d = 0. I'm trying to use the equations from here. How...

21
Votes
Published 23 Apr, 2022
Javascript - Implementation of Luhn algorithm

I am trying to implement simple validation of credit card numbers. I read about the Luhn algorithm on Wikipedia: Counting from the check digit, which is the rightmost, and moving left, double the va...

87
Votes
Published 24 Aug, 2022
Python - Hungarian algorithm: multiple jobs per worker

Is there an extension of the Hungarian algorithm that caters for the assignment of multiple jobs per worker? In its simplest form, the algorithm assigns a single job to a single worker. My applicati...

15
Votes
Published 01 May, 2022
Python - How can I check Hamming Weight without converting to binary?

How can I get the number of "1"s in the binary representation of a number without actually converting and counting ? e.g. def number_of_ones(n): # do something # I want to MAKE this FA...

95
Votes
Published 01 Sep, 2022
Java - Efficient data structure that checks for existence of String

I am writing a program which will add a growing number or unique strings to a data structure. Once this is done, I later need to constantly check for existence of the string in it. If I were to use...

2
Votes
Published 23 Apr, 2022
Javascript - Does any change in an array gets to change the entire array?

I got into that question by thinking about sorting algorithms. Does changing an element position inside an array would be the same, at the interpretation, compilation or run-time phases, to recreate...

95
Votes
Published 19 Aug, 2022
Python - Finding shortest combinations in array/sequence that equals sum

I'm totally stuck and have no idea how to go about solving this. Let's say I've an array arr = [1, 4, 5, 10] and a number n = 8 I need shortest sequence from within arr which equals n. So for...

26
Votes
Published 24 Aug, 2022
Python - Rock Paper Scissors - get winner mathematically

I just finished my rock paper scissors game in python. Right now, the user has to chose between three buttons, and a string user1 is set to either "Rock", "Paper", or "Scissors". And when I evaluate t...

29
Votes
Published 26 Apr, 2022
Python - Efficient calculation of Fibonacci series

I'm working on a Project Euler problem: the one about the sum of the even Fibonacci numbers. My code: def Fibonacci(n): if n == 0: return 0 elif n == 1: return 1 else:...

49
Votes
Published 19 Aug, 2022
Python - Codility Genomic Range Query

I recently discovered Codility and I'm going on with the demo training. I wrote this solution to the Genomic Range Query problem, it works fine, solution is provided with dynamic programming, but it s...

45
Votes
Published 29 Jun, 2022
Python - Vectorize finding closest value in an array for each element in another array

Input known_array : numpy array; consisting of scalar values only; shape: (m, 1) test_array : numpy array; consisting of scalar values only; shape: (n, 1) Output indices : numpy array; shape: (n,...

83
Votes
Published 25 Aug, 2022
Python - Breadth first search implementation in Python

I'm attempting to implement BFS in Python, I understand how the algorithm works but I don't have much programming experience. I've spent hours thinking about the best way to represent everything and h...

82
Votes
Published 06 Sep, 2022
Java - Count total subsequences whose sum is divisible by k

I am trying to write a DP solution for the problem: count total number of sub-sequences possible of an array whose elements' sum is divisible by k. I have written the following solution. But it is no...