algorithm

A*(Star) Prolog

泪湿孤枕 提交于 2021-02-08 05:56:19
问题 I have an A*(Star) Algorithm in prolog, and I need receive a list of destinations instead a single destination, and it should past for every element of the list and then back to the start. NOTE: It can pass in the same place twice. I tried it, but Swi-prolog returns false everytime, can I get any help? The code above receive a single destination, and it works, but as I said I need a list of destinations, and pass through them all. Example: astar(1,[2,2,4,6,8,9,10,13,15],C,P). /*area(Number

HackerRank Product Distribution

这一生的挚爱 提交于 2021-02-08 05:56:08
问题 I have been trying to solve this challenge, and somewhat figured out what to do. But after all my attempts, I am able to pass only the test cases, and one more case in the Submit Panel. All fails after that PROBLEM: A company has requested to streamline their product allocation strategy, and given n products, each of which has an associated value, you are required to arrange these products into segments for processing. There are infinite segments indexed as 1, 2, 3 and so on. However, there

python recursion list combinations without using generator

浪子不回头ぞ 提交于 2021-02-08 05:45:18
问题 I am learning python3. To think more about recursion, I want to implement a function comb(n, k) that returns a list consisting of all the combinations of kk elements out of a set {1,2,…,n}. I think it's not wise to use the loop since the number of the nested loop depends on k. So I consider it with recursion. I try to write the function inspired by This question while I can't get the right answer. def combinations(sub, data_set, index, still_needed): if still_needed == 0: return sub for i in

Hill climbing problem requiring Dynamic programming

人走茶凉 提交于 2021-02-08 05:44:22
问题 So, basically the problem is that there is a series of hill points. we can only move from one hill point to the next greater hill point. Please see the below image to get an idea. On above image black points are hill tops, each hill top has some spices with some taste value. we can move from one hill top to another tasting spices in them. Please See below image to know valid movements. green lines gives valid movements . So, Now we will be given a query with 2 integers b,c and for each query

Finding the longest palindrome within a string in Python

▼魔方 西西 提交于 2021-02-08 05:31:30
问题 I'm trying to solve the Longest Palindromic Substring problem on LeetCode. The problem statement is: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example: Input: "cbbd" Output: "bb" I've come up with the following solution (including some test cases): import pytest class Solution: def longestPalindrome(self, s): candidate = "" longest = "" contains

Spot it algorithm - js

ⅰ亾dé卋堺 提交于 2021-02-08 05:17:33
问题 In the game Dobble ( "Spot it") , there is a pack of 57 playing cards, each with 8 different symbols on them. The system is that any two cards chosen at random will have just one matching symbol. This spurred me on to the mathematical background behind the system and so I wrote an simple algorithm: let getCards = function(symbolCount) { let n = symbolCount - 1, cards = [], card = [] for (i = 0; i < n + 1; i++) card.push(i) cards.push(card) for (j = 1; j <= n; j++) { let card = [1] for (k = 1;

A BFS Algorithm for Weighted Graphs - To Find Shortest Distance

社会主义新天地 提交于 2021-02-08 04:56:07
问题 I've seen quite a few posts (viz. post1, post2, post3) on this topic but none of the posts provides an algorithm to back up respective queries. Consequently I'm not sure to accept the answers to those posts. Here I present a BFS based shortest-path (single source) algorithm that works for non-negative weighted graph. Can anyone help me understand why BFS (in light of below BFS based algorithm) are not used for such problems (involving weighted graph)! The Algorithm: SingleSourceShortestPath

A BFS Algorithm for Weighted Graphs - To Find Shortest Distance

别说谁变了你拦得住时间么 提交于 2021-02-08 04:55:28
问题 I've seen quite a few posts (viz. post1, post2, post3) on this topic but none of the posts provides an algorithm to back up respective queries. Consequently I'm not sure to accept the answers to those posts. Here I present a BFS based shortest-path (single source) algorithm that works for non-negative weighted graph. Can anyone help me understand why BFS (in light of below BFS based algorithm) are not used for such problems (involving weighted graph)! The Algorithm: SingleSourceShortestPath

Find all subsets that sum to a particular value and then pick the most valuable combination of those subsets

半世苍凉 提交于 2021-02-08 04:48:38
问题 Im trying to create a simple dice game. To calculate the score i have to find all subsets that sum to a particular value and then pick the most valuable combination. All numbers can only be picked once. Probably easiest to describe with an example: Values = {1, 1, 1, 2, 4, 4} Target value = 5 Possible subsets = {1, 1, 1, 2} and {1, 4} Now its possible to either choose: {1, 1, 1, 2} (= worth 5) or {1, 4} {1, 4} (= worth 10) So in this example I would like the algorithm to return 10 and not 5 .

Minimum coin change problem with limited amount of coins

我的梦境 提交于 2021-02-08 04:46:56
问题 To be specific, the problem is: Given array of denominations coins[] , array of limit for each coins limits[] and number amount , return minimum number of coins needed, to get the amount , or if it's not possible return null. Additionally fill array change with number of each coin used in the solution. This is my solution: public static int? Dynamic(int amount, int[] coins, int[] limits, out int[] change) { int[] minCoins = new int[amount + 1]; int[,] coinsUsedToAmount = new int[coins.Length,